Skip to content

Commit

Permalink
Adding a way to get compact version in wasm (and upcoming in grol) (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Aug 1, 2024
1 parent 38a3f9d commit 2d6229a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 6 additions & 1 deletion repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func EvalAll(s, macroState *eval.State, in io.Reader, out io.Writer, options Opt
EvalOne(s, macroState, what, out, options)
}

// Kinda ugly (global) but helpful to not change the signature of EvalString for now and
// yet allow the caller to set this (ie. the discord bot).
var CompactEvalString bool

// EvalString can be used from playground etc for single eval.
// returns the eval errors and an array of errors if any.
// also returns the normalized/reformatted input if no parsing errors
Expand All @@ -65,7 +69,8 @@ func EvalString(what string) (res string, errs []string, formatted string) {
out := &strings.Builder{}
s.Out = out
s.NoLog = true
_, errs, formatted = EvalOne(s, macroState, what, out, Options{All: true, ShowEval: true, NoColor: true})
_, errs, formatted = EvalOne(s, macroState, what, out,
Options{All: true, ShowEval: true, NoColor: true, Compact: CompactEvalString})
res = out.String()
return
}
Expand Down
6 changes: 3 additions & 3 deletions wasm/grol_wasm.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@
console.log('In run')
go.run(inst);
var input = document.getElementById('input').value
var compact = document.getElementById('compact').checked
// Call the grol function with the input
var output = grol(input);
var output = grol(input, compact);
console.log('Eval done:');
console.log(output);
if (output && output.result !== undefined) {
Expand Down Expand Up @@ -117,8 +118,7 @@
m={"str key": a, 42: "str val"}</textarea>
</div>
<div>

Hit enter or click <button onClick="run();" id="runButton" disabled>Run</button> (will also format the code)
Hit enter or click <button onClick="run();" id="runButton" disabled>Run</button> (will also format the code, also try <input type="checkbox" id="compact">compact)
</div>
<div>
<label for="output">Result:</label>
Expand Down
9 changes: 7 additions & 2 deletions wasm/wasm_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ import (
)

func jsEval(this js.Value, args []js.Value) interface{} {
if len(args) != 1 {
return "ERROR: number of arguments doesn't match"
if len(args) != 1 && len(args) != 2 {
return "ERROR: number of arguments doesn't match should be string or string, bool for compact mode"
}
input := args[0].String()
compact := false
if len(args) == 2 {
compact = args[1].Bool()
}
repl.CompactEvalString = compact
res, errs, formatted := repl.EvalString(input)
result := make(map[string]any)
result["result"] = strings.TrimSuffix(res, "\n")
Expand Down

0 comments on commit 2d6229a

Please sign in to comment.