Skip to content

Commit

Permalink
Lookup and Set op refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 3, 2024
1 parent 65ed554 commit e85a834
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 80 deletions.
7 changes: 5 additions & 2 deletions convex-core/src/main/java/convex/core/cvm/CVMTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ public class CVMTag {
// ==========================================
// CVM Ops

// General ops with byte flag
// General ops with a single byte flag
public static final byte OP_CODED = (byte) 0xC0;
public static final byte OPCODE_CONSTANT = (byte) 0xB0;
public static final byte OPCODE_LOOKUP = (byte) 0xB1;
public static final byte OPCODE_LAMBDA = (byte) 0xBF;


public static final byte OP_LOOKUP = (byte) 0xC1;

public static final byte OP_DEF = (byte) 0xCD;

public static final byte OP_DO = (byte)0xDA; // (do ...)
Expand Down
4 changes: 0 additions & 4 deletions convex-core/src/main/java/convex/core/cvm/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import convex.core.cvm.ops.Constant;
import convex.core.cvm.ops.Lambda;
import convex.core.cvm.ops.Let;
import convex.core.cvm.ops.Lookup;
import convex.core.cvm.ops.Query;
import convex.core.cvm.ops.Set;
import convex.core.cvm.ops.Try;
Expand All @@ -22,7 +21,6 @@ public class Ops {
public static final byte TRY = 3;
public static final byte LET = 4;
public static final byte LOOP = 5;
public static final byte LOOKUP = 7;
public static final byte LAMBDA = 8;
public static final byte QUERY = 9;

Expand Down Expand Up @@ -52,8 +50,6 @@ public static <T extends ACell> AOp<T> read(byte tag, Blob b, int pos) throws Ba
return Constant.read(b,pos);
case Ops.TRY:
return Try.read(b,pos);
case Ops.LOOKUP:
return Lookup.read(b,pos);
case CVMTag.OPCODE_LAMBDA:
return (AOp<T>) Lambda.read(b,pos);
case Ops.LET:
Expand Down
74 changes: 23 additions & 51 deletions convex-core/src/main/java/convex/core/cvm/ops/Lookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import convex.core.ErrorCodes;
import convex.core.cvm.AOp;
import convex.core.cvm.Address;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.cvm.Juice;
import convex.core.cvm.Ops;
import convex.core.data.ACell;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Ref;
import convex.core.data.Symbol;
import convex.core.data.util.BlobBuilder;
Expand All @@ -28,17 +26,13 @@
*
* @param <T> Result type of Op
*/
public class Lookup<T extends ACell> extends AOp<T> {
private final AOp<Address> address;
private final Symbol symbol;

private Lookup(AOp<Address> address,Symbol symbol) {
this.address=address;
this.symbol = symbol;
public class Lookup<T extends ACell> extends ACodedOp<T,AOp<Address>,Symbol> {
private Lookup(Ref<AOp<Address>> address,Ref<Symbol> symbol) {
super (CVMTag.OP_LOOKUP,address,symbol);
}

public static <T extends ACell> Lookup<T> create(AOp<Address> address, Symbol form) {
return new Lookup<T>(address,form);
return new Lookup<T>(Ref.get(address),form.getRef());
}

public static <T extends ACell> Lookup<T> create(AOp<Address> address, String name) {
Expand All @@ -61,6 +55,7 @@ public static <T extends ACell> Lookup<T> create(String name) {
public Context execute(Context context) {
Context rctx=context;
Address namespaceAddress=null;
AOp<Address> address=code.getValue();
if (address!=null) {
rctx=rctx.execute(address);
if (rctx.isExceptional()) return rctx;
Expand All @@ -71,30 +66,21 @@ public Context execute(Context context) {

// Do a dynamic lookup, with address if specified or address from current context otherwise
namespaceAddress=(address==null)?context.getAddress():namespaceAddress;
Symbol symbol=value.getValue();
return rctx.lookupDynamic(namespaceAddress,symbol).consumeJuice(Juice.LOOKUP_DYNAMIC);
}

@Override
public boolean print(BlobBuilder bb, long limit) {
AOp<Address> address=code.getValue();
if (address!=null) {
if (!address.print(bb,limit)) return false;
bb.append('/');
}
Symbol symbol=value.getValue();
return symbol.print(bb,limit);
}

@Override
public byte opCode() {
return Ops.LOOKUP;
}

@Override
public int encodeAfterOpcode(byte[] bs, int pos) {
pos= symbol.encode(bs, pos);
pos= Format.write(bs,pos, address); // might be null
return pos;
}

/**
* Reads a Lookup op from a Blob encoding
* @param <T> Type of Lookup value
Expand All @@ -104,48 +90,34 @@ public int encodeAfterOpcode(byte[] bs, int pos) {
* @throws BadFormatException In the event of any encoding error
*/
public static <T extends ACell> Lookup<T> read(Blob b,int pos) throws BadFormatException {
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data
int epos=pos+1; // skip tag to get to data

Symbol sym = Format.read(b,epos);
if (sym==null) throw new BadFormatException("Lookup symbol cannot be null");
epos+=Cells.getEncodingLength(sym);
Ref<AOp<Address>> addr=Format.readRef(b, epos);
epos+=addr.getEncodingLength();

AOp<Address> address = Format.read(b,epos);
epos+=Cells.getEncodingLength(address);
Ref<Symbol> sym=Format.readRef(b, epos);
epos+=sym.getEncodingLength();

Lookup<T> result= create(address,sym);
Lookup<T> result= new Lookup<T>(addr,sym);
result.attachEncoding(b.slice(pos, epos));
return result;
}

@Override
public int getRefCount() {
if (address==null) return 0;
return address.getRefCount();
}

@Override
public <R extends ACell> Ref<R> getRef(int i) {
if (address==null) throw new IndexOutOfBoundsException();
return address.getRef(i);
}

@Override
public Lookup<T> updateRefs(IRefFunction func) {
if (address==null) return this;
AOp<Address> newAddress=address.updateRefs(func);
if (address==newAddress) return this;
return create(newAddress,symbol);
}

@Override
public void validateCell() throws InvalidDataException {
if (address!=null) address.validateCell();
symbol.validateCell();
// TODO: any checks?
}

public AOp<Address> getAddress() {
return address;
return code.getValue();
}

@Override
protected AOp<T> rebuild(Ref<AOp<Address>> newCode, Ref<Symbol> newValue) {
if ((code==newCode)&&(value==newValue)) return this;
return new Lookup<T>(newCode,newValue);
}


Expand Down
14 changes: 6 additions & 8 deletions convex-core/src/main/java/convex/core/cvm/ops/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.cvm.Juice;
import convex.core.cvm.Ops;
import convex.core.data.ACell;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Ref;
import convex.core.data.prim.CVMLong;
Expand Down Expand Up @@ -77,15 +75,15 @@ public Context execute(Context ctx) {
* @throws BadFormatException In the event of any encoding error
*/
public static <R extends ACell> Set<R> read(Blob b, int pos) throws BadFormatException{
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data
int epos=pos+1; // skip tag to get to data

long position = Format.readVLQLong(b,epos);
epos+=Format.getVLQLongLength(position);
Ref<CVMLong> index=Format.readRef(b, epos);
epos+=index.getEncodingLength();

AOp<R> op = Format.read(b,epos);
epos+=Cells.getEncodingLength(op);
Ref<AOp<R>> op=Format.readRef(b, epos);
epos+=op.getEncodingLength();

Set<R> result= create(position, op);
Set<R> result= new Set<R>(index,op);
result.attachEncoding(b.slice(pos, epos));
return result;
}
Expand Down
36 changes: 21 additions & 15 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import convex.core.cvm.ops.Def;
import convex.core.cvm.ops.Do;
import convex.core.cvm.ops.Local;
import convex.core.cvm.ops.Lookup;
import convex.core.cvm.ops.Special;
import convex.core.cvm.transactions.Call;
import convex.core.cvm.transactions.Invoke;
Expand Down Expand Up @@ -499,21 +500,26 @@ private static <T extends ACell> T readDataStructure(byte tag, Blob b, int pos)
}

private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatException {

if (tag == CVMTag.OP_DEF) {
return Def.read(b, pos);
}

if (tag == CVMTag.OP_CODED) return Ops.read(tag,b, pos);

if (tag == CVMTag.FN_MULTI) {
AFn<?> fn = MultiFn.read(b,pos);
return fn;
}

if (tag == CVMTag.FN) {
AFn<?> fn = Fn.read(b,pos);
return fn;
try {
if (tag == CVMTag.OP_CODED) return Ops.read(tag,b, pos);

if (tag == CVMTag.OP_LOOKUP) return Lookup.read(b,pos);

if (tag == CVMTag.OP_DEF) {
return Def.read(b, pos);
}

if (tag == CVMTag.FN_MULTI) {
AFn<?> fn = MultiFn.read(b,pos);
return fn;
}

if (tag == CVMTag.FN) {
AFn<?> fn = Fn.read(b,pos);
return fn;
}
} catch (Exception e) {
// something went wrong, fall through to reading a generic coded value
}

return CodedValue.read(tag,b,pos);
Expand Down

0 comments on commit e85a834

Please sign in to comment.