Skip to content

Commit

Permalink
feat: merged w/ main
Browse files Browse the repository at this point in the history
  • Loading branch information
okhaimie-dev committed Jul 26, 2024
2 parents f3e2c57 + f0ca6ce commit 36e1cbf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub impl CompilerTraitImpl of CompilerTrait {
// Add the opcodes to the dict
opcodes.insert('OP_0', Opcode::OP_0);
opcodes.insert('OP_1', Opcode::OP_1);
opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH);
opcodes.insert('OP_1ADD', Opcode::OP_1ADD);
opcodes.insert('OP_ADD', Opcode::OP_ADD);

Expand Down
8 changes: 7 additions & 1 deletion src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod Opcode {
pub const OP_0: u8 = 0;
pub const OP_1: u8 = 81;
pub const OP_DEPTH: u8 = 116;
pub const OP_1ADD: u8 = 139;
pub const OP_ADD: u8 = 147;

Expand Down Expand Up @@ -124,7 +125,7 @@ pub mod Opcode {
113 => not_implemented(ref engine),
114 => not_implemented(ref engine),
115 => not_implemented(ref engine),
116 => not_implemented(ref engine),
116 => opcode_depth(ref engine),
117 => not_implemented(ref engine),
118 => not_implemented(ref engine),
119 => not_implemented(ref engine),
Expand Down Expand Up @@ -175,6 +176,11 @@ pub mod Opcode {
engine.dstack.push_int(a + b);
}

fn opcode_depth(ref engine: Engine) {
let depth: i64 = engine.dstack.len().into();
engine.dstack.push_int(depth);
}

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

#[test]
fn test_op_depth_empty_stack() {
let program = "OP_DEPTH";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);

let res = engine.step();
assert!(res, "Execution of step 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\0"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack");
}

#[test]
fn test_op_depth_one_item() {
let program = "OP_1 OP_DEPTH";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);

let _ = engine.step();
let res = engine.step();
assert!(res, "Execution of step failed");

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

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

#[test]
fn test_op_depth_multiple_items() {
let program = "OP_1 OP_1 OP_ADD OP_1 OP_DEPTH";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);

let _ = engine.step();
let _ = engine.step();
let _ = engine.step();
let _ = engine.step();
let res = engine.step();
assert!(res, "Execution of step failed");

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

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

#[test]
fn test_op_1add() {
let program = "OP_1 OP_1ADD";
Expand Down

0 comments on commit 36e1cbf

Please sign in to comment.