-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (52 loc) · 1.55 KB
/
main.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
from lexer import Lexer
from my_parser import Parser
from codegen import CodeGen
from ast import *
import os
from pathlib import Path
import sys
# Golf mode
# def golf_mode():
# if len(sys.argv[1].read()) > 5000:
# print("File too long!")
# print("In Golf mode, files must not exceed 5000 characters!")
# exit()
# Finding the source file in the directory tree
def find_file():
file_arg = sys.argv[1]
p = Path("golf_files")
for file in p.glob("*"):
if file.name == file_arg:
# Change directory
os.chdir(str(p))
# Reading the source file
with open(file_arg) as f:
return f.read()
text_input = find_file()
# print(text_input)
# Creating a lexer and passing the source code into it
lexer = Lexer().get_lexer()
tokens = lexer.lex(text_input)
# for tok in tokens:
# print(tok)
# Create a code generator object
codegen = CodeGen()
# Assign the variables to the code generator
module = codegen.module
builder = codegen.builder
printf = codegen.printf
# Create a parser object and parse the list of tokens created by the lexer.
pg = Parser(module, builder, printf)
# print(module)
pg.parse()
parser = pg.get_parser()
# Loop through the list of statements, and evaluate
# each one.
# for stmt in parser.parse(tokens):
# codegen.visit(stmt)
# Save the IR representation into an LL file
# codegen.create_ir()
# os.chdir("..")
# codegen.save_ir("output.ll")
# if sys.argv[2] == "--golf":
# golf_mode()