Skip to content

Commit

Permalink
Switch Invoke and Cond to new CAD3 dense format
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 2, 2024
1 parent 0d815a8 commit 46bf364
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
8 changes: 0 additions & 8 deletions convex-core/src/main/java/convex/core/cvm/Ops.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package convex.core.cvm;

import convex.core.cvm.ops.Cond;
import convex.core.cvm.ops.Constant;
import convex.core.cvm.ops.Invoke;
import convex.core.cvm.ops.Lambda;
import convex.core.cvm.ops.Let;
import convex.core.cvm.ops.Lookup;
Expand All @@ -21,8 +19,6 @@
*/
public class Ops {
public static final byte CONSTANT = 0;
public static final byte INVOKE = 1;
public static final byte COND = 2;
public static final byte TRY = 3;
public static final byte LET = 4;
public static final byte LOOP = 5;
Expand Down Expand Up @@ -58,10 +54,6 @@ public static <T extends ACell> AOp<T> read(byte tag, Blob b, int pos) throws Ba
switch (opCode) {
case CVMTag.OPCODE_CONSTANT:
return Constant.read(b,pos);
case Ops.INVOKE:
return Invoke.read(b,pos);
case Ops.COND:
return Cond.read(b,pos);
case Ops.TRY:
return Try.read(b,pos);
case Ops.LOOKUP:
Expand Down
27 changes: 6 additions & 21 deletions convex-core/src/main/java/convex/core/cvm/ops/Cond.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package convex.core.cvm.ops;

import convex.core.cvm.AOp;
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.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Vectors;
import convex.core.data.util.BlobBuilder;
import convex.core.exceptions.BadFormatException;
Expand All @@ -26,10 +24,10 @@
*
* @param <T> Result type of Op
*/
public class Cond<T extends ACell> extends AMultiOp<T> {
public class Cond<T extends ACell> extends AFlatMultiOp<T> {

protected Cond(AVector<AOp<ACell>> ops) {
super(ops);
super(CVMTag.OP_COND,ops);
}

/**
Expand All @@ -47,7 +45,7 @@ public static <T extends ACell> Cond<T> create(AOp<?>... ops) {
@Override
protected Cond<T> recreate(AVector<AOp<ACell>> newOps) {
if (ops==newOps) return this;
return new Cond<T>(newOps.toVector());
return new Cond<T>(newOps);
}

public static <T extends ACell> Cond<T> create(ASequence<AOp<ACell>> ops) {
Expand Down Expand Up @@ -93,12 +91,6 @@ public boolean print(BlobBuilder sb, long limit) {
return sb.check(limit);
}

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


/**
* Decodes a Cond op from a Blob encoding.
*
Expand All @@ -108,21 +100,14 @@ public byte opCode() {
* @throws BadFormatException In the event of any encoding error
*/
public static <T extends ACell> Cond<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;

AVector<AOp<ACell>> ops = Format.read(b,epos);
AVector<AOp<ACell>> ops = Vectors.read(b,epos);
epos+=Cells.getEncodingLength(ops);

Cond<T> result=create(ops);
result.attachEncoding(b.slice(pos, epos));
return result;
}

@Override
public Cond<T> updateRefs(IRefFunction func) {
AVector<AOp<ACell>> newOps= ops.updateRefs(func);
return recreate(newOps);
}


}
23 changes: 11 additions & 12 deletions convex-core/src/main/java/convex/core/cvm/ops/Invoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import convex.core.cvm.AFn;
import convex.core.cvm.AOp;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.cvm.Ops;
import convex.core.data.ACell;
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Vectors;
import convex.core.data.type.Types;
import convex.core.data.util.BlobBuilder;
Expand All @@ -24,10 +23,10 @@
*
* @param <T> Result type of Op
*/
public class Invoke<T extends ACell> extends AMultiOp<T> {
public class Invoke<T extends ACell> extends AFlatMultiOp<T> {

protected Invoke(AVector<AOp<ACell>> ops) {
super(ops);
super(CVMTag.OP_INVOKE,ops);
}

public static <T extends ACell> Invoke<T> create(ASequence<AOp<ACell>> ops) {
Expand All @@ -39,7 +38,12 @@ public static <T extends ACell> Invoke<T> create(AOp<?>... ops) {
return create(Vectors.create(ops));
}

// Build an invoke using the given values
/**
* Build an invoke using the given values. Slow, for testing purposes
* @param <T>
* @param vals
* @return
*/
public static <T extends ACell> Invoke<T> build(Object... vals) {
int n=vals.length;
AOp<?>[] ops=new AOp[n];
Expand Down Expand Up @@ -116,11 +120,6 @@ public boolean print(BlobBuilder bb, long limit) {
return bb.check(limit);
}

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

/**
* Read an Invoke Op from a Blob encoding
*
Expand All @@ -130,8 +129,8 @@ public byte opCode() {
* @throws BadFormatException In the event of any encoding error
*/
public static<T extends ACell> Invoke<T> read(Blob b, int pos) throws BadFormatException {
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data
AVector<AOp<ACell>> ops = Format.read(b,epos);
int epos=pos;
AVector<AOp<ACell>> ops = Vectors.read(b,epos);
epos+=Cells.getEncodingLength(ops);

Invoke<T> result=create(ops);
Expand Down
9 changes: 9 additions & 0 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import convex.core.cvm.PeerStatus;
import convex.core.cvm.State;
import convex.core.cvm.Syntax;
import convex.core.cvm.ops.Cond;
import convex.core.cvm.ops.Def;
import convex.core.cvm.ops.Do;
import convex.core.cvm.ops.Local;
Expand Down Expand Up @@ -723,7 +724,15 @@ private static <T extends ACell> T readDenseRecord(byte tag, Blob b, int pos) th
if (tag == CVMTag.OP_DO) {
return (T) Do.read(b,pos);
}

if (tag == CVMTag.OP_COND) {
return (T) Cond.read(b,pos);
}

if (tag == CVMTag.OP_INVOKE) {
// Note name clash with transaction type
return (T) convex.core.cvm.ops.Invoke.read(b,pos);
}

if (tag == CVMTag.PEER_STATUS) return (T) PeerStatus.read(b,pos);
if (tag == CVMTag.ACCOUNT_STATUS) return (T) AccountStatus.read(b,pos);
Expand Down

0 comments on commit 46bf364

Please sign in to comment.