diff --git a/parser.py b/parser.py index 6e54549..67e966e 100644 --- a/parser.py +++ b/parser.py @@ -20,6 +20,9 @@ class Parser(sly.Parser): self.last_used_temp += 1 return f't_{self.last_used_temp}' + def error(self, p): + self.had_errors = True + @_('declarations stmt_block') def program(self, p): self.lines.append('HALT') @@ -36,6 +39,20 @@ class Parser(sly.Parser): # Empty return None + # declaration errors + @_( + 'idlist error ":" type ";"', + 'error idlist ":" type ";"', + 'idlist error idlist ":" type ";"', + 'idlist error type ";"', + 'idlist ":" error ";"', + 'idlist ":" type error', + 'error ":" type ";"' + ) + def declaration(self, p): + self.had_errors = True + print_err(f"Invalid declaration in line {p.lineno}") + @_('idlist ":" type ";"') def declaration(self, p): floats = p[2].is_float @@ -68,6 +85,12 @@ class Parser(sly.Parser): def stmt(self, p): return p[0] + @_('error ";"') + def stmt(self, p): + self.had_errors = True + print_err(f'Invalid statement in line {p.lineno}') + return Statement([]) + @_('ID "=" expression ";"') def assignment_stmt(self, p): id = p[0] @@ -88,6 +111,14 @@ class Parser(sly.Parser): self.lines.append(f'{command} {id} {exp.result}') return Statement([]) + + + @_('INPUT error ";"') + def input_stmt(self, p): + self.had_errors = True + print_err(f'Invalid input statement in line {p.lineno}') + return Statement([]) + @_('INPUT "(" ID ")" ";"') def input_stmt(self, p): id = p[2] @@ -100,6 +131,11 @@ class Parser(sly.Parser): self.lines.append(f'{command} {id}') return Statement([]) + @_('OUTPUT error ";"') + def output_stmt(self, p): + self.had_errors = True + print_err(f'Invalid output statement in line {p.lineno}') + @_('OUTPUT "(" ID ")" ";"') def output_stmt(self, p): id = p[2] @@ -112,6 +148,12 @@ class Parser(sly.Parser): self.lines.append(f'{command} {id}') return Statement([]) + @_('IF error stmt') + def if_stmt(self, p): + self.had_errors = True + print_err(f'Invalid if statement in line {p.lineno}') + return Statement([]) + @_('IF "(" boolexpr ")" if_jump stmt if_jump ELSE stmt') def if_stmt(self, p): exp = p[2] @@ -129,6 +171,11 @@ class Parser(sly.Parser): self.lines.append('') return line + @_('WHILE error stmt') + def while_stmt(self, p): + self.had_errors = True + print_err(f'Invalid while statement in line {p.lineno}') + return Statement([]) @_('WHILE seen_WHILE "(" boolexpr ")" while_quit stmt') def while_stmt(self, p): @@ -157,6 +204,12 @@ class Parser(sly.Parser): # helper to get the line number of when we start the check thing for a while expr return len(self.lines) + @_('SWITCH error "}"', 'SWITCH "(" expression ")" "{" caselist "}"') + def switch_stmt(self, p): + self.had_errors = True + print_err(f'Invalid switch statement in line {p.lineno}') + return Statement([]) + @_('SWITCH "(" expression ")" "{" caselist DEFAULT ":" stmtlist "}"') def switch_stmt(self, p): exp = p[2] diff --git a/test.ou b/test.ou index 2a362f3..65f621d 100644 --- a/test.ou +++ b/test.ou @@ -5,8 +5,9 @@ a: int; if(a >= 6 || a == 5) break; else - a = a + 0; + a =] a + 0; } + input(a_; output(a); switch(a * 5) { case 1: @@ -18,5 +19,18 @@ a: int; break; } output(a); - + switch(c) { + cae 2: + a = 6; + + } + if (hello + break; + + if (a > 5) { + aasdl; + } + else { + break; + } }