Skip to content

Commit

Permalink
OP_2 to OP_16 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakymulla authored Jul 26, 2024
1 parent 2b2fa56 commit 6e00f2e
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ pub impl CompilerTraitImpl of CompilerTrait {
opcodes.insert('OP_0', Opcode::OP_0);
opcodes.insert('OP_1', Opcode::OP_1);
opcodes.insert('OP_TRUE', Opcode::OP_TRUE);
opcodes.insert('OP_2', Opcode::OP_2);
opcodes.insert('OP_3', Opcode::OP_3);
opcodes.insert('OP_4', Opcode::OP_4);
opcodes.insert('OP_5', Opcode::OP_5);
opcodes.insert('OP_6', Opcode::OP_6);
opcodes.insert('OP_7', Opcode::OP_7);
opcodes.insert('OP_8', Opcode::OP_8);
opcodes.insert('OP_9', Opcode::OP_9);
opcodes.insert('OP_10', Opcode::OP_10);
opcodes.insert('OP_11', Opcode::OP_11);
opcodes.insert('OP_12', Opcode::OP_12);
opcodes.insert('OP_13', Opcode::OP_13);
opcodes.insert('OP_14', Opcode::OP_14);
opcodes.insert('OP_15', Opcode::OP_15);
opcodes.insert('OP_16', Opcode::OP_16);
opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH);
opcodes.insert('OP_1ADD', Opcode::OP_1ADD);
opcodes.insert('OP_ADD', Opcode::OP_ADD);
Expand Down
45 changes: 30 additions & 15 deletions src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ pub mod Opcode {
pub const OP_0: u8 = 0;
pub const OP_1: u8 = 81;
pub const OP_TRUE: u8 = 81;
pub const OP_2: u8 = 82;
pub const OP_3: u8 = 83;
pub const OP_4: u8 = 84;
pub const OP_5: u8 = 85;
pub const OP_6: u8 = 86;
pub const OP_7: u8 = 87;
pub const OP_8: u8 = 88;
pub const OP_9: u8 = 89;
pub const OP_10: u8 = 90;
pub const OP_11: u8 = 91;
pub const OP_12: u8 = 92;
pub const OP_13: u8 = 93;
pub const OP_14: u8 = 94;
pub const OP_15: u8 = 95;
pub const OP_16: u8 = 96;
pub const OP_DEPTH: u8 = 116;
pub const OP_1ADD: u8 = 139;
pub const OP_ADD: u8 = 147;
Expand Down Expand Up @@ -93,21 +108,21 @@ pub mod Opcode {
79 => not_implemented(ref engine),
80 => not_implemented(ref engine),
81 => opcode_n(1, ref engine),
82 => not_implemented(ref engine),
83 => not_implemented(ref engine),
84 => not_implemented(ref engine),
85 => not_implemented(ref engine),
86 => not_implemented(ref engine),
87 => not_implemented(ref engine),
88 => not_implemented(ref engine),
89 => not_implemented(ref engine),
90 => not_implemented(ref engine),
91 => not_implemented(ref engine),
92 => not_implemented(ref engine),
93 => not_implemented(ref engine),
94 => not_implemented(ref engine),
95 => not_implemented(ref engine),
96 => not_implemented(ref engine),
82 => opcode_n(2, ref engine),
83 => opcode_n(3, ref engine),
84 => opcode_n(4, ref engine),
85 => opcode_n(5, ref engine),
86 => opcode_n(6, ref engine),
87 => opcode_n(7, ref engine),
88 => opcode_n(8, ref engine),
89 => opcode_n(9, ref engine),
90 => opcode_n(10, ref engine),
91 => opcode_n(11, ref engine),
92 => opcode_n(12, ref engine),
93 => opcode_n(13, ref engine),
94 => opcode_n(14, ref engine),
95 => opcode_n(15, ref engine),
96 => opcode_n(16, ref engine),
97 => not_implemented(ref engine),
98 => not_implemented(ref engine),
99 => not_implemented(ref engine),
Expand Down
239 changes: 239 additions & 0 deletions src/opcodes/tests/test_opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ fn test_op_depth_empty_stack() {
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack");
}

fn test_op_2() {
let program = "OP_2";
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\x02"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_depth_one_item() {
let program = "OP_1 OP_DEPTH";
Expand Down Expand Up @@ -159,3 +174,227 @@ fn test_op_1add() {
let expected_stack = array!["\0\0\0\0\0\0\0\x02"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_3() {
let program = "OP_3";
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\x03"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_4() {
let program = "OP_4";
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\x04"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_5() {
let program = "OP_5";
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\x05"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_6() {
let program = "OP_6";
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\x06"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_7() {
let program = "OP_7";
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\x07"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_8() {
let program = "OP_8";
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\x08"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_9() {
let program = "OP_9";
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\x09"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_10() {
let program = "OP_10";
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\x0a"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_11() {
let program = "OP_11";
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\x0b"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_12() {
let program = "OP_12";
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\x0c"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_13() {
let program = "OP_13";
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\x0d"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_14() {
let program = "OP_14";
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\x0e"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_15() {
let program = "OP_15";
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\x0f"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_16() {
let program = "OP_16";
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\x10"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

0 comments on commit 6e00f2e

Please sign in to comment.