Skip to content

Commit

Permalink
feat: keep-starknet-strange#5 add OP_[2..16] opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Jul 25, 2024
1 parent d0b45e0 commit 480a943
Show file tree
Hide file tree
Showing 3 changed files with 137 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 @@ -20,6 +20,21 @@ 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_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_ADD', Opcode::OP_ADD);
Compiler { opcodes }
}
Expand Down
45 changes: 30 additions & 15 deletions src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
pub mod Opcode {
pub const OP_0: u8 = 0;
pub const OP_1: 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_ADD: u8 = 147;

use shinigami::engine::Engine;
Expand Down Expand Up @@ -89,21 +104,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
92 changes: 92 additions & 0 deletions src/opcodes/tests/test_opcodes.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
use shinigami::compiler::CompilerTraitImpl;
use shinigami::engine::EngineTraitImpl;

fn test_op_n(value: u8) {
let program = format!("OP_{}", value);
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 mut byte = "";
byte.append_byte(value);
let stack_value = format!("\0\0\0\0\0\0\0{}", byte);
let expected_stack = array![stack_value];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_0() {
let program = "OP_0";
Expand Down Expand Up @@ -34,6 +51,81 @@ fn test_op_1() {
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_2() {
test_op_n(2);
}

#[test]
fn test_op_3() {
test_op_n(3);
}

#[test]
fn test_op_4() {
test_op_n(4);
}

#[test]
fn test_op_5() {
test_op_n(5);
}

#[test]
fn test_op_6() {
test_op_n(6);
}

#[test]
fn test_op_7() {
test_op_n(7);
}

#[test]
fn test_op_8() {
test_op_n(8);
}

#[test]
fn test_op_9() {
test_op_n(9);
}

#[test]
fn test_op_10() {
test_op_n(10);
}

#[test]
fn test_op_11() {
test_op_n(11);
}

#[test]
fn test_op_12() {
test_op_n(12);
}

#[test]
fn test_op_13() {
test_op_n(13);
}

#[test]
fn test_op_14() {
test_op_n(14);
}

#[test]
fn test_op_15() {
test_op_n(15);
}

#[test]
fn test_op_16() {
test_op_n(16);
}

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

0 comments on commit 480a943

Please sign in to comment.