Allow syscalls in infix operations

This commit is contained in:
2025-12-11 02:24:01 -07:00
parent 342b1ab107
commit 236b50c813
2 changed files with 20 additions and 17 deletions

View File

@@ -1562,31 +1562,33 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
mut expr: BlockExpression<'a>, mut expr: BlockExpression<'a>,
parent_scope: &'v mut VariableScope<'a, '_>, parent_scope: &'v mut VariableScope<'a, '_>,
) -> Result<(), Error<'a>> { ) -> Result<(), Error<'a>> {
fn get_expression_priority<'a>(expr: &Spanned<Expression<'a>>) -> u32 {
match expr.node {
Expression::ConstDeclaration(_) => 0,
Expression::DeviceDeclaration(_) => 1,
Expression::Function(_) => 2,
_ => 3,
}
}
// First, sort the expressions to ensure functions are hoisted // First, sort the expressions to ensure functions are hoisted
expr.0.sort_by(|a, b| { expr.0.sort_by(|a, b| {
if matches!( let a_cost = get_expression_priority(a);
b.node, let b_cost = get_expression_priority(b);
Expression::Function(_) | Expression::ConstDeclaration(_)
) && matches!( a_cost.cmp(&b_cost)
a.node,
Expression::Function(_) | Expression::ConstDeclaration(_)
) {
std::cmp::Ordering::Equal
} else if matches!(
a.node,
Expression::Function(_) | Expression::ConstDeclaration(_)
) {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
}); });
let mut scope = VariableScope::scoped(parent_scope); let mut scope = VariableScope::scoped(parent_scope);
for expr in expr.0 { for expr in expr.0 {
if !self.declared_main if !self.declared_main
&& !matches!(expr.node, Expression::Function(_)) && !matches!(
expr.node,
Expression::Function(_)
| Expression::ConstDeclaration(_)
| Expression::DeviceDeclaration(_)
)
&& !parent_scope.has_parent() && !parent_scope.has_parent()
{ {
self.write_output("main:")?; self.write_output("main:")?;

View File

@@ -766,6 +766,7 @@ impl<'a> Parser<'a> {
Expression::Binary(_) Expression::Binary(_)
| Expression::Logical(_) | Expression::Logical(_)
| Expression::Invocation(_) | Expression::Invocation(_)
| Expression::Syscall(_)
| Expression::Priority(_) | Expression::Priority(_)
| Expression::Literal(_) | Expression::Literal(_)
| Expression::Variable(_) | Expression::Variable(_)