Added Equals '='

This commit is contained in:
MCorange
2023-03-13 16:23:40 +02:00
parent e053d7bef3
commit f2b45e343c
9 changed files with 28 additions and 69 deletions

View File

@@ -81,6 +81,17 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " sub rbx, rax")?;
writeln!(writer, " push rbx")?;
},
OpType::Equals => {
writeln!(writer, " ; -- EQUALS")?;
writeln!(writer, " mov rcx, 0")?;
writeln!(writer, " mov rdx, 1")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rbx")?;
writeln!(writer, " cmp rax, rbx")?;
writeln!(writer, " cmove rcx, rdx")?;
writeln!(writer, " push rcx")?;
},
OpType::Print => {
writeln!(writer, " ; -- PRINT")?;
writeln!(writer, " pop rdi")?;

View File

@@ -5,9 +5,16 @@ pub enum OpType {
Pop,
Minus,
Plus,
Equals,
Print
}
// #[derive(Debug)]
// pub enum OpType {
// }
#[derive(Debug)]
pub struct Operator {
pub typ: OpType,

View File

@@ -30,6 +30,13 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
let b = stack_pop(&mut stack)?;
stack.push(b - a);
},
OpType::Equals => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
stack.push((a == b) as i32);
},
OpType::Print => {
let a = stack_pop(&mut stack)?;
println!("{a}");

View File

@@ -23,11 +23,12 @@ impl Parser {
let num = t.parse::<i32>().unwrap();
tokens.push(Operator::new(OpType::Push, num));
},
"pop" => tokens.push(Operator::new(OpType::Pop, 0)),
"+" => tokens.push(Operator::new(OpType::Plus, 0)),
"-" => tokens.push(Operator::new(OpType::Minus, 0)),
"print" => tokens.push(Operator::new(OpType::Print, 0)),
"=" => tokens.push(Operator::new(OpType::Equals, 0)),
t => {