Add break-keyword

This commit is contained in:
Sofia 2026-03-19 16:50:05 +02:00
parent c59c4e4e24
commit a7908fb39a
4 changed files with 19 additions and 1 deletions

View File

@ -42,4 +42,7 @@ global value, table = f(10, 11, 12)
print("hello")
for i=1,#table do
print(table[i])
if i > 2 then
break
end
end

View File

@ -263,6 +263,7 @@ pub enum Statement {
Node<Expression>,
Block,
),
Break,
}
impl Parse for Statement {
@ -354,6 +355,9 @@ impl Parse for Statement {
))
} else if let Ok(expr) = stream.parse() {
Ok(Self::Expression(expr))
} else if let Some(Token::Keyword(Keyword::Break)) = stream.peek() {
stream.next();
Ok(Self::Break)
} else {
Err(stream.expecting_err("statement"))
}

View File

@ -179,6 +179,7 @@ impl Statement {
constants.extend(block.find_constants(scope, Vec::new()));
constants
}
Statement::Break => HashSet::new(),
}
}
@ -417,13 +418,20 @@ impl Statement {
let instr_len = instr.len() as i32;
instructions.push(PreInstr::Instr(Instruction::Jmp(instr_len + 2)));
instructions.extend(instr);
for (i, pre_instr) in instr.into_iter().enumerate() {
match pre_instr {
PreInstr::Break => instructions
.push(PreInstr::Instr(Instruction::Jmp(instr_len - i as i32 + 1))),
_ => instructions.push(pre_instr),
}
}
instructions.push(PreInstr::Instr(Instruction::Add(
*init_reg, *init_reg, *step_reg,
)));
instructions.push(PreInstr::Instr(Instruction::Jmp(-(instr_len + 4))));
}
Statement::Break => instructions.push(PreInstr::Break),
}
for reg in 0..scope.register_counter.0 {

View File

@ -22,6 +22,7 @@ pub enum Keyword {
Not,
For,
Do,
Break,
}
impl Keyword {
@ -40,6 +41,7 @@ impl Keyword {
"not" => Keyword::Not,
"for" => Keyword::For,
"do" => Keyword::Do,
"break" => Keyword::Break,
_ => None?,
})
}
@ -61,6 +63,7 @@ impl ToString for Keyword {
Keyword::Not => "not",
Keyword::For => "for",
Keyword::Do => "do",
Keyword::Break => "break",
}
.to_string()
}