mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 09:28:03 +00:00
Replaced the TypedClass to TypedProgram in typeCheck and others convert methods
This commit is contained in:
parent
f5cc94316e
commit
98a46b7679
@ -98,6 +98,8 @@ public class Compiler {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ComplexClass.java"), List.of("ComplexClass"));
|
||||
generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java",
|
||||
"src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java"),
|
||||
List.of("ClassWithConstructor","ClassWithConstructorAndMethodCall"));
|
||||
}
|
||||
}
|
||||
|
@ -1,53 +1,50 @@
|
||||
package de.maishai.typedast.Help;
|
||||
|
||||
import de.maishai.ast.records.*;
|
||||
import de.maishai.typedast.Type;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.typedclass.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TypedExpressionHelp {
|
||||
|
||||
public static TypedExpression convertExpression( TypedClass clas, Expression expression) {
|
||||
public static TypedExpression convertExpression( TypedProgram typedProgram, Expression expression) {
|
||||
if (expression instanceof BoolLiteral boolLiteral) {
|
||||
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( clas, boolLiteral);
|
||||
typedBoolLiteral.typeCheck( clas);
|
||||
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( typedProgram, boolLiteral);
|
||||
typedBoolLiteral.typeCheck( typedProgram);
|
||||
return typedBoolLiteral;
|
||||
}
|
||||
else if (expression instanceof CharLiteral charLiteral) {
|
||||
TypedCharLiteral typedCharLiteral = new TypedCharLiteral( clas, charLiteral);
|
||||
typedCharLiteral.typeCheck( clas);
|
||||
TypedCharLiteral typedCharLiteral = new TypedCharLiteral( typedProgram, charLiteral);
|
||||
typedCharLiteral.typeCheck( typedProgram);
|
||||
return typedCharLiteral;
|
||||
}
|
||||
else if (expression instanceof IntLiteral intLiteral) {
|
||||
TypedIntLiteral typedIntLiteral = new TypedIntLiteral( clas, intLiteral);
|
||||
typedIntLiteral.typeCheck( clas);
|
||||
TypedIntLiteral typedIntLiteral = new TypedIntLiteral( typedProgram, intLiteral);
|
||||
typedIntLiteral.typeCheck( typedProgram);
|
||||
return typedIntLiteral;
|
||||
}
|
||||
else if (expression instanceof Binary binary) {
|
||||
TypedBinary typedBinary = new TypedBinary( clas, binary);
|
||||
typedBinary.typeCheck( clas);
|
||||
TypedBinary typedBinary = new TypedBinary( typedProgram, binary);
|
||||
typedBinary.typeCheck( typedProgram);
|
||||
return typedBinary;
|
||||
}
|
||||
else if (expression instanceof FieldVarAccess fieldVarAccess) {
|
||||
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( clas, fieldVarAccess);
|
||||
typedFieldVarAccess.typeCheck( clas);
|
||||
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( typedProgram, fieldVarAccess);
|
||||
typedFieldVarAccess.typeCheck( typedProgram);
|
||||
return typedFieldVarAccess;
|
||||
}
|
||||
else if (expression instanceof MethodCall methodCall) {
|
||||
TypedMethodCall typedMethodCall = new TypedMethodCall( clas, methodCall);
|
||||
typedMethodCall.typeCheck( clas);
|
||||
TypedMethodCall typedMethodCall = new TypedMethodCall( typedProgram, methodCall);
|
||||
typedMethodCall.typeCheck( typedProgram);
|
||||
return typedMethodCall;
|
||||
}
|
||||
else if (expression instanceof New newStmt) {
|
||||
TypedNew typedNew = new TypedNew( clas, newStmt);
|
||||
typedNew.typeCheck( clas);
|
||||
TypedNew typedNew = new TypedNew( typedProgram, newStmt);
|
||||
typedNew.typeCheck( typedProgram);
|
||||
return typedNew;
|
||||
}
|
||||
else if (expression instanceof Unary unary) {
|
||||
TypedUnary typedUnary = new TypedUnary( clas, unary);
|
||||
typedUnary.typeCheck( clas);
|
||||
TypedUnary typedUnary = new TypedUnary( typedProgram, unary);
|
||||
typedUnary.typeCheck( typedProgram);
|
||||
return typedUnary;
|
||||
} else {
|
||||
return null;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import de.maishai.typedast.typedclass.TypedClass;
|
||||
import de.maishai.typedast.typedclass.TypedProgram;
|
||||
|
||||
public interface TypedNode {
|
||||
Type typeCheck(TypedClass clas);
|
||||
Type typeCheck(TypedProgram typedProgram);
|
||||
}
|
||||
|
@ -1,18 +1,14 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Assignment;
|
||||
import de.maishai.ast.records.Expression;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.TypedStatement;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@Data
|
||||
@ -22,39 +18,39 @@ public class TypedAssignment implements TypedStatement {
|
||||
private TypedFieldVarAccess location;
|
||||
private Type type;
|
||||
|
||||
public TypedAssignment(TypedClass clas, Assignment untyped) {
|
||||
convertToTypedAssignment(clas, untyped);
|
||||
public TypedAssignment(TypedProgram typedProgram, Assignment untyped) {
|
||||
convertToTypedAssignment(typedProgram, untyped);
|
||||
}
|
||||
|
||||
public void convertToTypedAssignment(TypedClass clas, Assignment untyped) {
|
||||
value = convertExpression(clas, untyped.value());
|
||||
location = new TypedFieldVarAccess(clas, untyped.location());
|
||||
public void convertToTypedAssignment(TypedProgram typedProgram, Assignment untyped) {
|
||||
value = convertExpression(typedProgram, untyped.value());
|
||||
location = new TypedFieldVarAccess(typedProgram, untyped.location());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
Type typeLeft = null;
|
||||
|
||||
if (clas.isThereField(location.getName())) {
|
||||
typeLeft = clas.getFieldType(location.getName());
|
||||
if (typedProgram.getCurrentClass().isThereField(location.getName())) {
|
||||
typeLeft = typedProgram.getCurrentClass().getFieldType(location.getName());
|
||||
} else {
|
||||
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
|
||||
if (clas.getCurrentMethod().isLocalVariableInMethod(location.getName())) {
|
||||
typeLeft = clas.getCurrentMethod().getLocalVariableType(location.getName());
|
||||
if (typedProgram.getCurrentClass().isCurrentMethodPresent() && !typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(location.getName())) {
|
||||
typeLeft = typedProgram.getCurrentClass().getCurrentMethod().getLocalVariableType(location.getName());
|
||||
} else {
|
||||
throw new RuntimeException("Variable " + location.getName() + " not declared in method");
|
||||
}
|
||||
}
|
||||
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
|
||||
if (clas.getCurrentConstructor().isLocalVariableInConstructor(location.getName())) {
|
||||
typeLeft = clas.getCurrentConstructor().getLocalVariableType(location.getName());
|
||||
if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(location.getName())) {
|
||||
typeLeft = typedProgram.getCurrentClass().getCurrentConstructor().getLocalVariableType(location.getName());
|
||||
} else {
|
||||
throw new RuntimeException("Variable " + location.getName() + " not declared in constructor");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Type typeRight = value.typeCheck(clas);
|
||||
Type typeRight = value.typeCheck(typedProgram);
|
||||
|
||||
if (typeLeft.equals(typeRight)) {
|
||||
type = typeLeft;
|
||||
|
@ -8,9 +8,6 @@ import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@ -23,20 +20,20 @@ public class TypedBinary implements TypedExpression {
|
||||
private TypedExpression right;
|
||||
private Type type;
|
||||
|
||||
public TypedBinary(TypedClass clas, Binary unTypedBinary) {
|
||||
convertToTypedBinary(clas, unTypedBinary);
|
||||
public TypedBinary(TypedProgram typedProgram, Binary unTypedBinary) {
|
||||
convertToTypedBinary(typedProgram, unTypedBinary);
|
||||
}
|
||||
|
||||
public void convertToTypedBinary(TypedClass clas, Binary unTypedBinary) {
|
||||
left = convertExpression(clas, unTypedBinary.left());
|
||||
right = convertExpression(clas, unTypedBinary.right());
|
||||
public void convertToTypedBinary(TypedProgram typedProgram, Binary unTypedBinary) {
|
||||
left = convertExpression(typedProgram, unTypedBinary.left());
|
||||
right = convertExpression(typedProgram, unTypedBinary.right());
|
||||
op = unTypedBinary.op();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
Type leftType = left.typeCheck(clas);
|
||||
Type rightType = right.typeCheck(clas);
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
Type leftType = left.typeCheck(typedProgram);
|
||||
Type rightType = right.typeCheck(typedProgram);
|
||||
|
||||
if (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
|
||||
if (leftType == Type.INT && rightType == Type.INT) {
|
||||
|
@ -8,11 +8,9 @@ import de.maishai.typedast.TypedStatement;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@ -23,8 +21,8 @@ public class TypedBlock implements TypedNode {
|
||||
private Type type;
|
||||
|
||||
|
||||
public TypedBlock(TypedClass clas, Block unTypedBlock) {
|
||||
convertToTypedBlock(clas, unTypedBlock);
|
||||
public TypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
|
||||
convertToTypedBlock(typedProgram, unTypedBlock);
|
||||
}
|
||||
|
||||
public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) {
|
||||
@ -32,56 +30,56 @@ public class TypedBlock implements TypedNode {
|
||||
this.stmts = stmts;
|
||||
}
|
||||
|
||||
public void convertToTypedBlock(TypedClass clas, Block unTypedBlock) {
|
||||
public void convertToTypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
|
||||
|
||||
if (unTypedBlock == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Declaration var : unTypedBlock.localVariables()) {
|
||||
vars.add(new TypedLocalVariable(clas, var));
|
||||
vars.add(new TypedLocalVariable(typedProgram, var));
|
||||
}
|
||||
|
||||
for (var stmt : unTypedBlock.stmts()) {
|
||||
if (stmt instanceof Assignment assignment) {
|
||||
TypedAssignment typedAssignment = new TypedAssignment(clas, assignment);
|
||||
typedAssignment.typeCheck(clas);
|
||||
TypedAssignment typedAssignment = new TypedAssignment(typedProgram, assignment);
|
||||
typedAssignment.typeCheck(typedProgram);
|
||||
stmts.add(typedAssignment);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof For forStmt) {
|
||||
TypedFor typedFor = new TypedFor(clas, forStmt);
|
||||
typedFor.typeCheck(clas);
|
||||
TypedFor typedFor = new TypedFor(typedProgram, forStmt);
|
||||
typedFor.typeCheck(typedProgram);
|
||||
stmts.add(typedFor);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof IfElse ifElse) {
|
||||
TypedIfElse typedIfElse = new TypedIfElse(clas, ifElse);
|
||||
typedIfElse.typeCheck(clas);
|
||||
TypedIfElse typedIfElse = new TypedIfElse(typedProgram, ifElse);
|
||||
typedIfElse.typeCheck(typedProgram);
|
||||
stmts.add(typedIfElse);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof While whileStmt) {
|
||||
TypedWhile typedWhile = new TypedWhile(clas, whileStmt);
|
||||
typedWhile.typeCheck(clas);
|
||||
TypedWhile typedWhile = new TypedWhile(typedProgram, whileStmt);
|
||||
typedWhile.typeCheck(typedProgram);
|
||||
stmts.add(typedWhile);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof DoWhile doWhile) {
|
||||
TypedDoWhile typedDoWhile = new TypedDoWhile(clas, doWhile);
|
||||
typedDoWhile.typeCheck(clas);
|
||||
TypedDoWhile typedDoWhile = new TypedDoWhile(typedProgram, doWhile);
|
||||
typedDoWhile.typeCheck(typedProgram);
|
||||
stmts.add(typedDoWhile);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof Return returnStmt) {
|
||||
TypedReturn typedReturn = new TypedReturn(clas, returnStmt);
|
||||
typedReturn.typeCheck(clas);
|
||||
TypedReturn typedReturn = new TypedReturn(typedProgram, returnStmt);
|
||||
typedReturn.typeCheck(typedProgram);
|
||||
stmts.add(typedReturn);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof New newStmt) {
|
||||
TypedNew typedNew = new TypedNew(clas, newStmt);
|
||||
typedNew.typeCheck(clas);
|
||||
TypedNew typedNew = new TypedNew(typedProgram, newStmt);
|
||||
typedNew.typeCheck(typedProgram);
|
||||
stmts.add(typedNew);
|
||||
continue;
|
||||
}
|
||||
@ -92,19 +90,19 @@ public class TypedBlock implements TypedNode {
|
||||
}
|
||||
|
||||
if (stmt instanceof MethodCall methodCall) {
|
||||
TypedMethodCall typedMethodCall = new TypedMethodCall(clas, methodCall);
|
||||
typedMethodCall.typeCheck(clas);
|
||||
TypedMethodCall typedMethodCall = new TypedMethodCall(typedProgram, methodCall);
|
||||
typedMethodCall.typeCheck(typedProgram);
|
||||
stmts.add(typedMethodCall);
|
||||
}
|
||||
}
|
||||
this.typeCheck(clas);
|
||||
this.typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
Type chekType = null;
|
||||
for (TypedStatement stmt : stmts) {
|
||||
stmt.typeCheck(clas);
|
||||
stmt.typeCheck(typedProgram);
|
||||
if(stmt instanceof TypedReturn returnStmt) {
|
||||
chekType = returnStmt.getType();
|
||||
}
|
||||
|
@ -1,18 +1,13 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.BoolLiteral;
|
||||
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;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class TypedBoolLiteral implements TypedExpression {
|
||||
@ -21,17 +16,17 @@ public class TypedBoolLiteral implements TypedExpression {
|
||||
private Type type;
|
||||
|
||||
|
||||
public TypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) {
|
||||
convertToTypedBoolLiteral(clas, unTypedBoolLiteral);
|
||||
public TypedBoolLiteral(TypedProgram typedProgram, BoolLiteral unTypedBoolLiteral) {
|
||||
convertToTypedBoolLiteral(typedProgram, unTypedBoolLiteral);
|
||||
}
|
||||
|
||||
public void convertToTypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) {
|
||||
public void convertToTypedBoolLiteral(TypedProgram typedProgram, BoolLiteral unTypedBoolLiteral) {
|
||||
value = unTypedBoolLiteral.value();
|
||||
type = Type.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
@ -1,15 +1,10 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Break;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.TypedStatement;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class TypedBreak implements TypedStatement {
|
||||
@ -20,7 +15,7 @@ public class TypedBreak implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
@ -1,32 +1,27 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.CharLiteral;
|
||||
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 org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class TypedCharLiteral implements TypedExpression {
|
||||
private char value;
|
||||
private Type type;
|
||||
|
||||
public TypedCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) {
|
||||
convertToCharLiteral(clas, unTypedCharLiteral);
|
||||
public TypedCharLiteral(TypedProgram typedProgram, CharLiteral unTypedCharLiteral) {
|
||||
convertToCharLiteral(typedProgram, unTypedCharLiteral);
|
||||
}
|
||||
|
||||
public void convertToCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) {
|
||||
public void convertToCharLiteral(TypedProgram typedProgram, CharLiteral unTypedCharLiteral) {
|
||||
value = unTypedCharLiteral.value();
|
||||
type = Type.CHAR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
@ -14,9 +14,7 @@ import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@ -30,45 +28,12 @@ public class TypedClass implements TypedNode {
|
||||
private TypedConstructor currentConstructor;
|
||||
private Type type;
|
||||
|
||||
public TypedClass(Class c) {
|
||||
convertToTypedClass(c);
|
||||
public TypedClass(TypedProgram typedProgram, Class c) {
|
||||
className = c.classname();
|
||||
type = Type.REFERENCE(className);
|
||||
convertMethodsAndConstructorsAndFields(typedProgram, c);
|
||||
}
|
||||
|
||||
public void enterCurrentMethod(TypedMethod method) {
|
||||
currentMethod = method;
|
||||
}
|
||||
|
||||
public void exitCurrentMethod() {
|
||||
currentMethod = null;
|
||||
}
|
||||
|
||||
public void enterCurrentConstructor(TypedConstructor constructor) {
|
||||
currentConstructor = constructor;
|
||||
}
|
||||
|
||||
public void exitCurrentConstructor() {
|
||||
currentConstructor = null;
|
||||
}
|
||||
|
||||
public boolean isCurrentMethodPresent() {
|
||||
return currentMethod != null;
|
||||
}
|
||||
|
||||
public boolean isCurrentConstructorPresent() {
|
||||
return currentConstructor != null;
|
||||
}
|
||||
|
||||
public boolean isParameterNameInCurrentMethod(String parameterName) {
|
||||
if (currentMethod == null) {
|
||||
return false;
|
||||
}
|
||||
for (TypedParameter p : currentMethod.getTypedParameters()) {
|
||||
if (p.getParaName().equals(parameterName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isParameterNameInCurrentConstructor(String parameterName) {
|
||||
if (currentConstructor == null) {
|
||||
@ -82,28 +47,27 @@ public class TypedClass implements TypedNode {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void convertToTypedClass(Class c) {
|
||||
className = c.classname();
|
||||
type = Type.REFERENCE(className);
|
||||
|
||||
public void convertMethodsAndConstructorsAndFields(TypedProgram typedProgram, Class c){
|
||||
// Am Anfang werden die Attribute, die Konstruktoren und Methoden in die jeweilige Liste eingefügt.
|
||||
// damit die Methoden verwendet werden können, bevor deren Blöcke ausgeführt werden
|
||||
for (Declaration declaration : c.fieldDeclarations()) {
|
||||
typedDeclarations.add(new TypedDeclaration(this, declaration));
|
||||
typedDeclarations.add(new TypedDeclaration(typedProgram, declaration));
|
||||
}
|
||||
for (Constructor constructor : c.constructors()) {
|
||||
typedConstructors.add(new TypedConstructor(this, constructor));
|
||||
typedConstructors.add(new TypedConstructor(typedProgram, constructor));
|
||||
}
|
||||
|
||||
for (Method method : c.methods()) {
|
||||
typedMethods.add(new TypedMethod(this, method));
|
||||
typedMethods.add(new TypedMethod(typedProgram, method));
|
||||
}
|
||||
}
|
||||
|
||||
public void covertBlocksOfConstructorsAndMethods(TypedProgram typedProgram, Class c){
|
||||
// Hier werden die Blöcke der Konstruktoren ausgeführt
|
||||
int i = 0;
|
||||
for (Constructor constructor : c.constructors()) {
|
||||
enterCurrentConstructor(typedConstructors.get(i));
|
||||
typedConstructors.get(i).convertToBlock(this, constructor);
|
||||
typedConstructors.get(i).convertToBlock(typedProgram, constructor);
|
||||
exitCurrentConstructor();
|
||||
i++;
|
||||
}
|
||||
@ -111,24 +75,17 @@ public class TypedClass implements TypedNode {
|
||||
int j = 0;
|
||||
for (Method method : c.methods()) {
|
||||
enterCurrentMethod(typedMethods.get(j));
|
||||
typedMethods.get(j).convertToTypedBlock(this, method);
|
||||
typedMethods.get(j).convertToTypedBlock(typedProgram, method);
|
||||
exitCurrentMethod();
|
||||
j++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
return type;
|
||||
}
|
||||
|
||||
public TypedNode startConversion(Program p) {
|
||||
Map<String, Type> local = new HashMap<>();
|
||||
Class c = p.classes().get(0);
|
||||
return new TypedClass(c);
|
||||
}
|
||||
|
||||
public boolean isParameterWitNameInMethod(String parameterName) {
|
||||
for (TypedMethod m : typedMethods) {
|
||||
for (TypedParameter p : m.getTypedParameters()) {
|
||||
@ -189,13 +146,28 @@ public class TypedClass implements TypedNode {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Type getMethodType(String methodName) {
|
||||
for (TypedMethod m : typedMethods) {
|
||||
if (m.getName().equals(methodName)) {
|
||||
return m.getReturnType();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
public void enterCurrentMethod(TypedMethod method) {
|
||||
currentMethod = method;
|
||||
}
|
||||
|
||||
public void exitCurrentMethod() {
|
||||
currentMethod = null;
|
||||
}
|
||||
|
||||
public void enterCurrentConstructor(TypedConstructor constructor) {
|
||||
currentConstructor = constructor;
|
||||
}
|
||||
|
||||
public void exitCurrentConstructor() {
|
||||
currentConstructor = null;
|
||||
}
|
||||
|
||||
public boolean isCurrentMethodPresent() {
|
||||
return currentMethod != null;
|
||||
}
|
||||
|
||||
public boolean isCurrentConstructorPresent() {
|
||||
return currentConstructor != null;
|
||||
}
|
||||
|
||||
|
||||
|
@ -29,8 +29,8 @@ public class TypedConstructor implements TypedNode {
|
||||
this.typedBlock = typedBlock;
|
||||
}
|
||||
|
||||
public TypedConstructor(TypedClass clas, Constructor unTypedConstructor) {
|
||||
convertToTypedConstructor(clas, unTypedConstructor);
|
||||
public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) {
|
||||
convertToTypedConstructor(typedProgram, unTypedConstructor);
|
||||
}
|
||||
|
||||
public boolean isLocalVariablePresent(String localVarName) {
|
||||
@ -49,23 +49,23 @@ public class TypedConstructor implements TypedNode {
|
||||
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
|
||||
}
|
||||
|
||||
public void convertToTypedConstructor(TypedClass clas, Constructor unTypedConstructor) {
|
||||
public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) {
|
||||
name = unTypedConstructor.className();
|
||||
|
||||
for (Parameter param : unTypedConstructor.params()) {
|
||||
typedParameters.add(new TypedParameter(clas, param));
|
||||
typedParameters.add(new TypedParameter(typedProgram, param));
|
||||
}
|
||||
type = Type.VOID;
|
||||
}
|
||||
|
||||
public void convertToBlock(TypedClass clas, Constructor unTypedConstructor) {
|
||||
this.typedBlock = new TypedBlock(clas, unTypedConstructor.block());
|
||||
typeCheck(clas);
|
||||
public void convertToBlock(TypedProgram typedProgram, Constructor unTypedConstructor) {
|
||||
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
|
||||
typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
type = typedBlock.typeCheck(clas);
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
type = typedBlock.typeCheck(typedProgram);
|
||||
if (type != Type.VOID) {
|
||||
throw new RuntimeException("Constructor must not habe a return statement");
|
||||
}
|
||||
|
@ -1,18 +1,14 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Declaration;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Type.Kind.REFERENCE;
|
||||
|
||||
@Data
|
||||
@ -22,24 +18,24 @@ public final class TypedDeclaration implements TypedNode {
|
||||
private String name;
|
||||
private Type type;
|
||||
|
||||
public TypedDeclaration(TypedClass clas, Declaration declaration) {
|
||||
convertToTypedDeclaration(clas, declaration);
|
||||
public TypedDeclaration(TypedProgram typedProgram, Declaration declaration) {
|
||||
convertToTypedDeclaration(typedProgram, declaration);
|
||||
}
|
||||
|
||||
public void convertToTypedDeclaration(TypedClass clas, Declaration declaration) {
|
||||
public void convertToTypedDeclaration(TypedProgram typedProgram, Declaration declaration) {
|
||||
name = declaration.name();
|
||||
type = declaration.type();
|
||||
typeCheck(clas);
|
||||
typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
if (clas.isThereField(name)) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (typedProgram.getCurrentClass().isThereField(name)) {
|
||||
throw new RuntimeException("Field " + name + " already declared");
|
||||
}
|
||||
|
||||
if (type.getKind() == REFERENCE) {
|
||||
if (!type.getReference().equals(clas.getClassName())) {
|
||||
if (!type.getReference().equals(typedProgram.getCurrentClass().getClassName())) {
|
||||
throw new RuntimeException("Field " + name + " has wrong type");
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,6 @@ package de.maishai.typedast.typedclass;
|
||||
import de.maishai.ast.records.*;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@ -15,21 +12,21 @@ public class TypedDoWhile implements TypedStatement {
|
||||
private TypedExpression cond;
|
||||
private Type type;
|
||||
|
||||
public TypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) {
|
||||
convertToTypedDoWhile(clas, unTypedDoWhile);
|
||||
public TypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) {
|
||||
convertToTypedDoWhile(typedProgram, unTypedDoWhile);
|
||||
}
|
||||
|
||||
public void convertToTypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) {
|
||||
typedBlock = new TypedBlock(clas, unTypedDoWhile.block());
|
||||
cond = convertExpression(clas, unTypedDoWhile.cond());
|
||||
public void convertToTypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) {
|
||||
typedBlock = new TypedBlock(typedProgram, unTypedDoWhile.block());
|
||||
cond = convertExpression(typedProgram, unTypedDoWhile.cond());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
if (cond.typeCheck(clas) != Type.BOOL) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (cond.typeCheck(typedProgram) != Type.BOOL) {
|
||||
throw new RuntimeException("Condition must be boolean");
|
||||
}
|
||||
typedBlock.typeCheck(clas);
|
||||
typedBlock.typeCheck(typedProgram);
|
||||
this.type = Type.VOID;
|
||||
return Type.VOID;
|
||||
}
|
||||
|
@ -7,9 +7,6 @@ import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@ -22,50 +19,50 @@ public class TypedFieldVarAccess implements TypedExpression {
|
||||
private String name;
|
||||
private Type type;
|
||||
|
||||
public TypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
|
||||
convertToTypedFieldVarAccess(clas, unTypedFieldVarAccess);
|
||||
public TypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) {
|
||||
convertToTypedFieldVarAccess(typedProgram, unTypedFieldVarAccess);
|
||||
}
|
||||
|
||||
public void convertToTypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
|
||||
public void convertToTypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) {
|
||||
field = unTypedFieldVarAccess.field();
|
||||
recursiveOwnerChain = convertExpression(clas, unTypedFieldVarAccess.recursiveOwnerChain());
|
||||
recursiveOwnerChain = convertExpression(typedProgram, unTypedFieldVarAccess.recursiveOwnerChain());
|
||||
name = unTypedFieldVarAccess.id();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (field) {
|
||||
if (clas.isThereField(name)) {
|
||||
type = clas.getFieldType(name);
|
||||
return clas.getFieldType(name);
|
||||
if (typedProgram.getCurrentClass().isThereField(name)) {
|
||||
type = typedProgram.getCurrentClass().getFieldType(name);
|
||||
return typedProgram.getCurrentClass().getFieldType(name);
|
||||
}else{
|
||||
throw new RuntimeException("Field " + name + " not declared ");
|
||||
}
|
||||
} else {
|
||||
if (clas.isCurrentConstructorPresent()) {
|
||||
if (clas.isParameterNameInCurrentConstructor(name)) {
|
||||
type = clas.getParameterTypeInCurrentConstructor(name);
|
||||
if (typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
if (typedProgram.getCurrentClass().isParameterNameInCurrentConstructor(name)) {
|
||||
type = typedProgram.getCurrentClass().getParameterTypeInCurrentConstructor(name);
|
||||
return type;
|
||||
} else if (clas.getCurrentConstructor().isLocalVariablePresent(name)) {
|
||||
type = clas.getCurrentConstructor().getLocalVariableType(name);
|
||||
} else if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariablePresent(name)) {
|
||||
type = typedProgram.getCurrentClass().getCurrentConstructor().getLocalVariableType(name);
|
||||
return type;
|
||||
} else if(clas.isThereField(name)){
|
||||
type = clas.getFieldType(name);
|
||||
} else if(typedProgram.getCurrentClass().isThereField(name)){
|
||||
type = typedProgram.getCurrentClass().getFieldType(name);
|
||||
return type;
|
||||
}
|
||||
else {
|
||||
throw new RuntimeException("Variable " + name + " not declared in constructor");
|
||||
}
|
||||
|
||||
} else if (clas.isCurrentMethodPresent()) {
|
||||
if (clas.isParameterWitNameInMethod(name)) {
|
||||
type = clas.getParameterTypeInCurrentMethod(name);
|
||||
} else if (typedProgram.getCurrentClass().isCurrentMethodPresent()) {
|
||||
if (typedProgram.getCurrentClass().isParameterWitNameInMethod(name)) {
|
||||
type = typedProgram.getCurrentClass().getParameterTypeInCurrentMethod(name);
|
||||
return type;
|
||||
} else if (clas.getCurrentMethod().isLocalVariablePresent(name)) {
|
||||
type = clas.getCurrentMethod().getLocalVariableType(name);
|
||||
} else if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariablePresent(name)) {
|
||||
type = typedProgram.getCurrentClass().getCurrentMethod().getLocalVariableType(name);
|
||||
return type;
|
||||
} else if(clas.isThereField(name)){
|
||||
type = clas.getFieldType(name);
|
||||
} else if(typedProgram.getCurrentClass().isThereField(name)){
|
||||
type = typedProgram.getCurrentClass().getFieldType(name);
|
||||
return type;
|
||||
}
|
||||
else {
|
||||
|
@ -5,9 +5,6 @@ import de.maishai.typedast.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@ -21,25 +18,25 @@ public class TypedFor implements TypedStatement {
|
||||
private TypedBlock typedBlock;
|
||||
private Type type;
|
||||
|
||||
public TypedFor(TypedClass clas, For unTypedFor) {
|
||||
convertToTypedFor(clas, unTypedFor);
|
||||
public TypedFor(TypedProgram typedProgram, For unTypedFor) {
|
||||
convertToTypedFor(typedProgram, unTypedFor);
|
||||
}
|
||||
|
||||
public void convertToTypedFor(TypedClass clas, For unTypedFor) {
|
||||
assign = new TypedAssignment(clas, unTypedFor.assign());
|
||||
cond = convertExpression(clas, unTypedFor.cond());
|
||||
inc = new TypedAssignment(clas, unTypedFor.inc());
|
||||
typedBlock = new TypedBlock(clas, unTypedFor.block());
|
||||
public void convertToTypedFor(TypedProgram typedProgram, For unTypedFor) {
|
||||
assign = new TypedAssignment(typedProgram, unTypedFor.assign());
|
||||
cond = convertExpression(typedProgram, unTypedFor.cond());
|
||||
inc = new TypedAssignment(typedProgram, unTypedFor.inc());
|
||||
typedBlock = new TypedBlock(typedProgram, unTypedFor.block());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
assign.typeCheck(clas);
|
||||
if (!cond.typeCheck(clas).equals(Type.BOOL)) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
assign.typeCheck(typedProgram);
|
||||
if (!cond.typeCheck(typedProgram).equals(Type.BOOL)) {
|
||||
throw new RuntimeException("Condition must be a boolean");
|
||||
}
|
||||
inc.typeCheck(clas);
|
||||
type = typedBlock.typeCheck(clas);
|
||||
inc.typeCheck(typedProgram);
|
||||
type = typedBlock.typeCheck(typedProgram);
|
||||
return type;
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,8 @@ 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@Data
|
||||
@ -20,35 +17,27 @@ public class TypedIfElse implements TypedStatement {
|
||||
private TypedBlock elseTypedBlock;
|
||||
private Type type;
|
||||
|
||||
public TypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
|
||||
convertToTypedIfElse(clas, unTypedIfElse);
|
||||
public TypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) {
|
||||
convertToTypedIfElse(typedProgram, unTypedIfElse);
|
||||
}
|
||||
|
||||
public void convertToTypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
|
||||
ifTypedBlock = new TypedBlock(clas, unTypedIfElse.ifBlock());
|
||||
elseTypedBlock = new TypedBlock(clas, unTypedIfElse.elseBlock());
|
||||
typedCon = convertExpression(clas, unTypedIfElse.cond());
|
||||
public void convertToTypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) {
|
||||
ifTypedBlock = new TypedBlock(typedProgram, unTypedIfElse.ifBlock());
|
||||
elseTypedBlock = new TypedBlock(typedProgram, unTypedIfElse.elseBlock());
|
||||
typedCon = convertExpression(typedProgram, unTypedIfElse.cond());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
/*
|
||||
if (typedCon.typeCheck(clas) != Type.BOOL) {
|
||||
throw new RuntimeException("If condition must be a boolean");
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
|
||||
if (ifTypedBlock.typeCheck(typedProgram) == elseTypedBlock.typeCheck(typedProgram)) {
|
||||
type = ifTypedBlock.typeCheck(typedProgram);
|
||||
}
|
||||
if (ifTypedBlock.typeCheck(clas) != Type.VOID) {
|
||||
throw new RuntimeException("If block must be of type void");
|
||||
if (elseTypedBlock.typeCheck(typedProgram) == Type.VOID) {
|
||||
type = ifTypedBlock.typeCheck(typedProgram);
|
||||
}
|
||||
*/
|
||||
//TODO: it still not catching the all cases when return is used
|
||||
if (ifTypedBlock.typeCheck(clas) == elseTypedBlock.typeCheck(clas)) {
|
||||
type = ifTypedBlock.typeCheck(clas);
|
||||
}
|
||||
if (elseTypedBlock.typeCheck(clas) == Type.VOID) {
|
||||
type = ifTypedBlock.typeCheck(clas);
|
||||
}
|
||||
if (ifTypedBlock.typeCheck(clas) == Type.VOID) {
|
||||
type = elseTypedBlock.typeCheck(clas);
|
||||
if (ifTypedBlock.typeCheck(typedProgram) == Type.VOID) {
|
||||
type = elseTypedBlock.typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
return type;
|
||||
|
@ -3,14 +3,10 @@ package de.maishai.typedast.typedclass;
|
||||
import de.maishai.ast.records.IntLiteral;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@ -20,17 +16,17 @@ public class TypedIntLiteral implements TypedExpression {
|
||||
private Type type;
|
||||
|
||||
|
||||
public TypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) {
|
||||
convertToTypedIntLiteral(clas, unTypedIntLiteral);
|
||||
public TypedIntLiteral(TypedProgram typedProgram, IntLiteral unTypedIntLiteral) {
|
||||
convertToTypedIntLiteral(typedProgram, unTypedIntLiteral);
|
||||
}
|
||||
|
||||
public void convertToTypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) {
|
||||
public void convertToTypedIntLiteral(TypedProgram typedProgram, IntLiteral unTypedIntLiteral) {
|
||||
value = unTypedIntLiteral.value();
|
||||
type = Type.INT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
@ -16,30 +16,30 @@ public final class TypedLocalVariable implements TypedNode {
|
||||
private String name;
|
||||
private Type type;
|
||||
|
||||
public TypedLocalVariable(TypedClass clas, Declaration declaration) {
|
||||
convertToTypedLocalVariable(clas, declaration);
|
||||
public TypedLocalVariable(TypedProgram typedProgram, Declaration declaration) {
|
||||
convertToTypedLocalVariable(typedProgram, declaration);
|
||||
}
|
||||
|
||||
public void convertToTypedLocalVariable(TypedClass clas, Declaration declaration) {
|
||||
public void convertToTypedLocalVariable(TypedProgram typedProgram, Declaration declaration) {
|
||||
name = declaration.name();
|
||||
type = declaration.type();
|
||||
typeCheck(clas);
|
||||
typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
|
||||
if (clas.getCurrentMethod().isLocalVariableInMethod(name)) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (typedProgram.getCurrentClass().isCurrentMethodPresent() && !typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(name)) {
|
||||
throw new RuntimeException("Variable " + name + " already declared");
|
||||
}
|
||||
clas.getCurrentMethod().getLocalVariables().add(this);
|
||||
typedProgram.getCurrentClass().getCurrentMethod().getLocalVariables().add(this);
|
||||
return type;
|
||||
}
|
||||
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
|
||||
if (clas.getCurrentConstructor().isLocalVariableInConstructor(name)) {
|
||||
if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(name)) {
|
||||
throw new RuntimeException("Variable " + name + " already declared");
|
||||
}
|
||||
clas.getCurrentConstructor().getLocalVariables().add(this);
|
||||
typedProgram.getCurrentClass().getCurrentConstructor().getLocalVariables().add(this);
|
||||
return type;
|
||||
}
|
||||
throw new RuntimeException("not found method or constructor in class");
|
||||
|
@ -1,6 +1,5 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.IfElse;
|
||||
import de.maishai.ast.records.Method;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
@ -20,28 +19,28 @@ public class TypedMethod implements TypedNode {
|
||||
private List<TypedLocalVariable> localVariables = new ArrayList<>();
|
||||
private TypedBlock typedBlock;
|
||||
|
||||
public TypedMethod(TypedClass clas, Method unTypedMethod) {
|
||||
convertToTypedMethod(clas, unTypedMethod);
|
||||
public TypedMethod(TypedProgram typedProgram, Method unTypedMethod) {
|
||||
convertToTypedMethod(typedProgram, unTypedMethod);
|
||||
}
|
||||
|
||||
public void convertToTypedMethod(TypedClass clas, Method unTypedMethod) {
|
||||
public void convertToTypedMethod(TypedProgram typedProgram, Method unTypedMethod) {
|
||||
|
||||
name = unTypedMethod.methodName();
|
||||
returnType = unTypedMethod.type();
|
||||
for (var parameter : unTypedMethod.params()) {
|
||||
typedParameters.add(new TypedParameter(clas, parameter));
|
||||
typedParameters.add(new TypedParameter(typedProgram, parameter));
|
||||
}
|
||||
|
||||
clas.getTypedMethods().stream().filter(method -> method.equals(this)).findFirst().ifPresentOrElse(
|
||||
typedProgram.getCurrentClass().getTypedMethods().stream().filter(method -> method.equals(this)).findFirst().ifPresentOrElse(
|
||||
method -> {
|
||||
throw new RuntimeException("Method " + name + " already exists");
|
||||
}, () -> {
|
||||
});
|
||||
}
|
||||
|
||||
public void convertToTypedBlock(TypedClass clas, Method unTypedMethod) {
|
||||
typedBlock = new TypedBlock(clas, unTypedMethod.block());
|
||||
typeCheck(clas);
|
||||
public void convertToTypedBlock(TypedProgram typedProgram, Method unTypedMethod) {
|
||||
typedBlock = new TypedBlock(typedProgram, unTypedMethod.block());
|
||||
typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
public boolean isLocalVariablePresent(String localVarName) {
|
||||
@ -84,9 +83,9 @@ public class TypedMethod implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (returnType != Type.VOID && !hasEvenReturnsInIfElseBlocks()) {
|
||||
if (typedBlock.typeCheck(clas).getKind() != returnType.getKind()) {
|
||||
if (typedBlock.typeCheck(typedProgram).getKind() != returnType.getKind()) {
|
||||
if (hasEvenReturnsInIfElseBlocks()) {
|
||||
throw new RuntimeException("Method " + name + " must have even returns in if else blocks");
|
||||
} else {
|
||||
|
@ -20,22 +20,22 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
|
||||
private List<TypedExpression> args = new ArrayList<>();
|
||||
private Type type;
|
||||
|
||||
public TypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) {
|
||||
convertToTypedMethodCall(clas, unTypedMethodCall);
|
||||
public TypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) {
|
||||
convertToTypedMethodCall(typedProgram, unTypedMethodCall);
|
||||
}
|
||||
|
||||
public void convertToTypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) {
|
||||
recipient = new TypedFieldVarAccess(clas, unTypedMethodCall.recipient());
|
||||
public void convertToTypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) {
|
||||
recipient = new TypedFieldVarAccess(typedProgram, unTypedMethodCall.recipient());
|
||||
for (Expression arg : unTypedMethodCall.args()) {
|
||||
args.add(convertExpression(clas, arg));
|
||||
args.add(convertExpression(typedProgram, arg));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
if (clas.isCurrentMethodPresent() || clas.isCurrentConstructorPresent()) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (typedProgram.getCurrentClass().isCurrentMethodPresent() || typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
|
||||
List<TypedMethod> methods = clas.getTypedMethods().stream()
|
||||
List<TypedMethod> methods = typedProgram.getCurrentClass().getTypedMethods().stream()
|
||||
.filter(method -> method.getName().equals(recipient.getName()))
|
||||
.toList();
|
||||
|
||||
@ -43,7 +43,7 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
|
||||
if (method.getTypedParameters().size() == args.size()) {
|
||||
boolean allMatch = true;
|
||||
for (int i = 0; i < args.size(); i++) {
|
||||
if (!args.get(i).typeCheck(clas).equals(method.getTypedParameters().get(i).getType())) {
|
||||
if (!args.get(i).typeCheck(typedProgram).equals(method.getTypedParameters().get(i).getType())) {
|
||||
allMatch = false;
|
||||
break;
|
||||
}
|
||||
|
@ -4,11 +4,9 @@ import de.maishai.ast.records.Expression;
|
||||
import de.maishai.ast.records.New;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@ -17,30 +15,30 @@ public class TypedNew implements TypedExpression, TypedStatement {
|
||||
private Type type;
|
||||
private List<TypedExpression> args = new ArrayList<>();
|
||||
|
||||
public TypedNew(TypedClass clas, New unTypedNew) {
|
||||
convertToTypedNew(clas, unTypedNew);
|
||||
public TypedNew(TypedProgram typedProgram, New unTypedNew) {
|
||||
convertToTypedNew(typedProgram, unTypedNew);
|
||||
}
|
||||
|
||||
public void convertToTypedNew(TypedClass clas, New unTypedNew) {
|
||||
public void convertToTypedNew(TypedProgram typedProgram, New unTypedNew) {
|
||||
type = unTypedNew.type();
|
||||
for (Expression arg : unTypedNew.args()) {
|
||||
args.add(convertExpression(clas, arg));
|
||||
args.add(convertExpression(typedProgram, arg));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
for (var constructor : clas.getTypedConstructors()) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
for (var constructor : typedProgram.getCurrentClass().getTypedConstructors()) {
|
||||
if (constructor.getTypedParameters().size() == args.size()) {
|
||||
boolean valid = true;
|
||||
for (int i = 0; i < args.size(); i++) {
|
||||
if (!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(clas))) {
|
||||
if (!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(typedProgram))) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (valid) {
|
||||
return Type.REFERENCE(clas.getClassName());
|
||||
return Type.REFERENCE(typedProgram.getCurrentClass().getClassName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,19 +6,17 @@ import de.maishai.typedast.TypedNode;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class TypedParameter implements TypedNode {
|
||||
private String paraName;
|
||||
private Type type;
|
||||
|
||||
public TypedParameter(TypedClass clas, Parameter unTypedParameter) {
|
||||
convertToTypedParameter(clas, unTypedParameter);
|
||||
public TypedParameter(TypedProgram typedProgram, Parameter unTypedParameter) {
|
||||
convertToTypedParameter(typedProgram, unTypedParameter);
|
||||
}
|
||||
|
||||
public void convertToTypedParameter(TypedClass clas, Parameter unTypedParameter) {
|
||||
public void convertToTypedParameter(TypedProgram typedProgram, Parameter unTypedParameter) {
|
||||
paraName = unTypedParameter.name();
|
||||
type = unTypedParameter.type();
|
||||
}
|
||||
@ -34,14 +32,14 @@ public class TypedParameter implements TypedNode {
|
||||
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
|
||||
if (clas.isCurrentMethodPresent()) {
|
||||
if (clas.isParameterWitNameInMethod(paraName)) {
|
||||
if (typedProgram.getCurrentClass().isCurrentMethodPresent()) {
|
||||
if (typedProgram.getCurrentClass().isParameterWitNameInMethod(paraName)) {
|
||||
throw new RuntimeException("Parameter " + paraName + " already exists");
|
||||
}
|
||||
} else if (clas.isCurrentConstructorPresent()) {
|
||||
if (clas.isParameterWitNameInConstructor(paraName)) {
|
||||
} else if (typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
|
||||
if (typedProgram.getCurrentClass().isParameterWitNameInConstructor(paraName)) {
|
||||
throw new RuntimeException("Parameter " + paraName + " already exists");
|
||||
}
|
||||
}
|
||||
|
@ -11,14 +11,45 @@ import java.util.List;
|
||||
@AllArgsConstructor
|
||||
public class TypedProgram {
|
||||
private List<TypedClass> typedClasses = new ArrayList<>();
|
||||
private TypedClass currentClass;
|
||||
|
||||
|
||||
public TypedProgram(Program program){
|
||||
public TypedProgram(Program program) {
|
||||
startConversion(program);
|
||||
}
|
||||
public void startConversion(Program program){
|
||||
//TODO: implement
|
||||
program.classes().toString();
|
||||
|
||||
public void startConversion(Program program) {
|
||||
|
||||
int k = 0;
|
||||
for (var clas : program.classes()) {
|
||||
enterCurrentClass(typedClasses.get(k));
|
||||
typedClasses.add(new TypedClass(this, clas));
|
||||
exitCurrentClass();
|
||||
k++;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for(var clas : program.classes()){
|
||||
enterCurrentClass(typedClasses.get(i));
|
||||
typedClasses.get(i).covertBlocksOfConstructorsAndMethods(this, clas);
|
||||
exitCurrentClass();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public TypedClass getTypedClass(String className) {
|
||||
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get();
|
||||
}
|
||||
|
||||
public boolean isCurrentClassPresent() {
|
||||
return currentClass != null;
|
||||
}
|
||||
|
||||
public void enterCurrentClass(TypedClass clas) {
|
||||
currentClass = clas;
|
||||
}
|
||||
|
||||
public void exitCurrentClass() {
|
||||
currentClass = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,11 +5,8 @@ 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@Data
|
||||
@ -18,12 +15,12 @@ public class TypedReturn implements TypedStatement {
|
||||
private TypedExpression ret;
|
||||
private Type type;
|
||||
|
||||
public TypedReturn(TypedClass clas, Return unTypedReturn) {
|
||||
convertToTypedReturn(clas, unTypedReturn);
|
||||
public TypedReturn(TypedProgram typedProgram, Return unTypedReturn) {
|
||||
convertToTypedReturn(typedProgram, unTypedReturn);
|
||||
}
|
||||
|
||||
public void convertToTypedReturn(TypedClass clas, Return unTypedReturn) {
|
||||
ret = convertExpression(clas, unTypedReturn.ret());
|
||||
public void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) {
|
||||
ret = convertExpression(typedProgram, unTypedReturn.ret());
|
||||
if(ret == null){
|
||||
type = Type.VOID;
|
||||
}else{
|
||||
@ -32,14 +29,14 @@ public class TypedReturn implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
if (clas.isCurrentMethodPresent()) {
|
||||
if (clas.getCurrentMethod().getReturnType().getKind() != this.type.getKind()) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (typedProgram.getCurrentClass().isCurrentMethodPresent()) {
|
||||
if (typedProgram.getCurrentClass().getCurrentMethod().getReturnType().getKind() != this.type.getKind()) {
|
||||
StringBuilder exp = new StringBuilder();
|
||||
exp.append("\nMismatched return type: ");
|
||||
exp.append("\nExpected: ").append(clas.getCurrentMethod().getReturnType().getKind());
|
||||
exp.append("\nExpected: ").append(typedProgram.getCurrentClass().getCurrentMethod().getReturnType().getKind());
|
||||
exp.append("\nActual: ").append(this.type.getKind());
|
||||
exp.append("\nMethod name: ").append(clas.getCurrentMethod().getName());
|
||||
exp.append("\nMethod name: ").append(typedProgram.getCurrentClass().getCurrentMethod().getName());
|
||||
throw new RuntimeException(exp.toString());
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,8 @@ import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@Data
|
||||
@ -19,27 +16,27 @@ public class TypedUnary implements TypedExpression {
|
||||
private TypedExpression right;
|
||||
private Type type;
|
||||
|
||||
public TypedUnary(TypedClass clas, Unary unTypedUnary) {
|
||||
convertToTypedUnary(clas, unTypedUnary);
|
||||
public TypedUnary(TypedProgram typedProgram, Unary unTypedUnary) {
|
||||
convertToTypedUnary(typedProgram, unTypedUnary);
|
||||
}
|
||||
|
||||
public void convertToTypedUnary(TypedClass clas, Unary unTypedUnary) {
|
||||
public void convertToTypedUnary(TypedProgram typedProgram, Unary unTypedUnary) {
|
||||
op = unTypedUnary.op();
|
||||
right = convertExpression(clas, unTypedUnary.right());
|
||||
right = convertExpression(typedProgram, unTypedUnary.right());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
|
||||
if (op == UnaryOperator.NOT) {
|
||||
if (right.typeCheck(clas) != Type.BOOL) {
|
||||
if (right.typeCheck(typedProgram) != Type.BOOL) {
|
||||
throw new RuntimeException("Not operator must be applied to boolean");
|
||||
}
|
||||
return Type.BOOL;
|
||||
}
|
||||
|
||||
if (op == UnaryOperator.SUB) {
|
||||
if (right.typeCheck(clas) != Type.INT) {
|
||||
if (right.typeCheck(typedProgram) != Type.INT) {
|
||||
throw new RuntimeException("Minus operator must be applied to int");
|
||||
}
|
||||
return Type.INT;
|
||||
|
@ -3,9 +3,6 @@ package de.maishai.typedast.typedclass;
|
||||
import de.maishai.ast.records.*;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
||||
|
||||
@ -15,21 +12,21 @@ public class TypedWhile implements TypedStatement {
|
||||
private TypedBlock typedBlock;
|
||||
private Type type;
|
||||
|
||||
public TypedWhile(TypedClass clas, While unTypedWhile) {
|
||||
convertToTypedWhile(clas, unTypedWhile);
|
||||
public TypedWhile(TypedProgram typedProgram, While unTypedWhile) {
|
||||
convertToTypedWhile(typedProgram, unTypedWhile);
|
||||
}
|
||||
|
||||
public void convertToTypedWhile(TypedClass clas, While unTypedWhile) {
|
||||
cond = convertExpression(clas, unTypedWhile.cond());
|
||||
typedBlock = new TypedBlock(clas, unTypedWhile.block());
|
||||
public void convertToTypedWhile(TypedProgram typedProgram, While unTypedWhile) {
|
||||
cond = convertExpression(typedProgram, unTypedWhile.cond());
|
||||
typedBlock = new TypedBlock(typedProgram, unTypedWhile.block());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedClass clas) {
|
||||
if (cond.typeCheck(clas) != Type.BOOL) {
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
if (cond.typeCheck(typedProgram) != Type.BOOL) {
|
||||
throw new RuntimeException("While condition must be a boolean");
|
||||
}
|
||||
type = typedBlock.typeCheck(clas);
|
||||
type = typedBlock.typeCheck(typedProgram);
|
||||
return type;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user