-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
312 lines (253 loc) · 7.12 KB
/
parser.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
// import "C"
type Parser struct {
source Source
curToken Token
peekToken Token
emitter Emitter
}
// Return true if the current token matches.
func (p Parser) checkToken(kind int) bool {
return kind == p.curToken.kind
}
// Return true if the next token matches.
func (p Parser) checkPeek(kind int) bool {
return kind == p.peekToken.kind
}
func getTokenText(kind int) string {
for key, value := range tokens {
if kind == value {
return key
}
}
return ""
}
// Try to match current token. If not, error. Advances the current token.
func (p *Parser) match(kind int) {
if !p.checkToken(kind) {
msg := "Expected " + getTokenText(kind) + " got " + getTokenText(p.curToken.kind)
panic(msg)
}
p.nextToken()
}
// Advances the current token.
func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.source.getToken()
}
// ******************************************* PRODUCTION RULES *******************************************
func (p *Parser) nl() {
println("NEW LINE")
//require atleast one newline
p.match(tokens["NEWLINE"])
//skip any extra newlines
for p.checkToken(tokens["NEWLINE"]) {
p.nextToken()
}
}
// primary ::= number | ident
func (p *Parser) primary() {
println("PRIMARY(", p.curToken.text, ")")
if p.checkToken(tokens["NUMBER"]) {
p.emitter.emit(p.curToken.text)
p.nextToken()
} else if p.checkToken(tokens["IDENT"]) {
//ensure the variable already exists
if !p.source.symbols.Contains(p.curToken.text) {
msg := "Referencing variable before assignment -> " + p.curToken.text
panic(msg)
}
p.emitter.emit(p.curToken.text)
p.nextToken()
} else {
msg := "Unexpected token at start of primary: " + getTokenText(p.curToken.kind)
panic(msg)
}
}
// primary ::= number | ident
func (p *Parser) unary() {
println("UNARY")
if p.checkToken(tokens["MINUS"]) || p.checkToken(tokens["PLUS"]) {
p.emitter.emit(p.curToken.text)
p.nextToken()
}
p.primary()
}
// term ::= unary {( "/" | "*" ) unary}
func (p *Parser) term() {
println("TERM")
p.unary()
// # Can have 0 or more *// and expressions.
for p.checkToken(tokens["ASTERISK"]) || p.checkToken(tokens["SLASH"]) {
p.emitter.emit(p.curToken.text)
p.nextToken()
p.unary()
}
}
// expression ::= term {( "-" | "+" ) term}
func (p *Parser) expression() {
println("EXPRESSION")
p.term()
for p.checkToken(tokens["PLUS"]) || p.checkToken(tokens["MINUS"]) {
p.emitter.emit(p.curToken.text)
p.nextToken()
p.term()
}
}
func (p *Parser) isComparisonOperator() bool {
return p.checkToken(tokens["EQEQ"]) || p.checkToken(tokens["NOTEQ"]) || p.checkToken(tokens["GT"]) || p.checkToken(tokens["GTEQ"]) || p.checkToken(tokens["LT"]) || p.checkToken(tokens["LTEQ"])
}
// comparison ::= expression (("==" | "!=" | ">" | ">=" | "<" | "<=") expression)+
func (p *Parser) comparison() {
println("comparison")
p.expression()
// Must be at least one comparison operator and another expression.
if p.isComparisonOperator() {
p.emitter.emit(p.curToken.text)
p.nextToken()
p.expression()
} else {
msg := "Expected comparison operator, got " + getTokenText(p.curToken.kind)
panic(msg)
}
for p.isComparisonOperator() {
p.emitter.emit(p.curToken.text)
p.nextToken()
p.expression()
}
}
// statements
func (p *Parser) statement() {
//Check the first token to see what kind of statement this is.
// # "DEKHAO" (expression | string)
if p.checkToken(tokens["DEKHAO"]) {
// "DEKHAO" (expression | string)
println("DEKHAO-statement")
p.nextToken()
if p.checkToken(tokens["STRING"]) {
//string
println("STRING", p.curToken.text)
pr := "printf(\"" + p.curToken.text + "\\n\"" + ");"
p.emitter.emitLine(pr)
p.nextToken()
} else {
//expression
p.emitter.emit("printf(\"%" + ".2f\\n\", (float)(")
p.expression()
p.emitter.emitLine("));")
}
} else if p.checkToken((tokens["AGR"])) {
// "AGR" comparison "PHR" {statement} "AGRBND"
println("AGR-statement")
p.nextToken()
p.emitter.emit("if(")
p.comparison()
p.match(tokens["PHR"])
p.nl()
p.emitter.emitLine("){")
for !p.checkToken(tokens["AGRBND"]) {
p.statement()
}
p.match(tokens["AGRBND"])
p.emitter.emitLine("}")
} else if p.checkToken((tokens["JAB"])) {
// "JAB" comparison "KARO" {statement} "JABBND"
println("JAB-statement")
p.nextToken()
p.emitter.emit("while(")
p.comparison()
p.match(tokens["KARO"])
p.nl()
p.emitter.emitLine("){")
for !p.checkToken(tokens["JABBND"]) {
p.statement()
}
p.match(tokens["JABBND"])
p.emitter.emitLine("}")
} else if p.checkToken((tokens["DEKHAO"])) {
// "DEKHAO" ident
println("DEKHAO-statement")
p.nextToken()
// Make sure this label doesn't already exist.
if p.source.labelsDeclared.Contains(p.curToken.text) {
msg := "Label already exists " + p.curToken.text
panic(msg)
}
p.source.labelsDeclared.Add(p.curToken.text)
p.emitter.emitLine(p.curToken.text + ":")
p.match(tokens["IDENT"])
} else if p.checkToken((tokens["JAO"])) {
// "JAO" ident
println("JAO-statement")
p.nextToken()
p.source.labelsGotoed.Add(p.curToken.text)
p.emitter.emitLine("goto " + p.curToken.text + ";")
p.match(tokens["IDENT"])
} else if p.checkToken((tokens["NAM"])) {
// "NAM" ident "=" expression
println("NAM-statement")
p.nextToken()
// # Check if ident exists in symbol table. If not, declare it.
if !p.source.symbols.Contains(p.curToken.text) {
p.source.symbols.Add(p.curToken.text)
p.emitter.headerLine("float " + p.curToken.text + ";")
}
p.emitter.emit(p.curToken.text + " = ")
p.match(tokens["IDENT"])
p.match(tokens["EQ"])
p.expression()
p.emitter.emitLine(";")
} else if p.checkToken((tokens["BTAO"])) {
// "BTAO" ident
println("BTAO-statement")
p.nextToken()
//If variable doesn't already exist, declare it.
if !p.source.symbols.Contains(p.curToken.text) {
p.source.symbols.Add(p.curToken.text)
p.emitter.headerLine("float " + p.curToken.text + ";")
}
p.emitter.emitLine("if(0 == scanf(\"%" + "f\", &" + p.curToken.text + ")) {")
p.emitter.emitLine(p.curToken.text + " = 0;")
p.emitter.emit("scanf(\"%")
p.emitter.emitLine("*s\");")
p.emitter.emitLine("}")
p.match(tokens["IDENT"])
} else {
//error
println("ERROR")
msg := "Unexpected token at start of statement: " + getTokenText(p.curToken.kind) + "(" + p.curToken.text + ")"
panic(msg)
}
p.nl()
}
// program ::= {statement}
func (p *Parser) program() {
println("program")
//skip any newlines at the start
p.emitter.headerLine("#include <stdio.h>")
p.emitter.headerLine("int main(void){")
for p.checkToken(tokens["NEWLINE"]) {
p.nextToken()
}
for !p.checkToken(tokens["EOF"]) {
p.statement()
}
p.emitter.emitLine("return 0;")
p.emitter.emitLine("}")
// Check that each label referenced in a JAO is declared.
for key, _ := range p.source.labelsGotoed.m {
if !p.source.labelsDeclared.Contains(key) {
msg := "Attempting to JAO to undeclared label: " + key
panic(msg)
}
}
}
// ************************* MAIN *************************
func do_parsing(p Parser) {
//called two times to set curToken and peekToken
p.nextToken()
p.nextToken()
p.program()
p.emitter.writeFile()
// println("DONE PARSING")
}