Implemented typedblock (some statements and expressions) and add some TODOs

This commit is contained in:
Ahmad 2024-05-09 15:59:28 +02:00
parent f502e58e6e
commit ba7b23bd34

View File

@ -15,115 +15,114 @@ public class TypedBlock implements TypedNode {
private List<TypedStatement> stmts; private List<TypedStatement> stmts;
public TypedBlock blockToTypedBlock(Map<String, Type> localVar, Map<String, TypedClass> classes, Block unTypedBlock) {
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) { if (unTypedBlock == null) {
return null;
}
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>()); typedBlock.setStmts(new ArrayList<>());
typedBlock.setVars(new ArrayList<>()); typedBlock.setVars(new ArrayList<>());
if(unTypedBlock.localVariables().isEmpty()){
if (unTypedBlock.localVariables().isEmpty()) {
typedBlock.setVars(List.of()); typedBlock.setVars(List.of());
} } else {
if(unTypedBlock.stmts().isEmpty()){
typedBlock.setStmts(List.of());
}
for (Declaration var : unTypedBlock.localVariables()) { for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable(); TypedLocalVariable typedVar = new TypedLocalVariable()
typedVar.setName(var.name()); .convertToTypedLocalVariable(localVar, classes, var);
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar); typedBlock.getVars().add(typedVar);
} }
}
for(var stmt : unTypedBlock.stmts()){ if (unTypedBlock.stmts().isEmpty()) {
typedBlock.setStmts(List.of());
} else {
if(stmt instanceof Assignment assignment){ for (var stmt : unTypedBlock.stmts()) {
TypedAssignment typedAssignment = new TypedAssignment(); if (stmt instanceof Assignment assignment) {
typedAssignment.setVarName(assignment.location().id()); TypedAssignment typedAssignment = new TypedAssignment()
typedAssignment.setValue(null); .convertToTypedAssignment(localVar, classes, assignment);
typedAssignment.typeCheck(localVar, classes);
typedBlock.getStmts().add(typedAssignment); typedBlock.getStmts().add(typedAssignment);
continue; continue;
} }
if(stmt instanceof For forStmt){ if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor(); TypedFor typedFor = new TypedFor()
TypedBlock typedBlock1 = new TypedBlock(); .convertToTypedFor(localVar, classes, forStmt);
typedFor.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock((forStmt.block()))); typedFor.typeCheck(localVar, classes);
typedFor.setAssign(null);
typedFor.setCond(null);
typedFor.setInc(null);
typedBlock.getStmts().add(typedFor); typedBlock.getStmts().add(typedFor);
continue; continue;
} }
if(stmt instanceof IfElse ifElse){ if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse(); TypedIfElse typedIfElse = new TypedIfElse()
TypedBlock typedBlockIf = new TypedBlock(); .convertToTypedIfElse(localVar, classes, ifElse);
TypedBlock typedBlockElse = new TypedBlock(); typedIfElse.typeCheck(localVar, classes);
typedIfElse.setIfTypedBlock(typedBlockIf.unTypedBlockToTypedBlock(ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(typedBlockElse.unTypedBlockToTypedBlock(ifElse.elseBlock()));
typedIfElse.setTypedCon(null);
typedBlock.getStmts().add(typedIfElse); typedBlock.getStmts().add(typedIfElse);
continue; continue;
} }
if(stmt instanceof MethodCall methodCall){ if (stmt instanceof While whileStmt) {
TypedMethodCall typedMethodCall = new TypedMethodCall(); TypedWhile typedWhile = new TypedWhile()
typedMethodCall.setArgs(null); .convertToTypedWhile(localVar, classes, whileStmt);
typedMethodCall.setRecipient(null); //TODO: check the type
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); typedBlock.getStmts().add(typedWhile);
continue; continue;
} }
if(stmt instanceof DoWhile doWhile){ if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile(); TypedDoWhile typedDoWhile = new TypedDoWhile()
TypedBlock typedBlock1 = new TypedBlock(); .convertToTypedDoWhile(localVar, classes, doWhile);
typedDoWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(doWhile.block())); //TODO: check the type
typedDoWhile.setCond(null);
typedBlock.getStmts().add(typedDoWhile); typedBlock.getStmts().add(typedDoWhile);
continue; continue;
} }
if(stmt instanceof Break){ if (stmt instanceof Return returnStmt) {
TypedBreak typedBreak = new TypedBreak(); TypedReturn typedReturn = new TypedReturn()
typedBlock.getStmts().add(typedBreak); .convertToTypedReturn(localVar, classes, returnStmt);
//TODO: check the type
typedBlock.getStmts().add(typedReturn);
continue;
}
if (stmt instanceof New newStmt) {
TypedNew typedNew = new TypedNew();
//TODO: implement this
continue;
} }
if (stmt instanceof Break) {
TypedBreak typedBreak = new TypedBreak();
typedBreak.typeCheck(localVar, classes);
typedBlock.getStmts().add(typedBreak);
continue;
}
if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall();
//TODO: implement this
}
}
} }
//System.out.println("stmts:" + typedBlock.getStmts().toString());
return typedBlock; return typedBlock;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
Type typeOfLastStmt = Type.VOID; if (vars == null) {
vars = List.of();
} else {
for (TypedLocalVariable var : vars) { for (TypedLocalVariable var : vars) {
var.typeCheck(localVar, classes); var.typeCheck(localVar, classes);
} }
}
if (stmts == null) {
stmts = List.of();
} else {
for (TypedStatement stmt : stmts) { for (TypedStatement stmt : stmts) {
stmt.typeCheck(localVar, classes); stmt.typeCheck(localVar, classes);
typeOfLastStmt = stmt.typeCheck(localVar, classes); }
} }
return typeOfLastStmt; return Type.VOID;
} }
@Override @Override
@ -141,8 +140,8 @@ public class TypedBlock implements TypedNode {
} }
typedBlock.getStmts().add((TypedStatement) untyped.stmts()); typedBlock.getStmts().add((TypedStatement) untyped.stmts());
int i = 0; int i = 0;
for(var stmt : untyped.stmts()){ for (var stmt : untyped.stmts()) {
System.out.println(i++ +" :" + stmt); System.out.println(i++ + " :" + stmt);
} }
return typedBlock; return typedBlock;