diff --git a/rust_compiler/libs/compiler/src/v1.rs b/rust_compiler/libs/compiler/src/v1.rs index 4d56a11..4e2714e 100644 --- a/rust_compiler/libs/compiler/src/v1.rs +++ b/rust_compiler/libs/compiler/src/v1.rs @@ -1562,31 +1562,33 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> { mut expr: BlockExpression<'a>, parent_scope: &'v mut VariableScope<'a, '_>, ) -> Result<(), Error<'a>> { + fn get_expression_priority<'a>(expr: &Spanned>) -> u32 { + match expr.node { + Expression::ConstDeclaration(_) => 0, + Expression::DeviceDeclaration(_) => 1, + Expression::Function(_) => 2, + _ => 3, + } + } + // First, sort the expressions to ensure functions are hoisted expr.0.sort_by(|a, b| { - if matches!( - b.node, - Expression::Function(_) | Expression::ConstDeclaration(_) - ) && matches!( - 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 a_cost = get_expression_priority(a); + let b_cost = get_expression_priority(b); + + a_cost.cmp(&b_cost) }); let mut scope = VariableScope::scoped(parent_scope); for expr in expr.0 { if !self.declared_main - && !matches!(expr.node, Expression::Function(_)) + && !matches!( + expr.node, + Expression::Function(_) + | Expression::ConstDeclaration(_) + | Expression::DeviceDeclaration(_) + ) && !parent_scope.has_parent() { self.write_output("main:")?; diff --git a/rust_compiler/libs/parser/src/lib.rs b/rust_compiler/libs/parser/src/lib.rs index 661b943..35afdb2 100644 --- a/rust_compiler/libs/parser/src/lib.rs +++ b/rust_compiler/libs/parser/src/lib.rs @@ -766,6 +766,7 @@ impl<'a> Parser<'a> { Expression::Binary(_) | Expression::Logical(_) | Expression::Invocation(_) + | Expression::Syscall(_) | Expression::Priority(_) | Expression::Literal(_) | Expression::Variable(_)