Updated the method converToTypedAST

This commit is contained in:
Ahmad 2024-05-08 16:30:52 +02:00
parent 81d1cf6ca8
commit 622a0951f3
10 changed files with 40 additions and 28 deletions

View File

@ -31,13 +31,6 @@ public class TypedAssignment implements TypedStatement {
} }
throw new RuntimeException("type of left not equals with type of right"); throw new RuntimeException("type of left not equals with type of right");
} }
// int y = 5;
// int x = (y + 5 + z);
//TypedAssignment([
// localVar("x", "int"),
// localVar("y","int")
// ],
// "int")
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {

View File

@ -9,6 +9,7 @@ import de.maishai.typedast.Type;
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;
@ -51,18 +52,18 @@ public class TypedBlock implements TypedNode {
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Block untyped = (Block) unTypedAST; Block untyped = (Block) unTypedAST;
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>());
typedBlock.setVars(new ArrayList<>());
for (Declaration var : untyped.localVariables()) { for (Declaration var : untyped.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable(); TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.convertToTypedAST(localVar, classes, var); typedVar.setName(var.name());
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar); typedBlock.getVars().add(typedVar);
} }
typedBlock.getStmts().add((TypedStatement) untyped.stmts());
for (de.maishai.ast.records.Statement stmt : untyped.stmts()) { return typedBlock;
//TODO: Implement the conversion of the statements
}
return null;
} }
public void codeGen(MethodVisitor mv, MethodContext ctx) { public void codeGen(MethodVisitor mv, MethodContext ctx) {

View File

@ -1,5 +1,6 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Break;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement; import de.maishai.typedast.TypedStatement;
@ -17,6 +18,10 @@ public class TypedBreak implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Break untyped = (Break) unTypedAST;
if(untyped == null) {
throw new RuntimeException("Break not found");
}
return new TypedBreak(); return new TypedBreak();
} }

View File

@ -24,7 +24,8 @@ public class TypedDoWhile implements TypedStatement {
DoWhile untyped = (DoWhile) unTypedAST; DoWhile untyped = (DoWhile) unTypedAST;
TypedDoWhile typedDoWhile = new TypedDoWhile(); TypedDoWhile typedDoWhile = new TypedDoWhile();
typedDoWhile.setTypedBlock( (TypedBlock) typedBlock.convertToTypedAST(localVar, classes, untyped.block())); TypedBlock typedBlock = new TypedBlock();
typedDoWhile.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond())); typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
return typedDoWhile; return typedDoWhile;

View File

@ -39,8 +39,9 @@ public class TypedField implements TypedNode {
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Declaration untypedDeclaration = (Declaration) unTypedAST; Declaration untypedDeclaration = (Declaration) unTypedAST;
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();
typedField.setVarName(untypedDeclaration.name());
typedField.setType(untypedDeclaration.type()); typedField.setType(untypedDeclaration.type());
typedField.setVarName(untypedDeclaration.name());
return typedField; return typedField;
} }

View File

@ -34,7 +34,9 @@ public class TypedFor implements TypedStatement {
typedFor.setAssign((TypedExpression) assign.convertToTypedAST(localVar, classes, untyped.assign())); typedFor.setAssign((TypedExpression) assign.convertToTypedAST(localVar, classes, untyped.assign()));
typedFor.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond())); typedFor.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
typedFor.setInc((TypedExpression) inc.convertToTypedAST(localVar, classes, untyped.inc())); typedFor.setInc((TypedExpression) inc.convertToTypedAST(localVar, classes, untyped.inc()));
typedFor.setTypedBlock((TypedBlock) typedBlock.convertToTypedAST(localVar, classes, untyped.block()));
TypedBlock typedBlock = new TypedBlock();
typedFor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
return typedFor; return typedFor;
} }

View File

@ -32,8 +32,11 @@ public class TypedIfElse implements TypedStatement {
TypedIfElse typedIfElse = new TypedIfElse(); TypedIfElse typedIfElse = new TypedIfElse();
typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, classes, ifElse.cond())); typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, classes, ifElse.cond()));
typedIfElse.setIfTypedBlock((TypedBlock) ifTypedBlock.convertToTypedAST(localVar, classes, ifElse.ifBlock()));
typedIfElse.setElseTypedBlock((TypedBlock) elseTypedBlock.convertToTypedAST(localVar, classes, ifElse.elseBlock())); TypedBlock ifTypedBlock = new TypedBlock();
TypedBlock elseTypedBlock = new TypedBlock();
typedIfElse.setIfTypedBlock(ifTypedBlock.unTypedBlockToTypedBlock(ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(elseTypedBlock.unTypedBlockToTypedBlock(ifElse.elseBlock()));
return typedIfElse; return typedIfElse;
} }

View File

@ -8,12 +8,12 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class TypedMethod implements TypedNode { public class TypedMethod implements TypedNode {
private Boolean isPublic;
private String name; private String name;
private Type returnType; private Type returnType;
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
@ -50,20 +50,19 @@ public class TypedMethod implements TypedNode {
Method untypedMethod = (Method) unTypedAST; Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
typedMethod.setTypedParameters(new ArrayList<>());
typedMethod.setName(untypedMethod.methodName()); typedMethod.setName(untypedMethod.methodName());
typedMethod.setReturnType(untypedMethod.type()); typedMethod.setReturnType(untypedMethod.type());
if(untypedMethod.params().isEmpty()){
//TODO: Implement this
}
for(Parameter parameter : untypedMethod.params()) { for(Parameter parameter : untypedMethod.params()) {
TypedParameter typedParameter = getTypedParameter(parameter); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
typedParameter.setType(parameter.type());
typedMethod.getTypedParameters().add(typedParameter); typedMethod.getTypedParameters().add(typedParameter);
} }
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedMethod.setTypedBlock((TypedBlock) typedBlock.convertToTypedAST(localVar, classes, untypedMethod.block())); typedMethod.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untypedMethod.block()));
return typedMethod; return typedMethod;
} }

View File

@ -1,15 +1,18 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.Map; import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
@RequiredArgsConstructor @RequiredArgsConstructor
@NoArgsConstructor
@Data @Data
public class TypedParameter implements TypedNode { public class TypedParameter implements TypedNode {
private String paraName; private String paraName;
@ -23,10 +26,13 @@ public class TypedParameter implements TypedNode {
localVar.put(paraName, type); localVar.put(paraName, type);
return type; return type;
} }
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; Parameter untyped = (Parameter) unTypedAST;
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(untyped.name());
typedParameter.setType(untyped.type());
return typedParameter;
} }

View File

@ -24,7 +24,8 @@ public class TypedWhile implements TypedStatement {
While untyped = (While) unTypedAST; While untyped = (While) unTypedAST;
TypedWhile typedWhile = new TypedWhile(); TypedWhile typedWhile = new TypedWhile();
typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond())); typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
typedWhile.setTypedBlock((TypedBlock) typedBlock.convertToTypedAST(localVar, classes, untyped.block())); TypedBlock typedBlock = new TypedBlock();
typedWhile.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
return typedWhile; return typedWhile;
} }