Made the code better to read

This commit is contained in:
ahmad 2024-05-11 14:37:47 +02:00
parent 2448139ba0
commit 272c25d345
16 changed files with 58 additions and 53 deletions

View File

@ -1,4 +1,4 @@
package de.maishai.typedast.Util; package de.maishai.typedast.Help;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
@ -7,17 +7,23 @@ import de.maishai.typedast.typedclass.*;
import java.util.Map; import java.util.Map;
public class TypedExpressionUtil { public class TypedExpressionHelp {
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) {
return new TypedBoolLiteral(localVar, clas, boolLiteral); TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(localVar, clas, boolLiteral);
typedBoolLiteral.typeCheck(localVar, clas);
return typedBoolLiteral;
} }
else if (expression instanceof CharLiteral charLiteral) { else if (expression instanceof CharLiteral charLiteral) {
return new TypedCharLiteral(localVar, clas, charLiteral); TypedCharLiteral typedCharLiteral = new TypedCharLiteral(localVar, clas, charLiteral);
typedCharLiteral.typeCheck(localVar, clas);
return typedCharLiteral;
} }
else if (expression instanceof IntLiteral intLiteral) { else if (expression instanceof IntLiteral intLiteral) {
return new TypedIntLiteral(localVar, clas, intLiteral); TypedIntLiteral typedIntLiteral = new TypedIntLiteral(localVar, clas, intLiteral);
typedIntLiteral.typeCheck(localVar, clas);
return typedIntLiteral;
} }
else if (expression instanceof Binary binary) { else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary(localVar, clas, binary); TypedBinary typedBinary = new TypedBinary(localVar, clas, binary);
@ -30,24 +36,16 @@ public class TypedExpressionUtil {
return typedFieldVarAccess; return typedFieldVarAccess;
} }
else if (expression instanceof MethodCall methodCall) { else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(); TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall);
//TODO: make this beautiful to read after full implementation of TypedMethodCall typedMethodCall.typeCheck(localVar, clas);
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess();
typedFieldVarAccess.setName(methodCall.recipient().id());
typedFieldVarAccess.setField(methodCall.recipient().field());
typedMethodCall.setRecipient(typedFieldVarAccess);
for (var arg : methodCall.args()) {
typedMethodCall.getArgs().add(getKindOfExpression(localVar, clas, arg));
}
return typedMethodCall; return typedMethodCall;
} }
else if (expression instanceof New newStmt) { else if (expression instanceof New newStmt) {
return new TypedNew(localVar, clas, newStmt); return new TypedNew(localVar, clas, newStmt);
} }
else if (expression instanceof Unary unary) { else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary(); TypedUnary typedUnary = new TypedUnary(localVar, clas, unary);
typedUnary.setOp(unary.op()); typedUnary.typeCheck(localVar, clas);
typedUnary.setRight(getKindOfExpression(localVar, clas, unary.right()));
return typedUnary; return typedUnary;
} else { } else {
return null; return null;

View File

@ -3,7 +3,6 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Assignment; import de.maishai.ast.records.Assignment;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement; import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.Data; import lombok.Data;
@ -12,7 +11,7 @@ import org.objectweb.asm.Opcodes;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
public class TypedAssignment implements TypedStatement { public class TypedAssignment implements TypedStatement {

View File

@ -2,10 +2,8 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.records.Binary; import de.maishai.ast.records.Binary;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
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 lombok.NoArgsConstructor;
@ -13,7 +11,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
@NoArgsConstructor @NoArgsConstructor

View File

@ -92,8 +92,9 @@ public class TypedBlock implements TypedNode {
} }
if (stmt instanceof MethodCall methodCall) { if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(); TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall);
//TODO: implement this typedMethodCall.typeCheck(localVar, clas);
stmts.add(typedMethodCall);
} }
} }
this.typeCheck(localVar, clas); this.typeCheck(localVar, clas);

View File

@ -26,11 +26,11 @@ public class TypedBoolLiteral implements TypedExpression {
public void convertToTypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { public void convertToTypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) {
value = unTypedBoolLiteral.value(); value = unTypedBoolLiteral.value();
type = Type.BOOL;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
type = Type.BOOL;
return type; return type;
} }

View File

@ -13,17 +13,18 @@ import java.util.Map;
@Data @Data
public class TypedCharLiteral implements TypedExpression { public class TypedCharLiteral implements TypedExpression {
private char value; private char value;
private Type type = Type.CHAR; private Type type;
public TypedCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { public TypedCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
convertToCharLiteral(localVar, clas, unTypedCharLiteral); convertToCharLiteral(localVar, clas, unTypedCharLiteral);
} }
public void convertToCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { public void convertToCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
value = unTypedCharLiteral.value(); value = unTypedCharLiteral.value();
type = Type.CHAR;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
return Type.CHAR; return type;
} }

View File

@ -7,7 +7,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
public class TypedDoWhile implements TypedStatement { public class TypedDoWhile implements TypedStatement {

View File

@ -10,7 +10,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -27,7 +27,6 @@ public class TypedFieldVarAccess implements TypedExpression {
field = unTypedFieldVarAccess.field(); field = unTypedFieldVarAccess.field();
recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id(); name = unTypedFieldVarAccess.id();
} }
@Override @Override

View File

@ -9,7 +9,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
@AllArgsConstructor @AllArgsConstructor

View File

@ -10,7 +10,7 @@ import org.objectweb.asm.Opcodes;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
@NoArgsConstructor @NoArgsConstructor

View File

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

View File

@ -1,28 +1,38 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.MethodCall; import de.maishai.ast.records.MethodCall;
import de.maishai.ast.records.Node;
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 java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
@NoArgsConstructor
public class TypedMethodCall implements TypedExpression, TypedStatement { public class TypedMethodCall implements TypedExpression, TypedStatement {
private TypedFieldVarAccess recipient; private TypedFieldVarAccess recipient;
private List<TypedExpression> args; private List<TypedExpression> args;
private Type type; private Type type;
public TypedMethodCall convertToTypedMethodCall(MethodCall unTypedMethodCall) { public TypedMethodCall(Map<String, Type> localVar, TypedClass clas, MethodCall unTypedMethodCall) {
//TODO: Implement this convertToTypedMethodCall(localVar, clas, unTypedMethodCall);
return null; }
public void convertToTypedMethodCall(Map<String, Type> localVar, TypedClass clas, MethodCall unTypedMethodCall) {
recipient = new TypedFieldVarAccess(localVar, clas, unTypedMethodCall.recipient());
for (Expression arg : unTypedMethodCall.args()) {
args.add(getKindOfExpression(localVar, clas, arg));
}
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
//TODO: Implement typeCheck for MethodCall //TODO: Implement typeCheck
return null; return null;
} }

View File

@ -2,8 +2,6 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Expression; import de.maishai.ast.records.Expression;
import de.maishai.ast.records.New; import de.maishai.ast.records.New;
import de.maishai.ast.records.Node;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@ -12,7 +10,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
public class TypedNew implements TypedExpression, TypedStatement { public class TypedNew implements TypedExpression, TypedStatement {

View File

@ -10,7 +10,7 @@ import org.objectweb.asm.Opcodes;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
@NoArgsConstructor @NoArgsConstructor

View File

@ -1,11 +1,9 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.UnaryOperator; import de.maishai.ast.UnaryOperator;
import de.maishai.ast.records.Node;
import de.maishai.ast.records.Unary; import de.maishai.ast.records.Unary;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@ -13,7 +11,7 @@ import org.objectweb.asm.Opcodes;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
public class TypedUnary implements TypedExpression { public class TypedUnary implements TypedExpression {
@ -21,10 +19,12 @@ public class TypedUnary implements TypedExpression {
private TypedExpression right; private TypedExpression right;
private Type type; private Type type;
public TypedUnary convertToTypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary) { public TypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary){
convertToTypedUnary(localVar, clas, unTypedUnary);
}
public void convertToTypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary) {
op = unTypedUnary.op(); op = unTypedUnary.op();
right = getKindOfExpression(localVar, clas, unTypedUnary.right()); right = getKindOfExpression(localVar, clas, unTypedUnary.right());
return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {

View File

@ -7,7 +7,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data @Data
public class TypedWhile implements TypedStatement { public class TypedWhile implements TypedStatement {