Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPCODE GREATER THAN #43

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub impl CompilerTraitImpl of CompilerTrait {
opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH);
opcodes.insert('OP_1ADD', Opcode::OP_1ADD);
opcodes.insert('OP_ADD', Opcode::OP_ADD);
opcodes.insert('OP_GREATERTHAN', Opcode::OP_GREATERTHAN);
opcodes.insert('OP_MAX', Opcode::OP_MAX);
Compiler { opcodes }
}
Expand Down
13 changes: 12 additions & 1 deletion src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod Opcode {
pub const OP_DEPTH: u8 = 116;
pub const OP_1ADD: u8 = 139;
pub const OP_ADD: u8 = 147;
pub const OP_GREATERTHAN: u8 = 160;
pub const OP_MAX: u8 = 164;

use shinigami::engine::Engine;
Expand Down Expand Up @@ -186,7 +187,7 @@ pub mod Opcode {
157 => not_implemented(ref engine),
158 => not_implemented(ref engine),
159 => not_implemented(ref engine),
160 => not_implemented(ref engine),
160 => opcode_greater_than(ref engine),
161 => not_implemented(ref engine),
162 => not_implemented(ref engine),
163 => not_implemented(ref engine),
Expand Down Expand Up @@ -225,6 +226,16 @@ pub mod Opcode {
panic!("Opcode not implemented");
}

fn opcode_greater_than(ref engine: Engine) {
let a = engine.dstack.pop_int();
let b = engine.dstack.pop_int();
engine.dstack.push_int(if b > a {
1
} else {
0
});
}

fn opcode_max(ref engine: Engine) {
let a = engine.dstack.pop_int();
let b = engine.dstack.pop_int();
Expand Down
18 changes: 18 additions & 0 deletions src/opcodes/tests/test_opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ fn test_op_add() {
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_greater_than() {
let program = "OP_1 OP_0 OP_GREATERTHAN";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);
let _ = engine.step();
let _ = engine.step();
let res = engine.step();
assert!(res, "Execution of run failed");

let dstack = engine.get_dstack();
assert_eq!(dstack.len(), 1, "Stack length is not 1");

let expected_stack = array!["\0\0\0\0\0\0\0\x01"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_max() {
let program = "OP_1 OP_0 OP_MAX";
Expand Down