-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2t.py
More file actions
44 lines (32 loc) · 889 Bytes
/
b2t.py
File metadata and controls
44 lines (32 loc) · 889 Bytes
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
import sys
from lexer import Lexer
from parser import Parser
from transpiler import Transpiler
def main():
if len(sys.argv)!=3:
print("usage: python b2t.py <input.bas> <output.ts>")
sys.exit(1)
input_file=sys.argv[1]
output_file=sys.argv[2]
if not input_file.endswith('.bas'):
print("error: input file must end in .bas")
try:
with open(input_file,'r') as f:
statements=f.read().strip()
except FileNotFoundError:
printf(f"error: file {input_file} not found!")
lexer=Lexer(statements)
tokens=[]
token = lexer.next_token()
while token.typ!='EOF':
tokens.append(token)
token=lexer.next_token()
parser=Parser(tokens)
ast=parser.parse_program()
print(parser)
transpiler=Transpiler(ast)
ts_code=transpiler.transpile()
with open(output_file,'w') as f:
f.write(ts_code)
if __name__ == '__main__':
sys.exit(main())