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 27, 2024
1 parent 7825202 commit da73523
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 @@ -20,6 +20,7 @@ pub struct FunctionInvocation {
variant_id: usize,
id: usize,
line: usize,
col: usize,
failed: Failed,
modifier: CommandModifier,
is_failable: bool
Expand All @@ -43,6 +44,7 @@ impl SyntaxModule<ParserMetadata> for FunctionInvocation {
variant_id: 0,
id: 0,
line: 0,
col: 0,
failed: Failed::new(),
modifier: CommandModifier::new().parse_expr(),
is_failable: false
Expand All @@ -56,6 +58,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 @@ -135,15 +138,15 @@ 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);
swap(&mut is_silent, &mut meta.silenced);
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 @@ -1075,6 +1075,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 da73523

Please sign in to comment.