-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlg.go
240 lines (220 loc) · 5.4 KB
/
lg.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
237
238
239
240
/*
* Copyright (c) 2021 Marcin Gasperowicz <[email protected]>
* SPDX-License-Identifier: MIT
*/
package main
import (
"flag"
"fmt"
"os"
"strings"
"unicode"
"github.com/alimpfard/line"
"github.com/nooga/let-go/pkg/compiler"
"github.com/nooga/let-go/pkg/nrepl"
"github.com/nooga/let-go/pkg/rt"
"github.com/nooga/let-go/pkg/vm"
)
func motd() {
message := "\u001B[1m\u001b[37;1mLET-GO\u001B[0m \u001B[36mdev\u001b[0m \u001b[90m(Ctrl-C to quit)\u001b[0m\n"
fmt.Print(message)
}
func runForm(ctx *compiler.Context, in string) (vm.Value, error) {
_, val, err := ctx.CompileMultiple(strings.NewReader(in))
if err != nil {
return nil, err
}
// if debug {
// val, err = vm.NewDebugFrame(chunk, nil).Run()
// } else {
// val, err = vm.NewFrame(chunk, nil).Run()
// }
// if err != nil {
// return nil, err
// }
return val, err
}
var completionTerminators map[byte]bool
var styles map[compiler.TokenKind]line.Style
func repl(ctx *compiler.Context) {
interrupted := false
editor := line.NewEditor()
prompt := ctx.CurrentNS().Name() + "=> "
editor.SetInterruptHandler(func() {
interrupted = true
editor.Finish()
})
editor.SetTabCompletionHandler(func(editor line.Editor) []line.Completion {
lin := editor.Line()
prefix := ""
for i := len(lin) - 1; i >= -1; i-- {
if (i < 0 || completionTerminators[lin[i]] || unicode.IsSpace(rune(lin[i]))) && i+1 < len(lin) {
prefix = lin[i+1:]
break
}
}
cur := ctx.CurrentNS()
symbols := rt.FuzzyNamespacedSymbolLookup(cur, vm.Symbol(prefix))
completions := []line.Completion{}
for _, s := range symbols {
completions = append(completions, line.Completion{
Text: string(s) + " ",
InvariantOffset: uint32(len(prefix)),
AllowCommitWithoutListing: true,
})
}
return completions
})
editor.SetRefreshHandler(func(editor line.Editor) {
lin := editor.Line()
reader := compiler.NewLispReaderTokenizing(strings.NewReader(lin), "syntax")
reader.Read() //nolint:errcheck // We really don't care, just need partial parse
editor.StripStyles()
for _, t := range reader.Tokens {
if t.End == -1 {
continue
}
style, ok := styles[t.Kind]
if !ok {
continue
}
editor.Stylize(line.Span{Start: uint32(t.Start), End: uint32(t.End), Mode: line.SpanModeByte}, style)
}
})
for {
if interrupted {
break
}
in, err := editor.GetLine(prompt)
if err != nil {
fmt.Println("prompt failed: ", err)
continue
}
if in == "" {
continue
}
editor.AddToHistory(in)
ctx.SetSource("REPL")
val, err := runForm(ctx, in)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(val.String())
}
prompt = ctx.CurrentNS().Name() + "=> "
}
}
func runFile(ctx *compiler.Context, filename string) error {
ctx.SetSource(filename)
f, err := os.Open(filename)
if err != nil {
return err
}
_, _, err = ctx.CompileMultiple(f)
errc := f.Close()
if err != nil {
return err
}
if errc != nil {
return errc
}
//_, err = vm.NewFrame(chunk, nil).Run()
//if err != nil {
// return err
//}
return nil
}
var nreplServer *nrepl.NreplServer
func nreplServe(ctx *compiler.Context, port int) error {
nreplServer = nrepl.NewNreplServer(ctx)
err := nreplServer.Start(port)
if err != nil {
return err
}
return nil
}
var nreplPort int
var runNREPL bool
var runREPL bool
var expr string
var debug bool
func init() {
flag.BoolVar(&runREPL, "r", false, "attach REPL after running given files")
flag.StringVar(&expr, "e", "", "eval given expression")
flag.BoolVar(&debug, "d", false, "enable VM debug mode")
flag.BoolVar(&runNREPL, "n", false, "enable nREPL server")
flag.IntVar(&nreplPort, "p", 2137, "set nREPL port, default is 2137")
completionTerminators = map[byte]bool{
'(': true,
')': true,
'[': true,
']': true,
'{': true,
'}': true,
'"': true,
'\\': true,
'\'': true,
'@': true,
'`': true,
'~': true,
';': true,
'#': true,
}
styles = map[compiler.TokenKind]line.Style{
compiler.TokenNumber: {ForegroundColor: line.MakeXtermColor(line.XtermColorMagenta)},
compiler.TokenPunctuation: {ForegroundColor: line.MakeXtermColor(line.XtermColorYellow)},
compiler.TokenKeyword: {ForegroundColor: line.MakeXtermColor(line.XtermColorBlue)},
compiler.TokenString: {ForegroundColor: line.MakeXtermColor(line.XtermColorCyan)},
compiler.TokenSpecial: {ForegroundColor: line.MakeXtermColor(line.XtermColorUnchanged), Bold: true},
}
}
func initCompiler(debug bool) *compiler.Context {
consts := vm.NewConsts()
ns := rt.NS("user")
if ns == nil {
fmt.Println("namespace not found")
return nil
}
if debug {
return compiler.NewDebugCompiler(consts, ns)
} else {
return compiler.NewCompiler(consts, ns)
}
}
func main() {
flag.Parse()
files := flag.Args()
context := initCompiler(debug)
ranSomething := false
if len(files) >= 1 {
for i := range files {
err := runFile(context, files[i])
if err != nil {
fmt.Println(err)
continue
}
}
ranSomething = true
}
if expr != "" {
context.SetSource("EXPR")
val, err := runForm(context, expr)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(val)
}
ranSomething = true
}
if !ranSomething || runREPL {
motd()
if runNREPL {
err := nreplServe(context, nreplPort)
if err != nil {
fmt.Println("failed to run nREPL server on port", nreplPort, err)
}
fmt.Printf("nREPL server running at tcp://127.0.0.1:%d\n", nreplPort)
}
repl(context)
}
}