-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
68 lines (56 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"luna/compiler"
"strings"
"syscall/js"
"github.com/spatialcurrent/go-stringify/pkg/stringify"
)
func main() {
mode := "browser"
if mode != "browser" {
input := `
(module
(func (export "addNumbers") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add)
)
`
compile(input)
return
}
// Spin it in browser
wait := make(chan struct{}, 0)
js.Global().Set("startLuna", js.FuncOf(startLuna))
<-wait
}
//export compile
func compile(input string) string {
// c, err := ioutil.ReadFile()
// if err != nil {
// log.Fatal(err)
// }
// content := string(c)
// fmt.Println(content)
// Tokens
tokens := compiler.Tokenize(input)
fmt.Println("Tokens:", tokens)
fmt.Println("----------------------------------------------------------------")
// Ast
ast := compiler.Parser(tokens)
fmt.Println("Ast:", ast)
// Emitters
wasm := compiler.Compile(ast)
fmt.Println("Wasm", wasm)
// Str for Javascript
str := stringify.InterfaceSliceToStringSlice(wasm)
return strings.Join(str, " ")
}
// TINYGO NOTE: there is no export as we registered this function in global
func startLuna(this js.Value, args []js.Value) interface{} {
input := args[0].String()
return js.ValueOf(map[string]interface{}{
"module": compile(input),
})
}