-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.go
182 lines (161 loc) · 3.64 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
package main
import (
"bufio"
"fmt"
"io"
"os"
_ "github.com/candid82/joker/base64"
. "github.com/candid82/joker/core"
_ "github.com/candid82/joker/json"
_ "github.com/candid82/joker/os"
_ "github.com/candid82/joker/string"
"gopkg.in/readline.v1"
)
type (
ReplContext struct {
first *Var
second *Var
third *Var
exc *Var
}
)
const VERSION = "v0.6.1"
func NewReplContext(env *Env) *ReplContext {
first, _ := env.Resolve(MakeSymbol("core/*1"))
second, _ := env.Resolve(MakeSymbol("core/*2"))
third, _ := env.Resolve(MakeSymbol("core/*3"))
exc, _ := env.Resolve(MakeSymbol("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) {
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
}
reader = NewReader(bufio.NewReader(f), filename)
}
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 configureLinterMode() {
LINTER_MODE = true
lm, _ := GLOBAL_ENV.Resolve(MakeSymbol("core/*linter-mode*"))
lm.Value = Bool{B: true}
ProcessLinterData()
}
func main() {
GLOBAL_ENV.FindNamespace(MakeSymbol("user")).ReferAll(GLOBAL_ENV.FindNamespace(MakeSymbol("core")))
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":
configureLinterMode()
processFile(os.Args[2], PARSE)
default:
processFile(os.Args[1], EVAL)
}
}