Skip to content

Commit

Permalink
Edit for coded Set Op
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 3, 2024
1 parent f12c32d commit 65ed554
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 78 deletions.
11 changes: 4 additions & 7 deletions convex-core/src/main/java/convex/core/cvm/AOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ public ACell toCanonical() {
return this;
}

/**
* Returns the opcode for this op
*
* @return Opcode as a byte
*/
public abstract byte opCode();

@Override
public final int encode(byte[] bs, int pos) {
bs[pos++]=getTag();
Expand All @@ -70,6 +63,10 @@ public int encodeRaw(byte[] bs, int pos) {
return encodeAfterOpcode(bs,pos);
}

protected byte opCode() {
return 0;
}

/**
* Writes the raw data for this Op to the specified bytebuffer. Assumes Op tag
* and opcode already written.
Expand Down
10 changes: 4 additions & 6 deletions convex-core/src/main/java/convex/core/cvm/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ 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 DEF = 6;
public static final byte LOOKUP = 7;
public static final byte LAMBDA = 8;
public static final byte QUERY = 9;
public static final byte LOCAL=10;
public static final byte SET = 11;

public static final byte SPECIAL = 15;
// public static final byte CALL = 9;
// public static final byte RETURN = 10;

Expand Down Expand Up @@ -59,14 +55,16 @@ public static <T extends ACell> AOp<T> read(byte tag, Blob b, int pos) throws Ba
case Ops.LOOKUP:
return Lookup.read(b,pos);
case CVMTag.OPCODE_LAMBDA:
return (AOp<T>) Lambda.read(b,pos);
return (AOp<T>) Lambda.read(b,pos);
case Ops.LET:
return Let.read(b,pos,false);
case Ops.QUERY:
return Query.read(b,pos);
case Ops.LOOP:
return Let.read(b,pos,true);
case Ops.SET:

// These tags mean we must have a Long integer, which resolves to a Set operation
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: case 0x18:
return Set.read(b,pos);

default:
Expand Down
6 changes: 0 additions & 6 deletions convex-core/src/main/java/convex/core/cvm/ops/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.cvm.Juice;
import convex.core.cvm.Ops;
import convex.core.cvm.Syntax;
import convex.core.data.ACell;
import convex.core.data.Blob;
Expand Down Expand Up @@ -100,11 +99,6 @@ public boolean print(BlobBuilder sb, long limit) {
return sb.check(limit);
}

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

@Override
public int estimatedEncodingSize() {
return 100;
Expand Down
6 changes: 0 additions & 6 deletions convex-core/src/main/java/convex/core/cvm/ops/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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.Format;
Expand Down Expand Up @@ -55,11 +54,6 @@ public Context execute(Context ctx) {
T result = (T)env.get(position);
return ctx.withResult(Juice.LOOKUP,result);
}

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

@Override
public byte getTag() {
Expand Down
64 changes: 17 additions & 47 deletions convex-core/src/main/java/convex/core/cvm/ops/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import convex.core.ErrorCodes;
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;
Expand All @@ -10,33 +11,31 @@
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.prim.CVMLong;
import convex.core.data.util.BlobBuilder;
import convex.core.exceptions.BadFormatException;
import convex.core.exceptions.InvalidDataException;
import convex.core.util.ErrorMessages;

/**
* Op to set a lexical value in the local execution context. i.e. `set!`
*
* @param <T> Result type of Op
*/
public class Set<T extends ACell> extends AOp<T> {
public class Set<T extends ACell> extends ACodedOp<T,CVMLong,AOp<T>> {

/**
* Stack position in lexical stack
*/
private final long position;

/**
* Op to compute new value
*/
private final Ref<AOp<T>> op;

public Set(Ref<CVMLong> code, Ref<AOp<T>> value) {
super(CVMTag.OP_CODED,code,value);
this.position = code.getValue().longValue(); // safe because always embedded
}
private Set(long position, Ref<AOp<T>> op) {
this.position = position;
this.op = op;
this(CVMLong.create(position).getRef(),op);
}

/**
Expand All @@ -59,7 +58,7 @@ public Context execute(Context ctx) {
return ctx.withError(ErrorCodes.BOUNDS, "Bad position for set!: " + position);
}

ctx = ctx.execute(op.getValue());
ctx = ctx.execute(value.getValue());
if (ctx.isExceptional()) return ctx;
ACell value = ctx.getResult();

Expand All @@ -68,18 +67,6 @@ public Context execute(Context ctx) {
return ctx.consumeJuice(Juice.SET_BANG);
}

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

@Override
public int encodeAfterOpcode(byte[] bs, int pos) {
pos = Format.writeVLQLong(bs, pos, position);
pos = op.encode(bs,pos);
return pos;
}

/**
* Reads a Set Op from a Blob encoding
*
Expand All @@ -103,44 +90,27 @@ public static <R extends ACell> Set<R> read(Blob b, int pos) throws BadFormatExc
return result;
}

@Override
public Set<T> updateRefs(IRefFunction func) {
@SuppressWarnings("unchecked")
Ref<AOp<T>> newOp = (Ref<AOp<T>>) func.apply(op);
if (op == newOp) return this;
return new Set<T>(position, newOp);
}

@Override
public void validateCell() throws InvalidDataException {
if (op == null) {
throw new InvalidDataException("Null Set op ", this);
}
if (position < 0) {
throw new InvalidDataException("Invalid Local position " + position, this);
}
}

@Override
public int getRefCount() {
return 1;
}

@SuppressWarnings("unchecked")
@Override
public Ref<AOp<T>> getRef(int i) {
if (i != 0) throw new IndexOutOfBoundsException(ErrorMessages.badIndex(i));
return op;
}

@Override
public boolean print(BlobBuilder sb, long limit) {
sb.append("(set! %");
sb.append(Long.toString(position));
sb.append(' ');
if (!op.getValue().print(sb, limit)) return false;
if (!value.getValue().print(sb, limit)) return false;
sb.append(')');
return sb.check(limit);
}

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

}
5 changes: 0 additions & 5 deletions convex-core/src/main/java/convex/core/cvm/ops/Special.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ public Context execute(Context ctx) {
}
return ctx.consumeJuice(Juice.SPECIAL);
}

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

@Override
public byte getTag() {
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/test/java/convex/core/lang/OpsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void testSpecial() {
@Test
public void testSet() throws BadFormatException {
AOp<Address> op = Set.create(45, Constant.nil());
Blob expectedEncoding=Blob.fromHex("c00b2dc0b000");
Blob expectedEncoding=Blob.fromHex("c0112dc0b000");
assertEquals(expectedEncoding,op.getEncoding());
assertEquals(op,Format.read(expectedEncoding));
doOpTest(op);
Expand Down

0 comments on commit 65ed554

Please sign in to comment.