-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
236 lines (183 loc) · 5.32 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"flo/vm"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/antlr/antlr4/runtime/Go/antlr"
"flo/compiler"
"flo/parser"
"github.com/c-bata/go-prompt"
)
type ErrorListener struct {
}
func NewErrorListener() *ErrorListener {
return new(ErrorListener)
}
func (d *ErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {
panic("SyntaxError: invalid syntax, " + "line " + strconv.Itoa(line) + ", col " + strconv.Itoa(column) + " " + msg)
// fmt.Println(e.GetInputStream().Index())
// fmt.Fprintln(os.Stderr, "line "+strconv.Itoa(line)+":"+strconv.Itoa(column)+" "+msg)
}
func (d *ErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs antlr.ATNConfigSet) {
}
func (d *ErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs antlr.ATNConfigSet) {
}
func (d *ErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs antlr.ATNConfigSet) {
}
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
// {Text: "users", Description: "Store the username and age"},
// {Text: "articles", Description: "Store the article text posted by user"},
// {Text: "comments", Description: "Store the text commented to articles"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
type Exit int
func exit(_ *prompt.Buffer) {
panic(Exit(0))
}
func handleExit() {
switch v := recover().(type) {
case nil:
return
case Exit:
os.Exit(int(v))
default:
fmt.Println(v)
// fmt.Println(string(debug.Stack()))
}
}
func doEval(p *parser.FloParser, visitor *compiler.FloVisitor) {
defer func() {
if r := recover(); r != nil {
fmt.Println("-- traceback --")
for i, _ := range visitor.Stack {
fmt.Println(visitor.Stack[len(visitor.Stack)-i-1])
}
fmt.Println(r)
// fmt.Println(string(debug.Stack()))
}
}()
// Finally parse the expression
// antlr.ParseTreeWalkerDefault.Walk(listener, p.Start())
antlr.ParseTreeVisitor.Visit(visitor, p.Start())
// out := listener.Out()
// if out != nil {
// fmt.Println(out)
// }
}
func Start(in io.Reader, out io.Writer) {
defer handleExit()
// Initialise history slice
history := []string{}
fmt.Println("-- Welcome to Flo (0.0.1) --\n-- Ctrl-C to exit --")
// var listener eval.FloListener
var visitor compiler.FloVisitor
visitor.Init()
flovm := vm.VM{}
var previousEnvironment []map[vm.FloString]vm.FloObject = make([]map[vm.FloString]vm.FloObject, 1)
previousEnvironment[0] = make(map[vm.FloString]vm.FloObject, 5)
for {
input := prompt.Input(">> ", completer,
prompt.OptionHistory(history),
prompt.OptionAddKeyBind(prompt.KeyBind{
Key: prompt.ControlC,
Fn: exit,
}))
history = append(history, input)
if input == "" {
continue
}
input += "\n"
// Setup the input
is := antlr.NewInputStream(input)
// Create the Lexer
lexer := parser.NewFloLexer(is)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
// Create the Parser
p := parser.NewFloParser(stream)
p.RemoveErrorListeners()
e := NewErrorListener()
p.AddErrorListener(e)
doEval(p, &visitor)
visitor.Object.Environment = previousEnvironment
f := vm.FloCallable{
Args: ([]vm.FloObject{}),
Object: &visitor.Object,
Name: vm.FloString("main"),
}
flovm.Init(f)
flovm.Run(f)
previousEnvironment = visitor.Object.Environment
visitor.Object.Instructions = nil
}
}
func main() {
// defer func() {
// if r := recover(); r != nil {
// fmt.Println(r)
// }
// }()
// defer profile.Start().Stop()
// defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
// debug.SetGCPercent(200)
// defer profile.Start(profile.MemProfile).Stop()
args := os.Args
if len(args) < 2 {
// REPL
Start(os.Stdin, os.Stdout)
} else {
var visitor compiler.FloVisitor
flovm := vm.VM{}
visitor.Init()
file, err := os.Open(args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
b, err := ioutil.ReadAll(file)
input := string(b)
if input == "" {
return
}
// Setup the input
is := antlr.NewInputStream(input)
// Create the Lexer
lexer := parser.NewFloLexer(is)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
// Create the Parser
p := parser.NewFloParser(stream)
p.RemoveErrorListeners()
e := NewErrorListener()
p.AddErrorListener(e)
doEval(p, &visitor)
f := vm.FloCallable{
Args: ([]vm.FloObject{}),
Object: &visitor.Object,
Name: vm.FloString("main"),
}
// f.Object.Environment = make(map[vm.FloString]vm.FloObject, 5)
// f.Object.Environment = make(map[vm.FloString]vm.FloObject, 5)
f.Object.Environment = make([]map[vm.FloString]vm.FloObject, 1)
f.Object.Environment[0] = make(map[vm.FloString]vm.FloObject, 5)
func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("-- traceback --")
// for i, _ := range flovm.callStack {
// frame := flovm.callStack[i]
// fmt.Println(frame.name)
// }
fmt.Println(r)
// fmt.Println(string(debug.Stack()))
}
}()
flovm.Init(f)
flovm.Run(f)
}()
}
}