mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 16:48:03 +00:00
done TODOs for TypedReturn but not all
This commit is contained in:
parent
875d0adac4
commit
c7d756b083
@ -40,12 +40,7 @@ public class TypedExpressionUtil {
|
|||||||
return typedMethodCall;
|
return typedMethodCall;
|
||||||
}
|
}
|
||||||
else if (expression instanceof New newStmt) {
|
else if (expression instanceof New newStmt) {
|
||||||
TypedNew typedNew = new TypedNew();
|
return new TypedNew(localVar, clas, newStmt);
|
||||||
typedNew.setType(newStmt.type());
|
|
||||||
for (var arg : newStmt.args()) {
|
|
||||||
typedNew.getArgs().add(getKindOfExpression(localVar, clas, arg));
|
|
||||||
}
|
|
||||||
return typedNew;
|
|
||||||
}
|
}
|
||||||
else if (expression instanceof Unary unary) {
|
else if (expression instanceof Unary unary) {
|
||||||
TypedUnary typedUnary = new TypedUnary();
|
TypedUnary typedUnary = new TypedUnary();
|
||||||
|
@ -25,6 +25,10 @@ public class TypedBlock implements TypedNode {
|
|||||||
|
|
||||||
public void blockToTypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
|
public void blockToTypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
|
||||||
|
|
||||||
|
if(unTypedBlock == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (Declaration var : unTypedBlock.localVariables()) {
|
for (Declaration var : unTypedBlock.localVariables()) {
|
||||||
TypedLocalVariable typedVar = new TypedLocalVariable(localVar, clas, var);
|
TypedLocalVariable typedVar = new TypedLocalVariable(localVar, clas, var);
|
||||||
vars.add(typedVar);
|
vars.add(typedVar);
|
||||||
@ -68,8 +72,9 @@ public class TypedBlock implements TypedNode {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (stmt instanceof New newStmt) {
|
if (stmt instanceof New newStmt) {
|
||||||
TypedNew typedNew = new TypedNew();
|
TypedNew typedNew = new TypedNew(localVar, clas, newStmt);
|
||||||
//TODO: implement this
|
typedNew.typeCheck(localVar, clas);
|
||||||
|
stmts.add(typedNew);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,16 +37,16 @@ public class TypedClass implements TypedNode {
|
|||||||
typedFields.add(new TypedField(localVar, this, field));
|
typedFields.add(new TypedField(localVar, this, field));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (Constructor constructor : c.constructors()) {
|
||||||
|
typedConstructors.add(new TypedConstructor(localVar, this, constructor));
|
||||||
|
typedConstructors.get(typedConstructors.size() - 1).convertToBlock(localVar, this, constructor);
|
||||||
|
}
|
||||||
|
|
||||||
for (Method method : c.methods()) {
|
for (Method method : c.methods()) {
|
||||||
typedMethods.add(new TypedMethod(localVar, this, method));
|
typedMethods.add(new TypedMethod(localVar, this, method));
|
||||||
typedMethods.get(typedMethods.size() - 1).convertToTypedBlock(localVar, this, method);
|
typedMethods.get(typedMethods.size() - 1).convertToTypedBlock(localVar, this, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (Constructor constructor : c.constructors()) {
|
|
||||||
typedConstructors.add(new TypedConstructor(localVar, this, constructor));
|
|
||||||
typedConstructors.get(typedConstructors.size() - 1).convertToBlock(localVar, this, constructor);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -33,14 +33,28 @@ public class TypedMethod implements TypedNode {
|
|||||||
for (Parameter parameter : unTypedMethod.params()) {
|
for (Parameter parameter : unTypedMethod.params()) {
|
||||||
typedParameters.add(new TypedParameter(localVar, clas, parameter));
|
typedParameters.add(new TypedParameter(localVar, clas, parameter));
|
||||||
}
|
}
|
||||||
typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block());
|
for (var method : clas.getTypedMethods()) {
|
||||||
|
if (method.getName().equals(name) && method.getTypedParameters().size() == typedParameters.size()
|
||||||
|
&& method.getReturnType().equals(returnType)) {
|
||||||
|
|
||||||
|
for (int i = 0; i < method.getTypedParameters().size(); i++){
|
||||||
|
if(method.getTypedParameters().get(i).getType().equals(typedParameters.get(i).getType())){
|
||||||
|
throw new RuntimeException("Method " + name + " already exists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method.getTypedParameters().isEmpty() && typedParameters.isEmpty()) {
|
||||||
|
throw new RuntimeException("Method " + name + " already exists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void convertToTypedBlock(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
|
public void convertToTypedBlock(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
|
||||||
typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block());
|
typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||||
if (localVar.containsKey(name)) {
|
if (localVar.containsKey(name)) {
|
||||||
|
@ -1,39 +1,55 @@
|
|||||||
package de.maishai.typedast.typedclass;
|
package de.maishai.typedast.typedclass;
|
||||||
|
|
||||||
|
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.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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class TypedNew implements TypedExpression, TypedStatement {
|
public class TypedNew implements TypedExpression, TypedStatement {
|
||||||
private Type type;
|
private Type type;
|
||||||
private List<TypedExpression> args;
|
private List<TypedExpression> args = new ArrayList<>();
|
||||||
|
|
||||||
public TypedNew convertToTypedNew(New unTypedNew) {
|
public TypedNew(Map<String, Type> localVar, TypedClass clas, New unTypedNew) {
|
||||||
//TODO: Implement this
|
convertToTypedNew(localVar, clas, unTypedNew);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
public void convertToTypedNew(Map<String, Type> localVar, TypedClass clas, New unTypedNew) {
|
||||||
|
type = unTypedNew.type();
|
||||||
|
for (Expression arg : unTypedNew.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) {
|
||||||
if(clas == null){
|
if(clas == null){
|
||||||
throw new RuntimeException("Class not found");
|
throw new RuntimeException("Class not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clas.getTypedFields().size() != args.size()) {
|
for(var constructor : clas.getTypedConstructors()){
|
||||||
throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference());
|
if(constructor.getTypedParameters().size() == args.size()){
|
||||||
}
|
boolean valid = true;
|
||||||
for (int i = 0; i < args.size(); i++) {
|
for(int i = 0; i < args.size(); i++){
|
||||||
Type type = args.get(i).typeCheck(localVar, clas);
|
if(!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(localVar, clas))){
|
||||||
if (!type.equals(clas.getTypedFields().get(i).getType())) {
|
valid = false;
|
||||||
throw new RuntimeException("False type for argument " + i + " in class " + type.getKind());
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(valid){
|
||||||
|
return Type.REFERENCE(clas.getClassName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Type.VOID;
|
throw new RuntimeException("No matching constructor found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,10 +28,10 @@ public class TypedReturn implements TypedStatement {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||||
|
//TODO: not localvar
|
||||||
for(var typedMethod : clas.getTypedMethods()) {
|
for(var typedMethod : clas.getTypedMethods()) {
|
||||||
if(!localVar.containsKey(typedMethod.getName())) {
|
if(localVar.containsKey(typedMethod.getName())) {
|
||||||
if(typedMethod.getReturnType() != this.type) {
|
if(typedMethod.getReturnType().getKind() != this.type.getKind()) {
|
||||||
//TODO: exception auslagern
|
//TODO: exception auslagern
|
||||||
StringBuilder exp = new StringBuilder();
|
StringBuilder exp = new StringBuilder();
|
||||||
exp.append("\nMismatched return type: ");
|
exp.append("\nMismatched return type: ");
|
||||||
@ -42,7 +42,6 @@ public class TypedReturn implements TypedStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user