Skip to content

Commit

Permalink
distinguish function calls that appear on the same line
Browse files Browse the repository at this point in the history
  • Loading branch information
akihiro17 committed Jun 10, 2024
1 parent ec95070 commit 747cf3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/modules/function/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct FunctionInvocation {
variant_id: usize,
id: usize,
line: usize,
col: usize,
failed: Failed,
is_failable: bool
}
Expand All @@ -39,6 +40,7 @@ impl SyntaxModule<ParserMetadata> for FunctionInvocation {
variant_id: 0,
id: 0,
line: 0,
col: 0,
failed: Failed::new(),
is_failable: false
}
Expand All @@ -49,6 +51,7 @@ impl SyntaxModule<ParserMetadata> for FunctionInvocation {
let tok = meta.get_current_token();
if let Some(ref tok) = tok {
self.line = tok.pos.0;
self.col = tok.pos.1;
}
self.name = variable(meta, variable_name_extensions())?;
// Get the arguments
Expand Down Expand Up @@ -125,14 +128,14 @@ impl TranslateModule for FunctionInvocation {
}).collect::<Vec<String>>().join(" ");
meta.stmt_queue.push_back(format!("{name} {args}{silent}"));
let invocation_return = &format!("__AF_{}{}_v{}", self.name, self.id, self.variant_id);
let invocation_instance = &format!("__AF_{}{}_v{}__{}", self.name, self.id, self.variant_id, self.line);
let invocation_instance = &format!("__AF_{}{}_v{}__{}_{}", self.name, self.id, self.variant_id, self.line, self.col);
let parsed_invocation_return = self.get_variable(meta, invocation_return, true);
if self.is_failable {
let failed = self.failed.translate(meta);
meta.stmt_queue.push_back(failed);
}
meta.stmt_queue.push_back(
format!("__AF_{}{}_v{}__{}={}", self.name, self.id, self.variant_id, self.line, if matches!(self.kind, Type::Array(_)) {
format!("__AF_{}{}_v{}__{}_{}={}", self.name, self.id, self.variant_id, self.line, self.col, if matches!(self.kind, Type::Array(_)) {
// If the function returns an array we have to store the intermediate result in a variable that is of type array
format!("({})", parsed_invocation_return)
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,19 @@ fn variable_ref_function_invocation() {
test_amber!(code, "\"sram\"");
}

#[test]
fn function_calls_on_the_same_line() {
let code = "
fun f(arg: Num): Num {
return arg
}
let a = f(1) + f(2)
echo a
";
test_amber!(code, "3");
}

#[test]
fn main_args() {
let code = "
Expand Down

0 comments on commit 747cf3c

Please sign in to comment.