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) {
if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
typedBoolLiteral.setValue(boolLiteral.value());
return typedBoolLiteral;
return new TypedBoolLiteral(boolLiteral.value());
}
else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral()
.convertToCharLiteral(localVar, clas, charLiteral);
return typedCharLiteral;
return new TypedCharLiteral(localVar, clas, charLiteral);
}
else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral()
.convertToTypedIntLiteral(localVar, clas, intLiteral);
return typedIntLiteral;
return new TypedIntLiteral(localVar, clas, intLiteral);
}
else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary()
.convertToTypedBinary(localVar, clas, binary);
TypedBinary typedBinary = new TypedBinary(localVar, clas, binary);
typedBinary.typeCheck(localVar, clas);
return typedBinary;
}
else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess()
.convertToTypedFieldVarAccess(localVar, clas, fieldVarAccess);
return typedFieldVarAccess;
return new TypedFieldVarAccess(localVar, clas, fieldVarAccess);
}
else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall();

View File

@ -20,10 +20,12 @@ public class TypedAssignment implements TypedStatement {
private TypedExpression value;
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();
value = getKindOfExpression(localVar, clas, untyped.value());
return this;
}
@Override
@ -43,11 +45,11 @@ public class TypedAssignment implements TypedStatement {
@Override
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.setVarName(untyped.location().id());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value()));
return typedAssignment;
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value()));*/
return null;
}
@Override

View File

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

View File

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

View File

@ -7,19 +7,28 @@ import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
@Data
@NoArgsConstructor
public class TypedBoolLiteral implements TypedExpression {
private Boolean value;
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();
return this;
}
@Override

View File

@ -12,9 +12,9 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map;
@Data
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;
}
@Override

View File

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

View File

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

View File

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

View File

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

View File

@ -19,14 +19,17 @@ public class TypedField implements TypedNode {
private String varName;
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())){
throw new RuntimeException("Variable " + declaration.name() + " already declared");
}
varName = declaration.name();
type = declaration.type();
localVar.put(this.varName, this.type);
return this;
}
@Override

View File

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

View File

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

View File

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

View File

@ -18,9 +18,12 @@ import java.util.Map;
public class TypedIntLiteral implements TypedExpression {
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();
return this;
}
private Type type = Type.INT;

View File

@ -17,14 +17,17 @@ import java.util.Map;
public final class TypedLocalVariable implements TypedNode {
private String name;
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())){
throw new RuntimeException("Variable " + declaration.name() + " already declared");
}
this.setName(declaration.name());
this.setType(declaration.type());
localVar.put(this.name, this.type);
return this;
}
@Override

View File

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

View File

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

View File

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