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 (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment()
.convertToTypedAssignment(localVar, classes, assignment);
typedAssignment.typeCheck(localVar, classes);
typedBlock.getStmts().add(typedAssignment);
continue;
}
if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor()
.convertToTypedFor(localVar, classes, forStmt);
typedFor.typeCheck(localVar, classes);
typedBlock.getStmts().add(typedFor);
continue;
}
if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse()
.convertToTypedIfElse(localVar, classes, ifElse);
typedIfElse.typeCheck(localVar, classes);
typedBlock.getStmts().add(typedIfElse);
continue;
}
if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile()
.convertToTypedWhile(localVar, classes, whileStmt);
//TODO: check the type
typedBlock.getStmts().add(typedWhile);
continue;
}
if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile()
.convertToTypedDoWhile(localVar, classes, doWhile);
//TODO: check the type
typedBlock.getStmts().add(typedDoWhile);
continue;
}
if (stmt instanceof Return returnStmt) {
TypedReturn typedReturn = new TypedReturn()
.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
}
}
} }
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()){
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; 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) {
for (TypedLocalVariable var : vars) { vars = List.of();
var.typeCheck(localVar, classes); } else {
for (TypedLocalVariable var : vars) {
var.typeCheck(localVar, classes);
}
} }
for (TypedStatement stmt : stmts) { if (stmts == null) {
stmt.typeCheck(localVar, classes); stmts = List.of();
typeOfLastStmt = stmt.typeCheck(localVar, classes); } else {
for (TypedStatement stmt : stmts) {
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;