1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-20 02:32:36 +00:00

Now serializing all symbol types

This commit is contained in:
Jesper Gravgaard 2017-07-18 23:13:24 +02:00
parent 819c904419
commit 6e087e9a1c
5 changed files with 73 additions and 9 deletions

View File

@ -1,5 +1,6 @@
package dk.camelot64.kickc.icl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
@ -18,7 +19,8 @@ public class Procedure extends Scope {
this.returnType = returnType;
}
public Procedure(
@JsonCreator
private Procedure(
@JsonProperty("name") String name,
@JsonProperty("returntype") SymbolType returnType,
@JsonProperty("parameterNames") List<String> parameterNames,

View File

@ -15,7 +15,7 @@ public class ProgramScope extends Scope {
}
@JsonCreator
public ProgramScope(
private ProgramScope(
@JsonProperty("name") String name,
@JsonProperty("symbols") HashMap<String, Symbol> symbols,
@JsonProperty("intermediateVarCount") int intermediateVarCount,

View File

@ -1,11 +1,17 @@
package dk.camelot64.kickc.icl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/** A fixed size array of another type */
public class SymbolTypeArray extends SymbolTypePointer {
private int size;
public SymbolTypeArray(SymbolType elementType, int size) {
@JsonCreator
public SymbolTypeArray(
@JsonProperty("elementType") SymbolType elementType,
@JsonProperty("size") int size) {
super(elementType);
this.size = size;
}
@ -14,8 +20,36 @@ public class SymbolTypeArray extends SymbolTypePointer {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String getTypeName() {
return getElementType().getTypeName()+"["+size+"]";
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
SymbolTypeArray that = (SymbolTypeArray) o;
return size == that.size;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + size;
return result;
}
}

View File

@ -1,11 +1,17 @@
package dk.camelot64.kickc.icl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/** A pointer */
public class SymbolTypePointer implements SymbolType {
private SymbolType elementType;
public SymbolTypePointer(SymbolType elementType) {
@JsonCreator
public SymbolTypePointer(
@JsonProperty("elementType") SymbolType elementType) {
this.elementType = elementType;
}
@ -13,10 +19,32 @@ public class SymbolTypePointer implements SymbolType {
return elementType;
}
public void setElementType(SymbolType elementType) {
this.elementType = elementType;
}
@Override
@JsonIgnore
public String getTypeName() {
return elementType.getTypeName()+"*";
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SymbolTypePointer that = (SymbolTypePointer) o;
return elementType != null ? elementType.equals(that.elementType) : that.elementType == null;
}
@Override
public int hashCode() {
return elementType != null ? elementType.hashCode() : 0;
}
}

View File

@ -24,26 +24,26 @@ public class TestIclJson extends TestCase {
VariableUnversioned v1 = scope.addVariable("v1", SymbolTypeBasic.BYTE);
v1.createVersion();
scope.addVariableIntermediate();
scope.addVariable("v2", new SymbolTypePointer(SymbolTypeBasic.BYTE));
scope.addVariable("v3", new SymbolTypeArray(SymbolTypeBasic.WORD, 4));
scope.addLabel("main");
scope.addLabelIntermediate();
String json = "{\"@type\":\"program\",\"name\":\"\",\"symbols\":{\"v1\":{\"@type\":\"variable_unversioned\",\"name\":\"v1\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":1,\"inferredType\":false},\"v1#0\":{\"@type\":\"variable_versioned\",\"name\":\"v1#0\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"versionOfName\":\"v1\",\"inferredType\":false},\"$0\":{\"@type\":\"variable_intermediate\",\"name\":\"$0\",\"type\":{\"@type\":\"basic\",\"typeName\":\"var\"},\"inferredType\":false},\"main\":{\"@type\":\"label\",\"name\":\"main\",\"intermediate\":false},\"@1\":{\"@type\":\"label\",\"name\":\"@1\",\"intermediate\":true}},\"intermediateVarCount\":1,\"intermediateLabelCount\":2,\"allocation\":null}";
String json = "{\"@type\":\"program\",\"name\":\"\",\"symbols\":{\"v1\":{\"@type\":\"variable_unversioned\",\"name\":\"v1\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":1,\"inferredType\":false},\"v1#0\":{\"@type\":\"variable_versioned\",\"name\":\"v1#0\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"versionOfName\":\"v1\",\"inferredType\":false},\"$0\":{\"@type\":\"variable_intermediate\",\"name\":\"$0\",\"type\":{\"@type\":\"basic\",\"typeName\":\"var\"},\"inferredType\":false},\"v2\":{\"@type\":\"variable_unversioned\",\"name\":\"v2\",\"type\":{\"@type\":\"pointer\",\"elementType\":{\"@type\":\"basic\",\"typeName\":\"byte\"}},\"nextVersionNumber\":0,\"inferredType\":false},\"v3\":{\"@type\":\"variable_unversioned\",\"name\":\"v3\",\"type\":{\"@type\":\"array\",\"elementType\":{\"@type\":\"basic\",\"typeName\":\"word\"},\"size\":4},\"nextVersionNumber\":0,\"inferredType\":false},\"main\":{\"@type\":\"label\",\"name\":\"main\",\"intermediate\":false},\"@1\":{\"@type\":\"label\",\"name\":\"@1\",\"intermediate\":true}},\"intermediateVarCount\":1,\"intermediateLabelCount\":2,\"allocation\":null}";
assertJsonSerialization(scope, json, Scope.class);
}
public void testJsonScopeProcedure() throws IOException {
Scope scope = new ProgramScope();
scope.addVariable("v1", SymbolTypeBasic.BYTE);
Procedure procedure = scope.addProcedure("main", SymbolTypeBasic.VOID);
procedure.addVariable("v2", SymbolTypeBasic.BYTE);
ArrayList<Variable> parameters = new ArrayList<>();
parameters.add(new VariableUnversioned("p1", procedure, SymbolTypeBasic.BYTE));
parameters.add(new VariableUnversioned("p2", procedure, SymbolTypeBasic.BYTE));
parameters.add(new VariableUnversioned("p2", procedure, SymbolTypeBasic.BOOLEAN));
procedure.setParameters(parameters);
String json = "{\"@type\":\"program\",\"name\":\"\",\"symbols\":{\"v1\":{\"@type\":\"variable_unversioned\",\"name\":\"v1\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":0,\"inferredType\":false},\"main\":{\"@type\":\"procedure\",\"name\":\"main\",\"parameterNames\":[\"p1\",\"p2\"],\"symbols\":{\"v2\":{\"@type\":\"variable_unversioned\",\"name\":\"v2\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":0,\"inferredType\":false},\"p1\":{\"@type\":\"variable_unversioned\",\"name\":\"p1\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":0,\"inferredType\":false},\"p2\":{\"@type\":\"variable_unversioned\",\"name\":\"p2\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":0,\"inferredType\":false}},\"intermediateVarCount\":0,\"intermediateLabelCount\":1,\"returnType\":{\"@type\":\"basic\",\"typeName\":\"void\"}}},\"intermediateVarCount\":0,\"intermediateLabelCount\":1,\"allocation\":null}";
String json = "{\"@type\":\"program\",\"name\":\"\",\"symbols\":{\"main\":{\"@type\":\"procedure\",\"name\":\"main\",\"parameterNames\":[\"p1\",\"p2\"],\"symbols\":{\"v2\":{\"@type\":\"variable_unversioned\",\"name\":\"v2\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":0,\"inferredType\":false},\"p1\":{\"@type\":\"variable_unversioned\",\"name\":\"p1\",\"type\":{\"@type\":\"basic\",\"typeName\":\"byte\"},\"nextVersionNumber\":0,\"inferredType\":false},\"p2\":{\"@type\":\"variable_unversioned\",\"name\":\"p2\",\"type\":{\"@type\":\"basic\",\"typeName\":\"boolean\"},\"nextVersionNumber\":0,\"inferredType\":false}},\"intermediateVarCount\":0,\"intermediateLabelCount\":1,\"returnType\":{\"@type\":\"basic\",\"typeName\":\"void\"}}},\"intermediateVarCount\":0,\"intermediateLabelCount\":1,\"allocation\":null}";
assertJsonSerialization(scope, json, Scope.class);
}
public static void assertJsonSerialization(
Object object,
String json,