-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
136 lines (107 loc) · 3.53 KB
/
compiler.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
from collections import namedtuple
import re
Token = namedtuple('Token', ['type', 'value'])
DefNode = namedtuple('DefNode', ['name', 'argNames', 'body'])
IntegerNode = namedtuple('IntegerNode', ['value'])
CallNode = namedtuple('CallNode', ['name', 'argExprs'])
VarRefNode = namedtuple('VarRefNode', ['value'])
class Tokenizer:
TOKEN_TYPES = [
('def', r'\bdef\b'),
('end', r'\bend\b'),
('identifier', r'\b[a-zA-Z]+\b'),
('integer', r'\b[0-9]+\b'),
('oparen', r'\('),
('cparen', r'\)'),
('comma', r',')
]
def __init__(self, code):
self.code = code
def tokenize(self):
tokens = []
while self.code:
tokens.append(self.tokenizeOneToken())
self.code = self.code.strip()
return tokens
def tokenizeOneToken(self):
for tokenType in Tokenizer.TOKEN_TYPES:
type, regex = tokenType
m = re.compile(regex).match(self.code)
if m:
value = m.group()
self.code = self.code[len(value):]
return Token(type, value)
raise Exception(f'Couldn\'t match token on {self.code}')
class Parser:
def __init__(self, tokens):
self.tokens = tokens
def parse(self):
return self.parseDef()
def parseDef(self):
self.consume('def')
name = self.consume('identifier').value
argNames = self.parseArgNames()
body = self.parseExpr()
self.consume('end')
return DefNode(name, argNames, body)
def parseArgNames(self):
argNames = []
self.consume('oparen')
if self.peek('identifier'):
argNames.append(self.consume('identifier').value)
while self.peek('comma'):
self.consume('comma')
argNames.append(self.consume('identifier').value)
self.consume('cparen')
return argNames
def parseExpr(self):
if self.peek('integer'):
return self.parseInteger()
elif self.peek('identifier') and self.peek('oparen', 1):
return self.parseCall()
else:
return self.parseVarRef()
def parseInteger(self):
return IntegerNode(self.consume('integer').value)
def parseCall(self):
name = self.consume('identifier').value
argExprs = self.parseArgsExprs()
return CallNode(name, argExprs)
def parseArgsExprs(self):
argExprs = []
self.consume('oparen')
if not self.peek('cparen'):
argExprs.append(self.parseExpr())
while self.peek('comma'):
self.consume('comma')
argExprs.append(self.parseExpr())
self.consume('cparen')
return argExprs
def parseVarRef(self):
return VarRefNode(self.consume('identifier').value)
def consume(self, expectedType):
token = self.tokens.pop(0)
if token.type == expectedType:
return token
else:
raise Exception(f'Expected token type "{expectedType}" but got "{token.type}"')
def peek(self, expectedType, offset=0):
return self.tokens[offset].type == expectedType
class Generator:
def generate(self, node):
if type(node) is DefNode:
return f'function {node.name} ({", ".join(node.argNames)}) {{ return {self.generate(node.body)} }}'
elif type(node) is CallNode:
return f'{node.name}({", ".join(map(self.generate, node.argExprs))})'
elif type(node) is IntegerNode:
return node.value
elif type(node) is VarRefNode:
return node.value
else:
raise Exception(f'Unexpected node type: {type(node)}')
tokens = Tokenizer(open('test.src').read()).tokenize()
tree = Parser(tokens).parse()
generated = Generator().generate(tree)
RUNTIME = 'const add = (x, y) => x + y'
TEST = 'console.log(f(1, 2))'
print('\n'.join([RUNTIME, generated, TEST]))