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.typedast.Type;
@ -7,17 +7,23 @@ import de.maishai.typedast.typedclass.*;
import java.util.Map;
public class TypedExpressionUtil {
public class TypedExpressionHelp {
public static TypedExpression getKindOfExpression(Map<String, Type> localVar, TypedClass clas, Expression expression) {
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) {
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) {
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) {
TypedBinary typedBinary = new TypedBinary(localVar, clas, binary);
@ -30,24 +36,16 @@ public class TypedExpressionUtil {
return typedFieldVarAccess;
}
else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall();
//TODO: make this beautiful to read after full implementation of TypedMethodCall
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));
}
TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall);
typedMethodCall.typeCheck(localVar, clas);
return typedMethodCall;
}
else if (expression instanceof New newStmt) {
return new TypedNew(localVar, clas, newStmt);
}
else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary();
typedUnary.setOp(unary.op());
typedUnary.setRight(getKindOfExpression(localVar, clas, unary.right()));
TypedUnary typedUnary = new TypedUnary(localVar, clas, unary);
typedUnary.typeCheck(localVar, clas);
return typedUnary;
} else {
return null;

View File

@ -3,7 +3,6 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Assignment;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import lombok.Data;
@ -12,7 +11,7 @@ import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data
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.records.Binary;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -13,7 +11,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map;
import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data
@NoArgsConstructor

View File

@ -92,8 +92,9 @@ public class TypedBlock implements TypedNode {
}
if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall();
//TODO: implement this
TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall);
typedMethodCall.typeCheck(localVar, clas);
stmts.add(typedMethodCall);
}
}
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) {
value = unTypedBoolLiteral.value();
type = Type.BOOL;
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
type = Type.BOOL;
return type;
}

View File

@ -13,17 +13,18 @@ import java.util.Map;
@Data
public class TypedCharLiteral implements TypedExpression {
private char value;
private Type type = Type.CHAR;
private Type type;
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();
type = Type.CHAR;
}
@Override
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 static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data
public class TypedDoWhile implements TypedStatement {

View File

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

View File

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

View File

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

View File

@ -17,22 +17,23 @@ import java.util.Map;
@RequiredArgsConstructor
public class TypedIntLiteral implements TypedExpression {
private Integer value;
private Type type;
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();
type = Type.INT;
}
private Type type = Type.INT;
public TypedIntLiteral(Integer value) {
this.value = value;
}
@Override
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;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.MethodCall;
import de.maishai.ast.records.Node;
import de.maishai.typedast.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import java.util.List;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression;
@Data
@NoArgsConstructor
public class TypedMethodCall implements TypedExpression, TypedStatement {
private TypedFieldVarAccess recipient;
private List<TypedExpression> args;
private Type type;
public TypedMethodCall convertToTypedMethodCall(MethodCall unTypedMethodCall) {
//TODO: Implement this
return null;
public TypedMethodCall(Map<String, Type> localVar, TypedClass clas, MethodCall unTypedMethodCall) {
convertToTypedMethodCall(localVar, clas, unTypedMethodCall);
}
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
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
//TODO: Implement typeCheck for MethodCall
//TODO: Implement typeCheck
return null;
}

View File

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

View File

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

View File

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