added ifs with else
This commit is contained in:
parent
8f91098792
commit
6ac45cfcb7
|
@ -110,13 +110,16 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
|
|||
writeln!(writer, " ; -- IF")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " test rax, rax")?;
|
||||
writeln!(writer, " jz addr_{}", token.value)?;
|
||||
writeln!(writer, " jz addr_{}", token.value + 1)?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Else => {
|
||||
writeln!(writer, " ; -- ELSE")?;
|
||||
writeln!(writer, " jmp addr_{}", token.value)?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::End => {
|
||||
writeln!(writer, " ; -- END")?;
|
||||
ti += 1;
|
||||
},
|
||||
}
|
||||
|
|
|
@ -51,12 +51,13 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
|
|||
OpType::If => {
|
||||
let a = stack_pop(&mut stack)?;
|
||||
if a == 0 {
|
||||
ti = token.value as usize;
|
||||
ti = (token.value + 1) as usize;
|
||||
} else {
|
||||
ti += 1;
|
||||
}
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Else => {
|
||||
ti += 1;
|
||||
ti = token.value as usize;
|
||||
|
||||
},
|
||||
OpType::End => ti += 1
|
||||
|
|
|
@ -11,6 +11,18 @@ pub fn cross_ref(mut tokens: Vec<Operator>) -> Vec<Operator> {
|
|||
}
|
||||
|
||||
OpType::End => {
|
||||
let block_ip = stack.pop().unwrap();
|
||||
let mut block_og = &mut tokens[block_ip as usize];
|
||||
if vec![OpType::If, OpType::Else].contains(&(*block_og).typ) {
|
||||
(*block_og).value = ip as i32;
|
||||
tokens[block_ip as usize] = block_og.clone();
|
||||
} else {
|
||||
util::logger::pos_error(op.clone().pos,"'end' can only close 'if' blocks");
|
||||
std::process::exit(1); // idc
|
||||
}
|
||||
|
||||
}
|
||||
OpType::Else => {
|
||||
let if_ip = stack.pop().unwrap();
|
||||
let mut if_og = &mut tokens[if_ip as usize];
|
||||
if !vec![OpType::If].contains(&(*if_og).typ) {
|
||||
|
@ -19,7 +31,7 @@ pub fn cross_ref(mut tokens: Vec<Operator>) -> Vec<Operator> {
|
|||
}
|
||||
|
||||
(*if_og).value = ip as i32;
|
||||
|
||||
stack.push(ip as u32);
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user