added 'dup'

This commit is contained in:
MCorange 2023-03-13 23:20:35 +02:00
parent 6ac45cfcb7
commit a88e4efff0
5 changed files with 24 additions and 5 deletions

View File

@ -106,6 +106,16 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " call print")?; writeln!(writer, " call print")?;
ti += 1; ti += 1;
}, },
OpType::Dup => {
writeln!(writer, " ; -- DUP")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " push rax")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::If => { OpType::If => {
writeln!(writer, " ; -- IF")?; writeln!(writer, " ; -- IF")?;
writeln!(writer, " pop rax")?; writeln!(writer, " pop rax")?;

View File

@ -10,6 +10,7 @@ pub enum OpType {
If, If,
Else, Else,
End, End,
Dup,
} }

View File

@ -48,6 +48,12 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
// let _ = io::stdout().flush(); // let _ = io::stdout().flush();
ti += 1; ti += 1;
}, },
OpType::Dup => {
let a = stack_pop(&mut stack)?;
stack.push(a);
stack.push(a);
},
OpType::If => { OpType::If => {
let a = stack_pop(&mut stack)?; let a = stack_pop(&mut stack)?;
if a == 0 { if a == 0 {

View File

@ -70,6 +70,7 @@ impl Parser {
"if" => tokens.push(Operator::new(OpType::If, 0, token.file.clone(), token.line, token.col)), "if" => tokens.push(Operator::new(OpType::If, 0, token.file.clone(), token.line, token.col)),
"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)),
t => { t => {

View File

@ -1,5 +1,6 @@
1 1 = if 1
1111 print dup print
else dup print
2222 print dup print
end dup print
dup print