-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
85 lines (77 loc) · 1.45 KB
/
repl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package rgors
import (
"fmt"
"github.com/chzyer/readline"
)
func display(p *Parser, line string) {
p.SetString(line)
tokens, _ := p.ReadTokens()
// display innput
fmt.Println("Lexer------------")
for i, token := range tokens {
fmt.Printf("%d: %+v\n", i, token)
if token.Text == "quit" {
return
}
}
fmt.Println("------------Lexer")
}
func Repl() {
var line string
var err error
var contFlag bool // for multiple lines
fmt.Println("Lispy Version 0.0.0.0.1")
fmt.Println("Press Ctrl+c to Exit")
rl, err := readline.New("rgors> ")
if err != nil {
panic(err)
}
p := Parser{}
for {
// readline
tmpline, err := rl.Readline()
if contFlag {
line = line + "\n" + tmpline
contFlag = false
rl.SetPrompt("rgors> ")
} else {
line = tmpline
}
if err != nil {
break
}
// lexer check
// display(&p, line)
// parse
program, err := p.ParseString(line)
if err != nil {
switch err.(type) {
case *UnclosedError: // continue
contFlag = true
rl.SetPrompt("... ")
continue
default:
fmt.Println(err.Error())
continue
}
}
// eval???
vm := NewVM()
for _, expr := range program {
comp, err := expr.Compile()
if err != nil {
fmt.Println("compile error:", err.Error())
continue
}
fmt.Println(comp)
// eval!!
vm.Load(comp)
ans, err := vm.Run()
if err != nil {
fmt.Println("vm error:", err.Error())
continue
}
fmt.Println("=>", comp, "\n=>", ans)
}
}
}