-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
168 lines (131 loc) · 2.19 KB
/
nodes.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
package main
import "github.com/ajsnow/llvm"
// Node Nodes
type node interface {
Kind() nodeType
// String() string
Position() Pos
codegen() llvm.Value
}
type nodeType int
// Pos defines a byte offset from the beginning of the input text.
type Pos int
func (p Pos) Position() Pos {
return p
}
// In text/template/parse/node.go Rob adds an unexported() method to Pos
// I do know why he did that rather than make Pos -> pos
// Type returns itself, embedding into Nodes
func (t nodeType) Kind() nodeType {
return t
}
const (
// literals
nodeNumber nodeType = iota
// expressions
nodeIf
nodeFor
nodeUnary
nodeBinary
nodeFnCall
nodeVariable
nodeVariableExpr
// non-expression statements
nodeFnPrototype
nodeFunction
// other
nodeList
)
type numberNode struct {
nodeType
Pos
val float64
}
// func NewNumberNode(t token, val float64) *numberNode {
// return &numberNode{
// nodeType: nodeNumber,
// Pos: t.pos,
// val: val,
// }
// }
type ifNode struct {
nodeType
Pos
// psudeo-Hungarian notation as 'if' & 'else' are Go keywords
ifN node
thenN node
elseN node
}
// func NewIfNode(t token, ifN, thenN, elseN node) *ifNode {
// return &ifNode{
// nodeType: nodeIf,
// Pos: t.pos,
// ifN: ifN,
// thenN: thenN,
// elseN: elseN,
// }
// }
type forNode struct {
nodeType
Pos
counter string
start node
test node
step node
body node
}
// func NewForNode(t token, counter string, start, test, step, body node) *forNode {
// return &forNode{nodeFor, t.pos, counter, start, test, step, body}
// }
type unaryNode struct {
nodeType
Pos
name string
operand node
}
type binaryNode struct {
nodeType
Pos
op string
left node
right node
}
type fnCallNode struct {
nodeType
Pos
callee string
args [](node)
}
type variableNode struct {
nodeType
Pos
name string
}
type variableExprNode struct {
nodeType
Pos
vars []struct {
name string
node node
}
body node
}
type fnPrototypeNode struct {
nodeType
Pos
name string
args []string
isOperator bool
precedence int
}
type functionNode struct {
nodeType
Pos
proto node
body node
}
type listNode struct {
nodeType
Pos
nodes []node
}