0.5.0 -- tuples and more optimizations #12

Merged
dbidwell merged 34 commits from 43-tuple-return into master 2025-12-31 17:03:51 -07:00
8 changed files with 303 additions and 87 deletions
Showing only changes of commit 04c205b31d - Show all commits

View File

@@ -4,15 +4,21 @@ use pretty_assertions::assert_eq;
#[test] #[test]
fn simple_binary_expression() -> Result<()> { fn simple_binary_expression() -> Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let i = 1 + 2; let i = 1 + 2;
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -27,8 +33,8 @@ fn simple_binary_expression() -> Result<()> {
#[test] #[test]
fn nested_binary_expressions() -> Result<()> { fn nested_binary_expressions() -> Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
fn calculateArgs(arg1, arg2, arg3) { fn calculateArgs(arg1, arg2, arg3) {
return (arg1 + arg2) * arg3; return (arg1 + arg2) * arg3;
@@ -38,8 +44,14 @@ fn nested_binary_expressions() -> Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -72,15 +84,21 @@ fn nested_binary_expressions() -> Result<()> {
#[test] #[test]
fn stress_test_constant_folding() -> Result<()> { fn stress_test_constant_folding() -> Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let negationHell = (-1 + -2) * (-3 + (-4 * (-5 + -6))); let negationHell = (-1 + -2) * (-3 + (-4 * (-5 + -6)));
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -95,16 +113,22 @@ fn stress_test_constant_folding() -> Result<()> {
#[test] #[test]
fn test_constant_folding_with_variables_mixed_in() -> Result<()> { fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
let compiled = compile! { let result = compile! {
debug check
r#" r#"
device self = "db"; device self = "db";
let i = 1 - 3 * (1 + 123.4) * self.Setting + 245c; let i = 1 - 3 * (1 + 123.4) * self.Setting + 245c;
"# "#
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -123,15 +147,21 @@ fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
#[test] #[test]
fn test_ternary_expression() -> Result<()> { fn test_ternary_expression() -> Result<()> {
let compiled = compile! { let result = compile! {
debug check
r#" r#"
let i = 1 > 2 ? 15 : 20; let i = 1 > 2 ? 15 : 20;
"# "#
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -148,16 +178,22 @@ fn test_ternary_expression() -> Result<()> {
#[test] #[test]
fn test_ternary_expression_assignment() -> Result<()> { fn test_ternary_expression_assignment() -> Result<()> {
let compiled = compile! { let result = compile! {
debug check
r#" r#"
let i = 0; let i = 0;
i = 1 > 2 ? 15 : 20; i = 1 > 2 ? 15 : 20;
"# "#
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -175,15 +211,21 @@ fn test_ternary_expression_assignment() -> Result<()> {
#[test] #[test]
fn test_negative_literals() -> Result<()> { fn test_negative_literals() -> Result<()> {
let compiled = compile!( let result = compile!(
debug check
r#" r#"
let item = -10c - 20c; let item = -10c - 20c;
"# "#
); );
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -198,16 +240,22 @@ fn test_negative_literals() -> Result<()> {
#[test] #[test]
fn test_mismatched_temperature_literals() -> Result<()> { fn test_mismatched_temperature_literals() -> Result<()> {
let compiled = compile!( let result = compile!(
debug check
r#" r#"
let item = -10c - 100k; let item = -10c - 100k;
let item2 = item + 500c; let item2 = item + 500c;
"# "#
); );
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main

View File

@@ -3,8 +3,8 @@ use pretty_assertions::assert_eq;
#[test] #[test]
fn test_if_statement() -> anyhow::Result<()> { fn test_if_statement() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 10; let a = 10;
if (a > 5) { if (a > 5) {
@@ -13,8 +13,14 @@ fn test_if_statement() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -33,8 +39,8 @@ fn test_if_statement() -> anyhow::Result<()> {
#[test] #[test]
fn test_if_else_statement() -> anyhow::Result<()> { fn test_if_else_statement() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 0; let a = 0;
if (10 > 5) { if (10 > 5) {
@@ -45,8 +51,14 @@ fn test_if_else_statement() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -68,8 +80,8 @@ fn test_if_else_statement() -> anyhow::Result<()> {
#[test] #[test]
fn test_if_else_if_statement() -> anyhow::Result<()> { fn test_if_else_if_statement() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 0; let a = 0;
if (a == 1) { if (a == 1) {
@@ -82,8 +94,14 @@ fn test_if_else_if_statement() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -111,8 +129,8 @@ fn test_if_else_if_statement() -> anyhow::Result<()> {
#[test] #[test]
fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> { fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 1; let a = 1;
let b = 2; let b = 2;
@@ -129,8 +147,14 @@ fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main

View File

@@ -3,14 +3,20 @@ use pretty_assertions::assert_eq;
#[test] #[test]
fn no_arguments() -> anyhow::Result<()> { fn no_arguments() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
fn doSomething() {}; fn doSomething() {};
let i = doSomething(); let i = doSomething();
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
let to_test = indoc! { let to_test = indoc! {
" "
j main j main
@@ -25,15 +31,15 @@ fn no_arguments() -> anyhow::Result<()> {
" "
}; };
assert_eq!(compiled, to_test); assert_eq!(result.output, to_test);
Ok(()) Ok(())
} }
#[test] #[test]
fn let_var_args() -> anyhow::Result<()> { fn let_var_args() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
fn mul2(arg1) { fn mul2(arg1) {
return arg1 * 2; return arg1 * 2;
@@ -46,8 +52,14 @@ fn let_var_args() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -99,8 +111,8 @@ fn incorrect_args_count() -> anyhow::Result<()> {
#[test] #[test]
fn inline_literal_args() -> anyhow::Result<()> { fn inline_literal_args() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
fn doSomething(arg1, arg2) { fn doSomething(arg1, arg2) {
return 5; return 5;
@@ -110,8 +122,14 @@ fn inline_literal_args() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -141,8 +159,8 @@ fn inline_literal_args() -> anyhow::Result<()> {
#[test] #[test]
fn mixed_args() -> anyhow::Result<()> { fn mixed_args() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let arg1 = 123; let arg1 = 123;
let returnValue = doSomething(arg1, 456); let returnValue = doSomething(arg1, 456);
@@ -150,8 +168,14 @@ fn mixed_args() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -179,8 +203,8 @@ fn mixed_args() -> anyhow::Result<()> {
#[test] #[test]
fn with_return_statement() -> anyhow::Result<()> { fn with_return_statement() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
fn doSomething(arg1) { fn doSomething(arg1) {
return 456; return 456;
@@ -190,8 +214,14 @@ fn with_return_statement() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -216,8 +246,8 @@ fn with_return_statement() -> anyhow::Result<()> {
#[test] #[test]
fn with_negative_return_literal() -> anyhow::Result<()> { fn with_negative_return_literal() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
fn doSomething() { fn doSomething() {
return -1; return -1;
@@ -226,8 +256,14 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main

View File

@@ -58,7 +58,9 @@ fn negative_number_handling() -> anyhow::Result<()> {
j main j main
main: main:
move r8 -100 move r8 -100
move r9 50 sub r1 0 r8
move r9 r1
move r10 50
" "
} }
); );

View File

@@ -3,8 +3,8 @@ use pretty_assertions::assert_eq;
#[test] #[test]
fn test_comparison_expressions() -> anyhow::Result<()> { fn test_comparison_expressions() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let isGreater = 10 > 5; let isGreater = 10 > 5;
let isLess = 5 < 10; let isLess = 5 < 10;
@@ -15,8 +15,14 @@ fn test_comparison_expressions() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -42,8 +48,8 @@ fn test_comparison_expressions() -> anyhow::Result<()> {
#[test] #[test]
fn test_logical_and_or_not() -> anyhow::Result<()> { fn test_logical_and_or_not() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let logic1 = 1 && 1; let logic1 = 1 && 1;
let logic2 = 1 || 0; let logic2 = 1 || 0;
@@ -51,8 +57,14 @@ fn test_logical_and_or_not() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -72,15 +84,21 @@ fn test_logical_and_or_not() -> anyhow::Result<()> {
#[test] #[test]
fn test_complex_logic() -> anyhow::Result<()> { fn test_complex_logic() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let logic = (10 > 5) && (5 < 10); let logic = (10 > 5) && (5 < 10);
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -98,15 +116,21 @@ fn test_complex_logic() -> anyhow::Result<()> {
#[test] #[test]
fn test_math_with_logic() -> anyhow::Result<()> { fn test_math_with_logic() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let logic = (1 + 2) > 1; let logic = (1 + 2) > 1;
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -122,15 +146,21 @@ fn test_math_with_logic() -> anyhow::Result<()> {
#[test] #[test]
fn test_boolean_in_logic() -> anyhow::Result<()> { fn test_boolean_in_logic() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let res = true && false; let res = true && false;
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -146,8 +176,8 @@ fn test_boolean_in_logic() -> anyhow::Result<()> {
#[test] #[test]
fn test_invert_a_boolean() -> anyhow::Result<()> { fn test_invert_a_boolean() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let i = true; let i = true;
let y = !i; let y = !i;
@@ -156,8 +186,14 @@ fn test_invert_a_boolean() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main

View File

@@ -3,8 +3,8 @@ use pretty_assertions::assert_eq;
#[test] #[test]
fn test_infinite_loop() -> anyhow::Result<()> { fn test_infinite_loop() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 0; let a = 0;
loop { loop {
@@ -13,9 +13,15 @@ fn test_infinite_loop() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
// __internal_Labels: L1 (start), L2 (end) // __internal_Labels: L1 (start), L2 (end)
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -35,8 +41,8 @@ fn test_infinite_loop() -> anyhow::Result<()> {
#[test] #[test]
fn test_loop_break() -> anyhow::Result<()> { fn test_loop_break() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 0; let a = 0;
loop { loop {
@@ -48,9 +54,15 @@ fn test_loop_break() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
// __internal_Labels: L1 (start), L2 (end), L3 (if end - implicit else label) // __internal_Labels: L1 (start), L2 (end), L3 (if end - implicit else label)
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -74,8 +86,8 @@ fn test_loop_break() -> anyhow::Result<()> {
#[test] #[test]
fn test_while_loop() -> anyhow::Result<()> { fn test_while_loop() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
" "
let a = 0; let a = 0;
while (a < 10) { while (a < 10) {
@@ -84,9 +96,15 @@ fn test_while_loop() -> anyhow::Result<()> {
" "
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
// __internal_Labels: L1 (start), L2 (end) // __internal_Labels: L1 (start), L2 (end)
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main
@@ -108,8 +126,8 @@ fn test_while_loop() -> anyhow::Result<()> {
#[test] #[test]
fn test_loop_continue() -> anyhow::Result<()> { fn test_loop_continue() -> anyhow::Result<()> {
let compiled = compile! { let result = compile! {
debug check
r#" r#"
let a = 0; let a = 0;
loop { loop {
@@ -122,9 +140,15 @@ fn test_loop_continue() -> anyhow::Result<()> {
"# "#
}; };
assert!(
result.errors.is_empty(),
"Expected no errors, got: {:?}",
result.errors
);
// __internal_Labels: L1 (start), L2 (end), L3 (if end) // __internal_Labels: L1 (start), L2 (end), L3 (if end)
assert_eq!( assert_eq!(
compiled, result.output,
indoc! { indoc! {
" "
j main j main

View File

@@ -39,6 +39,8 @@ fn negation_of_variable() -> anyhow::Result<()> {
j main j main
main: main:
move r8 10 move r8 10
sub r1 0 r8
move r9 r1
" "
} }
); );
@@ -218,13 +220,15 @@ fn negation_of_expression() -> anyhow::Result<()> {
" "
}; };
// Should be -5 // Should be -5 (constant folded)
assert_eq!( assert_eq!(
compiled, compiled,
indoc! { indoc! {
" "
j main j main
main: main:
sub r1 0 5
move r8 r1
" "
} }
); );
@@ -240,13 +244,15 @@ fn complex_negation_and_priority() -> anyhow::Result<()> {
" "
}; };
// Should be -(5 * 2) = -10 // Should be -(5 * 2) = -10 (folded to constant)
assert_eq!( assert_eq!(
compiled, compiled,
indoc! { indoc! {
" "
j main j main
main: main:
sub r1 0 10
move r8 r1
" "
} }
); );

View File

@@ -846,6 +846,46 @@ impl<'a> Compiler<'a> {
} }
(var_loc, None) (var_loc, None)
} }
Expression::Negation(_) => {
// Use try_fold_negation to see if this is a constant folded negation
if let Some(num) = self.try_fold_negation(&expr.node) {
let loc = scope.add_variable(
name_str.clone(),
LocationRequest::Persist,
Some(name_span),
)?;
self.emit_variable_assignment(&loc, Operand::Number(num.into()))?;
return Ok(Some(CompileLocation {
location: loc,
temp_name: None,
}));
}
// Otherwise, compile the negation expression
let result = self.expression(expr, scope)?;
let var_loc = scope.add_variable(
name_str.clone(),
LocationRequest::Persist,
Some(name_span),
)?;
if let Some(res) = result {
// Move result from temp to new persistent variable
let result_reg = self.resolve_register(&res.location)?;
self.emit_variable_assignment(&var_loc, Operand::Register(result_reg))?;
// Free the temp result
if let Some(name) = res.temp_name {
scope.free_temp(name, None)?;
}
} else {
return Err(Error::Unknown(
format!("`{name_str}` negation expression did not produce a value"),
Some(name_span),
));
}
(var_loc, None)
}
_ => { _ => {
return Err(Error::Unknown( return Err(Error::Unknown(
format!("`{name_str}` declaration of this type is not supported/implemented."), format!("`{name_str}` declaration of this type is not supported/implemented."),