umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs

commit 4dea3b2e446b0a0628a452a4a69552cb28c841b3
parent 620e9910e6807c8bf8ab9fcf0a9a982f7cf491c9
Author: bsandro <email@bsandro.tech>
Date:   Fri,  1 Jul 2022 01:05:57 +0300

support for running script files from command line

Diffstat:
Mmain.go | 15+++++++++++++--
Mrepl/repl.go | 14+++++++++++++-
Mtest.in | 12++++++++++--
3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/main.go b/main.go @@ -12,6 +12,17 @@ func main() { if err != nil { panic(err) } - fmt.Printf("ehlo %s, this is a potato interpretator cleverly named 'interp'\n", user.Username) - repl.Start(os.Stdin, os.Stdout) + + if len(os.Args) > 1 { + fname := os.Args[1] + script, err := os.ReadFile(fname) + if err != nil { + fmt.Printf("error opening file %s: %s\n", fname, err.Error()) + return + } + repl.Run(string(script), os.Stdout) + } else { + fmt.Printf("ehlo %s, this is a potato interpretator cleverly named 'interp'\n", user.Username) + repl.Start(os.Stdin, os.Stdout) + } } diff --git a/repl/repl.go b/repl/repl.go @@ -5,8 +5,8 @@ import ( "fmt" "interp/eval" "interp/lexer" - "interp/parser" "interp/object" + "interp/parser" "io" ) @@ -42,3 +42,15 @@ func printParserErrors(out io.Writer, errors []string) { io.WriteString(out, "\t"+msg+"\n") } } + +func Run(script string, out io.Writer) { + ctx := object.NewContext() + l := lexer.New(script) + p := parser.New(l) + prg := p.ParseProgram() + if len(p.Errors()) == 0 { + eval.Eval(prg, ctx) + } else { + printParserErrors(out, p.Errors()) + } +} diff --git a/test.in b/test.in @@ -1,6 +1,14 @@ -let myhash = {"value1": 1,"value2": "two"}; +let myhash = { + "value1": 1, + "value2": "two", + "value3": "three" +}; let one = "value"; let two = "2"; -puts(one, two, myhash[one+two]); +let myfunc = fn(h) { + puts(h[one+two]); +}; + +myfunc(myhash);