diff --git a/compiler/passes/src/code_generation/visit_expressions.rs b/compiler/passes/src/code_generation/visit_expressions.rs index 0cd5752508..b48b1a9f85 100644 --- a/compiler/passes/src/code_generation/visit_expressions.rs +++ b/compiler/passes/src/code_generation/visit_expressions.rs @@ -273,7 +273,7 @@ impl<'a> CodeGenerator<'a> { } fn visit_call(&mut self, input: &'a CallExpression) -> (String, String) { - let mut call_instruction = format!("call {} ", input.function); + let mut call_instruction = format!(" call {} ", input.function); let mut instructions = String::new(); for argument in input.arguments.iter() { diff --git a/compiler/passes/src/code_generation/visit_program.rs b/compiler/passes/src/code_generation/visit_program.rs index bd46855664..e9f95dcef8 100644 --- a/compiler/passes/src/code_generation/visit_program.rs +++ b/compiler/passes/src/code_generation/visit_program.rs @@ -77,17 +77,17 @@ impl<'a> CodeGenerator<'a> { let function_string = self.visit_function(function); if self.is_program_function { - closures.push_str(&function_string); - closures.push('\n'); - } else { functions.push_str(&function_string); functions.push('\n'); + } else { + closures.push_str(&function_string); + closures.push('\n'); } - - // Unset the `is_program_function` flag. - self.is_program_function = false; }); + // Unset the `is_program_function` flag. + self.is_program_function = false; + // Closures must precede functions in the Aleo program. program_string.push_str(&closures); program_string.push('\n'); @@ -164,7 +164,11 @@ impl<'a> CodeGenerator<'a> { self.current_function = Some(function); // Construct the header of the function. - let mut function_string = format!("function {}:\n", function.identifier); + // If a function is a program function, generate an Aleo `function`, otherwise generate an Aleo `closure`. + let mut function_string = match self.is_program_function { + true => format!("function {}:\n", function.identifier), + false => format!("closure {}:\n", function.identifier), + }; // Construct and append the input declarations of the function. for input in function.input.iter() { diff --git a/tests/compiler/function/function_call.leo b/tests/compiler/function/function_call.leo new file mode 100644 index 0000000000..aaa99c398f --- /dev/null +++ b/tests/compiler/function/function_call.leo @@ -0,0 +1,22 @@ +/* +namespace: Compile +expectation: Pass +input_file: inputs/dummy.in +*/ + +@program +function main(a: u32, b: u32, y: bool) -> u32 { + if y { + return adder(a, b); + } else { + return subber(a, b); + } +} + +function adder(a: u32, b: u32) -> u32 { + return a + b; +} + +function subber(a: u32, b: u32) -> u32 { + return a - b; +} \ No newline at end of file diff --git a/tests/expectations/compiler/function/function_call.out b/tests/expectations/compiler/function/function_call.out new file mode 100644 index 0000000000..e1013a5d39 --- /dev/null +++ b/tests/expectations/compiler/function/function_call.out @@ -0,0 +1,9 @@ +--- +namespace: Compile +expectation: Pass +outputs: + - output: + - initial_input_ast: 7771059685b08997e10c889f701cab07503de86515a498e7e3b332eb0499bd80 + initial_ast: e4d2863962de0163515a775493ed831b2f06bb58bdbb7906de9e9a9f3c08db7c + unrolled_ast: e4d2863962de0163515a775493ed831b2f06bb58bdbb7906de9e9a9f3c08db7c + ssa_ast: 01b6f834110362be6c0c449feaac4c5957a1f0daa02df7cb872e78bf8e06f527 diff --git a/tests/expectations/compiler/integers/i128/min_fail.out b/tests/expectations/compiler/integers/i128/min_fail.out index e623bcd976..c2513046ff 100644 --- a/tests/expectations/compiler/integers/i128/min_fail.out +++ b/tests/expectations/compiler/integers/i128/min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Tag)), (\"\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i128/negate_min_fail.out b/tests/expectations/compiler/integers/i128/negate_min_fail.out index b6b2fb0a60..f7660a69f3 100644 --- a/tests/expectations/compiler/integers/i128/negate_min_fail.out +++ b/tests/expectations/compiler/integers/i128/negate_min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Tag)), (\"\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i16/min_fail.out b/tests/expectations/compiler/integers/i16/min_fail.out index cfc269a703..0d84cf58e3 100644 --- a/tests/expectations/compiler/integers/i16/min_fail.out +++ b/tests/expectations/compiler/integers/i16/min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Tag)), (\"\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i16/negate_min_fail.out b/tests/expectations/compiler/integers/i16/negate_min_fail.out index ba69433d0d..d12c306f26 100644 --- a/tests/expectations/compiler/integers/i16/negate_min_fail.out +++ b/tests/expectations/compiler/integers/i16/negate_min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Tag)), (\"\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i32/min_fail.out b/tests/expectations/compiler/integers/i32/min_fail.out index 942ed77053..eb1e7f565e 100644 --- a/tests/expectations/compiler/integers/i32/min_fail.out +++ b/tests/expectations/compiler/integers/i32/min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Tag)), (\"\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i32/negate_min_fail.out b/tests/expectations/compiler/integers/i32/negate_min_fail.out index 88ea5d17a8..c96611f73d 100644 --- a/tests/expectations/compiler/integers/i32/negate_min_fail.out +++ b/tests/expectations/compiler/integers/i32/negate_min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Tag)), (\"\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i64/min_fail.out b/tests/expectations/compiler/integers/i64/min_fail.out index 0604eb10ba..b0f4ab8637 100644 --- a/tests/expectations/compiler/integers/i64/min_fail.out +++ b/tests/expectations/compiler/integers/i64/min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Tag)), (\"\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i64/negate_min_fail.out b/tests/expectations/compiler/integers/i64/negate_min_fail.out index 841c986650..b5b91a76f3 100644 --- a/tests/expectations/compiler/integers/i64/negate_min_fail.out +++ b/tests/expectations/compiler/integers/i64/negate_min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Tag)), (\"\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i8/min_fail.out b/tests/expectations/compiler/integers/i8/min_fail.out index d87cba5587..8d316c4736 100644 --- a/tests/expectations/compiler/integers/i8/min_fail.out +++ b/tests/expectations/compiler/integers/i8/min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Tag)), (\"\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\\n\", Nom(Many1))] }" diff --git a/tests/expectations/compiler/integers/i8/negate_min_fail.out b/tests/expectations/compiler/integers/i8/negate_min_fail.out index 04546d7896..618b6a840b 100644 --- a/tests/expectations/compiler/integers/i8/negate_min_fail.out +++ b/tests/expectations/compiler/integers/i8/negate_min_fail.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Tag)), (\"\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Many1))] }" + - "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\\n\", Nom(Tag)), (\"closure main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\\n\", Nom(Alt)), (\"closure main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\\n\", Nom(Many1))] }"