From f639b548a30220497cbaeeb6452941d7887f7e38 Mon Sep 17 00:00:00 2001 From: "Todd V. Jonker" Date: Tue, 14 Apr 2026 15:41:10 -0700 Subject: [PATCH] Simplify compiled top-level definitions. --- .../java/dev/ionfusion/fusion/Namespace.java | 2 +- .../ionfusion/fusion/TopLevelNamespace.java | 56 ++++++------------- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/runtime/src/main/java/dev/ionfusion/fusion/Namespace.java b/runtime/src/main/java/dev/ionfusion/fusion/Namespace.java index 390bb732..c76e72e5 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/Namespace.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/Namespace.java @@ -540,7 +540,7 @@ SyntaxSymbol predefine(SyntaxSymbol identifier, SyntaxValue formForErrors) } /** - * Creates or updates a namespace-level binding. + * Creates or updates a namespace-level binding and infers the object's name. * Allows rebinding of existing names! * * @param value must not be null diff --git a/runtime/src/main/java/dev/ionfusion/fusion/TopLevelNamespace.java b/runtime/src/main/java/dev/ionfusion/fusion/TopLevelNamespace.java index 598e8909..8262184d 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/TopLevelNamespace.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/TopLevelNamespace.java @@ -4,7 +4,6 @@ package dev.ionfusion.fusion; import static dev.ionfusion.fusion.FusionVoid.voidValue; -import static dev.ionfusion.fusion.NamedValue.inferObjectName; import static dev.ionfusion.fusion.ResultFailure.makeResultError; import static dev.ionfusion.fusion.UnboundIdentifierException.makeUnboundError; @@ -252,6 +251,14 @@ public BaseSymbol getDefinedName(int address) throw new UnsupportedOperationException(); } + void define(SyntaxSymbol id, Object value) + throws FusionException + { + SyntaxSymbol boundId = predefine(id, id); + TopLevelDefinedBinding binding = (TopLevelDefinedBinding) boundId.getBinding(); + bind(binding, value); + } + //======================================================================== // Compiled Forms @@ -273,31 +280,20 @@ public Object doEval(Evaluator eval, Store store) throws FusionException { Object value = eval.eval(store, myValueForm); - value = processValue(eval, store, value); + eval.checkSingleResult(value, "top-level definition"); TopLevelNamespace ns = (TopLevelNamespace) store.namespace(); - SyntaxSymbol boundId = ns.predefine(myId, myId); - TopLevelDefinedBinding binding = (TopLevelDefinedBinding) boundId.getBinding(); - - ns.set(binding.myAddress, value); - - inferObjectName(value, myId.getName()); + ns.define(myId, value); return voidValue(eval); } - - Object processValue(Evaluator eval, Store store, Object value) - throws FusionException - { - eval.checkSingleResult(value, "top-level definition"); - return value; - } } /** * Interprets non-single-binding {@code define_values} at top-level. * Single-binding forms are interpreted by {@link CompiledTopDefine}. + * Zero-binding forms are handled here. */ static final class CompiledTopDefineValues implements CompiledForm @@ -307,6 +303,7 @@ static final class CompiledTopDefineValues CompiledTopDefineValues(SyntaxSymbol[] ids, CompiledForm valuesForm) { + assert ids.length != 1; myIds = ids; myValuesForm = valuesForm; } @@ -319,13 +316,8 @@ public Object doEval(Evaluator eval, Store store) TopLevelNamespace ns = (TopLevelNamespace) store.namespace(); - int expectedCount = myIds.length; - if (expectedCount == 1) - { - eval.checkSingleResult(values, "top-level definition"); - defineAndBind(ns, 0, values); - } - else if (values instanceof Object[]) + int expectedCount = myIds.length; // != 1 + if (values instanceof Object[]) { Object[] vals = (Object[]) values; int actualCount = vals.length; @@ -339,33 +331,17 @@ else if (values instanceof Object[]) for (int i = 0; i < expectedCount; i++) { - Object value = vals[i]; - defineAndBind(ns, i, value); + ns.define(myIds[i], vals[i]); } } else { - String expectation = - expectedCount + " results but received 1"; + String expectation = expectedCount + " results but received 1"; throw makeResultError(eval, "top-level definition", expectation, values); } return voidValue(eval); } - - private void defineAndBind(TopLevelNamespace ns, int i, Object value) - throws FusionException - { - SyntaxSymbol boundId = myIds[i]; - boundId = ns.predefine(boundId, boundId); - - TopLevelDefinedBinding binding = - (TopLevelDefinedBinding) boundId.getBinding(); - - ns.set(binding.myAddress, value); - - inferObjectName(value, boundId.getName()); - } }