Make the code better to read

This commit is contained in:
ahmad 2024-05-09 21:11:10 +02:00
parent cb4c637ce4
commit 08658fc8c4
19 changed files with 194 additions and 129 deletions

View File

@ -11,30 +11,21 @@ public class TypedExpressionUtil {
public static TypedExpression getKindOfExpression(Map<String, Type> localVar, TypedClass clas, Expression expression) { public static TypedExpression getKindOfExpression(Map<String, Type> localVar, TypedClass clas, Expression expression) {
if (expression instanceof BoolLiteral boolLiteral) { if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); return new TypedBoolLiteral(boolLiteral.value());
typedBoolLiteral.setValue(boolLiteral.value());
return typedBoolLiteral;
} }
else if (expression instanceof CharLiteral charLiteral) { else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral() return new TypedCharLiteral(localVar, clas, charLiteral);
.convertToCharLiteral(localVar, clas, charLiteral);
return typedCharLiteral;
} }
else if (expression instanceof IntLiteral intLiteral) { else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral() return new TypedIntLiteral(localVar, clas, intLiteral);
.convertToTypedIntLiteral(localVar, clas, intLiteral);
return typedIntLiteral;
} }
else if (expression instanceof Binary binary) { else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary() TypedBinary typedBinary = new TypedBinary(localVar, clas, binary);
.convertToTypedBinary(localVar, clas, binary);
typedBinary.typeCheck(localVar, clas); typedBinary.typeCheck(localVar, clas);
return typedBinary; return typedBinary;
} }
else if (expression instanceof FieldVarAccess fieldVarAccess) { else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess() return new TypedFieldVarAccess(localVar, clas, fieldVarAccess);
.convertToTypedFieldVarAccess(localVar, clas, fieldVarAccess);
return typedFieldVarAccess;
} }
else if (expression instanceof MethodCall methodCall) { else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(); TypedMethodCall typedMethodCall = new TypedMethodCall();

View File

@ -20,10 +20,12 @@ public class TypedAssignment implements TypedStatement {
private TypedExpression value; private TypedExpression value;
private Type type; private Type type;
public TypedAssignment convertToTypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) { public TypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
convertToTypedAssignment(localVar, clas, untyped);
}
public void convertToTypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
varName = untyped.location().id(); varName = untyped.location().id();
value = getKindOfExpression(localVar, clas, untyped.value()); value = getKindOfExpression(localVar, clas, untyped.value());
return this;
} }
@Override @Override
@ -43,11 +45,11 @@ public class TypedAssignment implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) {
Assignment untyped = (Assignment) unTypedAST; /* Assignment untyped = (Assignment) unTypedAST;
TypedAssignment typedAssignment = new TypedAssignment(); TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setVarName(untyped.location().id()); typedAssignment.setVarName(untyped.location().id());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value())); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value()));*/
return typedAssignment; return null;
} }
@Override @Override

View File

@ -8,6 +8,7 @@ import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
@ -15,17 +16,21 @@ import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
@Data @Data
@NoArgsConstructor
public class TypedBinary implements TypedExpression { public class TypedBinary implements TypedExpression {
private TypedExpression left; private TypedExpression left;
private Operator op; private Operator op;
private TypedExpression right; private TypedExpression right;
private Type type; private Type type;
public TypedBinary convertToTypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) { public TypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) {
convertToTypedBinary(localVar, clas, unTypedBinary);
}
public void convertToTypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) {
left = getKindOfExpression(localVar, clas, unTypedBinary.left()); left = getKindOfExpression(localVar, clas, unTypedBinary.left());
right = getKindOfExpression(localVar, clas, unTypedBinary.right()); right = getKindOfExpression(localVar, clas, unTypedBinary.right());
op = unTypedBinary.op(); op = unTypedBinary.op();
return this;
} }
@Override @Override

View File

@ -19,70 +19,70 @@ public class TypedBlock implements TypedNode {
private List<TypedStatement> stmts; private List<TypedStatement> stmts;
public TypedBlock blockToTypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) { public TypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
blockToTypedBlock(localVar, clas, unTypedBlock);
}
public void blockToTypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
if (unTypedBlock == null) { if (unTypedBlock == null) {
return null; vars = List.of();
stmts = List.of();
return;
} }
TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>()); stmts = new ArrayList<>();
typedBlock.setVars(new ArrayList<>()); vars = new ArrayList<>();
if (unTypedBlock.localVariables().isEmpty()) { if (unTypedBlock.localVariables().isEmpty()) {
typedBlock.setVars(List.of()); vars = List.of();
} else { } else {
for (Declaration var : unTypedBlock.localVariables()) { for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable() TypedLocalVariable typedVar = new TypedLocalVariable(localVar, clas, var);
.convertToTypedLocalVariable(localVar, clas, var); vars.add(typedVar);
typedBlock.getVars().add(typedVar);
} }
} }
if (unTypedBlock.stmts().isEmpty()) { if (unTypedBlock.stmts().isEmpty()) {
typedBlock.setStmts(List.of()); stmts = List.of();
} else { } else {
for (var stmt : unTypedBlock.stmts()) { for (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) { if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment() TypedAssignment typedAssignment = new TypedAssignment(localVar, clas, assignment);
.convertToTypedAssignment(localVar, clas, assignment);
typedAssignment.typeCheck(localVar, clas); typedAssignment.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedAssignment); stmts.add(typedAssignment);
continue; continue;
} }
if (stmt instanceof For forStmt) { if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, clas, forStmt); TypedFor typedFor = new TypedFor(localVar, clas, forStmt);
typedFor.typeCheck(localVar, clas); typedFor.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedFor); stmts.add(typedFor);
continue; continue;
} }
if (stmt instanceof IfElse ifElse) { if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse() TypedIfElse typedIfElse = new TypedIfElse(localVar, clas, ifElse);
.convertToTypedIfElse(localVar, clas, ifElse);
typedIfElse.typeCheck(localVar, clas); typedIfElse.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedIfElse); stmts.add(typedIfElse);
continue; continue;
} }
if (stmt instanceof While whileStmt) { if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile() TypedWhile typedWhile = new TypedWhile(localVar, clas, whileStmt);
.convertToTypedWhile(localVar, clas, whileStmt);
//TODO: check the type //TODO: check the type
typedBlock.getStmts().add(typedWhile); stmts.add(typedWhile);
continue; continue;
} }
if (stmt instanceof DoWhile doWhile) { if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile() TypedDoWhile typedDoWhile = new TypedDoWhile(localVar, clas, doWhile);
.convertToTypedDoWhile(localVar, clas, doWhile);
//TODO: check the type //TODO: check the type
typedBlock.getStmts().add(typedDoWhile); stmts.add(typedDoWhile);
continue; continue;
} }
if (stmt instanceof Return returnStmt) { if (stmt instanceof Return returnStmt) {
TypedReturn typedReturn = new TypedReturn() TypedReturn typedReturn = new TypedReturn(localVar, clas, returnStmt);
.convertToTypedReturn(localVar, clas, returnStmt);
//TODO: check the type //TODO: check the type
typedBlock.getStmts().add(typedReturn); stmts.add(typedReturn);
continue; continue;
} }
if (stmt instanceof New newStmt) { if (stmt instanceof New newStmt) {
@ -94,7 +94,7 @@ public class TypedBlock implements TypedNode {
if (stmt instanceof Break) { if (stmt instanceof Break) {
TypedBreak typedBreak = new TypedBreak(); TypedBreak typedBreak = new TypedBreak();
typedBreak.typeCheck(localVar, clas); typedBreak.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedBreak); stmts.add(typedBreak);
continue; continue;
} }
@ -104,8 +104,7 @@ public class TypedBlock implements TypedNode {
} }
} }
} }
System.out.println("typedBlock: " + typedBlock.toString()); System.out.println("typedBlock: " + this.toString());
return typedBlock;
} }
@Override @Override

View File

@ -7,19 +7,28 @@ import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import java.util.Map; import java.util.Map;
@Data @Data
@NoArgsConstructor
public class TypedBoolLiteral implements TypedExpression { public class TypedBoolLiteral implements TypedExpression {
private Boolean value; private Boolean value;
private Type type; private Type type;
public TypedBoolLiteral convertToTypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { public TypedBoolLiteral(Boolean value) {
this.value = value;
}
public TypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) {
convertToTypedBoolLiteral(localVar, clas, unTypedBoolLiteral);
}
public void convertToTypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) {
value = unTypedBoolLiteral.value(); value = unTypedBoolLiteral.value();
return this;
} }
@Override @Override

View File

@ -12,9 +12,9 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
@Data @Data
public class TypedBreak implements TypedStatement { public class TypedBreak implements TypedStatement {
private Type type; private Type type = Type.VOID;
public TypedBreak convertToTypedBreak(Break unTypedBreak) { public TypedBreak convertToTypedBreak(Map<String, Type> localVar, TypedClass clas, Break unTypedBreak) {
return this; return this;
} }
@Override @Override

View File

@ -15,9 +15,11 @@ public class TypedCharLiteral implements TypedExpression {
private char value; private char value;
private Type type = Type.CHAR; private Type type = Type.CHAR;
public TypedCharLiteral convertToCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { public TypedCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
convertToCharLiteral(localVar, clas, unTypedCharLiteral);
}
public void convertToCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
value = unTypedCharLiteral.value(); value = unTypedCharLiteral.value();
return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
@ -26,10 +28,11 @@ public class TypedCharLiteral implements TypedExpression {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
CharLiteral untyped = (CharLiteral) unTypedAST; /* CharLiteral untyped = (CharLiteral) unTypedAST;
TypedCharLiteral typedCharLiteral = new TypedCharLiteral(); TypedCharLiteral typedCharLiteral = new TypedCharLiteral();
typedCharLiteral.setValue(untyped.value()); typedCharLiteral.setValue(untyped.value());
return typedCharLiteral; return typedCharLiteral; */
return null;
} }
@Override @Override

View File

@ -26,6 +26,30 @@ public class TypedClass implements TypedNode {
private List<TypedMethod> typedMethods; private List<TypedMethod> typedMethods;
private List<TypedConstructor> typedConstructors; private List<TypedConstructor> typedConstructors;
public TypedClass(Map<String, Type> localVar, TypedClass clas, Node node) {
convertToTypedClass(localVar, clas, node);
}
public void convertToTypedClass(Map<String, Type> localVar, TypedClass clas, Node node) {
Class c = (Class) node;
className = c.classname();
typedFields = new ArrayList<>();
typedMethods = new ArrayList<>();
typedConstructors = new ArrayList<>();
for (Declaration field : c.fieldDeclarations()) {
typedFields.add(new TypedField(localVar, clas, field));
}
for (Method method : c.methods()) {
typedMethods.add(new TypedMethod(localVar, clas, method));
}
for (Constructor constructor : c.constructors()) {
typedConstructors.add(new TypedConstructor(localVar, clas, constructor));
}
}
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
@ -45,16 +69,16 @@ public class TypedClass implements TypedNode {
return Type.REFERENCE(className); return Type.REFERENCE(className);
} }
public TypedNode startConversion(de.maishai.ast.records.Class c) { public TypedNode startConversion(Class c) {
Map<String, Type> local = new HashMap<>(); Map<String, Type> local = new HashMap<>();
TypedClass classMap = new TypedClass(); TypedClass classMap = new TypedClass();
return convertToTypedAST(local, classMap, c); return new TypedClass(local, classMap, c);
} }
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
TypedClass typedClass = new TypedClass(); /* TypedClass typedClass = new TypedClass();
Class c = (Class) unTypedAST; Class c = (Class) unTypedAST;
typedClass.setClassName(c.classname()); typedClass.setClassName(c.classname());
typedClass.setTypedFields(new ArrayList<>()); typedClass.setTypedFields(new ArrayList<>());
@ -68,16 +92,15 @@ public class TypedClass implements TypedNode {
for (Method method : c.methods()) { for (Method method : c.methods()) {
TypedMethod typedMethod = new TypedMethod(); typedClass.getTypedMethods().add(new TypedMethod(localVar, clas, method));
typedClass.getTypedMethods().add(typedMethod.convertToTypedMethod(localVar, clas, method));
} }
for (Constructor constructor : c.constructors()) { for (Constructor constructor : c.constructors()) {
TypedConstructor typedConstructor = new TypedConstructor(); TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, clas, constructor)); typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, clas, constructor));
} }
*/
return typedClass; return null;
} }
public byte[] codeGen() { public byte[] codeGen() {

View File

@ -27,26 +27,26 @@ public class TypedConstructor implements TypedNode {
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedConstructor constructorToTypedConstructor(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) { public TypedConstructor(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) {
TypedConstructor typedConstructor = new TypedConstructor(); constructorToTypedConstructor(localVar, clas, unTypedConstructor);
typedConstructor.setName(unTypedConstructor.className()); }
typedConstructor.setTypedParameters(new ArrayList<>());
public void constructorToTypedConstructor(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) {
name = unTypedConstructor.className();
typedParameters = new ArrayList<>();
if (unTypedConstructor.params().isEmpty()) { if (unTypedConstructor.params().isEmpty()) {
typedConstructor.setTypedParameters(List.of()); typedParameters = List.of();
} else { } else {
for (Parameter param : unTypedConstructor.params()) { for (Parameter param : unTypedConstructor.params()) {
TypedParameter typedParam = new TypedParameter(); TypedParameter typedParam = new TypedParameter();
typedParam.setParaName(param.name()); typedParam.setParaName(param.name());
typedParam.setType(param.type()); typedParam.setType(param.type());
typedConstructor.getTypedParameters().add(typedParam); typedParameters.add(typedParam);
} }
} }
TypedBlock typedBlock = new TypedBlock(); this.typedBlock = new TypedBlock(localVar, clas ,unTypedConstructor.block());
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas ,unTypedConstructor.block()));
return typedConstructor;
} }
@Override @Override
@ -83,8 +83,7 @@ public class TypedConstructor implements TypedNode {
} }
} }
TypedBlock typedBlock = new TypedBlock(); typedConstructor.setTypedBlock(new TypedBlock(localVar, clas, untyped.block()));
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
return typedConstructor; return typedConstructor;
} }

View File

@ -14,12 +14,12 @@ public class TypedDoWhile implements TypedStatement {
private TypedBlock typedBlock; private TypedBlock typedBlock;
private TypedExpression cond; private TypedExpression cond;
public TypedDoWhile() { public TypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) {
convertToTypedDoWhile(localVar, clas, unTypedDoWhile);
} }
public TypedDoWhile convertToTypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) { public void convertToTypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) {
typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedDoWhile.block()); typedBlock = new TypedBlock(localVar, clas, unTypedDoWhile.block());
cond = getKindOfExpression(localVar, clas, unTypedDoWhile.cond()); cond = getKindOfExpression(localVar, clas, unTypedDoWhile.cond());
return this;
} }
@Override @Override
@ -33,14 +33,15 @@ public class TypedDoWhile implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
DoWhile untyped = (DoWhile) unTypedAST; /* DoWhile untyped = (DoWhile) unTypedAST;
TypedDoWhile typedDoWhile = new TypedDoWhile(); TypedDoWhile typedDoWhile = new TypedDoWhile();
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond())); typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond()));
return typedDoWhile; return typedDoWhile; */
return null;
} }
@Override @Override

View File

@ -19,14 +19,17 @@ public class TypedField implements TypedNode {
private String varName; private String varName;
private Type type; private Type type;
public TypedField convertToTypedField(Map<String, Type> localVar, TypedClass clas, Declaration declaration){ public TypedField(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
convertToTypedField(localVar, clas, declaration);
}
public void convertToTypedField(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
if(localVar.containsKey(declaration.name())){ if(localVar.containsKey(declaration.name())){
throw new RuntimeException("Variable " + declaration.name() + " already declared"); throw new RuntimeException("Variable " + declaration.name() + " already declared");
} }
varName = declaration.name(); varName = declaration.name();
type = declaration.type(); type = declaration.type();
localVar.put(this.varName, this.type); localVar.put(this.varName, this.type);
return this;
} }
@Override @Override

View File

@ -6,6 +6,7 @@ import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
@ -13,18 +14,21 @@ import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
@Data @Data
@NoArgsConstructor
public class TypedFieldVarAccess implements TypedExpression { public class TypedFieldVarAccess implements TypedExpression {
private Boolean field; private Boolean field;
private TypedExpression recursiveOwnerChain; private TypedExpression recursiveOwnerChain;
private String name; private String name;
private Type type; private Type type;
public TypedFieldVarAccess convertToTypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { public TypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
convertToTypedFieldVarAccess(localVar, clas, unTypedFieldVarAccess);
}
public void convertToTypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
field = unTypedFieldVarAccess.field(); field = unTypedFieldVarAccess.field();
recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id(); name = unTypedFieldVarAccess.id();
type = localVar.get(unTypedFieldVarAccess.id()); type = localVar.get(unTypedFieldVarAccess.id());
return this;
} }
@Override @Override
@ -43,13 +47,14 @@ public class TypedFieldVarAccess implements TypedExpression {
} }
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST; /* FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST;
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(); TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess();
typedFieldVarAccess.setField(untypedFieldVarAccess.field()); typedFieldVarAccess.setField(untypedFieldVarAccess.field());
typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, clas, untypedFieldVarAccess.recursiveOwnerChain())); typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, clas, untypedFieldVarAccess.recursiveOwnerChain()));
typedFieldVarAccess.setName(untypedFieldVarAccess.id()); typedFieldVarAccess.setName(untypedFieldVarAccess.id());
return typedFieldVarAccess; return typedFieldVarAccess; */
return null;
} }
@Override @Override

View File

@ -20,12 +20,14 @@ public class TypedFor implements TypedStatement {
private TypedAssignment inc; private TypedAssignment inc;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedFor convertToTypedFor(Map<String, Type> localVar,TypedClass clas, For unTypedFor) { public TypedFor(Map<String, Type> localVar, TypedClass clas, For unTypedFor) {
assign = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.assign()); convertToTypedFor(localVar, clas, unTypedFor);
}
public void convertToTypedFor(Map<String, Type> localVar,TypedClass clas, For unTypedFor) {
assign = new TypedAssignment(localVar, clas, unTypedFor.assign());
cond = getKindOfExpression(localVar, clas, unTypedFor.cond()); cond = getKindOfExpression(localVar, clas, unTypedFor.cond());
inc = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.inc()); inc = new TypedAssignment(localVar, clas, unTypedFor.inc());
typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedFor.block()); typedBlock = new TypedBlock(localVar, clas, unTypedFor.block());
return this;
} }
@Override @Override
@ -40,7 +42,7 @@ public class TypedFor implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
For untyped = (For) unTypedAST; /*For untyped = (For) unTypedAST;
TypedFor typedFor = new TypedFor(); TypedFor typedFor = new TypedFor();
TypedAssignment assign = new TypedAssignment(); TypedAssignment assign = new TypedAssignment();
typedFor.setAssign(assign.convertToTypedAssignment(localVar,clas,untyped.assign())); typedFor.setAssign(assign.convertToTypedAssignment(localVar,clas,untyped.assign()));
@ -50,8 +52,8 @@ public class TypedFor implements TypedStatement {
typedFor.setCond(null); typedFor.setCond(null);
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
*/
return typedFor; return null;
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.Label; import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -12,16 +13,19 @@ import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
@Data @Data
@NoArgsConstructor
public class TypedIfElse implements TypedStatement { public class TypedIfElse implements TypedStatement {
private TypedExpression typedCon; private TypedExpression typedCon;
private TypedBlock ifTypedBlock; private TypedBlock ifTypedBlock;
private TypedBlock elseTypedBlock; private TypedBlock elseTypedBlock;
public TypedIfElse convertToTypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) { public TypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.ifBlock()); convertToTypedIfElse(localVar, clas, unTypedIfElse);
elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.elseBlock()); }
public void convertToTypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.ifBlock());
elseTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.elseBlock());
typedCon = getKindOfExpression(localVar, clas, unTypedIfElse.cond()); typedCon = getKindOfExpression(localVar, clas, unTypedIfElse.cond());
return this;
} }
@Override @Override
@ -43,10 +47,8 @@ public class TypedIfElse implements TypedStatement {
TypedIfElse typedIfElse = new TypedIfElse(); TypedIfElse typedIfElse = new TypedIfElse();
typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, clas, ifElse.cond())); typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, clas, ifElse.cond()));
TypedBlock ifTypedBlock = new TypedBlock(); typedIfElse.setIfTypedBlock(new TypedBlock(localVar, clas, ifElse.ifBlock()));
TypedBlock elseTypedBlock = new TypedBlock(); typedIfElse.setElseTypedBlock(new TypedBlock(localVar, clas, ifElse.elseBlock()));
typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, clas, ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, clas, ifElse.elseBlock()));
return typedIfElse; return typedIfElse;
} }

View File

@ -18,9 +18,12 @@ import java.util.Map;
public class TypedIntLiteral implements TypedExpression { public class TypedIntLiteral implements TypedExpression {
private Integer value; private Integer value;
public TypedIntLiteral convertToTypedIntLiteral(Map<String, Type> localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { public TypedIntLiteral(Map<String, Type> localVar, TypedClass clas, IntLiteral unTypedIntLiteral) {
convertToTypedIntLiteral(localVar, clas, unTypedIntLiteral);
}
public void convertToTypedIntLiteral(Map<String, Type> localVar, TypedClass clas, IntLiteral unTypedIntLiteral) {
value = unTypedIntLiteral.value(); value = unTypedIntLiteral.value();
return this;
} }
private Type type = Type.INT; private Type type = Type.INT;

View File

@ -17,14 +17,17 @@ import java.util.Map;
public final class TypedLocalVariable implements TypedNode { public final class TypedLocalVariable implements TypedNode {
private String name; private String name;
private Type type; private Type type;
public TypedLocalVariable convertToTypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
public TypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
convertToTypedLocalVariable(localVar, clas, declaration);
}
public void convertToTypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
if(localVar.containsKey(declaration.name())){ if(localVar.containsKey(declaration.name())){
throw new RuntimeException("Variable " + declaration.name() + " already declared"); throw new RuntimeException("Variable " + declaration.name() + " already declared");
} }
this.setName(declaration.name()); this.setName(declaration.name());
this.setType(declaration.type()); this.setType(declaration.type());
localVar.put(this.name, this.type); localVar.put(this.name, this.type);
return this;
} }
@Override @Override

View File

@ -5,6 +5,7 @@ import de.maishai.ast.records.Node;
import de.maishai.ast.records.Parameter; import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -14,31 +15,34 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
@NoArgsConstructor
public class TypedMethod implements TypedNode { public class TypedMethod implements TypedNode {
private String name; private String name;
private Type returnType; private Type returnType;
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedMethod convertToTypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) { public TypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
TypedMethod typedMethod = new TypedMethod(); convertToTypedMethod(localVar, clas, unTypedMethod);
typedMethod.setName(unTypedMethod.methodName()); }
typedMethod.setTypedParameters(new ArrayList<>());
typedMethod.setReturnType(unTypedMethod.type()); public void convertToTypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
name = unTypedMethod.methodName();
returnType = unTypedMethod.type();
typedParameters = new ArrayList<>();
if(unTypedMethod.params().isEmpty()){ if(unTypedMethod.params().isEmpty()){
typedMethod.setTypedParameters(List.of()); typedParameters = List.of();
}else { }else {
for (Parameter parameter : unTypedMethod.params()) { for (Parameter parameter : unTypedMethod.params()) {
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name()); typedParameter.setParaName(parameter.name());
typedParameter.setType(parameter.type()); typedParameter.setType(parameter.type());
typedMethod.getTypedParameters().add(typedParameter); typedParameters.add(typedParameter);
} }
} }
TypedBlock block = new TypedBlock(); typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block());
typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, clas, unTypedMethod.block()));
return typedMethod;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
@ -54,7 +58,7 @@ public class TypedMethod implements TypedNode {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
Method untypedMethod = (Method) unTypedAST; /* Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
typedMethod.setTypedParameters(new ArrayList<>()); typedMethod.setTypedParameters(new ArrayList<>());
@ -71,7 +75,8 @@ public class TypedMethod implements TypedNode {
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas,untypedMethod.block())); typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas,untypedMethod.block()));
return typedMethod; return typedMethod; */
return null;
} }
public void codeGen(ClassWriter cw) { public void codeGen(ClassWriter cw) {

View File

@ -4,6 +4,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -12,9 +13,14 @@ import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
@Data @Data
@NoArgsConstructor
public class TypedReturn implements TypedStatement { public class TypedReturn implements TypedStatement {
private TypedExpression ret; private TypedExpression ret;
public TypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) {
convertToTypedReturn(localVar, clas, unTypedReturn);
}
public TypedReturn convertToTypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) { public TypedReturn convertToTypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) {
ret = getKindOfExpression(localVar, clas, unTypedReturn.ret()); ret = getKindOfExpression(localVar, clas, unTypedReturn.ret());
return this; return this;
@ -28,10 +34,11 @@ public class TypedReturn implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
Return untyped = (Return) unTypedAST; /* Return untyped = (Return) unTypedAST;
TypedReturn typedReturn = new TypedReturn(); TypedReturn typedReturn = new TypedReturn();
typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, clas, untyped.ret())); typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, clas, untyped.ret()));
return typedReturn; return typedReturn; */
return null;
} }
@Override @Override

View File

@ -14,10 +14,13 @@ public class TypedWhile implements TypedStatement {
private TypedExpression cond; private TypedExpression cond;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedWhile convertToTypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) { public TypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) {
convertToTypedWhile(localVar, clas, unTypedWhile);
}
public void convertToTypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) {
cond = getKindOfExpression(localVar, clas, unTypedWhile.cond()); cond = getKindOfExpression(localVar, clas, unTypedWhile.cond());
typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedWhile.block()); typedBlock = new TypedBlock(localVar, clas, unTypedWhile.block());
return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
@ -30,12 +33,12 @@ public class TypedWhile implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
While untyped = (While) unTypedAST; /* While untyped = (While) unTypedAST;
TypedWhile typedWhile = new TypedWhile(); TypedWhile typedWhile = new TypedWhile();
typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond())); typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond()));
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));*/
return typedWhile; return null;
} }