diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 85ae242..e71eef5 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -142,6 +142,7 @@ pub enum Keyword { Function, Boolean, Struct, + New, Unknown, } @@ -363,6 +364,7 @@ impl Cursor<'_> { c if c == "break" => Keyword::Break, c if c == "continue" => Keyword::Continue, c if c == "struct" => Keyword::Struct, + c if c == "new" => Keyword::New, _ => Keyword::Unknown, } } diff --git a/src/parser/rules.rs b/src/parser/rules.rs index 6806b6e..cd45bf9 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -306,7 +306,7 @@ impl Parser { Ok(state) } TokenKind::SquareBraceOpen => self.parse_array(), - TokenKind::Keyword(Keyword::Struct) => self.parse_struct_initialization(), + TokenKind::Keyword(Keyword::New) => self.parse_struct_initialization(), other => Err(format!("Expected Expression, found {:?}", other)), } } diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 5159f84..30fb9e7 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -776,3 +776,25 @@ fn test_array_as_argument() { let tree = parse(tokens, Some(raw.to_string())); assert!(tree.is_ok()); } + +#[test] +fn test_struct_initialization() { + let raw = " + struct User { + username: string, + first_name: string, + last_name: string + } + + fn main() { + let foo = new User { + username: 'foobar', + first_name: 'Foo', + last_name: 'Bar' + } + } + "; + let tokens = tokenize(raw); + let tree = parse(tokens, Some(raw.to_string())); + assert!(tree.is_ok()); +} diff --git a/tests/struct_initialization.sb b/tests/struct_initialization.sb new file mode 100644 index 0000000..d2588a3 --- /dev/null +++ b/tests/struct_initialization.sb @@ -0,0 +1,13 @@ +struct User { + username: string, + first_name: string, + last_name: string +} + +fn main() { + let foo = new User { + username: 'foobar', + first_name: 'Foo', + last_name: 'Bar' + } +} \ No newline at end of file