diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index a79df9b..3f2f554 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -99,6 +99,30 @@ pub fn compile(tokens: Vec, args: Args) -> Result<()>{ writeln!(writer, " push rcx")?; ti += 1; + }, + OpType::Lt => { + writeln!(writer, " ; -- EQUALS")?; + writeln!(writer, " mov rcx, 0")?; + writeln!(writer, " mov rdx, 1")?; + writeln!(writer, " pop rbx")?; + writeln!(writer, " pop rax")?; + writeln!(writer, " cmp rax, rbx")?; + writeln!(writer, " cmovl rcx, rdx")?; + writeln!(writer, " push rcx")?; + ti += 1; + + }, + OpType::Gt => { + writeln!(writer, " ; -- EQUALS")?; + writeln!(writer, " mov rcx, 0")?; + writeln!(writer, " mov rdx, 1")?; + writeln!(writer, " pop rbx")?; + writeln!(writer, " pop rax")?; + writeln!(writer, " cmp rax, rbx")?; + writeln!(writer, " cmovg rcx, rdx")?; + writeln!(writer, " push rcx")?; + ti += 1; + }, OpType::Print => { writeln!(writer, " ; -- PRINT")?; diff --git a/src/constants.rs b/src/constants.rs index bdee1f8..2c61ecc 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -11,6 +11,8 @@ pub enum OpType { Else, End, Dup, + Gt, + Lt } diff --git a/src/interpret/linux_x86_64.rs b/src/interpret/linux_x86_64.rs index 24854cd..d5ac5bb 100644 --- a/src/interpret/linux_x86_64.rs +++ b/src/interpret/linux_x86_64.rs @@ -41,6 +41,18 @@ pub fn run(tokens: Vec) -> Result<(), &'static str>{ stack.push((a == b) as i32); ti += 1; }, + OpType::Gt => { + let a = stack_pop(&mut stack)?; + let b = stack_pop(&mut stack)?; + stack.push((a > b) as i32); + ti += 1; + }, + OpType::Lt => { + let a = stack_pop(&mut stack)?; + let b = stack_pop(&mut stack)?; + stack.push((a < b) as i32); + ti += 1; + }, OpType::Print => { let a = stack_pop(&mut stack)?; diff --git a/src/parser.rs b/src/parser.rs index 6dbac5b..23d657e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -55,6 +55,9 @@ impl Parser { let mut tokens = Vec::new(); for token in &self.tokens { + if token.text.is_empty() { + continue; + } let pos = (token.file.clone(), token.line, token.col); match token.text.as_str() { t if t.parse::().is_ok() => { @@ -71,6 +74,8 @@ impl Parser { "else" => tokens.push(Operator::new(OpType::Else, 0, token.file.clone(), token.line, token.col)), "end" => tokens.push(Operator::new(OpType::End, 0, token.file.clone(), token.line, token.col)), "dup" => tokens.push(Operator::new(OpType::Dup, 0, token.file.clone(), token.line, token.col)), + ">" => tokens.push(Operator::new(OpType::Gt, 0, token.file.clone(), token.line, token.col)), + "<" => tokens.push(Operator::new(OpType::Lt, 0, token.file.clone(), token.line, token.col)), t => { diff --git a/test.mcl b/test.mcl index 9d35081..ac1e00b 100644 --- a/test.mcl +++ b/test.mcl @@ -1,6 +1,5 @@ -1 -dup print -dup print -dup print -dup print -dup print \ No newline at end of file +1 2 < if + 1 print +else + 0 print +end