-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.py
698 lines (575 loc) · 21.6 KB
/
stream.py
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
from fractions import Fraction
from dataclasses import dataclass
from typing import Optional, NewType
from utils.errors import EndOfStream, EndOfTokens, TokenError, StringError, ListOpError
from utils.datatypes import Num, Bool, Keyword, Symbols, ListUtils, Identifier, StringToken, ListToken, Operator, Whitespace, NumLiteral, BinOp, UnOp, Variable, Let, Assign, If, BoolLiteral, UnOp, ASTSequence, AST, Buffer, ForLoop, Range, Declare, While, DoWhile, Print, funct_call, funct_def, funct_ret, StringLiteral, StringSlice, ListObject, ListCons, ListOp, ListIndex
from core import RuntimeEnvironment
keywords = "let assign for while repeat print declare range do to if then else in deffunct callfun functret".split()
symbolic_operators = "+ - * ** / < > <= >= == != = % & && || | !".split()
word_operators = "and or not ".split()
whitespace = [" ", "\n"]
symbols = "; , ( ) { } [ ] ' .".split()
list_utils = "cons head tail empty".split()
r = RuntimeEnvironment()
@dataclass
class Stream:
source: str
pos: int
def from_string(string:str , position:int = 0):
"""
Creates a stream from a string. Position reset to 0.
"""
return Stream(string, position)
def next_char(self):
"""
Returns the next character in the stream.
"""
if self.pos >= len(self.source):
raise EndOfStream()
self.pos = self.pos + 1
return self.source[self.pos - 1]
def unget(self):
"""
Moves the stream back one character.
"""
assert self.pos > 0
self.pos = self.pos - 1
# Define the token types.
Token = Num | Bool | Keyword | Identifier | Operator | Symbols | StringToken | ListToken | Whitespace
def word_to_token(word):
if word in keywords:
return Keyword(word)
if word in word_operators:
return Operator(word)
if word == "True":
return Bool(True)
if word == "False":
return Bool(False)
if word in symbolic_operators:
return Operator(word)
if word in symbols:
return Symbols(word)
if word in whitespace:
return Whitespace(word)
return Identifier(word)
@dataclass
class Lexer:
stream: Stream
save: Token = None
def from_stream(s):
return Lexer(s)
def next_token(self) -> Token:
try:
match self.stream.next_char():
case c if c.isdigit():
# TODO: Handle different bases.
n = int(c)
base_multiplier = 10
floating = False
while True:
try:
c = self.stream.next_char()
if c.isdigit() and not floating:
n = n*base_multiplier + int(c)
elif c.isdigit() and floating:
n = n + int(c) / base_multiplier
base_multiplier = base_multiplier * 10
elif c == ".":
if floating:
raise Exception("Cannot have two decimal points in a number.")
floating = floating | True
else:
self.stream.unget()
return Num(n, floating= floating)
except EndOfStream:
return Num(n)
case c if c in symbolic_operators:
s = c
while True:
try:
c = self.stream.next_char()
if c =="-":
self.stream.unget()
return word_to_token(s)
elif c in symbolic_operators:
s = s + c
else:
self.stream.unget()
return word_to_token(s)
except EndOfStream:
return word_to_token(s)
case c if c.isalpha():
s = c
while True:
try:
c = self.stream.next_char()
if c.isalpha():
s = s + c
else:
self.stream.unget()
return word_to_token(s)
except EndOfStream:
return word_to_token(s)
case c if c in symbols:
s = c
return word_to_token(s)
case c if c in whitespace:
return self.next_token()
except EndOfStream:
raise EndOfTokens
def peek_token(self):
"""
Peeks at the next token, but doesn't advance the stream.
"""
if self.save is not None:
return self.save
self.save = self.next_token()
return self.save
def match(self, expected):
"""
Matches the next token to the expected token.
"""
if self.peek_token() == expected:
return self.advance()
else:
raise TokenError(f"Expected {expected}, got {self.peek_token()}")
def advance(self):
"""
Advances the stream by one token.
"""
assert self.save is not None
self.save = None
def __iter__(self):
return self
def __next__(self):
try:
self.curr_token = self.next_token()
return self.curr_token
except EndOfTokens:
raise StopIteration
@dataclass
class Parser:
lexer: Lexer
def from_lexer(lexer):
"""
Generate a parser from a lexer.
"""
return Parser(lexer)
def parse_expression(self):
"""
Parse a complete expression.
For | a == b |, this will parse the entire expression.
"""
match self.lexer.peek_token():
case Keyword("if"):
return self.parse_if()
case Keyword("while"):
return self.parse_while()
case Keyword("let"):
return self.parse_let()
case Keyword("assign"):
return self.parse_assign()
case Keyword("for"):
return self.parse_for()
case Keyword("range"):
return self.parse_range()
case Keyword("print"):
return self.parse_print()
case Keyword("declare"):
return self.parse_declare()
case Keyword("while"):
return self.parse_while()
case Keyword("repeat"):
return self.parse_repeat()
case Keyword("deffunct"):
return self.parse_funct_def()
case Keyword("callfun"):
return self.parse_funct_call()
case Keyword("functret"):
return self.parse_funct_ret()
case Symbols(";"):
return self.lexer.__next__()
case Symbols("{"):
return self.parse_AST_sequence()
case Symbols("}"):
return self.lexer.__next__()
case Symbols(","):
return self.lexer.__next__()
case Symbols("'"):
return self.parse_string()
case Symbols("["):
return self.parse_list()
case _:
return self.parse_simple()
def parse_list_op(self,obj):
self.lexer.match(Symbols("."))
op_val_var = self.parse_atomic_expression()
if not isinstance(op_val_var,Variable):
raise ListOpError("Invalid function for lists.")
op_val = op_val_var.name
if op_val not in list_utils:
raise ListOpError("Invalid function for lists.")
# print(op_val)
if(op_val == 'cons'):
self.lexer.match(Symbols("("))
to_add = self.parse_expression()
self.lexer.match(Symbols(")"))
# print("In Cons")
# print(ListCons(to_add,obj))
return ListCons(to_add,obj)
else:
# print("In Rest Operations")
if(op_val=='empty'):
s = "is-"
s+=op_val
s+="?"
return ListOp(s,obj)
else:
return ListOp(op_val,obj)
def parse_slice(self,obj):
ind1 = self.parse_expression()
if(self.lexer.peek_token()==Symbols(",")):
self.lexer.advance()
ind2 = self.parse_expression()
return StringSlice(obj,ind1,ind2)
else:
return ListIndex(ind1,obj)
def parse_index(self,obj):
self.lexer.match(Symbols("["))
x = self.parse_slice(obj)
self.lexer.match(Symbols("]"))
# if not isinstance(op_val_var,Variable):
# raise ListOpError("Invalid function for lists.")
# op_val = op_val_var.name
# if op_val not in list_utils:
# raise ListOpError("Invalid function for lists.")
# print(op_val)
# if(op_val == 'cons'):
# self.lexer.match(Symbols("("))
# to_add = self.parse_expression()
# self.lexer.match(Symbols(")"))
# # print("In Cons")
# # print(ListCons(to_add,obj))
# return ListCons(to_add,obj)
# else:
# # print("In Rest Operations")
# if(op_val=='empty'):
# s = "is-"
# s+=op_val
# s+="?"
# return ListOp(s,obj)
# else:
# return ListOp(op_val,obj)
return x
def parse_list(self):
self.lexer.match(Symbols("["))
list_elems = []
while self.lexer.peek_token() != Symbols("]"):
x = self.parse_expression()
list_elems.append(x)
if self.lexer.peek_token() == Symbols("]"):
break
else:
self.lexer.match(Symbols(","))
self.lexer.match(Symbols("]"))
list_type = type(r.eval(list_elems[0]))
return ListObject(list_elems,list_type)
def parse_string(self):
self.lexer.match(Symbols("'"))
str_val_var = self.parse_atomic_expression()
if not isinstance(str_val_var,Variable):
raise StringError
str_val = str_val_var.name
self.lexer.match(Symbols("'"))
return StringLiteral(value=str_val)
def parse_atomic_expression(self):
"""
Parse an atomic expression.
For | a == b |, this will parse | a |.
"""
match self.lexer.peek_token():
case Identifier(name):
self.lexer.advance()
if(self.lexer.peek_token()==Symbols(".")):
return self.parse_list_op(obj=Variable(name))
elif(self.lexer.peek_token()==Symbols("[")):
return self.parse_index(obj=Variable(name))
return Variable(name)
case Num(value):
self.lexer.advance()
return NumLiteral(value)
case Bool(value):
self.lexer.advance()
return BoolLiteral(value)
def parse_addition(self):
"""
Parse an addition expression.
For | a + b |, this will parse the entire expression.
"""
left = self.parse_multiplication()
while True:
match self.lexer.peek_token():
case Operator(op) if op in "+-":
self.lexer.advance()
m = self.parse_multiplication()
left = BinOp(op, left, m)
case _:
break
return left
def parse_multiplication(self):
"""
Parse a multiplication expression.
For | a * b |, this will parse the entire expression.
"""
left = self.parse_mod()
while True:
match self.lexer.peek_token():
case Operator(op) if op in "*/":
self.lexer.advance()
m = self.parse_mod()
left = BinOp(op, left, m)
case _:
break
return left
def parse_uneg(self):
right = None
if(self.lexer.peek_token()==Operator("-")):
self.lexer.advance()
# right = self.parse_atomic_expression()
# return UnOp("-",right)
if (isinstance( self.lexer.peek_token(),Identifier) or isinstance( self.lexer.peek_token(),Num) or isinstance( self.lexer.peek_token(),Bool)):
m = self.parse_atomic_expression()
right = UnOp("-",m)
else:
m = self.parse_expression()
right = UnOp("-",m)
else:
if (isinstance( self.lexer.peek_token(),Identifier) or isinstance( self.lexer.peek_token(),Num) or isinstance( self.lexer.peek_token(),Bool)):
right = self.parse_atomic_expression()
else:
right = self.parse_expression()
return right
def parse_mod(self):
"""
Parse a mod expression.
For | a % b |, this will parse the entire expression.
"""
left = self.parse_uneg()
while True:
match self.lexer.peek_token():
case Operator(op) if op in "%":
self.lexer.advance()
m = self.parse_uneg()
left = BinOp(op, left, m)
case _:
break
return left
def parse_mod(self):
"""
Parse a mod expression.
For | a % b |, this will parse the entire expression.
"""
if (isinstance( self.lexer.peek_token(),Identifier) or isinstance( self.lexer.peek_token(),Num) or isinstance( self.lexer.peek_token(),Bool)):
left = self.parse_atomic_expression()
else:
left = self.parse_expression()
while True:
match self.lexer.peek_token():
case Operator(op) if op in "%":
self.lexer.advance()
if (isinstance( self.lexer.peek_token(),Identifier) or isinstance( self.lexer.peek_token(),Num) or isinstance( self.lexer.peek_token(),Bool)):
m = self.parse_atomic_expression()
left = BinOp(op, left, m)
else:
m = self.parse_expression()
left = BinOp(op, left, m)
case _:
break
return left
def parse_simple(self):
"""
Parse a &&/|| expression.
For | a && b |, this will parse the entire expression.
"""
left = self.parse_comparison()
match self.lexer.peek_token():
case Operator(op) if op in "&& ||":
self.lexer.advance()
right = self.parse_comparison()
return BinOp(op, left, right)
return left
def parse_comparison(self):
"""
Parse a comparison expression.
For | a == b |, this will parse the entire expression.
"""
left = self.parse_addition()
match self.lexer.peek_token():
case Operator(op) if op in symbolic_operators:
self.lexer.advance()
right = self.parse_addition()
return BinOp(op, left, right)
return left
def parse_let(self):
"""
Parse a let function.
Examples: | let a = 6 end |, to define a.
| let a = 6 in a + 1 end |, to define a and use it in an expression.
"""
self.lexer.match(Keyword("let"))
var = self.parse_atomic_expression()
self.lexer.match(Operator("="))
a = self.parse_expression()
self.lexer.match(Keyword("in"))
b = self.parse_expression()
self.lexer.match(Symbols(";"))
return Let(var, a, b)
def parse_if(self):
"""
Parse an if function.
Examples: | if a == b then a+2 else a+1 end |, to define a and use it in an expression.
"""
self.lexer.match(Keyword("if"))
cond = self.parse_expression()
self.lexer.match(Keyword("then"))
e1 = self.parse_expression()
if self.lexer.peek_token() != Keyword("else"):
return If(cond, e1, None)
self.lexer.match(Keyword("else"))
e2 = self.parse_expression()
self.lexer.match(Symbols(";"))
return If(cond, e1, e2)
def parse_assign(self):
"""
Parse an assignment.
Examples: | a = 6 |, to assign 6 to a.
"""
self.lexer.match(Keyword("assign"))
var = self.parse_atomic_expression()
self.lexer.match(Operator("="))
a = self.parse_expression()
self.lexer.match(Symbols(";"))
return Assign(var, a)
def parse_for(self):
"""
Parse a for loop.
Examples: | for a = 1 in 10 do a + 1 end |, to define a and use it in an expression.
"""
self.lexer.match(Keyword("for"))
var = self.parse_atomic_expression()
self.lexer.match(Keyword("in"))
iter = self.parse_expression()
self.lexer.match(Keyword("do"))
task = self.parse_expression()
self.lexer.match(Symbols(";"))
return ForLoop(var, iter, task)
def parse_range(self):
"""
Parse a range.
Examples: | range 1 to 10 |, to define a range from 1 to 10.
"""
self.lexer.match(Keyword("range"))
self.lexer.match(Symbols("("))
left = self.parse_atomic_expression()
self.lexer.match(Symbols(","))
right = self.parse_atomic_expression()
self.lexer.match(Symbols(")"))
return Range(left, right)
def parse_print(self):
"""
Parse a print statement.
Examples: | print a |, to print the value of a.
"""
self.lexer.match(Keyword("print"))
self.lexer.match(Symbols("("))
expression = self.parse_expression()
self.lexer.match(Symbols(")"))
self.lexer.match(Symbols(";"))
return Print(expression)
def parse_while(self):
"""
Parse a while loop.
Examples: | while a == b do a + 1 end |, to define a and use it in an expression.
"""
self.lexer.match(Keyword("while"))
cond = self.parse_expression()
self.lexer.match(Keyword("do"))
task = self.parse_expression()
self.lexer.match(Symbols(";"))
return While(cond, task)
def parse_repeat(self):
"""
Parse a repeat loop.
Examples: | repeat a + 1 until a == b |, to define a and use it in an expression.
"""
self.lexer.match(Keyword("repeat"))
task = self.parse_expression()
self.lexer.match(Keyword("while"))
cond = self.parse_expression()
self.lexer.match(Symbols(";"))
return DoWhile(task, cond)
def parse_declare(self):
"""
Parse a declaration.
Examples: | declare a = 10 |, to declare a."""
self.lexer.match(Keyword("declare"))
var = self.parse_atomic_expression()
self.lexer.match(Operator("="))
a = self.parse_expression()
self.lexer.match(Symbols(";"))
return Declare(var, a)
def parse_AST_sequence(self):
li = []
self.lexer.match(Symbols("{"))
while self.lexer.peek_token() != Symbols("}"):
var = self.parse_expression()
li.append(var)
self.lexer.match(Symbols("}"))
return ASTSequence(li)
def parse_funct_def(self):
li_3= []
li = []
self.lexer.match(Keyword("deffunct"))
var = self.parse_atomic_expression()
self.lexer.match(Symbols("("))
while self.lexer.peek_token() != Symbols(")"):
var_1 = self.parse_atomic_expression()
li.append(var_1)
if(self.lexer.peek_token() == Symbols(")")):
break
self.lexer.match(Symbols(","))
self.lexer.match(Symbols(")"))
li_2 = self.parse_AST_sequence()
li_3.append(var)
li_3.append(li)
li_3.append(li_2)
self.lexer.match(Symbols(";"))
return funct_def(var, li, li_2)
def parse_funct_call(self):
li = []
self.lexer.match(Keyword("callfun"))
var = self.parse_atomic_expression()
self.lexer.match(Symbols("("))
while self.lexer.peek_token() != Symbols(")"):
var_1 = self.parse_expression()
li.append(var_1)
if(self.lexer.peek_token() == Symbols(")")):
break
self.lexer.match(Symbols(","))
self.lexer.match(Symbols(")"))
self.lexer.match(Symbols(";"))
return funct_call(var, li)
def parse_funct_ret(self):
self.lexer.match(Keyword("functret"))
self.lexer.match(Symbols("("))
li = self.parse_expression()
self.lexer.match(Symbols(")"))
self.lexer.match(Symbols(";"))
return funct_ret(li)
def __iter__(self):
return self
def __next__(self):
try:
return self.parse_expression()
except EndOfTokens:
raise StopIteration