diff --git a/src/compile/linux_x86_64.rs b/src/compile/linux_x86_64.rs index ab469da..a79df9b 100644 --- a/src/compile/linux_x86_64.rs +++ b/src/compile/linux_x86_64.rs @@ -106,6 +106,16 @@ pub fn compile(tokens: Vec, 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")?; diff --git a/src/constants.rs b/src/constants.rs index 0cd6624..bdee1f8 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -10,6 +10,7 @@ pub enum OpType { If, Else, End, + Dup, } diff --git a/src/interpret/linux_x86_64.rs b/src/interpret/linux_x86_64.rs index e533d9e..24854cd 100644 --- a/src/interpret/linux_x86_64.rs +++ b/src/interpret/linux_x86_64.rs @@ -48,6 +48,12 @@ pub fn run(tokens: Vec) -> 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 { diff --git a/src/parser.rs b/src/parser.rs index ec13563..6dbac5b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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 => { diff --git a/test.mcl b/test.mcl index 7150359..9d35081 100644 --- a/test.mcl +++ b/test.mcl @@ -1,5 +1,6 @@ -1 1 = if - 1111 print -else - 2222 print -end \ No newline at end of file +1 +dup print +dup print +dup print +dup print +dup print \ No newline at end of file