-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: Support index-traits in bytecode generator
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |