Skip to content

Commit 703927a

Browse files
committed
Environment lookup refactoring
1 parent fad8e94 commit 703927a

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

convex-core/src/main/java/convex/core/lang/Compiler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private static Context compileSymbol(Symbol sym, Context context) {
202202
}
203203

204204
private static Context compileEnvSymbol(Symbol sym, Context context) {
205-
Address address=context.getAddress();// Optional code for :static embedding
205+
// Optional code for :static embedding
206206
if (Constants.OPT_STATIC) {
207207
// Get metadata for symbol.
208208
AHashMap<ACell, ACell> meta=context.lookupMeta(sym);
@@ -214,6 +214,8 @@ private static Context compileEnvSymbol(Symbol sym, Context context) {
214214
}
215215
}
216216

217+
Address address=context.getAddress();
218+
217219
// Check if the symbol references an existing declaration
218220
context=context.lookupDefiningAddress(address, sym);
219221
if (context.isExceptional()) return context; // could be juice error?
@@ -795,6 +797,7 @@ public Context invoke(Context context,ACell[] args ) {
795797
if (ctx.isExceptional()) return ctx;
796798

797799
ACell newElement = ctx.getResult();
800+
// TODO: can be faster if no changes?
798801
updated = updated.conj(newElement);
799802
}
800803
return ctx.withResult(Juice.EXPAND_SEQUENCE, updated);
@@ -815,6 +818,7 @@ public Context invoke(Context context,ACell[] args ) {
815818
if (ctx.isExceptional()) return ctx;
816819
ACell newValue = ctx.getResult();
817820

821+
// TODO: can be faster if no changes?
818822
updated = updated.assoc(newKey, newValue);
819823
}
820824
return ctx.withResult(Juice.EXPAND_SEQUENCE, updated);

convex-core/src/main/java/convex/core/lang/Context.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -526,23 +526,11 @@ public boolean checkJuice(long gulp) {
526526
/**
527527
* Looks up a symbol's value in the current execution context, without any effect on the Context (no juice consumed etc.)
528528
*
529-
* @param symbol Symbol to look up. May be qualified
529+
* @param symbol Symbol to look up
530530
* @return Context with the result of the lookup (may be an undeclared exception)
531531
*/
532532
public Context lookup(Symbol symbol) {
533533
// try lookup in dynamic environment
534-
return lookupDynamic(symbol);
535-
}
536-
537-
/**
538-
* Looks up a value in the dynamic environment. Consumes no juice.
539-
*
540-
* Returns an UNDECLARED exception if the symbol cannot be resolved.
541-
*
542-
* @param symbol Symbol to look up
543-
* @return Updated Context
544-
*/
545-
public Context lookupDynamic(Symbol symbol) {
546534
Address address=getAddress();
547535
return lookupDynamic(address,symbol);
548536
}
@@ -558,7 +546,7 @@ public Context lookupDynamic(Symbol symbol) {
558546
*/
559547
public Context lookupDynamic(Address address, Symbol symbol) {
560548
AccountStatus as=getAccountStatus(address);
561-
if (as==null) return withError(ErrorCodes.NOBODY,"No account found for: "+address+"/"+symbol.toString());
549+
if (as==null) return withError(ErrorCodes.NOBODY,"No account found for: "+address);
562550
MapEntry<Symbol,ACell> envEntry=lookupDynamicEntry(as,symbol);
563551

564552
// if not found, return UNDECLARED error

convex-core/src/test/java/convex/core/lang/CoreTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2643,6 +2643,7 @@ public void testLookup() {
26432643
assertNobodyError(step("(lookup #77777777 count)"));
26442644
assertNobodyError(step("(do (def foo 1) (lookup #66666666 foo))"));
26452645

2646+
26462647
// COMPILE Errors for bad symbols
26472648
assertCompileError(step("(lookup :count)"));
26482649
assertCompileError(step("(lookup \"count\")"));
@@ -2656,6 +2657,9 @@ public void testLookup() {
26562657
// CAST Errors for bad Addresses
26572658
assertCastError(step("(lookup 8 count)"));
26582659
assertCastError(step("(lookup :foo count)"));
2660+
2661+
// CAST for lookups on a nil address?
2662+
assertCastError(step("(lookup nil count)"));
26592663

26602664
assertArityError(step("(lookup)"));
26612665
assertArityError(step("(lookup 1 2 3)"));
@@ -3979,7 +3983,7 @@ public void testBlobPred() {
39793983
@Test
39803984
public void testLongPred() {
39813985
assertTrue(evalB("(long? 1)"));
3982-
assertTrue(evalB("(long? (long *balance*))")); // TODO: is this sane?
3986+
assertTrue(evalB("(long? *balance*)"));
39833987
assertTrue(evalB("(long? (byte 1))"));
39843988

39853989
assertFalse(evalB("(long? nil)"));

0 commit comments

Comments
 (0)