-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
executable file
·338 lines (250 loc) · 8.69 KB
/
bootstrap.py
File metadata and controls
executable file
·338 lines (250 loc) · 8.69 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
#!/usr/bin/env python3
# Blaest Bootstrap, or Blaeststrap if you will
# This is a simple python transpiler that transpiles Blaest into C, this is to
# enable the next generation Blaest compiler to be written in Blaest itself
# This may also help illustrate how a compiler/transpiler works, so care has
# been taken to make this code as readable as possible
import sys
if len(sys.argv) != 2:
print("USAGE: bootstrap.py [FILE.B]\n")
print("Output file will be named whatever the given file is named, with .c")
print("appended to the end. Ex: file.b -> file.b.c\n")
print("Make sure the directory you compile the resulting c file in has")
print("The library 'bglue.c' in the same folder")
exit(1)
fname = sys.argv[1]
# The file we will read from and write to
file = open(fname, "r")
out = open(fname + ".c", "w")
# A registry of our variable we define
# TODO: this does not account for scopes, like blocks. This is just every
# variable ever defined. Eventually this should clear when we leave a scope
# where the variable was defined
VARIABLES = []
# Gets a single character from a file
def getChar():
return file.read(1)
# Writes our transpiled output
def tpileWrite(oput):
out.write(oput)
# Loop to parse a string
# - buffer: The buffer containing the string we are making sense of
def parseString(buffer):
stringBuffer = ""
ret = 0
for i in range(0, len(buffer)):
c = buffer[i]
if c == '\"':
# C transpiler output
tpileWrite("(bword_t)\"" + stringBuffer + "\"")
return i
stringBuffer += c
print("Error: Unclosed string!")
exit(1)
# Loop to parse a function with arguments
# - buffer: the buffer that contains the arguments
def parseFunctionArgs(buffer):
argBuffer = ""
for c in buffer:
if c == ')':
parseValue(argBuffer)
tpileWrite(")")
return
elif c == ',':
parseValue(argBuffer)
tpileWrite(", ")
argBuffer = ""
else:
argBuffer += c
print("Error: Unclosed function")
exit(1)
# Loop to parse a function argument defintion
# - buffer: The buffer with our definition
def parseFunctionArgsDefn(buffer):
argBuffer = ""
for c in buffer:
if c == ')':
tpileWrite("bword_t " + argBuffer)
tpileWrite(")")
return
elif c == ',':
tpileWrite("bword_t " + argBuffer)
tpileWrite(", ")
argBuffer = ""
else:
argBuffer += c
print("Error: Unclosed function")
exit(1)
# Parse a value. This could be something like '5' or as broad as 'x = 5'. As
# long as it returns something. Note that this function truncates spaces unless
# a string is found.
# - buffer: the buffer containing the value
def parseValue(buffer):
global VARIABLES
valueBuffer = ""
i = 0
while i < len(buffer):
c = buffer[i]
if not c:
print("Encountered EOF while parsing statement")
exit(1)
if c == '\"':
i += parseString(buffer[i + 1:])
return
elif c == '=':
# We have a value assign statement
# Make sure the variable is real
if not valueBuffer in VARIABLES:
print("Variable " + valueBuffer + " does not exist!")
exit(1)
tpileWrite(valueBuffer + " = ")
valueBuffer = ""
# Now the rest of this should be another value statement
parseValue(buffer[i + 1:])
return
elif c == '(' and valueBuffer[-1].isalpha():
# We have a function call
tpileWrite("B_" + valueBuffer + "(")
parseFunctionArgs(buffer[i + 1:])
return
elif c > ' ':
valueBuffer += c
i += 1
# If we get here, its either a math statement or a variable
# We should probably error if its wrong, but lets let C handle that for now
if valueBuffer.isalpha() and not valueBuffer in VARIABLES:
print("ERROR: Variable " + valueBuffer + " not found!")
exit(1)
# C Transpiler output:
tpileWrite(valueBuffer)
# Parse an auto statement. This is a keyword to define a varaible.
def parseAuto():
global VARIABLES
nameBuffer = ""
while True:
c = getChar()
if not c:
print("Encountered EOF while parsing statement")
exit(1)
# TODO: Make sure the variable we are defining has not previously been
# defined in this scope
#TODO: Add support for initializing arrays, such as 'auto x[5]'
if c == ',':
# Got a line like "auto x, y;"
# C transpiler output:
tpileWrite("bword_t " + nameBuffer + ";\n")
VARIABLES.append(nameBuffer)
nameBuffer = ""
elif c == '=':
# Got a line like "auto x = 5"
# C transpiler output:
tpileWrite("bword_t " + nameBuffer + " = ")
VARIABLES.append(nameBuffer)
nameBuffer = ""
valueBuffer = ""
while c != ';':
c = getChar()
if not c:
print ("EOF")
exit(1)
valueBuffer += c
parseValue(valueBuffer)
# C transpiler output:
tpileWrite(";\n")
break
elif c == ';':
tpileWrite("bword_t " + nameBuffer + ";\n")
VARIABLES.append(nameBuffer)
break
elif c > ' ':
nameBuffer += c
# Parse a block of code, that is code that is between { and }
def parseBlock():
lineBuffer = ""
# C transpiler output
tpileWrite("{\n")
while True:
c = getChar()
if not c:
print("Encountered EOF while parsing block")
return
# Exit the loop if we find }
if c == '}':
# C transpiler output
tpileWrite("}\n")
break
# We got a block in our block
elif c == '{':
parseBlock()
# We have a line that didnt start with any keywords
elif c == ';':
parseValue(lineBuffer)
tpileWrite(";\n")
lineBuffer = ""
else:
lineBuffer += c
# Look for patterns in the lineBuffer
if lineBuffer.lstrip() == "auto":
parseAuto()
lineBuffer = ""
elif lineBuffer.lstrip() == "return":
c = getChar()
tpileWrite("return ")
lineBuffer = ""
while c != ';':
lineBuffer += c
c = getChar()
# Parse the return value
parseValue(lineBuffer)
lineBuffer = ""
tpileWrite(";\n")
# Parse our global scope. This is code that is not in any function.
# This is the entrypoint to the transpiler
def parseGlobalScope():
global VARIABLES
globalBuffer = ""
tpileWrite("#include \"bglue.c\"\n")
while True:
c = getChar()
if not c:
return
# We have a function definition
if c == '(':
tpileWrite("bword_t B_" + globalBuffer + "(")
VARIABLES.append(globalBuffer)
globalBuffer = ""
while c != ')':
c = getChar()
if not c:
print("ERROR: EOF while reading function definiton")
exit(1)
else:
globalBuffer += c
parseFunctionArgsDefn(globalBuffer)
while c != '{':
c = getChar()
if not c:
print("ERROR: EOF while looking for function block")
exit(1)
parseBlock()
globalBuffer = ""
# We have a global variable definiton
elif c == '=':
tpileWrite("bword_t " + globalBuffer + " = ")
VARIABLES.append(globalBuffer)
globalBuffer = ""
c = getChar()
while c != ';':
if not c:
print("ERROR: EOF while looking for global variable value")
exit(1)
else:
globalBuffer += c
c = getChar()
parseValue(globalBuffer)
tpileWrite(";\n")
globalBuffer = ""
elif c > ' ':
globalBuffer += c
# Start the transpilation
parseGlobalScope()