Skip to content

Commit

Permalink
udate wasm to do reformat on run
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Jul 27, 2024
1 parent 1e36aed commit 0e6b4dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 3 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

package main_test

import (
Expand Down
20 changes: 13 additions & 7 deletions wasm/grol_wasm.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
if (output && output.result !== undefined) {
// Write the result to the output textarea
document.getElementById('output').value = output.result;
document.getElementById('input').value = output.formatted;
document.getElementById('errors').value = output.errors.join("\n");
} else {
document.getElementById('errors').value = "Unexpected runtime error, see JS console";
Expand All @@ -85,6 +86,7 @@
} finally {
inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
}
resizeTextarea(document.getElementById('input'));
resizeTextarea(document.getElementById('output'));
resizeTextarea(document.getElementById('errors'));
}
Expand All @@ -102,21 +104,25 @@
<label for="input">Edit the sample/Enter your GROL code here:</label>
<textarea id="input" rows="12" cols="80">
print("Outputting a smiley: 😀\n")
fact=func(n) { // function
log("called fact ", n) // log output
// function example:
fact=func(n) {
// log output
log("called fact ", n)
if (n<=1) {
return 1
}
n*fact(n-1) // recursion
// recursion
n*fact(n-1)
}

a=[fact(5), "abc", 76-3] // heterogeneous array
m={"str key": a, 42: "str val"} // maps also can have any key,value types
// heterogeneous array
a=[fact(5), "abc", 76-3]
// maps also can have any key,value types
m={"str key": a, 42: "str val"}
</textarea>
</div>
<div>

Hit enter or click <button onClick="run();" id="runButton" disabled>Run</button>
Hit enter or click <button onClick="run();" id="runButton" disabled>Run</button> (will also format the code)
</div>
<div>
<label for="output">Result:</label>
Expand Down
3 changes: 2 additions & 1 deletion wasm/wasm_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func jsEval(this js.Value, args []js.Value) interface{} {
return "ERROR: number of arguments doesn't match"
}
input := args[0].String()
res, errs := repl.EvalString(input)
res, errs, formatted := repl.EvalString(input)
result := make(map[string]any)
result["result"] = res
// transfer errors to []any (!)
Expand All @@ -31,6 +31,7 @@ func jsEval(this js.Value, args []js.Value) interface{} {
anyErrs[i] = v
}
result["errors"] = anyErrs
result["formatted"] = formatted
return result
}

Expand Down

0 comments on commit 0e6b4dc

Please sign in to comment.