Implement block scopes (code inside braces)
- Putting code in between braces will create a new scope
This commit is contained in:
parent
cbf31fa513
commit
8b67c4d59c
@ -113,6 +113,7 @@ pub struct If {
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Statement {
|
||||
Expr(Expression),
|
||||
Block(BlockScope),
|
||||
Loop(Loop),
|
||||
If(If),
|
||||
Print(Expression),
|
||||
|
||||
@ -18,6 +18,7 @@ impl SimpleAstOptimizer {
|
||||
for stmt in block {
|
||||
match stmt {
|
||||
Statement::Expr(expr) => Self::optimize_expr(expr),
|
||||
Statement::Block(block) => Self::optimize_block(block),
|
||||
Statement::Loop(Loop {
|
||||
condition,
|
||||
advancement,
|
||||
|
||||
@ -78,6 +78,10 @@ impl Interpreter {
|
||||
self.resolve_expr(expr);
|
||||
}
|
||||
|
||||
Statement::Block(block) => {
|
||||
self.run_block(block);
|
||||
}
|
||||
|
||||
Statement::Loop(looop) => {
|
||||
// loop runs as long condition != 0
|
||||
loop {
|
||||
|
||||
@ -50,6 +50,14 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
}
|
||||
Token::EoF | Token::RBraces => break,
|
||||
|
||||
Token::LBraces => {
|
||||
self.next();
|
||||
prog.push(Statement::Block(self.parse_scoped_block()));
|
||||
if !matches!(self.next(), Token::RBraces) {
|
||||
panic!("Error parsing block: Expectected closing braces '}}'");
|
||||
}
|
||||
}
|
||||
|
||||
// By default try to lex a statement
|
||||
_ => prog.push(self.parse_stmt()),
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user