Add break-keyword
This commit is contained in:
parent
c59c4e4e24
commit
a7908fb39a
@ -42,4 +42,7 @@ global value, table = f(10, 11, 12)
|
|||||||
print("hello")
|
print("hello")
|
||||||
for i=1,#table do
|
for i=1,#table do
|
||||||
print(table[i])
|
print(table[i])
|
||||||
|
if i > 2 then
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@ -263,6 +263,7 @@ pub enum Statement {
|
|||||||
Node<Expression>,
|
Node<Expression>,
|
||||||
Block,
|
Block,
|
||||||
),
|
),
|
||||||
|
Break,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for Statement {
|
impl Parse for Statement {
|
||||||
@ -354,6 +355,9 @@ impl Parse for Statement {
|
|||||||
))
|
))
|
||||||
} else if let Ok(expr) = stream.parse() {
|
} else if let Ok(expr) = stream.parse() {
|
||||||
Ok(Self::Expression(expr))
|
Ok(Self::Expression(expr))
|
||||||
|
} else if let Some(Token::Keyword(Keyword::Break)) = stream.peek() {
|
||||||
|
stream.next();
|
||||||
|
Ok(Self::Break)
|
||||||
} else {
|
} else {
|
||||||
Err(stream.expecting_err("statement"))
|
Err(stream.expecting_err("statement"))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -179,6 +179,7 @@ impl Statement {
|
|||||||
constants.extend(block.find_constants(scope, Vec::new()));
|
constants.extend(block.find_constants(scope, Vec::new()));
|
||||||
constants
|
constants
|
||||||
}
|
}
|
||||||
|
Statement::Break => HashSet::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -417,13 +418,20 @@ impl Statement {
|
|||||||
let instr_len = instr.len() as i32;
|
let instr_len = instr.len() as i32;
|
||||||
|
|
||||||
instructions.push(PreInstr::Instr(Instruction::Jmp(instr_len + 2)));
|
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(
|
instructions.push(PreInstr::Instr(Instruction::Add(
|
||||||
*init_reg, *init_reg, *step_reg,
|
*init_reg, *init_reg, *step_reg,
|
||||||
)));
|
)));
|
||||||
instructions.push(PreInstr::Instr(Instruction::Jmp(-(instr_len + 4))));
|
instructions.push(PreInstr::Instr(Instruction::Jmp(-(instr_len + 4))));
|
||||||
}
|
}
|
||||||
|
Statement::Break => instructions.push(PreInstr::Break),
|
||||||
}
|
}
|
||||||
|
|
||||||
for reg in 0..scope.register_counter.0 {
|
for reg in 0..scope.register_counter.0 {
|
||||||
|
|||||||
@ -22,6 +22,7 @@ pub enum Keyword {
|
|||||||
Not,
|
Not,
|
||||||
For,
|
For,
|
||||||
Do,
|
Do,
|
||||||
|
Break,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Keyword {
|
impl Keyword {
|
||||||
@ -40,6 +41,7 @@ impl Keyword {
|
|||||||
"not" => Keyword::Not,
|
"not" => Keyword::Not,
|
||||||
"for" => Keyword::For,
|
"for" => Keyword::For,
|
||||||
"do" => Keyword::Do,
|
"do" => Keyword::Do,
|
||||||
|
"break" => Keyword::Break,
|
||||||
_ => None?,
|
_ => None?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -61,6 +63,7 @@ impl ToString for Keyword {
|
|||||||
Keyword::Not => "not",
|
Keyword::Not => "not",
|
||||||
Keyword::For => "for",
|
Keyword::For => "for",
|
||||||
Keyword::Do => "do",
|
Keyword::Do => "do",
|
||||||
|
Keyword::Break => "break",
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user