Skip to content

Commit

Permalink
Simplify lookup calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 28, 2024
1 parent 7cc0dec commit e72facd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
4 changes: 1 addition & 3 deletions convex-core/src/main/java/convex/core/lang/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ 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?
Address a=context.getResult();
Address a=context.lookupDefiningAddress(address, sym);
if (a!=null) return context.withResult(Juice.COMPILE_LOOKUP,Lookup.create(Constant.of(a),sym));

// Finally revert to a lookup in the current address / environment
Expand Down
24 changes: 11 additions & 13 deletions convex-core/src/main/java/convex/core/lang/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,16 +575,19 @@ public AHashMap<ACell,ACell> lookupMeta(Symbol sym) {
* @return Metadata for given symbol (may be empty) or null if undeclared
*/
public AHashMap<ACell,ACell> lookupMeta(Address address,Symbol sym) {
AccountStatus as=(address==null)?getAccountStatus():getAccountStatus(address);
if (address==null) address=getAddress();
for (int i=0; i<Constants.LOOKUP_DEPTH; i++) {
AccountStatus as=getAccountStatus(address);
if (as==null) return null;
AHashMap<Symbol, ACell> env=as.getEnvironment();
if (env.containsKey(sym)) {
return as.getMetadata().get(sym,Maps.empty());
}

// go to parent
if (Core.CORE_ADDRESS.equals(address)) break;
address=getParentAddress(as);
if (address==null) return null;
as=getAccountStatus(address);
}
return null;
}
Expand All @@ -593,31 +596,26 @@ public AHashMap<ACell,ACell> lookupMeta(Address address,Symbol sym) {
* Looks up the address of the account that defines a given Symbol
* @param sym Symbol to look up
* @param address Address to look up in first instance (null for current address).
* @return Context with result as the address defining the given symbol (or null if undeclared)
* @return Address defining the given symbol (or null if undeclared)
*/
public Context lookupDefiningAddress(Address address,Symbol sym) {
Context ctx=this;
public Address lookupDefiningAddress(Address address,Symbol sym) {
Address addr=(address==null)?getAddress():address;

for (int i=0; i<Constants.LOOKUP_DEPTH; i++) {
if (addr==null) break;
AccountStatus as=getAccountStatus(addr);
if (as==null) return ctx.withResult(Juice.LOOKUP, null);
if (as==null) return null;

AHashMap<Symbol, ACell> env=as.getEnvironment();
MapEntry<Symbol, ACell> entry = env.getEntry(sym);
if (entry!=null) {
return ctx.withResult(Juice.LOOKUP, addr);
return addr;
}

ctx=ctx.consumeJuice(Juice.LOOKUP);
if (ctx.isExceptional()) return ctx;

// go to parent
if (addr.equals(Core.CORE_ADDRESS)) break;
addr=getParentAddress(as);
}

return ctx.withResult(Juice.LOOKUP, null);
return null;
}

private Address getParentAddress(AccountStatus as) {
Expand Down
7 changes: 3 additions & 4 deletions convex-core/src/main/java/convex/core/lang/Juice.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,16 @@ public class Juice {
/**
* Juice cost to compile a lookup which is defined in account
*/
public static final long COMPILE_LOOKUP_DEFINED = COMPILE_LOOKUP+LOOKUP;
public static final long COMPILE_LOOKUP_DEFINED = COMPILE_LOOKUP+LOOKUP; // ??

/**
* Juice cost to compile a lookup which is defined in core
*/
public static final long COMPILE_LOOKUP_CORE = COMPILE_LOOKUP+LOOKUP*2;

public static final long COMPILE_LOOKUP_CORE = COMPILE_LOOKUP; //+LOOKUP*2?
/**
* Juice cost to compile a lookup which is undefined in account and core
*/
public static final long COMPILE_LOOKUP_UNDEFINED = COMPILE_LOOKUP+LOOKUP*3;
public static final long COMPILE_LOOKUP_UNDEFINED = COMPILE_LOOKUP; //+LOOKUP*3?

/**
* Juice cost to compile a general AST node
Expand Down
12 changes: 12 additions & 0 deletions convex-core/src/test/java/convex/core/lang/JuiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.junit.jupiter.api.Test;

import convex.core.Constants;
import convex.core.data.Address;

import static convex.test.Assertions.*;
Expand Down Expand Up @@ -129,6 +130,17 @@ public void testBalanceAway() {
public void testDef() {
assertEquals(Juice.DEF + Juice.CONSTANT, juice("(def a 1)"));
}

@Test public void testLookup() {
if (Constants.OPT_STATIC) {
assertEquals(Juice.CONSTANT,juice("count"));
} else {
assertEquals(Juice.LOOKUP*3,juice("count"));
}

assertEquals(Juice.LOOKUP*3,juice("missing"));
assertEquals(Juice.LOOKUP*3,juice("if"));
}

@Test
public void testReturn() {
Expand Down

0 comments on commit e72facd

Please sign in to comment.