34 lines
812 B
Python
34 lines
812 B
Python
import sly
|
|
import sys
|
|
# for ease of reading, the compiler is broken down to multiple files
|
|
# parser.py for the parser
|
|
# lexer.py for the lexer
|
|
# and helper.py for helper functions(such as print_err)
|
|
from lexer import Lexer
|
|
from parser import Parser
|
|
from helper import print_err
|
|
|
|
# print signature
|
|
print_err('Aviv Romem')
|
|
|
|
# invalid usage
|
|
if len(sys.argv) != 2 or not sys.argv[1].endswith('.ou'):
|
|
print('USAGE: python cpq.py <file-name>.ou')
|
|
exit(1)
|
|
|
|
# load file and parser
|
|
file = sys.argv[1]
|
|
output = file[:-3] + '.qud'
|
|
f = open(file, 'r')
|
|
text = f.read()
|
|
f.close()
|
|
lexer = Lexer()
|
|
parser = Parser()
|
|
|
|
file = sys.argv[1]
|
|
# parse and write file if valid
|
|
parser.parse(lexer.tokenize(text))
|
|
if not parser.had_errors:
|
|
f = open(output, 'w')
|
|
f.writelines([l + '\n' for l in parser.lines])
|
|
f.close()
|