From 124c9536b478415c088c94d700766765b399bc84 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 23 Feb 2026 19:01:58 -0600 Subject: [PATCH] no more implicit returning in programs and mdouels --- audit.ce | 2 -- mcode.cm | 22 +++++++--------------- vm_suite.ce | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/audit.ce b/audit.ce index 1dea25d0..45ae149c 100644 --- a/audit.ce +++ b/audit.ce @@ -89,5 +89,3 @@ if (length(all_unresolved) > 0) summary = summary + ", " + text(length(all_unres log.console(summary) } run() - -$stop() diff --git a/mcode.cm b/mcode.cm index d70fa983..073e6691 100644 --- a/mcode.cm +++ b/mcode.cm @@ -3103,7 +3103,6 @@ var mcode = function(ast) { var local_slot = 0 var dest = 0 var statements = ast.statements - var last_expr_slot = -1 var stmt = null var kind = null var null_slot = 0 @@ -3170,21 +3169,18 @@ var mcode = function(ast) { kind = stmt.kind if (kind != null) { if (kind == "call") { - last_expr_slot = gen_expr(stmt.expression, -1) + gen_expr(stmt.expression, -1) } else if (kind == "return" || kind == "disrupt" || - kind == "break" || kind == "continue") { - gen_statement(stmt) - last_expr_slot = -1 - } else if (kind == "var" || kind == "def" || + kind == "break" || kind == "continue" || + kind == "var" || kind == "def" || kind == "var_list" || kind == "def_list" || kind == "function" || kind == "block" || kind == "if" || kind == "while" || kind == "do" || kind == "for" || kind == "switch") { gen_statement(stmt) - last_expr_slot = -1 } else { - last_expr_slot = gen_expr(stmt, -1) + gen_expr(stmt, -1) } } else { gen_statement(stmt) @@ -3192,13 +3188,9 @@ var mcode = function(ast) { _i = _i + 1 } - if (last_expr_slot >= 0) { - emit_1("return", last_expr_slot) - } else { - null_slot = alloc_slot() - emit_1("null", null_slot) - emit_1("return", null_slot) - } + null_slot = alloc_slot() + emit_1("null", null_slot) + emit_1("return", null_slot) result = {} result.name = filename != null ? filename : "" diff --git a/vm_suite.ce b/vm_suite.ce index 2ac3f651..3581b5de 100644 --- a/vm_suite.ce +++ b/vm_suite.ce @@ -7650,6 +7650,59 @@ run("string index in text() call", function() { assert_eq(s[0] + sub, "$abc", "reassemble from index + substring") }) +// ============================================================================ +// NO IMPLICIT RETURN +// ============================================================================ + +run("function trailing call no implicit return", function() { + var fn = function() { text(42) } + assert_eq(fn(), null, "trailing call should not implicitly return") +}) + +run("function trailing expression no implicit return", function() { + var fn = function() { var x = 1 + 2 } + assert_eq(fn(), null, "trailing expression should not implicitly return") +}) + +run("function trailing arithmetic no implicit return", function() { + var fn = function() { + var x = 10 + x + 5 + } + assert_eq(fn(), null, "bare arithmetic should not implicitly return") +}) + +run("function explicit return still works", function() { + var fn = function() { return 42 } + assert_eq(fn(), 42, "explicit return should work") +}) + +run("arrow trailing call no implicit return", function() { + var fn = (x) => { text(x) } + assert_eq(fn(42), null, "arrow trailing call should not implicitly return") +}) + +run("arrow trailing arithmetic no implicit return", function() { + var fn = (x) => { + var y = x + 1 + y * 2 + } + assert_eq(fn(5), null, "arrow bare arithmetic should not implicitly return") +}) + +run("arrow explicit return still works", function() { + var fn = (x) => { return x * 2 } + assert_eq(fn(5), 10, "arrow explicit return should work") +}) + +run("function explicit return before trailing expr", function() { + var fn = function() { + return 7 + text(99) + } + assert_eq(fn(), 7, "explicit return should take precedence") +}) + // ============================================================================ // SUMMARY // ============================================================================