some error stuff

This commit is contained in:
Rusty Striker 2024-04-01 19:55:27 +03:00
parent 272b9801a9
commit 954ef31cd2
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
2 changed files with 69 additions and 2 deletions

View file

@ -20,6 +20,9 @@ class Parser(sly.Parser):
self.last_used_temp += 1 self.last_used_temp += 1
return f't_{self.last_used_temp}' return f't_{self.last_used_temp}'
def error(self, p):
self.had_errors = True
@_('declarations stmt_block') @_('declarations stmt_block')
def program(self, p): def program(self, p):
self.lines.append('HALT') self.lines.append('HALT')
@ -36,6 +39,20 @@ class Parser(sly.Parser):
# Empty # Empty
return None 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 ";"') @_('idlist ":" type ";"')
def declaration(self, p): def declaration(self, p):
floats = p[2].is_float floats = p[2].is_float
@ -68,6 +85,12 @@ class Parser(sly.Parser):
def stmt(self, p): def stmt(self, p):
return p[0] 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 ";"') @_('ID "=" expression ";"')
def assignment_stmt(self, p): def assignment_stmt(self, p):
id = p[0] id = p[0]
@ -88,6 +111,14 @@ class Parser(sly.Parser):
self.lines.append(f'{command} {id} {exp.result}') self.lines.append(f'{command} {id} {exp.result}')
return Statement([]) 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 ")" ";"') @_('INPUT "(" ID ")" ";"')
def input_stmt(self, p): def input_stmt(self, p):
id = p[2] id = p[2]
@ -100,6 +131,11 @@ class Parser(sly.Parser):
self.lines.append(f'{command} {id}') self.lines.append(f'{command} {id}')
return Statement([]) 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 ")" ";"') @_('OUTPUT "(" ID ")" ";"')
def output_stmt(self, p): def output_stmt(self, p):
id = p[2] id = p[2]
@ -112,6 +148,12 @@ class Parser(sly.Parser):
self.lines.append(f'{command} {id}') self.lines.append(f'{command} {id}')
return Statement([]) 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') @_('IF "(" boolexpr ")" if_jump stmt if_jump ELSE stmt')
def if_stmt(self, p): def if_stmt(self, p):
exp = p[2] exp = p[2]
@ -129,6 +171,11 @@ class Parser(sly.Parser):
self.lines.append('') self.lines.append('')
return line 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') @_('WHILE seen_WHILE "(" boolexpr ")" while_quit stmt')
def while_stmt(self, p): 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 # helper to get the line number of when we start the check thing for a while expr
return len(self.lines) 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 "}"') @_('SWITCH "(" expression ")" "{" caselist DEFAULT ":" stmtlist "}"')
def switch_stmt(self, p): def switch_stmt(self, p):
exp = p[2] exp = p[2]

18
test.ou
View file

@ -5,8 +5,9 @@ a: int;
if(a >= 6 || a == 5) if(a >= 6 || a == 5)
break; break;
else else
a = a + 0; a =] a + 0;
} }
input(a_;
output(a); output(a);
switch(a * 5) { switch(a * 5) {
case 1: case 1:
@ -18,5 +19,18 @@ a: int;
break; break;
} }
output(a); output(a);
switch(c) {
cae 2:
a = 6;
}
if (hello
break;
if (a > 5) {
aasdl;
}
else {
break;
}
} }