varaq-interpreter-go/webmain.go

45 lines
825 B
Go

//go:build (js && wasm)
// +build js,wasm
package main
import (
"bytes"
"fmt"
"syscall/js"
"varaq/varaq"
)
func main() {
done := make(chan struct{}, 0)
js.Global().Set("varaq", js.FuncOf(jsInterpret))
fmt.Println("varaq WASM init, Qa'pla!")
<-done
}
func jsInterpret(this js.Value, args []js.Value) interface{} {
if len(args) < 1 {
return js.ValueOf("no input")
}
input := args[0].String()
tokens, err := varaq.Tokenize(input)
if err != nil {
return js.ValueOf(fmt.Errorf("%v", err))
}
code := varaq.Parse(tokens)
buf := bytes.NewBufferString("")
strings := make([]string, len(args[1:]))
for i, jsVal := range args[1:] {
strings[i] = jsVal.String()
}
err = varaq.Interpret(code, strings, buf)
if err != nil {
return js.ValueOf(fmt.Errorf("%v", err))
}
return js.ValueOf(buf.String())
}