-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
290 lines (258 loc) · 6.85 KB
/
Copy pathparser.js
File metadata and controls
290 lines (258 loc) · 6.85 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
/*
* Parser module
*
* var parse = require('./parser')
*
* var program = parse(tokens)
*/
var scanner = require('./scanner')
var error = require('./error')
var Program = require('./entities/program')
var Block = require('./entities/block')
var Type = require('./entities/type')
var VariableDeclaration = require('./entities/variableDeclaration')
var MinFunction = require('./entities/minFunction')
var FunctionCall = require('./entities/functionCall')
var VariableReference = require('./entities/variableReference')
var AssignmentStatement = require('./entities/assignmentStatement')
var ConditionalStatement = require('./entities/conditionalStatement')
var ForStatement = require('./entities/forStatement')
var WhileStatement = require('./entities/whileStatement')
var ReturnStatement = require('./entities/returnStatement')
var BinaryExpression = require('./entities/binaryExpression')
var UnaryExpression = require('./entities/unaryExpression')
var NumericLiteral = require('./entities/numericLiteral')
var StringLiteral = require('./entities/stringLiteral')
var WriteStatement = require('./entities/writeStatement')
var tokens
module.exports = function (scanner_output) {
tokens = scanner_output
var program = parseProgram()
match('EOF')
return program
}
function parseProgram() {
return new Program(parseBlock())
}
function parseBlock() {
var statements = []
while (at(['?',':','\'','`','@','%',':?','_',';','#','$','ID'])) {
statements.push(parseStatement())
at('DEDENT') ? match('DEDENT') : match('NEWLINE')
}
if (at(['DEDENT', 'EOF'])) { //DEDENT or eof expected to end a block
return new Block(statements)
}
}
function parseStatement() {
if (at(['#', '$', ';', '_'])) {
return parseDeclaration()
} else if (at(['?',':?'])) {
return parseConditional()
} else if (at(['@','%'])) {
return parseLoop()
} else if (at(['\''])) {
return parsePrint()
} else if (at(['`'])) {
return parseReturn()
} else if (at(['ID']) && tokens.length > 1 && tokens[1].kind === '(') {
return parseFunctionCall()
} else if (at(['ID'])) {
return parseAssignmentStatement()
}
}
function parseDeclaration() {
var decTypeSymbol = match().kind,
type = "",
id = match('ID'),
assignment = ""
if (decTypeSymbol==='#') {
type = Type.NUMBER
} else if (decTypeSymbol==='$') {
type = Type.STRING
} else if (decTypeSymbol==='_') {
type = Type.FUNCTION
}
assignment = null
if(at('=')) {
match('=')
if (decTypeSymbol==='#') {
assignment = parseExpression()
} else if (decTypeSymbol==='$') {
assignment = parseString()
} else if (decTypeSymbol==='_') {
assignment = parseFunction()
}
}
return new VariableDeclaration(id, type, assignment)
}
function parseFunction() {
var parameters = []
match('(')
while(!at(')')) {
if(at(',')) {
match(',')
}
parameters.push(parseDeclaration())
}
match(')')
body = parseBody()
return new MinFunction(parameters, body)
}
function parseFunctionCall() {
var parameters = []
id = new VariableReference(match('ID'))
match('(')
while(!at(')')) {
if(at(',')) {
match(',')
}
parameters.push(parseExpression())
}
match(')')
return new FunctionCall(id, parameters)
}
function parseAssignmentStatement() {
var target = new VariableReference(match('ID')),
assignment = '='
if (at(['=','+=','-=','*=','/=']))
assignment = match().lexeme
var source = parseExpression()
return new AssignmentStatement(target, assignment, source)
}
function parseBody() {
match('BLOCK')
match('INDENT')
return parseBlock()
}
function parseConditional() {
var conditional = match().kind,
elseBody,
condition,
body
if (conditional == '?' || conditional == ':?')
condition = parseExpression()
body = parseBody()
if (at('DEDENT') && tokens[1].kind === ':') {
match('DEDENT')
elseBody = parseElse()
} else if (at('DEDENT') && tokens[1].kind === ':?') {
match('DEDENT')
elseBody = new Block([parseStatement()])
}
return new ConditionalStatement(condition, body, elseBody)
}
function parseElse() {
match(':')
return parseBody()
}
function parseLoop() {
var loopType = match().kind
if (loopType == '%') { //for loop
var declaration = parseDeclaration()
match(',')
var condition = parseExpression()
match(',')
var assignment = parseAssignmentStatement(),
body = parseBody()
return new ForStatement(declaration, condition, assignment, body)
} else { //while
var condition = parseExpression(),
body = parseBody()
return new WhileStatement(condition, body)
}
}
function parsePrint() {
match('\'')
return new WriteStatement(parseString())
}
function parseReturn() {
match('`')
var returnValue = parseExpression()
return new ReturnStatement(returnValue)
}
function parseString() { //TODO fix up with concatenation with IDs and such
var left = match().lexeme
while (at(' ')) {
match(' ')
left += parseString()
}
return new StringLiteral(left)
}
function parseExpression() {
var left = parseExp1()
while (at(['|','&'])) {
var op = match()
var right = parseExp1()
left = new BinaryExpression(op, left, right)
}
return left
}
function parseExp1() {
var left = parseExp2()
if (at(['~','~>','<~','<','>'])) {
var op = match()
var right = parseExp2()
left = new BinaryExpression(op, left, right)
}
return left
}
function parseExp2() {
var left = parseExp3()
while (at(['+','-', ' '])) {
var op = match()
var right = parseExp3()
left = new BinaryExpression(op, left, right)
}
return left
}
function parseExp3() {
var left = parseExp4()
while (at(['*','/'])) {
var op = match()
var right = parseExp4()
left = new BinaryExpression(op, left, right)
}
return left
}
function parseExp4() {
if (at('-')) {
var op = match()
operand = parseExp5()
return new UnaryExpression(op, operand)
} else {
return parseExp5()
}
}
function parseExp5() {
if (at('NUMLIT')) {
return new NumericLiteral(match())
} else if (at('ID')) {
return new VariableReference(match())
} else if (at('STRLIT')) {
return new StringLiteral(match())
} else if (at('(')) {
match()
var expression = parseExpression()
match(')')
return expression
}
}
function at(symbol) {
if (tokens.length === 0) {
return false
} else if (Array.isArray(symbol)) {
return symbol.some(function (s) {return at(s)})
} else {
return symbol === tokens[0].kind
}
}
function match(symbol) {
if (tokens.length === 0) {
error('Unexpected end of input')
} else if (symbol === undefined || symbol === tokens[0].kind) {
return tokens.shift()
} else {
error('Expected ' + symbol + ' but found ' + tokens[0].kind, tokens[0])
}
}