diff options
| author | Marc Vertes <mvertes@free.fr> | 2023-08-31 17:53:54 +0200 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2023-08-31 17:53:54 +0200 |
| commit | 851c793da43be9e4d3319afe440d603c85834045 (patch) | |
| tree | fd4b3d3812f5743213b5849858c459c8196dbf7f /cmd/gint | |
| parent | 0f4bfe6e70263fbeb580014b62632f403b29b414 (diff) | |
codegen: fix interpreter re-entrance
So multiple successive incremental Evals function correctly.
Also improve the following:
- Apply the same Eval API to vm0 and vm1
- parser: dot diagram display is now synchronous
- codegen: outsource complex code generation for readability
- vm1: Pop take the number of values to pop as operand
Diffstat (limited to 'cmd/gint')
| -rw-r--r-- | cmd/gint/main.go | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/cmd/gint/main.go b/cmd/gint/main.go index 5b2b3a2..037614c 100644 --- a/cmd/gint/main.go +++ b/cmd/gint/main.go @@ -15,7 +15,7 @@ import ( ) type Interpreter interface { - Eval(string) error + Eval(string) (any, error) } func main() { @@ -23,7 +23,7 @@ func main() { var interp Interpreter = vm0.New(golang.GoParser) if len(os.Args) > 1 && os.Args[1] == "1" { interp = codegen.NewInterpreter(golang.GoParser) - interp.(*codegen.Interpreter).AddSym(fmt.Println, "println", false) + interp.(*codegen.Interpreter).AddSym(fmt.Println, "println") } in := os.Stdin @@ -31,19 +31,22 @@ func main() { // Provide an interactive line oriented Read Eval Print Loop (REPL). liner := bufio.NewScanner(in) text, prompt := "", "> " - fmt.Printf(prompt) + fmt.Print(prompt) for liner.Scan() { text += liner.Text() - err := interp.Eval(text + "\n") + res, err := interp.Eval(text + "\n") if err == nil { + if res != nil { + fmt.Println(": ", res) + } text, prompt = "", "> " } else if errors.Is(err, scanner.ErrBlock) { prompt = ">> " } else { - text, prompt = "", "> " fmt.Println("Error:", err) + text, prompt = "", "> " } - fmt.Printf(prompt) + fmt.Print(prompt) } return } @@ -52,7 +55,7 @@ func main() { if err != nil { log.Fatal(err) } - if err := interp.Eval(string(buf)); err != nil { + if _, err := interp.Eval(string(buf)); err != nil { log.Fatal(err) } } |
