From 3305d3c16256dc59ae92805af6934054d1668707 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sun, 15 Mar 2026 15:23:37 +0200 Subject: [PATCH] Allow returning expression lists --- examples/test.lua | 6 ++---- src/ast.rs | 2 +- src/compile.rs | 22 ++++++++++++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/examples/test.lua b/examples/test.lua index 5f4ad17..9cbc050 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -1,7 +1,5 @@ function add(x) - return function (y) - return x + y - end + return x end -global c = print(add(5)(7)) \ No newline at end of file +global c = print(add(5)) \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index bf6b742..89e4670 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -207,7 +207,7 @@ impl Parse for Block { #[derive(Debug, Clone)] pub enum Statement { Assignment(AccessModifier, Node, Node), - Return(Node), + Return(ExpressionList), If(Node, Block), } diff --git a/src/compile.rs b/src/compile.rs index b095b10..d869835 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -77,7 +77,13 @@ impl Statement { constants.extend(expr.kind.find_constants(scope)); constants } - Statement::Return(expr) => expr.kind.find_constants(scope), + Statement::Return(expr_list) => { + let mut constants = HashSet::new(); + for expr in &expr_list.0 { + constants.extend(expr.kind.find_constants(scope)); + } + constants + } Statement::If(cond, then) => { let mut constants = HashSet::new(); constants.extend(cond.kind.find_constants(scope)); @@ -106,12 +112,16 @@ impl Statement { } } } - Statement::Return(expr) => { - let (instr, registers) = expr.kind.compile(state, scope, 1); - instructions.extend(instr); + Statement::Return(expr_list) => { + let mut ret_registers = Vec::new(); + for expr in &expr_list.0 { + let (instr, registers) = expr.kind.compile(state, scope, 1); + instructions.extend(instr); + ret_registers.extend(registers); + } instructions.push(Instruction::Return( - *registers.first().unwrap(), - *registers.last().unwrap(), + *ret_registers.first().unwrap(), + *ret_registers.last().unwrap(), )); } Statement::If(node, block) => todo!(),