diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index b31bafb..ab469da 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -110,13 +110,16 @@ pub fn compile(tokens: Vec, 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; }, } diff --git a/src/interpret/linux_x86_64.rs b/src/interpret/linux_x86_64.rs index e7f1485..e533d9e 100644 --- a/src/interpret/linux_x86_64.rs +++ b/src/interpret/linux_x86_64.rs @@ -51,12 +51,13 @@ pub fn run(tokens: Vec) -> 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 diff --git a/src/parser.rs b/src/parser.rs index e16542a..ec13563 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,6 +11,18 @@ pub fn cross_ref(mut tokens: Vec) -> Vec { } 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) -> Vec { } (*if_og).value = ip as i32; - + stack.push(ip as u32); } _ => () } diff --git a/test.mcl b/test.mcl index 06ed952..7150359 100644 --- a/test.mcl +++ b/test.mcl @@ -1,3 +1,5 @@ -35 34 + 692 = if - 21 print +1 1 = if + 1111 print +else + 2222 print end \ No newline at end of file