Skip to content

Commit

Permalink
*: include the generated go files, update Makefile and README etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao committed Oct 22, 2019
1 parent f9353be commit 8b5dcd2
Show file tree
Hide file tree
Showing 22 changed files with 55,442 additions and 154 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ esc: ${GOPATH}/bin/esc
kl:
go install github.com/tiancaiamao/shen-go/cmd/kl

codegen/codegen.so:
cd codegen;
go build -buildmode=plugin

shen:
go build -o shen cmd/shen/main.go
go build -o shen github.com/tiancaiamao/shen-go/cmd/shen

docker:
docker build -t shen-go .
Expand Down
77 changes: 1 addition & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,81 +36,6 @@ cd ShenOSKernel-21.0/tests
(load "README.shen")
(load "tests.shen")
```

## Native Calls

Native call is implemented through Go plugin.

First, write a plugin Go file like:

```Go
package main

import (
"github.com/tiancaiamao/shen-go/runtime"
)

func hello(args ...runtime.Obj) runtime.Obj {
return runtime.MakeString("hello " + runtime.ObjString(args[0]))
}

func Main() {
runtime.RegistNativeCall("hello", 1, hello)
}
```

Compile it to a plugin file:

```
go build -o test.so -buildmode=plugin
```

Run in shen-go repl:

```
(load-plugin "test.so")
(native "hello" "world")
```

The `native` keyword tells the compiler to use the correct calling convention, the first argument is the function name, which you regist in the Go plugin file using `runtime.RegistNativeCall`.

Notice that native call is not curry function, If you want to partial apply them, you have to wrap it:

```
(defun hello (Str) (native "hello" Str))
```

## Bootstrap from scratch

Run shen repl, then

```
(load "compiler/bootstrap.shen")
```

This would compile all necessary shen files into bytecode.

Move the `.bc` files to bytecode directory

```
mv ShenOSKernel-21.0/sources/*.bc bytecode/
mv compiler/*.bc bytecode/
```

And try it with

```
./shen -boot=.
```

If everything is ok, you can embedded those bytecode files into `asset.go`

```
make generate
```

And make shen again to get the new binary.

## Learn Shen
* [Official website of Shen](http://shenlanguage.org/)
* [The Shen OS Kernel Manual](http://shenlanguage.org/learn-shen/index.html)
Expand All @@ -121,4 +46,4 @@ And make shen again to get the new binary.
## License

- Shen, Copyright © 2010-2015 Mark Tarver - [License](http://www.shenlanguage.org/license.pdf).
- shen-go, Copyright © 2017-2018 Arthur Mao under [BSD 3-Clause License](http://opensource.org/licenses/BSD-3-Clause).
- shen-go, Copyright © 2017-2019 Arthur Mao under [BSD 3-Clause License](http://opensource.org/licenses/BSD-3-Clause).
3 changes: 2 additions & 1 deletion ROADMAP
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Provide some basic packages
- [x] kl to bytecode compiler, writen in shen
- [x] Abstract machine, used to execute the generated bytecode, written in Go
- [x] Wrap Go packages to be used by shen
- [x] Shen source code from https://github.com/Shen-Language/shen-sources
- [x] Shen source code from https://github.com/Shen-Language/shen-sources
- [x] Compile shen code to golang
99 changes: 99 additions & 0 deletions cmd/shen-vm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"flag"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"path"

"github.com/tiancaiamao/shen-go/runtime"
)

var pprof bool
var boot string
var kl bool

func init() {
flag.BoolVar(&kl, "kl", false, "run kl")
flag.BoolVar(&pprof, "pprof", false, "enable pprof")
flag.StringVar(&boot, "boot", "", "use bootstrap file for testing")
}

func main() {
flag.Parse()

if pprof {
go http.ListenAndServe(":8080", nil)
}
runtime.Boot = boot
runtime.RegistNativeCall("primitive.load-file", 1, loadFile)

m := runtime.NewVM()

if kl {
runtime.BootstrapMin()
r := runtime.NewSexpReader(os.Stdin)
for i := 0; ; i++ {
fmt.Printf("%d #> ", i)
sexp, err := r.Read()
if err != nil {
fmt.Println("read error:", err)
break
}

// var a vm.Assember
// a.FromSexp(sexp)
// code := a.Comiple()
// res := m.Eval(code)
// fmt.Println(kl.ObjString(res))

res := m.Eval(sexp)
fmt.Println(runtime.ObjString(res))
}
} else {
runtime.BootstrapShen()
res := m.Eval(runtime.Cons(runtime.MakeSymbol("shen.shen"), runtime.Nil))
fmt.Println(runtime.ObjString(res))
}
return
}

func loadFile(args ...runtime.Obj) runtime.Obj {
file := runtime.GetString(args[0])
var filePath string
if _, err := os.Stat(file); err == nil {
filePath = file
} else {
filePath = path.Join(runtime.PackagePath(), file)
if _, err := os.Stat(filePath); err != nil {
return runtime.MakeError(err.Error())
}
}

f, err := os.Open(filePath)
if err != nil {
return runtime.MakeError(err.Error())
}
defer f.Close()

r := runtime.NewSexpReader(f)
for {
exp, err := r.Read()
if err != nil {
if err != io.EOF {
return runtime.MakeError(err.Error())
}
break
}

res := runtime.Eval(exp)

if runtime.IsError(res) {
return res
}
}
return args[0]
}
Loading

0 comments on commit 8b5dcd2

Please sign in to comment.