Added Equals '='
This commit is contained in:
@@ -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")?;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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}");
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user