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 {
for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable()
.convertToTypedLocalVariable(localVar, classes, var);
typedBlock.getVars().add(typedVar);
}
} }
if (unTypedBlock.stmts().isEmpty()) { if (unTypedBlock.stmts().isEmpty()) {
typedBlock.setStmts(List.of()); typedBlock.setStmts(List.of());
} } else {
for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.setName(var.name());
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar);
}
for (var stmt : unTypedBlock.stmts()) { for (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) { if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment(); TypedAssignment typedAssignment = new TypedAssignment()
typedAssignment.setVarName(assignment.location().id()); .convertToTypedAssignment(localVar, classes, assignment);
typedAssignment.setValue(null); 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){
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) { if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile(); TypedWhile typedWhile = new TypedWhile()
TypedBlock typedBlock1 = new TypedBlock(); .convertToTypedWhile(localVar, classes, whileStmt);
typedWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(whileStmt.block())); //TODO: check the type
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