Browse Source

feat: nested structs

structs
Garrit Franke 3 years ago
parent
commit
3fd6098627
  1. 7
      src/parser/rules.rs
  2. 23
      tests/structs.sb

7
src/parser/rules.rs

@ -341,7 +341,12 @@ impl Parser {
fn parse_field_access(&mut self, lhs: Expression) -> Result<Expression, String> {
self.match_token(TokenKind::Dot)?;
let field = self.match_identifier()?;
Ok(Expression::FieldAccess(Box::new(lhs), field))
let expr = Expression::FieldAccess(Box::new(lhs), field);
if self.peek_token(TokenKind::Dot).is_ok() {
self.parse_field_access(expr)
} else {
Ok(expr)
}
}
/// TODO: Cleanup

23
tests/structs.sb

@ -42,9 +42,32 @@ fn test_field_access_on_function() {
assert(user_stub().first_name == "Foo")
}
struct Foo {
x: int,
bar: Bar
}
struct Bar {
y: string
}
fn test_nested_structs() {
let foo = new Foo {
x: 5,
bar: new Bar {
y: "Nested field"
}
}
assert(foo.x == 5)
println(foo.bar.y)
assert(foo.bar.y == "Nested field")
}
fn main() {
test_initialization()
test_simple_field_access()
test_field_access_in_function_call()
test_field_access_on_function()
test_nested_structs()
}
Loading…
Cancel
Save