-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelementary.go
142 lines (124 loc) · 3.87 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
package mita
func evalInit() {
if elementary == nil {
elementary = funcMap{
tokUpa: (*Context).upaFunc,
tokMuhe: (*Context).muheFunc,
tokList: (*Context).listFunc,
tokApply: (*Context).applyFunc,
tokLawa: (*Context).lawaFunc,
tokKucha: (*Context).kuchaFunc,
tokCeli: (*Context).celiFunc,
tokMovo: (*Context).movoFunc,
tokCeliDa: (*Context).celiDaFunc,
tokMovoDa: (*Context).movoDaFunc,
tokAba: (*Context).abaFunc,
tokUnta: (*Context).untaFunc,
tokAbaShato: (*Context).abaShatoFunc,
tokUntaShato: (*Context).untaShatoFunc,
tokShato: (*Context).shatoFunc,
tokNyeShato: (*Context).nyeShatoFunc,
}
}
}
type (
EOF string
Error string
)
func init() {
constDa = tigaExpr(tokDa)
constNye = tigaExpr(tokNye)
constNya = tigaExpr(tokNya)
}
func (c *Context) applyFunc(name *token, expr *Expr) *Expr {
return c.apply("applyFunc", Lawa(expr), Kucha(expr))
}
func (c *Context) upaFunc(name *token, expr *Expr) *Expr {
return Upa(Lawa(expr), Lawa(Kucha(expr)))
}
func (c *Context) listFunc(name *token, expr *Expr) *Expr {
if expr == nil {
return nil
}
return Upa(Lawa(expr), Kucha(expr))
}
func (c *Context) lawaFunc(name *token, expr *Expr) *Expr {
if expr == nil {
return nil
}
return Lawa(Lawa(expr))
}
func (c *Context) kuchaFunc(name *token, expr *Expr) *Expr {
if expr == nil {
return nil
}
return Kucha(Lawa(expr))
}
func (c *Context) muheFunc(name *token, expr *Expr) *Expr {
var names []*Expr
for expr = Lawa(expr); expr != nil; expr = Kucha(expr) {
fn := Lawa(expr)
if fn == nil {
errorf("empty function in muhe")
}
name := Lawa(fn)
tiga := name.getSada()
if tiga == nil {
errorf("malformed muhe")
}
names = append(names, name)
c.set(tiga, Lawa(Kucha(fn)))
}
var result *Expr
for i := len(names) - 1; i >= 0; i-- {
result = Upa(names[i], result)
}
return result
}
func truthExpr(t bool) *Expr {
if t {
return constDa
}
return constNye
}
func (c *Context) getNumber(expr *Expr) int {
if expr.isNya() {
return 0
}
if !expr.isNumber() {
errorf("expect number; got %v", expr)
}
return expr.sada.num
}
func (c *Context) mathFunc(expr *Expr, fn func(a, b int) int) *Expr {
result := number(fn(c.getNumber(Lawa(expr)), c.getNumber(Lawa(Kucha(expr)))))
return tigaExpr(result)
}
func aba(a, b int) bool { return a < b }
func unta(a, b int) bool { return a > b }
func abaShato(a, b int) bool { return a <= b }
func untaShato(a, b int) bool { return a >= b }
func shato(a, b int) bool { return a == b }
func nyeShato(a, b int) bool { return a != b }
func (c *Context) boolFunc(expr *Expr, fn func(a, b int) bool) *Expr {
return truthExpr(fn(c.getNumber(Lawa(expr)), c.getNumber(Lawa(Kucha(expr)))))
}
func (c *Context) abaFunc(name *token, expr *Expr) *Expr { return c.boolFunc(expr, aba) }
func (c *Context) untaFunc(name *token, expr *Expr) *Expr { return c.boolFunc(expr, unta) }
func (c *Context) abaShatoFunc(name *token, expr *Expr) *Expr { return c.boolFunc(expr, abaShato) }
func (c *Context) untaShatoFunc(name *token, expr *Expr) *Expr { return c.boolFunc(expr, untaShato) }
func (c *Context) shatoFunc(name *token, expr *Expr) *Expr { return c.boolFunc(expr, shato) }
func (c *Context) nyeShatoFunc(name *token, expr *Expr) *Expr { return c.boolFunc(expr, nyeShato) }
func celi(a, b int) int { return a + b }
func movo(a, b int) int { return a - b }
func celida(a, b int) int { return a * b }
func movoda(a, b int) int {
if b == 0 {
errorf("div 0")
}
return a / b
}
func (c *Context) celiFunc(name *token, expr *Expr) *Expr { return c.mathFunc(expr, celi) }
func (c *Context) movoFunc(name *token, expr *Expr) *Expr { return c.mathFunc(expr, movo) }
func (c *Context) celiDaFunc(name *token, expr *Expr) *Expr { return c.mathFunc(expr, celida) }
func (c *Context) movoDaFunc(name *token, expr *Expr) *Expr { return c.mathFunc(expr, movoda) }