Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to call methods without a let statement. #161

Merged
merged 24 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions examples/impl_block_mut.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod ImplBlockMut {
struct A {
a: i32,
b: i32,
}

impl A {
pub fn hello(&self, other: i32) -> i32 {
return self.a * other;
}

pub fn set_a(&mut self, value: i32) {
self.a = value;
return;
}
}

pub fn main() -> i32 {
let mut x: A = A {
a: 2,
b: 3,
};

x.set_a(4);

return x.a;
}
}
2 changes: 2 additions & 0 deletions src/ast/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum Statement {
Return(ReturnStmt),
While(WhileStmt),
FnCall(FnCallOp),
// To allow method calls.
PathOp(PathOp),
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ Statement: ast::statements::Statement = {
<LetStmt> ";" => ast::statements::Statement::Let(<>),
<AssignStmt> ";" => ast::statements::Statement::Assign(<>),
<FnCallOp> ";" => ast::statements::Statement::FnCall(<>),
<PathOp> ";" => ast::statements::Statement::PathOp(<>),
<ReturnStmt> ";" => ast::statements::Statement::Return(<>),
}

Expand Down
3 changes: 3 additions & 0 deletions src/ir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ fn lower_statement(
statements::Statement::FnCall(info) => {
lower_fn_call(builder, info, None, None)?;
}
statements::Statement::PathOp(info) => {
lower_path(builder, info)?;
}
}
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod common;
#[test_case(include_str!("../examples/fib_if.con"), "fib_if", false, 55 ; "fib_if.con")]
#[test_case(include_str!("../examples/import.con"), "import", false, 12 ; "import.con")]
#[test_case(include_str!("../examples/impl_block.con"), "impl_block", false, 8 ; "impl_block.con")]
#[test_case(include_str!("../examples/impl_block_mut.con"), "impl_block_mut", false, 4 ; "impl_block_mut.con")]
#[test_case(include_str!("../examples/simple.con"), "simple", false, 8 ; "simple.con")]
#[test_case(include_str!("../examples/while.con"), "while", false, 16 ; "while.con")]
#[test_case(include_str!("../examples/chars.con"), "chars", false, 117 ; "chars.con")]
Expand Down
Loading