From 851c793da43be9e4d3319afe440d603c85834045 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 31 Aug 2023 17:53:54 +0200 Subject: 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 --- cmd/gint/main.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'cmd/gint') 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) } } -- cgit v1.2.3