Skip to content

Commit

Permalink
Port interpreter test cases to a dir-test based test
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Oct 13, 2024
1 parent 2d7c78d commit a3bd8b9
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 265 deletions.
430 changes: 178 additions & 252 deletions crates/interpreter/src/lib.rs

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions crates/interpreter/test_files/arith.sntn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
target = "evm-ethereum-london"


#[(0.i8) -> 255.i8]
#[(255.i8) -> 0.i8]
func private %not(v0.i8) -> i8 {
block0:
v1.i8 = not v0;
return v1;
}

#[(255.i32, 250.i32) -> 505.i32]
#[(-1.i32, 1.i32) -> 0.i32]
func private %add(v0.i32, v1.i32) -> i32 {
block0:
v2.i32 = add v0 v1;
return v2;
}


#[(1.i32, 1.i32) -> 0.i32]
#[(-1.i32, -1.i32) -> 0.i32]
func private %sub(v0.i32, v1.i32) -> i32 {
block0:
v2.i32 = sub v0 v1;
return v2;
}

17 changes: 17 additions & 0 deletions crates/interpreter/test_files/call.sntn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
target = "evm-ethereum-london"


func public %mult_by_two(v0.i8) -> i8 {
block0:
v1.i8 = mul v0 2.i8;
return v1;
}

#[(0.i8) -> 0.i8]
#[(2.i8) -> 4.i8]
func public %call_test(v0.i8) -> i8 {
block0:
v1.i8 = call %mult_by_two v0;
return v1;
}

50 changes: 50 additions & 0 deletions crates/interpreter/test_files/control_flow.sntn
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
target = "evm-ethereum-london"

#[() -> 0.i1]
func private %jump() -> i1 {
block0:
jump block2;
block1:
return 1.i1;
block2:
return 0.i1;
}

#[(1.i1) -> 1.i8]
#[(0.i1) -> 2.i8]
func private %branch(v0.i1) -> i8 {
block0:
br v0 block1 block2;
block1:
return 1.i8;
block2:
return 2.i8;
}

#[(3.i64) -> 1.i64]
#[(10.i64) -> 2.i64]
#[(0.i64) -> 3.i64]
#[(20.i64) -> 3.i64]
func private %br_table(v0.i64) -> i64 {
block0:
br_table v0 block3 (3.i64 block1) (10.i64 block2);
block1:
return 1.i64;
block2:
return 2.i64;
block3:
return 3.i64;
}


#[(0.i1) -> 1.i8]
#[(1.i1) -> -1.i8]
func private %simple_phi(v0.i1) -> i8 {
block0:
br v0 block1 block2;
block1:
jump block2;
block2:
v1.i8 = phi (1.i8 block0) (-1.i8 block1);
return v1;
}
10 changes: 10 additions & 0 deletions crates/interpreter/test_files/data.sntn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
target = "evm-ethereum-london"

#[(10.i256, 255.i8) -> 255.i8]
func private %store_load(v0.i256, v1.i8) -> i8 {
block0:
v2.*i8 = int_to_ptr v0 *i8;
mstore v2 v1 i8;
v3.i8 = mload v2 i8;
return v3;
}
34 changes: 34 additions & 0 deletions crates/interpreter/test_files/gep.sntn
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
target = "evm-ethereum-london"

type @s1 = {i32, i64, i1};
type @nested = {i32, [i16; 3], [i8; 2]};

#[(0.i256) -> 12.i256]
#[(8.i256) -> 20.i256]
func private %gep_basic(v0.i256) -> i256 {
block0:
v1.*@s1 = int_to_ptr v0 *@s1;
v2.*i1 = gep v1 0.i256 2.i256;
v3.i256 = ptr_to_int v2 i256;
return v3;
}


#[(0.i256) -> 64.i256]
func private %gep_ptr_ty(v0.i256) -> i256 {
block0:
v1.*[*i32; 3] = int_to_ptr v0 *[*i32; 3];
v2.**i32 = gep v1 0.i256 2.i256;
v3.i256 = ptr_to_int v2 i256;
return v3;
}


#[(0.i256) -> 11.i256]
func private %gep_aggregate(v0.i256) -> i256 {
block0:
v1.*@nested = int_to_ptr v0 *@nested;
v2.*i8 = gep v1 0.i256 2.i256 1.i256;
v3.i256 = ptr_to_int v2 i256;
return v3;
}
27 changes: 16 additions & 11 deletions crates/interpreter/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::Write;

use once_cell::sync::Lazy;
use regex::Regex;
use sonatina_interpreter::machine::Machine;
use sonatina_interpreter::Machine;
use sonatina_ir::{interpret::EvalValue, module::FuncRef, Immediate};
use sonatina_parser::{
ast::{Value, ValueKind},
Expand All @@ -18,7 +18,7 @@ pub fn parse_module(file_path: &str) -> ParsedModule {
Err(errs) => {
let mut v: Vec<u8> = Vec::new();
for err in errs {
err.print(&mut v, &file_path, &content, false).unwrap();
err.print(&mut v, file_path, &content, false).unwrap();
writeln!(&mut v).unwrap();
}
let err_str = String::from_utf8(v).unwrap();
Expand Down Expand Up @@ -90,13 +90,18 @@ impl TestCase {
&format!("invalid `{comment}`, `#[(args_list) -> ret]` is expected"),
));
};
let args = caps["args"]
.split(",")
.map(|arg| {
let arg = arg.trim();
parse_value(module, func, arg)
})
.collect::<Result<_, _>>()?;
let args = if !caps["args"].is_empty() {
caps["args"]
.split(",")
.map(|arg| {
let arg = arg.trim();
dbg!(arg);
parse_value(module, func, arg)
})
.collect::<Result<_, _>>()?
} else {
vec![]
};

let ret = caps
.name("ret")
Expand Down Expand Up @@ -150,8 +155,8 @@ static PATTERN: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
\[
\((?P<args>[a-zA-Z0-9_.@*]+(?:,\s*[a-zA-Z0-9_.@*]+)*,?)\)
(?:\s*->\s*(?P<ret>[a-zA-Z0-9_.@*]+))?
\((?P<args>[a-zA-Z0-9_.@*-]*(?:,\s*[a-zA-Z0-9_.@*-]+)*,?)\)
(?:\s*->\s*(?P<ret>[a-zA-Z0-9_.@*-]+))?
\]
",
)
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ mod common;

use common::parse_test_cases;
use dir_test::{dir_test, Fixture};
use sonatina_interpreter::machine::Machine;
use sonatina_interpreter::Machine;
use sonatina_parser::ParsedModule;

#[dir_test(
dir: "$CARGO_MANIFEST_DIR/test_files",
glob: "*.sntn",
loader: common::parse_module,
)]
fn test_arith(fixture: Fixture<ParsedModule>) {
fn test(fixture: Fixture<ParsedModule>) {
let parsed_module = fixture.into_content();
let test_cases = match parse_test_cases(&parsed_module) {
Ok(test_cases) => test_cases,
Expand Down

0 comments on commit a3bd8b9

Please sign in to comment.