compile a file(into another file)

This commit is contained in:
Rusty Striker 2024-03-30 14:38:42 +03:00
parent ae2337376c
commit 272b9801a9
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
5 changed files with 72 additions and 30 deletions

46
cpq.py
View file

@ -1,4 +1,5 @@
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
@ -8,35 +9,24 @@ from parser import Parser
from helper import print_err
print_err('Aviv Romem')
if len(sys.argv) != 2 or not sys.argv[1].endswith('.ou'):
print('USAGE: python cpq.py <file-name>.ou')
exit(1)
file = sys.argv[1]
output = file[:-3] + '.qud'
f = open(file, 'r')
text = f.read()
f.close()
lexer = Lexer()
parser = Parser()
text = '''
a: int;
{
while(a < 10) {
a = a + 1;
if(a >= 6 || a == 5)
break;
else
a = a + 0;
}
output(a);
switch(a * 5) {
case 1:
a = 5;
break;
case 5:
output(a);
default:
break;
}
output(a);
}
'''
file = sys.argv[1]
parser.parse(lexer.tokenize(text))
for l, t in enumerate(parser.lines):
print(l,':',t)
if not parser.had_errors:
f = open(output, 'w')
f.writelines([l + '\n' for l in parser.lines])
f.close()