Skip to content

Commit

Permalink
[feat] OP_[2..16] and OP_MIN implementation (#33)
Browse files Browse the repository at this point in the history
* feat: #5 add `OP_[2..16]` opcodes

* feat: #17 add `OP_MIN` opcode

---------

Co-authored-by: Brandon Roberts <brandonjroberts22@gmail.com>
  • Loading branch information
ptisserand and b-j-roberts authored Jul 29, 2024
1 parent 66bbffd commit 256c2da
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 219 deletions.
1 change: 1 addition & 0 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub impl CompilerTraitImpl of CompilerTrait {
opcodes.insert('OP_NOT', Opcode::OP_NOT);
opcodes.insert('OP_ADD', Opcode::OP_ADD);
opcodes.insert('OP_SUB', Opcode::OP_SUB);
opcodes.insert('OP_MIN', Opcode::OP_MIN);
opcodes.insert('OP_MAX', Opcode::OP_MAX);
Compiler { opcodes }
}
Expand Down
14 changes: 13 additions & 1 deletion src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod Opcode {
pub const OP_NOT: u8 = 145;
pub const OP_ADD: u8 = 147;
pub const OP_SUB: u8 = 148;
pub const OP_MIN: u8 = 163;
pub const OP_MAX: u8 = 164;

use shinigami::engine::{Engine, EngineTrait};
Expand Down Expand Up @@ -196,7 +197,7 @@ pub mod Opcode {
160 => not_implemented(ref engine),
161 => not_implemented(ref engine),
162 => not_implemented(ref engine),
163 => not_implemented(ref engine),
163 => opcode_min(ref engine),
164 => opcode_max(ref engine),
_ => not_implemented(ref engine)
}
Expand Down Expand Up @@ -297,6 +298,17 @@ pub mod Opcode {
}
}

fn opcode_min(ref engine: Engine) {
let a = engine.dstack.pop_int();
let b = engine.dstack.pop_int();

engine.dstack.push_int(if a < b {
a
} else {
b
});
}

fn not_implemented(ref engine: Engine) {
panic!("Opcode not implemented");
}
Expand Down
Loading

0 comments on commit 256c2da

Please sign in to comment.