forked from candid82/joker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (207 loc) · 4.76 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
. "github.com/candid82/joker/core"
_ "github.com/candid82/joker/std/base64"
_ "github.com/candid82/joker/std/json"
_ "github.com/candid82/joker/std/os"
_ "github.com/candid82/joker/std/string"
"gopkg.in/readline.v1"
)
type (
ReplContext struct {
first *Var
second *Var
third *Var
exc *Var
}
)
const VERSION = "v0.8.1"
func NewReplContext(env *Env) *ReplContext {
first, _ := env.Resolve(MakeSymbol("joker.core/*1"))
second, _ := env.Resolve(MakeSymbol("joker.core/*2"))
third, _ := env.Resolve(MakeSymbol("joker.core/*3"))
exc, _ := env.Resolve(MakeSymbol("joker.core/*e"))
first.Value = NIL
second.Value = NIL
third.Value = NIL
exc.Value = NIL
return &ReplContext{
first: first,
second: second,
third: third,
exc: exc,
}
}
func (ctx *ReplContext) PushValue(obj Object) {
ctx.third.Value = ctx.second.Value
ctx.second.Value = ctx.first.Value
ctx.first.Value = obj
}
func (ctx *ReplContext) PushException(exc Object) {
ctx.exc.Value = exc
}
func processFile(filename string, phase Phase) error {
var reader *Reader
if filename == "--" {
reader = NewReader(bufio.NewReader(os.Stdin), "<stdin>")
filename = ""
} else {
f, err := os.Open(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: ", err)
return err
}
reader = NewReader(bufio.NewReader(f), filename)
}
return ProcessReader(reader, filename, phase)
}
func skipRestOfLine(reader *Reader) {
for {
switch reader.Get() {
case EOF, '\n':
return
}
}
}
func processReplCommand(reader *Reader, phase Phase, parseContext *ParseContext, replContext *ReplContext) (exit bool) {
defer func() {
if r := recover(); r != nil {
switch r := r.(type) {
case *ParseError:
replContext.PushException(r)
fmt.Fprintln(os.Stderr, r)
case *EvalError:
replContext.PushException(r)
fmt.Fprintln(os.Stderr, r)
case Error:
replContext.PushException(r)
fmt.Fprintln(os.Stderr, r)
// case *runtime.TypeAssertionError:
// fmt.Fprintln(os.Stderr, r)
default:
panic(r)
}
}
}()
obj, err := TryRead(reader)
if err == io.EOF {
return true
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
skipRestOfLine(reader)
return
}
if phase == READ {
fmt.Println(obj.ToString(true))
return false
}
expr := Parse(obj, parseContext)
if phase == PARSE {
fmt.Println(expr)
return false
}
res := Eval(expr, nil)
replContext.PushValue(res)
fmt.Println(res.ToString(true))
return false
}
func repl(phase Phase) {
fmt.Printf("Welcome to joker %s. Use ctrl-c to exit.\n", VERSION)
parseContext := &ParseContext{GlobalEnv: GLOBAL_ENV}
replContext := NewReplContext(parseContext.GlobalEnv)
rl, err := readline.New("")
if err != nil {
fmt.Println("Error: " + err.Error())
}
defer rl.Close()
reader := NewReader(NewLineRuneReader(rl), "<repl>")
for {
rl.SetPrompt(GLOBAL_ENV.CurrentNamespace().Name.ToString(false) + "=> ")
if processReplCommand(reader, phase, parseContext, replContext) {
return
}
}
}
func makeDialectKeyword(dialect Dialect) Keyword {
switch dialect {
case EDN:
return MakeKeyword("clj")
case CLJ:
return MakeKeyword("clj")
case CLJS:
return MakeKeyword("cljs")
default:
return MakeKeyword("joker ")
}
}
func configureLinterMode(dialect Dialect) {
LINTER_MODE = true
DIALECT = dialect
lm, _ := GLOBAL_ENV.Resolve(MakeSymbol("joker.core/*linter-mode*"))
lm.Value = Bool{B: true}
GLOBAL_ENV.Features = GLOBAL_ENV.Features.Disjoin(MakeKeyword("joker")).Conj(makeDialectKeyword(dialect)).(Set)
ProcessLinterData(dialect)
}
func detectDialect(filename string) Dialect {
switch {
case strings.HasSuffix(filename, ".edn"):
return EDN
case strings.HasSuffix(filename, ".cljs"):
return CLJS
case strings.HasSuffix(filename, ".joke"):
return JOKER
}
return CLJ
}
func lintFile(filename string, dialect Dialect) {
phase := PARSE
if dialect == EDN {
phase = READ
}
ReadConfig(filename)
configureLinterMode(dialect)
if processFile(filename, phase) == nil {
WarnOnUnusedNamespaces()
WarnOnUnusedVars()
}
}
func main() {
GLOBAL_ENV.FindNamespace(MakeSymbol("user")).ReferAll(GLOBAL_ENV.CoreNamespace)
if len(os.Args) == 1 {
repl(EVAL)
return
}
if len(os.Args) == 2 {
if os.Args[1] == "-v" || os.Args[1] == "--version" {
println(VERSION)
return
}
processFile(os.Args[1], EVAL)
return
}
switch os.Args[1] {
case "--read":
processFile(os.Args[2], READ)
case "--parse":
processFile(os.Args[2], PARSE)
case "--lint":
dialect := detectDialect(os.Args[2])
lintFile(os.Args[2], dialect)
case "--lintclj":
lintFile(os.Args[2], CLJ)
case "--lintcljs":
lintFile(os.Args[2], CLJS)
case "--lintjoker":
lintFile(os.Args[2], JOKER)
case "--lintedn":
lintFile(os.Args[2], EDN)
default:
processFile(os.Args[1], EVAL)
}
}