-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.go
98 lines (81 loc) · 1.71 KB
/
symbol.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
package main
import "math"
var consts = map[string]float64{
"PI": 3.14159265358979323846,
"E": 2.71828182845904523536,
"GAMMA": 0.57721566490153286060, // Euler’s constant
"DEG": 57.29577951308232087680, // degrees/radian
"PHI": 1.61803398874989484820, // golden ratio
}
var keywords = map[string]func() int{
"if": func() int { return IF },
"else": func() int { return ELSE },
"while": func() int { return WHILE },
"print": func() int { return PRINT },
}
func sin(x float64) float64 {
return math.Sin(x)
}
func cos(x float64) float64 {
return math.Cos(x)
}
func atan(x float64) float64 {
return math.Atan(x)
}
func elog(x float64) float64 {
return math.Log(x)
}
func log10(x float64) float64 {
return math.Log10(x)
}
func exp(x float64) float64 {
return math.Exp(x)
}
func sqrt(x float64) float64 {
return math.Sqrt(x)
}
func fabs(x float64) float64 {
return math.Abs(x)
}
var builtins = map[string](func(float64) float64){
"sin": sin,
"cos": cos,
"atan": atan,
"log": elog,
"logten": log10,
"exp": exp,
"sqrt": sqrt,
"abs": fabs,
}
type Symbol struct {
Name string
Type int
Val float64
F func(float64) float64
}
var symMap map[string]Symbol
func Lookup(name string) *Symbol {
value, ok := symMap[name]
if ok {
return &value
}
return nil
}
func (symbol *Symbol) Install() {
symMap[symbol.Name] = *symbol
}
func Init() {
symMap = make(map[string]Symbol, 100)
for key, value := range consts {
s := &Symbol{Name: key, Type: VAR, Val: value}
s.Install()
}
for key, value := range builtins {
s := &Symbol{Name: key, Type: BLTIN, F: value}
s.Install()
}
for key, value := range keywords {
s := &Symbol{Name: key, Type: value()}
s.Install()
}
}