Skip to content

Commit

Permalink
(UnB-CIC#37) implemented more typecheckers functions for statments an…
Browse files Browse the repository at this point in the history
…d expressions
  • Loading branch information
waltim committed Nov 2, 2019
1 parent 0950a97 commit 3fd89cd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
63 changes: 62 additions & 1 deletion sample-code/oberon/src/lang/oberon/TypeChecker.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,72 @@ public Type wellTyped(LoEq(lhs, rhs), ctx) {
}
}

public Type wellTyped(LoEq(lhs, rhs), ctx) {
public Type wellTyped(Eq(lhs, rhs), ctx) {
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) {
case <TInt(), TInt()> : return TBool();
case <TBool(), TBool()> : return TBool();
default : return TError();
}
}

//Statments TypeChecker

public Type wellTyped(WhileStmt(c,stmt), ctx){
switch(<wellTyped(c,ctx)>) {
case <TBool()> : return TBool();
default : return TError();
}
}

public Type wellTyped(IfStmt(c,stmt), ctx){
switch(<wellTyped(c,ctx)>) {
case <TBool()> : return TBool();
default : return TError();
}
}


public Type wellTyped(IfElseStmt(c,stmtThen, stmtElse), ctx){
switch(<wellTyped(c,ctx)>) {
case <TBool()> : return TBool();
default : return TError();
}
}


public Type wellTyped(Print(e), ctx) {
switch(<wellTyped(e, ctx)>) {
case <TBool()>: return TBool();
case <TInt()>: return TInt();
default : return TError();
}
}

public Type wellTyped(Return(e), ctx) {
switch(<wellTyped(e, ctx)>) {
case <TBool()>: return TBool();
case <TInt()>: return TInt();
default : return TError();
}
}

public Type wellTyped(Assignment(n, e), ctx){
switch(<wellTyped(e, ctx)>) {
case <TBool()>: return TBool();
case <TInt()>: return TInt();
case <TUndef()>: return TUndef();
default : return TError();
}
}

//public Type wellTyped(VarDecl(v), ctx){
// switch(<wellTyped(v, ctx)>) {
// case <TBool()>: return TBool();
// case <TInt()>: return TInt();
// case <TUndef()>: return TUndef();
// default : return TError();
// }
//}



35 changes: 31 additions & 4 deletions sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import lang::oberon::Interpreter;
import lang::util::Stack;
import lang::oberon::ExecutionContext;

Variable var = variable("x",TInt());
Statement attrib1 = Assignment("x", IntValue(0));
Statement stmt = Print(VarRef("x"));
Expression exp = Lt(VarRef("x"),IntValue(10));
Statement attrib2 = Assignment("x",Invoke("inc", [VarRef("x")]));
Statement whileBlk = BlockStmt([stmt, attrib2]);
Statement whileStmt = WhileStmt(exp,whileBlk);
Statement mainBlock = BlockStmt([attrib1, whileStmt]);
Expression undefined = Undefined();



public Context emptyContext = context((), (), empty());

test bool testWellTypedAddExp() = TInt() == wellTyped(Add(IntValue(10), IntValue(7)), emptyContext);
Expand All @@ -24,10 +36,25 @@ test bool testWellTypedNotExp() = TBool() == wellTyped(Not(BoolValue(true)), emp

test bool testWellTypedGtExp() = TBool() == wellTyped(Gt(BoolValue(true), BoolValue(false)), emptyContext);

test bool testWellTypedLtExp() = TBool() == wellTyped(Or(BoolValue(true), BoolValue(false)), emptyContext);
test bool testWellTypedLtExp() = TBool() == wellTyped(Lt(BoolValue(true), BoolValue(false)), emptyContext);

test bool testWellTypedGoEqExp() = TBool() == wellTyped(GoEq(BoolValue(true), BoolValue(false)), emptyContext);

test bool testWellTypedLoEqExp() = TBool() == wellTyped(LoEq(BoolValue(true), BoolValue(false)), emptyContext);

test bool testWellTypedEqExp() = TBool() == wellTyped(Eq(BoolValue(true), BoolValue(false)), emptyContext);

test bool testWellTypedWhileStmt() = TBool() == wellTyped(WhileStmt(BoolValue(true),attrib1),emptyContext);

test bool testWellTypedIfStmt() = TBool() == wellTyped(IfStmt(BoolValue(true),attrib2),emptyContext);

test bool testWellTypedIfElseStmt() = TBool() == wellTyped(IfElseStmt(BoolValue(true),attrib1,attrib2),emptyContext);

test bool testWellTypedPrintStmt() = TInt() == wellTyped(Print(IntValue(10)),emptyContext);

test bool testWellTypedReturnStmt() = TInt() == wellTyped(Return(IntValue(10)),emptyContext);

test bool testWellTypedGoEqExp() = TBool() == wellTyped(Or(BoolValue(true), BoolValue(false)), emptyContext);
test bool testWellTypedAssignmentStmt() = TUndef() == wellTyped(Assignment("w",undefined),emptyContext);

test bool testWellTypedLoEqExp() = TBool() == wellTyped(Or(BoolValue(true), BoolValue(false)), emptyContext);
//test bool testWellTypedVarDeclStmt() = TUndef() == wellTyped(VarDecl(var),emptyContext);

test bool testWellTypedEqExp() = TBool() == wellTyped(Or(BoolValue(true), BoolValue(false)), emptyContext);

0 comments on commit 3fd89cd

Please sign in to comment.