/
elementary.go
147 lines (130 loc) · 3.3 KB
/
elementary.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
// Copyright 2020 Rob Pike. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// This file contains the definitions of the non-math elementary
// (builtin) functions.
package lisp1_5
func evalInit() {
if elementary == nil {
// Initialized here to avoid initialization loop.
elementary = funcMap{
tokAdd: (*Context).addFunc,
tokAnd: (*Context).andFunc,
tokApply: (*Context).applyFunc,
tokAtom: (*Context).atomFunc,
tokCar: (*Context).carFunc,
tokCdr: (*Context).cdrFunc,
tokCons: (*Context).consFunc,
tokDefn: (*Context).defnFunc,
tokDiv: (*Context).divFunc,
tokEq: (*Context).eqFunc,
tokGe: (*Context).geFunc,
tokGt: (*Context).gtFunc,
tokLe: (*Context).leFunc,
tokList: (*Context).listFunc,
tokLt: (*Context).ltFunc,
tokMul: (*Context).mulFunc,
tokNe: (*Context).neFunc,
tokNull: (*Context).nullFunc,
tokOr: (*Context).orFunc,
tokRem: (*Context).remFunc,
tokSub: (*Context).subFunc,
}
}
constT = atomExpr(tokT)
constF = atomExpr(tokF)
constNIL = atomExpr(tokNil)
}
func (c *Context) applyFunc(name *token, expr *Expr) *Expr {
return c.apply("applyFunc", Car(expr), Cdr(expr))
}
func (c *Context) defnFunc(name *token, expr *Expr) *Expr {
var names []*Expr
for expr = Car(expr); expr != nil; expr = Cdr(expr) {
fn := Car(expr)
if fn == nil {
errorf("empty function in defn")
}
name := Car(fn)
atom := name.getAtom()
if atom == nil {
errorf("malformed defn")
}
names = append(names, name)
c.set(atom, Car(Cdr(fn)))
}
var result *Expr
for i := len(names) - 1; i >= 0; i-- {
result = Cons(names[i], result)
}
return result
}
func (c *Context) atomFunc(name *token, expr *Expr) *Expr {
atom := Car(expr)
return truthExpr(atom != nil && atom.atom != nil)
}
func (c *Context) carFunc(name *token, expr *Expr) *Expr {
return Car(Car(expr))
}
func (c *Context) cdrFunc(name *token, expr *Expr) *Expr {
return Cdr(Car(expr))
}
func (c *Context) cadrFunc(name *token, expr *Expr) *Expr {
str := name.text
if !isCadR(str) {
return nil
}
expr = Car(expr)
for i := len(str) - 2; expr != nil && i > 0; i-- {
if str[i] == 'a' {
expr = Car(expr)
} else {
expr = Cdr(expr)
}
}
return expr
}
func (c *Context) consFunc(name *token, expr *Expr) *Expr {
return Cons(Car(expr), Car(Cdr(expr)))
}
func (c *Context) eqFunc(name *token, expr *Expr) *Expr {
a := Car(expr)
b := Car(Cdr((expr)))
return truthExpr(eq(a, b))
}
func eq(a, b *Expr) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
if a.atom == nil || b.atom == nil || a.atom.typ != b.atom.typ {
return false
}
if a.atom.typ == tokenNumber {
return a.atom.num.Cmp(b.atom.num) == 0
}
return a.atom == b.atom
}
func (c *Context) listFunc(name *token, expr *Expr) *Expr {
if expr == nil {
return nil
}
return Cons(Car(expr), Cdr(expr))
}
func (c *Context) nullFunc(name *token, expr *Expr) *Expr {
return truthExpr(Car(expr) == nil)
}
func atomExpr(tok *token) *Expr {
return &Expr{
atom: tok,
}
}
// truthExpr converts the boolean argument to the constant atom T or F.
func truthExpr(t bool) *Expr {
if t {
return constT
}
return constF
}
func (e *Expr) isNumber() bool {
return e != nil && e.atom != nil && e.atom.typ == tokenNumber
}