Implemented the statements in TypedBlock

This commit is contained in:
Ahmad 2024-05-08 20:26:09 +02:00
parent 622a0951f3
commit db646c3399
9 changed files with 99 additions and 83 deletions

View File

@ -1,11 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Block;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
@ -21,7 +17,18 @@ public class TypedBlock implements TypedNode {
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) {
TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>());
typedBlock.setVars(new ArrayList<>());
if(unTypedBlock.localVariables().isEmpty()){
typedBlock.setVars(List.of());
}
if(unTypedBlock.stmts().isEmpty()){
typedBlock.setStmts(List.of());
}
for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
@ -30,7 +37,78 @@ public class TypedBlock implements TypedNode {
typedBlock.getVars().add(typedVar);
}
typedBlock.getStmts().add((TypedStatement) unTypedBlock.stmts());
for(var stmt : unTypedBlock.stmts()){
if(stmt instanceof Assignment assignment){
TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setVarName(assignment.location().id());
typedAssignment.setValue(null);
typedBlock.getStmts().add(typedAssignment);
continue;
}
if(stmt instanceof For forStmt){
TypedFor typedFor = new TypedFor();
TypedBlock typedBlock1 = new TypedBlock();
typedFor.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock((forStmt.block())));
typedFor.setAssign(null);
typedFor.setCond(null);
typedFor.setInc(null);
typedBlock.getStmts().add(typedFor);
continue;
}
if(stmt instanceof IfElse ifElse){
TypedIfElse typedIfElse = new TypedIfElse();
TypedBlock typedBlockIf = new TypedBlock();
TypedBlock typedBlockElse = new TypedBlock();
typedIfElse.setIfTypedBlock(typedBlockIf.unTypedBlockToTypedBlock(ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(typedBlockElse.unTypedBlockToTypedBlock(ifElse.elseBlock()));
typedIfElse.setTypedCon(null);
typedBlock.getStmts().add(typedIfElse);
continue;
}
if(stmt instanceof MethodCall methodCall){
TypedMethodCall typedMethodCall = new TypedMethodCall();
typedMethodCall.setArgs(null);
typedMethodCall.setRecipient(null);
typedBlock.getStmts().add(typedMethodCall);
continue;
}
if(stmt instanceof New newStmt){
TypedNew typedNew = new TypedNew();
typedNew.setArgs(null);
typedNew.setType(newStmt.type());
typedBlock.getStmts().add(typedNew);
continue;
}
if(stmt instanceof Return returnStmt){
TypedReturn typedReturn = new TypedReturn();
typedReturn.setRet(null);
typedBlock.getStmts().add(typedReturn);
continue;
}
if(stmt instanceof While whileStmt){
TypedWhile typedWhile = new TypedWhile();
TypedBlock typedBlock1 = new TypedBlock();
typedWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(whileStmt.block()));
typedWhile.setCond(null);
typedBlock.getStmts().add(typedWhile);
continue;
}
if(stmt instanceof DoWhile doWhile){
TypedDoWhile typedDoWhile = new TypedDoWhile();
TypedBlock typedBlock1 = new TypedBlock();
typedDoWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(doWhile.block()));
typedDoWhile.setCond(null);
typedBlock.getStmts().add(typedDoWhile);
continue;
}
if(stmt instanceof Break){
TypedBreak typedBreak = new TypedBreak();
typedBlock.getStmts().add(typedBreak);
}
}
//System.out.println("stmts:" + typedBlock.getStmts().toString());
return typedBlock;
}
@ -62,6 +140,10 @@ public class TypedBlock implements TypedNode {
typedBlock.getVars().add(typedVar);
}
typedBlock.getStmts().add((TypedStatement) untyped.stmts());
int i = 0;
for(var stmt : untyped.stmts()){
System.out.println(i++ +" :" + stmt);
}
return typedBlock;
}

View File

@ -67,21 +67,17 @@ public class TypedClass implements TypedNode {
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field));
}
for (Constructor constructor : c.constructors()) {
TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
}
for(Constructor constructor : c.constructors()){
TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
}
for (Method method : c.methods()) {
TypedMethod typedMethod = new TypedMethod();
typedClass.getTypedMethods().add(typedMethod.unTypedMethodToTypedMethod(method));
}
for (Constructor constructor : c.constructors()) {
TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
}
return typedClass;
}

View File

@ -45,9 +45,10 @@ public class TypedConstructor implements TypedNode {
public TypedConstructor unTypedContructorToTypedConstructor(de.maishai.ast.records.Constructor unTypedConstructor) {
TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(unTypedConstructor.className());
typedConstructor.setTypedParameters(new ArrayList<>());
if (unTypedConstructor.params().isEmpty()) {
typedConstructor.setTypedParameters(null);
typedConstructor.setTypedParameters(List.of());
} else {
for (Parameter param : unTypedConstructor.params()) {
@ -58,9 +59,8 @@ public class TypedConstructor implements TypedNode {
}
}
Block block = unTypedConstructor.block();
TypedBlock typedBlock = new TypedBlock();
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(block));
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(unTypedConstructor.block()));
return typedConstructor;
}

View File

@ -9,9 +9,7 @@ import java.util.Map;
@Data
public class TypedMethodCall implements TypedExpression, TypedStatement {
private Boolean field;
private TypedExpression recipient;
private String name;
private List<TypedExpression> args;
@Override

View File

@ -12,7 +12,6 @@ import java.util.Map;
@AllArgsConstructor
@RequiredArgsConstructor
@NoArgsConstructor
@Data
public class TypedParameter implements TypedNode {
private String paraName;

View File

@ -1,26 +0,0 @@
package de.maishai.typedast.typedclass;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class TypedProgram implements TypedNode {
private List<TypedClass> typedClasses;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
for(TypedClass c : this.typedClasses){
c.typeCheck(localVar, classes);
}
return Type.VOID;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
//TODO: Implement this
return null;
}
}

View File

@ -1,33 +0,0 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Method;
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 TypedReturnVoid implements TypedStatement {
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.VOID;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Method untyped = (Method) unTypedAST;
if(untyped.type() != Type.VOID){
throw new RuntimeException("Return type is not void");
}
return new TypedReturnVoid();
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
}
}

View File

@ -1,6 +1,6 @@
public class ClassWithConstructor {
int x;
public classWithConstructor() {
public ClassWithConstructor() {
this.x = 10;
int i;
for (i = 0; i < 6; i = i + 1) {

View File

@ -1,6 +1,6 @@
public class ClassWithConstructorWithParameters {
int x;
public classWithConstructorWithParameters(int startValue, int repetitions) {
public ClassWithConstructorWithParameters(int startValue, int repetitions) {
this.x = startValue;
while (repetitions > 0) {
int innerRepetitions;