Skip to content

Commit fb5e067

Browse files
committed
Add repl
1 parent ad915e4 commit fb5e067

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cmd/exp/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"flag"
56
"fmt"
67
"github.com/antonmedv/expr/compiler"
@@ -16,13 +17,15 @@ var (
1617
debug bool
1718
run bool
1819
ast bool
20+
repl bool
1921
)
2022

2123
func init() {
2224
flag.BoolVar(&bytecode, "bytecode", false, "disassemble bytecode")
2325
flag.BoolVar(&debug, "debug", false, "debug program")
2426
flag.BoolVar(&run, "run", false, "run program")
2527
flag.BoolVar(&ast, "ast", false, "print ast")
28+
flag.BoolVar(&repl, "repl", false, "start repl")
2629
}
2730

2831
func main() {
@@ -44,6 +47,10 @@ func main() {
4447
debugger()
4548
os.Exit(0)
4649
}
50+
if repl {
51+
startRepl()
52+
os.Exit(0)
53+
}
4754

4855
flag.Usage()
4956
os.Exit(2)
@@ -92,3 +99,37 @@ func runProgram() {
9299

93100
litter.Dump(out)
94101
}
102+
103+
func startRepl() {
104+
scanner := bufio.NewScanner(os.Stdin)
105+
prompt()
106+
107+
for scanner.Scan() {
108+
line := scanner.Text()
109+
110+
tree, err := parser.Parse(line)
111+
if err != nil {
112+
fmt.Printf("%v\n", err)
113+
continue
114+
}
115+
116+
program, err := compiler.Compile(tree)
117+
if err != nil {
118+
fmt.Printf("%v\n", err)
119+
continue
120+
}
121+
122+
out, err := vm.Run(program, nil)
123+
if err != nil {
124+
fmt.Printf("%v\n", err)
125+
continue
126+
}
127+
128+
fmt.Printf("%v\n", litter.Sdump(out))
129+
prompt()
130+
}
131+
}
132+
133+
func prompt() {
134+
fmt.Print("> ")
135+
}

0 commit comments

Comments
 (0)