added < and >

This commit is contained in:
MCorange 2023-03-13 23:31:55 +02:00
parent a88e4efff0
commit b6bbb2d251
5 changed files with 48 additions and 6 deletions

View File

@ -99,6 +99,30 @@ pub fn compile(tokens: Vec<Operator>, 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")?;

View File

@ -11,6 +11,8 @@ pub enum OpType {
Else,
End,
Dup,
Gt,
Lt
}

View File

@ -41,6 +41,18 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> 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)?;

View File

@ -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::<i32>().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 => {

View File

@ -1,6 +1,5 @@
1
dup print
dup print
dup print
dup print
dup print
1 2 < if
1 print
else
0 print
end