Skip to content

Commit

Permalink
frontend: Remove position() helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Nov 15, 2024
1 parent 990bd42 commit 8e181d7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 47 deletions.
103 changes: 71 additions & 32 deletions dora-frontend/src/generator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@ use crate::generator::{bty_from_ty, generate_fct_id};
use crate::sema::{create_tuple, Sema};
use crate::sema::{ClassDefinitionId, FieldId, SemaFlags};
use crate::stdlib_lookup::{lookup_fct, resolve_path};
use crate::test;
use crate::{check_program, SourceType, SourceTypeArray};
use dora_bytecode::{
self as bytecode, BytecodeFunction, BytecodeOffset, BytecodeType, BytecodeTypeArray,
BytecodeVisitor, ClassId, ConstPoolEntry, ConstPoolIdx, EnumId, FunctionId, GlobalId, Register,
StructId, TraitId,
};

fn position(code: &'static str) -> Vec<(u32, u32)> {
test::check_valid(code, |sa| {
let fct_id = lookup_fct(sa, "<prog>::f");
let fct = generate_fct_id(sa, fct_id);
fct.locations()
.iter()
.map(|(bci, location)| (bci.0, location.line()))
.collect()
})
fn positions(fct: &BytecodeFunction) -> Vec<(u32, u32)> {
fct.locations()
.iter()
.map(|(bci, location)| (bci.0, location.line()))
.collect()
}

fn sema(code: &'static str) -> Sema {
Expand Down Expand Up @@ -97,7 +92,9 @@ fn gen_load_field_uint8() {

#[test]
fn gen_position_load_field_uint8() {
let result = position("class Foo { bar: UInt8 } fn f(a: Foo): UInt8 { return a.bar; }");
let sa = sema("class Foo { bar: UInt8 } fn f(a: Foo): UInt8 { return a.bar; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand All @@ -121,7 +118,9 @@ fn gen_store_field_uint8() {

#[test]
fn gen_position_store_field_uint8() {
let result = position("class Foo{bar: UInt8} fn f(a: Foo, b: UInt8) { a.bar = b; }");
let sa = sema("class Foo{bar: UInt8} fn f(a: Foo, b: UInt8) { a.bar = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -226,7 +225,9 @@ fn gen_div_int() {

#[test]
fn gen_position_div_int() {
let result = position("fn f(a: Int32, b: Int32): Int32 { return a / b; }");
let sa = sema("fn f(a: Int32, b: Int32): Int32 { return a / b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -754,7 +755,9 @@ fn gen_expr_mod() {

#[test]
fn gen_position_mod_int32() {
let result = position("fn f(a: Int32, b: Int32): Int32 { return a % b; }");
let sa = sema("fn f(a: Int32, b: Int32): Int32 { return a % b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -2777,7 +2780,9 @@ fn gen_new_object_initialized() {

#[test]
fn gen_position_new_object() {
let result = position("class Object fn f(): Object { return Object(); }");
let sa = sema("class Object fn f(): Object { return Object(); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -2825,7 +2830,9 @@ fn gen_array_length() {

#[test]
fn gen_position_array_length() {
let result = position("fn f(a: Array[Int32]): Int64 { return a.size(); }");
let sa = sema("fn f(a: Array[Int32]): Int64 { return a.size(); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand All @@ -2840,7 +2847,9 @@ fn gen_array_length_effect() {

#[test]
fn gen_position_array_length_effect() {
let result = position("fn f(a: Array[Int32]) { a.size(); }");
let sa = sema("fn f(a: Array[Int32]) { a.size(); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(0, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -2911,49 +2920,63 @@ fn gen_load_array_ptr() {

#[test]
fn gen_position_load_array_bool() {
let result = position("fn f(a: Array[Bool]): Bool { return a(0); }");
let sa = sema("fn f(a: Array[Bool]): Bool { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_load_array_char() {
let result = position("fn f(a: Array[Char]): Char { return a(0); }");
let sa = sema("fn f(a: Array[Char]): Char { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_load_array_int32() {
let result = position("fn f(a: Array[Int32]): Int32 { return a(0); }");
let sa = sema("fn f(a: Array[Int32]): Int32 { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_load_array_int64() {
let result = position("fn f(a: Array[Int64]): Int64 { return a(0); }");
let sa = sema("fn f(a: Array[Int64]): Int64 { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_load_array_float32() {
let result = position("fn f(a: Array[Float32]): Float32 { return a(0); }");
let sa = sema("fn f(a: Array[Float32]): Float32 { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_load_array_float64() {
let result = position("fn f(a: Array[Float64]): Float64 { return a(0); }");
let sa = sema("fn f(a: Array[Float64]): Float64 { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_load_array_ptr() {
let result = position("class Object fn f(a: Array[Object]): Object { return a(0); }");
let sa = sema("class Object fn f(a: Array[Object]): Object { return a(0); }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -3056,49 +3079,63 @@ fn gen_store_array_ptr() {

#[test]
fn gen_position_store_array_bool() {
let result = position("fn f(a: Array[Bool], b: Bool) { a(0) = b; }");
let sa = sema("fn f(a: Array[Bool], b: Bool) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_store_array_char() {
let result = position("fn f(a: Array[Char], b: Char) { a(0) = b; }");
let sa = sema("fn f(a: Array[Char], b: Char) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_store_array_int32() {
let result = position("fn f(a: Array[Int32], b: Int32) { a(0) = b; }");
let sa = sema("fn f(a: Array[Int32], b: Int32) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_store_array_int64() {
let result = position("fn f(a: Array[Int64], b: Int64) { a(0) = b; }");
let sa = sema("fn f(a: Array[Int64], b: Int64) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_store_array_float32() {
let result = position("fn f(a: Array[Float32], b: Float32) { a(0) = b; }");
let sa = sema("fn f(a: Array[Float32], b: Float32) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_store_array_float64() {
let result = position("fn f(a: Array[Float64], b: Float64) { a(0) = b; }");
let sa = sema("fn f(a: Array[Float64], b: Float64) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}

#[test]
fn gen_position_store_array_ptr() {
let result = position("class Object fn f(a: Array[Object], b: Object) { a(0) = b; }");
let sa = sema("class Object fn f(a: Array[Object], b: Object) { a(0) = b; }");
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(3, 1)];
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -3139,13 +3176,15 @@ fn gen_new_object_with_multiple_args() {

#[test]
fn gen_position_new_object_with_multiple_args() {
let result = position(
let sa = sema(
"
class Foo { a: Int32, b: Int32, c: Int32 }
fn f(): Foo {
return Foo(a = 1i32, b = 2i32, c = 3i32);
}",
);
let (fct, _code) = bc(&sa, "<prog>::f");
let result = positions(&fct);
let expected = vec![(15, 4)];
assert_eq!(expected, result);
}
Expand Down
15 changes: 0 additions & 15 deletions dora-frontend/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
use crate::check_program;
use crate::sema::{Sema, SemaFlags};

pub fn check_valid<F, T>(code: &'static str, f: F) -> T
where
F: FnOnce(&Sema) -> T,
{
check(code, |sa| {
if sa.diag.borrow().has_errors() {
sa.diag.borrow_mut().dump(sa, false);
println!("{}", code);
panic!("unexpected error in test");
}

f(sa)
})
}

pub fn check<F, T>(code: &'static str, f: F) -> T
where
F: FnOnce(&Sema) -> T,
Expand Down

0 comments on commit 8e181d7

Please sign in to comment.