-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpl0.ts
More file actions
373 lines (322 loc) · 7.62 KB
/
pl0.ts
File metadata and controls
373 lines (322 loc) · 7.62 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
369
370
371
372
373
import {
FixedEither,
Either,
Left,
OneOrMore,
Optional,
Pair,
Parser,
ParseResult,
Right,
Sequence,
ZeroOrMore,
} from "./combinators.ts";
import { isNumeric, Item, Match, Identifier } from "./simple.ts";
/// Helper parser combinators
const WhiteSpace = new Item()
.pred((ch) => ch === " ")
.map((_) => null)
const WhiteSpaces = new ZeroOrMore(WhiteSpace);
class BinaryOp<T, O> extends Parser<[T, O, T]> {
operand: Parser<T>
op: Parser<O>
parse(input: string): ParseResult<[T, O, T]> {
return new Pair(
this.operand,
new Pair(
this.op,
this.operand
)
)
.map(([a, [b, c]]: [T, [O, T]]) => [a, b, c] as [T, O, T])
.parse(input)
}
constructor(operand: Parser<T>, op: Parser<O>) {
super()
this.operand = operand
this.op = op
}
}
class Surrounded<T> extends Parser<T> {
left: Parser<any>
right: Parser<any>
middle: Parser<T>
parse(input: string): ParseResult<T> {
return new Right(this.left, new Left(this.middle, this.right)).parse(input)
}
constructor(left: Parser<any>, middle: Parser<T>, right?: Parser<any>) {
super()
this.left = left
this.middle = middle
if (right) {
this.right = right
} else {
this.right = left
}
}
}
class SpaceSurrounded<T> extends Parser<T> {
parser: Parser<T>
parse(input: string): ParseResult<T> {
return new Surrounded(WhiteSpaces, this.parser).parse(input)
}
constructor(parser: Parser<T>) {
super()
this.parser = parser
}
}
/// Syntax tree parser combinators
const IntLiteral = new OneOrMore(new Item().pred(isNumeric))
.map((value) => new LiteralExpr(parseInt(value.join(""))))
class Factor extends Parser<Expr> {
parse(input: string): ParseResult<Expr> {
return new SpaceSurrounded(new FixedEither(
new Identifier()
.map((ident: string) => new IdentExpr(ident)),
IntLiteral,
new Surrounded(
new Match("("),
new ExprParser(),
new Match(")")
)
))
.parse(input)
}
}
class Term extends Parser<Expr> {
parse(input: string): ParseResult<Expr> {
return new Pair(
new Factor(),
new ZeroOrMore(
new Pair(
new FixedEither(new Match("*"), new Match("/")),
new Factor()
)
)
)
// Transform the Term parse result to a syntax tree
.map(([factor, right]) => {
// Check if there are right elements
if (right.length == 0) {
return factor;
}
// There is at least 1 right element, create the base bin_op_expr
let it = right[Symbol.iterator]();
let [op, left_expr] = it.next().value;
let bin_op_expr = new BinOpExpr(factor, op, left_expr);
// With the remaining [op, left_expr] element create the syntax tree from
// bottom to top
let res = it.next();
while (!res.done) {
let [op, left_expr] = res.value;
bin_op_expr = new BinOpExpr(bin_op_expr, op, left_expr);
res = it.next();
}
return bin_op_expr;
})
.parse(input)
}
}
class ExprParser extends Parser<Expr> {
parse(input: string): ParseResult<Expr> {
return new Pair(
new Pair(
new Optional(new FixedEither(
new Match("+"),
new Match("-")
)),
new Term()
)
.map(([unary_op, term]) => {
// Build unary expression in case there is a unary operator
if (unary_op) {
return new UnaryOpExpr(unary_op, term)
}
return term
}),
new ZeroOrMore(
new Pair(
new FixedEither(new Match("+"), new Match("-")),
new Term()
)
)
)
.map(([term, right]) => {
// Check if there is any right elements
if (right.length == 0) {
return term
}
// There is at least 1 right element, create the base bin_op_expr
let it = right[Symbol.iterator]()
let [op, left_expr] = it.next().value;
let bin_op_expr = new BinOpExpr(term, op, left_expr)
// With the remaining [op, left_expr] element create the syntax tree from
// bottom to top
let res = it.next()
while (!res.done) {
let [op, left_expr] = res.value;
bin_op_expr = new BinOpExpr(bin_op_expr, op, left_expr)
res = it.next()
}
return bin_op_expr
})
.parse(input)
}
}
class ConditionParser extends Parser<Condition> {
parse(input: string): ParseResult<Condition> {
return new FixedEither(
new Pair(
new Match("odd"),
new ExprParser()
)
.map(([op, expr]) => {
return new UnaryCondition(op, expr) as Condition
}),
new BinaryOp(
new ExprParser(),
new FixedEither(
new Match("="),
new Match("#"),
new Match("<"),
new Match("<="),
new Match(">"),
new Match(">=")
)
)
.map(([left_expr, op, right_expr]) => {
return new BinCondition(left_expr, op, right_expr) as Condition
})
)
.parse(input)
}
}
class VarAssignStmtParser extends Parser<VarAssignStmt> {
parse(input: string): ParseResult<VarAssignStmt> {
return new Pair(
new SpaceSurrounded(new Identifier()),
new Right(
new Match(":="),
new ExprParser()
)
)
.map(([id, expr]) => {
return new VarAssignStmt(id, expr)
})
.parse(input)
}
}
class CallStmtParser extends Parser<CallStmt> {
parse(input: string): ParseResult<CallStmt> {
return new Right(
new SpaceSurrounded(new Match("call")),
new Identifier()
)
.map((id) => new CallStmt(id))
.parse(input)
}
}
class WriteStmtParser extends Parser<WriteStmt> {
parse(input: string): ParseResult<WriteStmt> {
return new Right(
new Pair(
new Match("!"),
WhiteSpaces
),
new ExprParser()
)
.map((expr) => new WriteStmt(expr))
.parse(input)
}
}
class StmtParser extends Parser<Stmt> {
parse(input: string): ParseResult<Stmt> {
return new FixedEither(
new VarAssignStmtParser(),
new CallStmtParser(),
new WriteStmtParser()
)
.parse(input)
}
}
/// AST definitions
abstract class Expr {}
class LiteralExpr extends Expr {
literal: number;
constructor(literal: number) {
super();
this.literal = literal;
}
}
class IdentExpr extends Expr {
name: string;
constructor(name: string) {
super();
this.name = name;
}
}
class BinOpExpr extends Expr {
left: Expr
op: string
right: Expr
constructor(left: Expr, op: string, right: Expr) {
super()
this.left = left
this.op = op
this.right = right
}
}
class UnaryOpExpr extends Expr {
op: string
e: Expr
constructor(op: string, e: Expr) {
super()
this.op = op
this.e = e
}
}
abstract class Condition {}
class UnaryCondition extends Condition {
op: string
expr: Expr
constructor(op: string, expr: Expr) {
super()
this.op = op
this.expr = expr
}
}
class BinCondition extends Condition {
left: Expr
op: string
right: Expr
constructor(left: Expr, op: string, right: Expr) {
super()
this.left = left
this.op = op
this.right = right
}
}
abstract class Stmt {}
class VarAssignStmt extends Stmt {
id: string
value: Expr
constructor(id: string, value: Expr) {
super()
this.id = id
this.value = value
}
}
class CallStmt extends Stmt {
id: string
constructor(id: string) {
super()
this.id = id
}
}
class WriteStmt extends Stmt {
expr: Expr
constructor(expr: Expr) {
super()
this.expr = expr
}
}