-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
368 lines (328 loc) · 11.4 KB
/
parser.js
File metadata and controls
368 lines (328 loc) · 11.4 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { WebScriptError } from './stdlib.js'
import { TOKENS } from './lexer.js'
import Ast from './ast.js'
// Function to check if a token is an operator
const isOp = type =>
[
TOKENS.Or,
TOKENS.And,
TOKENS.Equiv,
TOKENS.NotEquiv,
TOKENS.Gt,
TOKENS.Gte,
TOKENS.Lt,
TOKENS.Lte,
TOKENS.Plus,
TOKENS.Minus,
TOKENS.Asterisk,
TOKENS.Slash
].includes(type)
// Define the order of operations (PEDMAS, BODMAS, BIMDAS, etc.)
const opOrder = {
'<': 0,
'<=': 0,
'>': 0,
'>=': 0,
'!=': 0,
'==': 0,
'&&': 0,
'||': 0,
'+': 1,
'-': 1,
'*': 2,
'/': 2
}
// Contains all the attributes and methods in the parser
export class Parser {
constructor(tokens) {
this.tokens = tokens
this.ast = []
this.current = 0
}
error(token, msg) {
throw new WebScriptError(
`Syntax error on ${token.line}:${token.column}: ${msg}`
)
}
// Return the next token unless it's EOF
peek() {
if (this.current >= this.tokens.length) return null
return this.tokens[this.current]
}
// Return the type of the next token
peekType() {
if (this.current >= this.tokens.length) return null
return this.tokens[this.current].type
}
// Return the next token if it's a keyword
peekKeyword(keyword) {
if (this.peekType() !== TOKENS.Keyword || this.peek().value !== keyword)
return null
return this.peek()
}
// Return the next token if it's the specified type, otherwise throw an error
eat(type) {
if (this.peekType() === type) return this.tokens[this.current++]
this.error(
this.peek(),
`Expected ${type} but got ${this.peekType().toString()}`
)
}
// Return the next token if it's the specified keyword, otherwise throw an error
eatKeyword(keyword) {
if (this.peekType() !== TOKENS.Keyword)
this.error(
this.peek(),
`Expected ${TOKENS.Keyword} but got ${this.peekType()}`
)
else if (this.peek().value !== keyword)
this.error(
this.peek(),
`Expected keyword ${keyword} but got keyword ${this.peek().value}`
)
return this.eat(TOKENS.Keyword)
}
// Return a list of expressions
exprList() {
let exprs = []
exprs.push(this.expr())
while (this.peekType() === TOKENS.Comma) {
this.eat(TOKENS.Comma)
exprs.push(this.expr())
}
return exprs
}
// Return a list of identifiers
identifierList() {
let identifiers = []
identifiers.push(this.eat(TOKENS.Identifier).value)
while (this.peekType() === TOKENS.Comma) {
this.eat(TOKENS.Comma)
identifiers.push(this.eat(TOKENS.Identifier).value)
}
return identifiers
}
// Handle instances, strings, numbers, booleans, arrays, and variables
simple() {
let token = this.eat(this.peekType())
switch (token.type) {
case TOKENS.Keyword: {
if (token.value === 'prep') {
const id = this.eat(TOKENS.Identifier).value
this.eat(TOKENS.LeftParen)
let members = {}
while (this.peekType() !== TOKENS.RightParen) {
const member = this.eat(TOKENS.Identifier).value
this.eat(TOKENS.Colon)
members[member] = this.expr()
if (this.peekType() === TOKENS.Comma) this.eat(TOKENS.Comma)
}
this.eat(TOKENS.RightParen)
return new Ast.Instance(id, members)
}
break
}
case TOKENS.String:
case TOKENS.Number:
case TOKENS.Boolean: {
return new Ast.Literal(token.content)
}
case TOKENS.LeftBracket: {
let items = []
if (this.peekType() !== TOKENS.RightBracket) items = this.exprList()
this.eat(TOKENS.RightBracket)
return new Ast.Array(items)
}
case TOKENS.Identifier: {
return new Ast.Var(token.value)
}
case TOKENS.LeftParen: {
const expr = this.expr()
this.eat(TOKENS.RightParen)
return expr
}
}
this.error(token, "Expected expression but got " + token)
}
// Handle calls (object.func(), object.attr, etc.)
call() {
let expr = this.simple()
while (true) {
if (this.peekType() === TOKENS.LeftParen) {
this.eat(TOKENS.LeftParen)
let args = []
if (this.peekType() !== TOKENS.RightParen) args = this.exprList()
this.eat(TOKENS.RightParen)
expr = new Ast.Call(expr, args)
} else if (this.peekType() === TOKENS.LeftBracket) {
this.eat(TOKENS.LeftBracket)
const property = this.expr()
this.eat(TOKENS.RightBracket)
expr = new Ast.Get(expr, property, true)
} else if (this.peekType() === TOKENS.Period) {
this.eat(TOKENS.Period)
const property = this.eat(TOKENS.Identifier).value
expr = new Ast.Get(expr, property)
} else break
}
return expr
}
// Handle NOT
unary() {
if (this.peekType() === TOKENS.Not) {
const op = this.eat(this.peekType()).value
return new Ast.Unary(op, this.unary())
}
return this.call()
}
// Handle binary expressions
expr() {
const left = this.unary()
if (isOp(this.peekType())) {
const op = this.eat(this.peekType()).value
let right = this.expr()
if (right instanceof Ast.Binary && opOrder[op] > opOrder[right.operator])
return new Ast.Binary(
new Ast.Binary(left, op, right.left),
right.operator,
right.right
)
return new Ast.Binary(left, op, right)
}
return left
}
// Handle statements: returns, functions, for loops, while loops, structs, conditionals, and variable assignments
stmt() {
const returnStmt = () => {
this.eatKeyword('return')
return new Ast.Return(this.expr())
}
// Functions
const funcStmt = () => {
this.eatKeyword('func')
const name = this.eat(TOKENS.Identifier).value
// Parameters
let params = []
if (this.peekKeyword('needs')) {
this.eatKeyword('needs')
this.eat(TOKENS.LeftParen)
params = this.identifierList()
this.eat(TOKENS.RightParen)
}
// Body
this.eat(TOKENS.LeftBrace)
let body = []
while (this.peekType() !== TOKENS.RightBrace) body.push(this.stmt())
this.eat(TOKENS.RightBrace)
return new Ast.Func(name, params, body)
}
const forStmt = () => {
// Counter (i)
this.eatKeyword('loop')
const id = this.eat(TOKENS.Identifier).value
this.eatKeyword('through')
// Start and End
this.eat(TOKENS.LeftParen)
const range = this.exprList()
if (range.length !== 2)
this.error(
range[range.length - 1],
'Expected (start, end) range but received more arguments than expected'
)
this.eat(TOKENS.RightParen)
// Body
this.eat(TOKENS.LeftBrace)
let body = []
while (this.peekType() !== TOKENS.RightBrace) body.push(this.stmt())
this.eat(TOKENS.RightBrace)
return new Ast.For(id, range, body)
}
const whileStmt = () => {
this.eatKeyword('while')
// Condition
this.eat(TOKENS.LeftParen)
const condition = this.expr()
this.eat(TOKENS.RightParen)
// Body
this.eat(TOKENS.LeftBrace)
let body = []
while (this.peekType() !== TOKENS.RightBrace) body.push(this.stmt())
this.eat(TOKENS.RightBrace)
return new Ast.While(condition, body)
}
const conditionalStmt = keyword => {
this.eatKeyword(keyword)
// Condition
let condition = new Ast.Literal(true)
if (keyword !== 'else') {
this.eat(TOKENS.LeftParen)
condition = this.expr()
this.eat(TOKENS.RightParen)
}
// Body
this.eat(TOKENS.LeftBrace)
let body = []
while (this.peekType() !== TOKENS.RightBrace) body.push(this.stmt())
this.eat(TOKENS.RightBrace)
// Elif/Else
let otherwise = []
while (this.peekKeyword('elif') || this.peekKeyword('else'))
otherwise.push(conditionalStmt(this.peek().value))
return new Ast.Conditional(condition, body, otherwise)
}
// Variable assignments
const assignStmt = () => {
this.eatKeyword('prepare')
const name = this.eat(TOKENS.Identifier).value
if (this.peekType() === TOKENS.Period) {
this.eat(TOKENS.Period)
const property = this.eat(TOKENS.Identifier).value
this.eatKeyword('as')
const value = this.expr()
return new Ast.Set(name, property, value)
}
this.eatKeyword('as')
const value = this.expr()
return new Ast.Var(name, value)
}
const structStmt = () => {
this.eatKeyword('type')
const name = this.eat(TOKENS.Identifier).value
this.eatKeyword('has')
this.eat(TOKENS.LeftBrace)
const members = this.identifierList()
this.eat(TOKENS.RightBrace)
return new Ast.Struct(name, members)
}
// Call one of the above functions depending on the Keyword
const next = this.peek()
switch(next.type) {
case TOKENS.Keyword: {
switch (next.value) {
case 'type':
return structStmt()
case 'prepare':
return assignStmt()
case 'if':
return conditionalStmt('if')
case 'while':
return whileStmt()
case 'loop':
return forStmt()
case 'finished':
return returnStmt()
case 'func':
return funcStmt()
}
}
default: {
return this.expr()
}
}
}
// Run the reverse chain (statements, expressions, unary operators, calls, and simple expressions) in a loop over all tokens
parse() {
while (this.peekType() !== TOKENS.EOF) this.ast.push(this.stmt())
return this.ast
}
}