forked from zhemao/glisp
/
expressions.go
212 lines (176 loc) · 3.43 KB
/
expressions.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
package glisp
import (
"reflect"
"strconv"
"strings"
"bytes"
)
type Sexp interface {
SexpString() string
}
type SexpSentinel int
const (
SexpNull SexpSentinel = iota
SexpEnd
SexpMarker
)
func (sent SexpSentinel) SexpString() string {
if sent == SexpNull {
return "()"
}
if sent == SexpEnd {
return "End"
}
if sent == SexpMarker {
return "Marker"
}
return ""
}
type SexpPair struct {
head Sexp
tail Sexp
}
func Cons(a Sexp, b Sexp) SexpPair {
return SexpPair{a, b}
}
func (pair SexpPair) Head() Sexp {
return pair.head
}
func (pair SexpPair) Tail() Sexp {
return pair.tail
}
func (pair SexpPair) SexpString() string {
str := "("
for {
switch pair.tail.(type) {
case SexpPair:
str += pair.head.SexpString() + " "
pair = pair.tail.(SexpPair)
continue
}
break
}
str += pair.head.SexpString()
if pair.tail == SexpNull {
str += ")"
} else {
str += " . " + pair.tail.SexpString() + ")"
}
return str
}
type SexpArray []Sexp
type SexpHash struct {
TypeName *string
Map map[int][]SexpPair
KeyOrder *[]Sexp // must user pointers here, else hset! will fail to update.
GoStruct *interface{}
NumKeys *int
}
type SexpInt int
type SexpBool bool
type SexpFloat float64
type SexpChar rune
type SexpStr string
type SexpData []byte
var SexpIntSize = reflect.TypeOf(SexpInt(0)).Bits()
var SexpFloatSize = reflect.TypeOf(SexpFloat(0.0)).Bits()
func (arr SexpArray) SexpString() string {
if len(arr) == 0 {
return "[]"
}
str := "[" + arr[0].SexpString()
for _, sexp := range arr[1:] {
str += " " + sexp.SexpString()
}
str += "]"
return str
}
func (hash SexpHash) SexpString() string {
str := "{"
for _, arr := range hash.Map {
for _, pair := range arr {
str += pair.head.SexpString() + " "
str += pair.tail.SexpString() + " "
}
}
if len(str) > 1 {
return str[:len(str)-1] + "}"
}
return str + "}"
}
func (b SexpBool) SexpString() string {
if b {
return "true"
}
return "false"
}
func (i SexpInt) SexpString() string {
return strconv.Itoa(int(i))
}
func (f SexpFloat) SexpString() string {
return strconv.FormatFloat(float64(f), 'g', 5, SexpFloatSize)
}
func (c SexpChar) SexpString() string {
return "#" + strings.Trim(strconv.QuoteRune(rune(c)), "'")
}
func (s SexpStr) SexpString() string {
return strconv.Quote(string(s))
}
func (b SexpData) SexpString() string {
data := []byte(b)
str := &bytes.Buffer{}
str.WriteString("<")
if len(data) > 0 {
for _, v := range data[0:len(data)-1] {
str.WriteString(strconv.Itoa(int(v)))
str.WriteRune(',')
}
str.WriteString(strconv.Itoa(int(data[len(data)-1])))
}
str.WriteString(">")
return str.String()
}
type SexpSymbol struct {
name string
number int
}
func (sym SexpSymbol) SexpString() string {
return sym.name
}
func (sym SexpSymbol) Name() string {
return sym.name
}
func (sym SexpSymbol) Number() int {
return sym.number
}
type SexpFunction struct {
name string
user bool
nargs int
varargs bool
fun GlispFunction
userfun GlispUserFunction
closeScope *Stack
}
func (sf SexpFunction) SexpString() string {
return "fn [" + sf.name + "]"
}
func IsTruthy(expr Sexp) bool {
switch e := expr.(type) {
case SexpBool:
return bool(e)
case SexpInt:
return e != 0
case SexpChar:
return e != 0
case SexpSentinel:
return e != SexpNull
}
return true
}
type SexpStackmark struct {
sym SexpSymbol
}
func (mark SexpStackmark) SexpString() string {
return "stackmark " + mark.sym.name
}