added < and >
This commit is contained in:
parent
a88e4efff0
commit
b6bbb2d251
|
@ -99,6 +99,30 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
|
||||||
writeln!(writer, " push rcx")?;
|
writeln!(writer, " push rcx")?;
|
||||||
ti += 1;
|
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 => {
|
OpType::Print => {
|
||||||
writeln!(writer, " ; -- PRINT")?;
|
writeln!(writer, " ; -- PRINT")?;
|
||||||
|
|
|
@ -11,6 +11,8 @@ pub enum OpType {
|
||||||
Else,
|
Else,
|
||||||
End,
|
End,
|
||||||
Dup,
|
Dup,
|
||||||
|
Gt,
|
||||||
|
Lt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,18 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
|
||||||
stack.push((a == b) as i32);
|
stack.push((a == b) as i32);
|
||||||
ti += 1;
|
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 => {
|
OpType::Print => {
|
||||||
let a = stack_pop(&mut stack)?;
|
let a = stack_pop(&mut stack)?;
|
||||||
|
|
|
@ -55,6 +55,9 @@ impl Parser {
|
||||||
let mut tokens = Vec::new();
|
let mut tokens = Vec::new();
|
||||||
|
|
||||||
for token in &self.tokens {
|
for token in &self.tokens {
|
||||||
|
if token.text.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let pos = (token.file.clone(), token.line, token.col);
|
let pos = (token.file.clone(), token.line, token.col);
|
||||||
match token.text.as_str() {
|
match token.text.as_str() {
|
||||||
t if t.parse::<i32>().is_ok() => {
|
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)),
|
"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)),
|
"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)),
|
"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 => {
|
t => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user