From 703927afa17e32de90f5d6dbbdf492b1a13acf6b Mon Sep 17 00:00:00 2001 From: mikera Date: Sat, 28 Sep 2024 00:19:39 +0100 Subject: [PATCH] Environment lookup refactoring --- .../src/main/java/convex/core/lang/Compiler.java | 6 +++++- .../src/main/java/convex/core/lang/Context.java | 16 ++-------------- .../src/test/java/convex/core/lang/CoreTest.java | 6 +++++- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/convex-core/src/main/java/convex/core/lang/Compiler.java b/convex-core/src/main/java/convex/core/lang/Compiler.java index 677d3ad21..7c62687d7 100644 --- a/convex-core/src/main/java/convex/core/lang/Compiler.java +++ b/convex-core/src/main/java/convex/core/lang/Compiler.java @@ -202,7 +202,7 @@ private static Context compileSymbol(Symbol sym, Context context) { } private static Context compileEnvSymbol(Symbol sym, Context context) { - Address address=context.getAddress();// Optional code for :static embedding + // Optional code for :static embedding if (Constants.OPT_STATIC) { // Get metadata for symbol. AHashMap meta=context.lookupMeta(sym); @@ -214,6 +214,8 @@ private static Context compileEnvSymbol(Symbol sym, Context context) { } } + Address address=context.getAddress(); + // Check if the symbol references an existing declaration context=context.lookupDefiningAddress(address, sym); if (context.isExceptional()) return context; // could be juice error? @@ -795,6 +797,7 @@ public Context invoke(Context context,ACell[] args ) { if (ctx.isExceptional()) return ctx; ACell newElement = ctx.getResult(); + // TODO: can be faster if no changes? updated = updated.conj(newElement); } return ctx.withResult(Juice.EXPAND_SEQUENCE, updated); @@ -815,6 +818,7 @@ public Context invoke(Context context,ACell[] args ) { if (ctx.isExceptional()) return ctx; ACell newValue = ctx.getResult(); + // TODO: can be faster if no changes? updated = updated.assoc(newKey, newValue); } return ctx.withResult(Juice.EXPAND_SEQUENCE, updated); diff --git a/convex-core/src/main/java/convex/core/lang/Context.java b/convex-core/src/main/java/convex/core/lang/Context.java index 67f72aba6..dc65b5a6a 100644 --- a/convex-core/src/main/java/convex/core/lang/Context.java +++ b/convex-core/src/main/java/convex/core/lang/Context.java @@ -526,23 +526,11 @@ public boolean checkJuice(long gulp) { /** * Looks up a symbol's value in the current execution context, without any effect on the Context (no juice consumed etc.) * - * @param symbol Symbol to look up. May be qualified + * @param symbol Symbol to look up * @return Context with the result of the lookup (may be an undeclared exception) */ public Context lookup(Symbol symbol) { // try lookup in dynamic environment - return lookupDynamic(symbol); - } - - /** - * Looks up a value in the dynamic environment. Consumes no juice. - * - * Returns an UNDECLARED exception if the symbol cannot be resolved. - * - * @param symbol Symbol to look up - * @return Updated Context - */ - public Context lookupDynamic(Symbol symbol) { Address address=getAddress(); return lookupDynamic(address,symbol); } @@ -558,7 +546,7 @@ public Context lookupDynamic(Symbol symbol) { */ public Context lookupDynamic(Address address, Symbol symbol) { AccountStatus as=getAccountStatus(address); - if (as==null) return withError(ErrorCodes.NOBODY,"No account found for: "+address+"/"+symbol.toString()); + if (as==null) return withError(ErrorCodes.NOBODY,"No account found for: "+address); MapEntry envEntry=lookupDynamicEntry(as,symbol); // if not found, return UNDECLARED error diff --git a/convex-core/src/test/java/convex/core/lang/CoreTest.java b/convex-core/src/test/java/convex/core/lang/CoreTest.java index 360f57153..23e50b17c 100644 --- a/convex-core/src/test/java/convex/core/lang/CoreTest.java +++ b/convex-core/src/test/java/convex/core/lang/CoreTest.java @@ -2643,6 +2643,7 @@ public void testLookup() { assertNobodyError(step("(lookup #77777777 count)")); assertNobodyError(step("(do (def foo 1) (lookup #66666666 foo))")); + // COMPILE Errors for bad symbols assertCompileError(step("(lookup :count)")); assertCompileError(step("(lookup \"count\")")); @@ -2656,6 +2657,9 @@ public void testLookup() { // CAST Errors for bad Addresses assertCastError(step("(lookup 8 count)")); assertCastError(step("(lookup :foo count)")); + + // CAST for lookups on a nil address? + assertCastError(step("(lookup nil count)")); assertArityError(step("(lookup)")); assertArityError(step("(lookup 1 2 3)")); @@ -3979,7 +3983,7 @@ public void testBlobPred() { @Test public void testLongPred() { assertTrue(evalB("(long? 1)")); - assertTrue(evalB("(long? (long *balance*))")); // TODO: is this sane? + assertTrue(evalB("(long? *balance*)")); assertTrue(evalB("(long? (byte 1))")); assertFalse(evalB("(long? nil)"));