Skip to content

Commit

Permalink
Environment lookup refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 27, 2024
1 parent fad8e94 commit 703927a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
6 changes: 5 additions & 1 deletion convex-core/src/main/java/convex/core/lang/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ACell, ACell> meta=context.lookupMeta(sym);
Expand All @@ -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?
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
16 changes: 2 additions & 14 deletions convex-core/src/main/java/convex/core/lang/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<Symbol,ACell> envEntry=lookupDynamicEntry(as,symbol);

// if not found, return UNDECLARED error
Expand Down
6 changes: 5 additions & 1 deletion convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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\")"));
Expand All @@ -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)"));
Expand Down Expand Up @@ -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)"));
Expand Down

0 comments on commit 703927a

Please sign in to comment.