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")?;
ti += 1;
},
OpType::Dup => {
writeln!(writer, " ; -- DUP")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " push rax")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::If => {
writeln!(writer, " ; -- IF")?;
writeln!(writer, " pop rax")?;

View File

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

View File

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

View File

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