From 272b9801a941a47284c3bb82d50fa5fef089e21c Mon Sep 17 00:00:00 2001 From: Rusty Striker Date: Sat, 30 Mar 2024 14:38:42 +0300 Subject: [PATCH] compile a file(into another file) --- README.md | 2 +- cpq.py | 46 ++++++++++++++++++---------------------------- parser.py | 3 ++- test.ou | 22 ++++++++++++++++++++++ test.qud | 29 +++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 test.ou create mode 100644 test.qud diff --git a/README.md b/README.md index dc67a15..63222df 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ - [ ] Error handling(so it wont just print sly: unexpected token or whatever it says) - [ ] Proper readme about files -- [ ] Make cpq.py act as a cmd util - get input and output and such \ No newline at end of file +- [x] Make cpq.py act as a cmd util - get input and output and such \ No newline at end of file diff --git a/cpq.py b/cpq.py index 5b2970d..93cb5e3 100644 --- a/cpq.py +++ b/cpq.py @@ -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 .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() diff --git a/parser.py b/parser.py index f36ec2d..6e54549 100644 --- a/parser.py +++ b/parser.py @@ -13,7 +13,7 @@ class Parser(sly.Parser): tokens = Lexer.tokens symbol_table = {} had_errors = False # Simply to know if we need to write the program in the end - lines = [ ] + lines = [ '' ] # Start with en empty line, so len(lines) will represent the next line last_used_temp = 0 # cpl doesnt allow _ in ids, so use t_NUM as variables def next_temp(self): @@ -24,6 +24,7 @@ class Parser(sly.Parser): def program(self, p): self.lines.append('HALT') self.lines.append('Aviv Romem') + self.lines.pop(0) # remove the empty line return None @_('declarations declaration') diff --git a/test.ou b/test.ou new file mode 100644 index 0000000..2a362f3 --- /dev/null +++ b/test.ou @@ -0,0 +1,22 @@ +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); + +} diff --git a/test.qud b/test.qud new file mode 100644 index 0000000..d875e6c --- /dev/null +++ b/test.qud @@ -0,0 +1,29 @@ +ILSS t_1 a 10 +JMPZ 15 t_1 +IADD t_2 a 1 +IASN a t_2 +ILSS t_3 a 6 +IEQL t_3 t_3 0 +IEQL t_4 a 5 +IADD t_5 t_3 t_4 +JMPZ 12 t_5 +JUMP 15 +JUMP 14 +IADD t_6 a 0 +IASN a t_6 +JUMP 1 +IPRT a +IMLT t_7 a 5 +IEQL t_8 t_7 1 +JMPZ 22 t_8 +IASN a 5 +JUMP 27 +JUMP 24 +IEQL t_8 t_7 5 +JMPZ 26 t_8 +IPRT a +JUMP 26 +JUMP 27 +IPRT a +HALT +Aviv Romem