Skip to content

Commit

Permalink
frontend: Support index-traits in bytecode generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Nov 10, 2024
1 parent d50ad86 commit 514a7e9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dora-frontend/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ impl<'a> AstBytecodeGen<'a> {
CallType::Method(..)
| CallType::GenericMethod(..)
| CallType::TraitObjectMethod(..) => {
let obj_expr = expr.object().expect("method target required");
let obj_expr = expr.object().unwrap_or(expr.callee());
let reg = gen_expr(self, obj_expr, DataDest::Alloc);

Some(reg)
Expand Down
29 changes: 29 additions & 0 deletions tests/ops/ops-index-get1.dora
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
struct Foo { a: Float64, b: Float64 }

impl std::traits::IndexGet for Foo {
type Index = Int;
type Item = Float64;

fn get(index: Self::Index): Self::Item {
if index == 0 {
self.a
} else {
assert(index == 1);
self.b
}
}
}

fn get0(x: Foo): Float64 {
x(0)
}

fn get1(x: Foo): Float64 {
x(1)
}

fn main() {
let x = Foo(a=12.0, b=17.0);
assert(get0(x) == 12.0);
assert(get1(x) == 17.0);
}
34 changes: 34 additions & 0 deletions tests/ops/ops-index-set1.dora
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Foo { a: Float64, b: Float64 }

impl std::traits::IndexSet for Foo {
type Index = Int;
type Item = Float64;

fn set(index: Self::Index, value: Self::Item) {
if index == 0 {
self.a = value;
} else {
assert(index == 1);
self.b = value;
}
}
}

fn set0(x: Foo, value: Float64) {
x(0) = value;
}

fn set1(x: Foo, value: Float64) {
x(1) = value;
}

fn main() {
let x = Foo(a=12.0, b=18.0);
set0(x, 16.0);
assert(x.a == 16.0);
assert(x.b == 18.0);

set1(x, 32.0);
assert(x.a == 16.0);
assert(x.b == 32.0);
}

0 comments on commit 514a7e9

Please sign in to comment.