Skip to content

Commit e72facd

Browse files
committed
Simplify lookup calculations
1 parent 7cc0dec commit e72facd

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ private static Context compileEnvSymbol(Symbol sym, Context context) {
217217
Address address=context.getAddress();
218218

219219
// Check if the symbol references an existing declaration
220-
context=context.lookupDefiningAddress(address, sym);
221-
if (context.isExceptional()) return context; // could be juice error?
222-
Address a=context.getResult();
220+
Address a=context.lookupDefiningAddress(address, sym);
223221
if (a!=null) return context.withResult(Juice.COMPILE_LOOKUP,Lookup.create(Constant.of(a),sym));
224222

225223
// Finally revert to a lookup in the current address / environment

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -575,16 +575,19 @@ public AHashMap<ACell,ACell> lookupMeta(Symbol sym) {
575575
* @return Metadata for given symbol (may be empty) or null if undeclared
576576
*/
577577
public AHashMap<ACell,ACell> lookupMeta(Address address,Symbol sym) {
578-
AccountStatus as=(address==null)?getAccountStatus():getAccountStatus(address);
578+
if (address==null) address=getAddress();
579579
for (int i=0; i<Constants.LOOKUP_DEPTH; i++) {
580+
AccountStatus as=getAccountStatus(address);
580581
if (as==null) return null;
581582
AHashMap<Symbol, ACell> env=as.getEnvironment();
582583
if (env.containsKey(sym)) {
583584
return as.getMetadata().get(sym,Maps.empty());
584585
}
586+
587+
// go to parent
588+
if (Core.CORE_ADDRESS.equals(address)) break;
585589
address=getParentAddress(as);
586590
if (address==null) return null;
587-
as=getAccountStatus(address);
588591
}
589592
return null;
590593
}
@@ -593,31 +596,26 @@ public AHashMap<ACell,ACell> lookupMeta(Address address,Symbol sym) {
593596
* Looks up the address of the account that defines a given Symbol
594597
* @param sym Symbol to look up
595598
* @param address Address to look up in first instance (null for current address).
596-
* @return Context with result as the address defining the given symbol (or null if undeclared)
599+
* @return Address defining the given symbol (or null if undeclared)
597600
*/
598-
public Context lookupDefiningAddress(Address address,Symbol sym) {
599-
Context ctx=this;
601+
public Address lookupDefiningAddress(Address address,Symbol sym) {
600602
Address addr=(address==null)?getAddress():address;
601-
602603
for (int i=0; i<Constants.LOOKUP_DEPTH; i++) {
603604
if (addr==null) break;
604605
AccountStatus as=getAccountStatus(addr);
605-
if (as==null) return ctx.withResult(Juice.LOOKUP, null);
606+
if (as==null) return null;
606607

607608
AHashMap<Symbol, ACell> env=as.getEnvironment();
608609
MapEntry<Symbol, ACell> entry = env.getEntry(sym);
609610
if (entry!=null) {
610-
return ctx.withResult(Juice.LOOKUP, addr);
611+
return addr;
611612
}
612613

613-
ctx=ctx.consumeJuice(Juice.LOOKUP);
614-
if (ctx.isExceptional()) return ctx;
615-
614+
// go to parent
616615
if (addr.equals(Core.CORE_ADDRESS)) break;
617616
addr=getParentAddress(as);
618617
}
619-
620-
return ctx.withResult(Juice.LOOKUP, null);
618+
return null;
621619
}
622620

623621
private Address getParentAddress(AccountStatus as) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,16 @@ public class Juice {
258258
/**
259259
* Juice cost to compile a lookup which is defined in account
260260
*/
261-
public static final long COMPILE_LOOKUP_DEFINED = COMPILE_LOOKUP+LOOKUP;
261+
public static final long COMPILE_LOOKUP_DEFINED = COMPILE_LOOKUP+LOOKUP; // ??
262262

263263
/**
264264
* Juice cost to compile a lookup which is defined in core
265265
*/
266-
public static final long COMPILE_LOOKUP_CORE = COMPILE_LOOKUP+LOOKUP*2;
267-
266+
public static final long COMPILE_LOOKUP_CORE = COMPILE_LOOKUP; //+LOOKUP*2?
268267
/**
269268
* Juice cost to compile a lookup which is undefined in account and core
270269
*/
271-
public static final long COMPILE_LOOKUP_UNDEFINED = COMPILE_LOOKUP+LOOKUP*3;
270+
public static final long COMPILE_LOOKUP_UNDEFINED = COMPILE_LOOKUP; //+LOOKUP*3?
272271

273272
/**
274273
* Juice cost to compile a general AST node

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.junit.jupiter.api.Test;
66

7+
import convex.core.Constants;
78
import convex.core.data.Address;
89

910
import static convex.test.Assertions.*;
@@ -129,6 +130,17 @@ public void testBalanceAway() {
129130
public void testDef() {
130131
assertEquals(Juice.DEF + Juice.CONSTANT, juice("(def a 1)"));
131132
}
133+
134+
@Test public void testLookup() {
135+
if (Constants.OPT_STATIC) {
136+
assertEquals(Juice.CONSTANT,juice("count"));
137+
} else {
138+
assertEquals(Juice.LOOKUP*3,juice("count"));
139+
}
140+
141+
assertEquals(Juice.LOOKUP*3,juice("missing"));
142+
assertEquals(Juice.LOOKUP*3,juice("if"));
143+
}
132144

133145
@Test
134146
public void testReturn() {

0 commit comments

Comments
 (0)