From 3fd89cd2a64e31e2e5dc35daa387adffa28536c6 Mon Sep 17 00:00:00 2001 From: waltim Date: Sat, 2 Nov 2019 12:11:09 -0300 Subject: [PATCH] (#37) implemented more typecheckers functions for statments and expressions --- .../oberon/src/lang/oberon/TypeChecker.rsc | 63 ++++++++++++++++++- .../src/lang/oberon/test/TestTypeChecker.rsc | 35 +++++++++-- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/sample-code/oberon/src/lang/oberon/TypeChecker.rsc b/sample-code/oberon/src/lang/oberon/TypeChecker.rsc index 0fe13c9..696caf0 100644 --- a/sample-code/oberon/src/lang/oberon/TypeChecker.rsc +++ b/sample-code/oberon/src/lang/oberon/TypeChecker.rsc @@ -91,7 +91,7 @@ public Type wellTyped(LoEq(lhs, rhs), ctx) { } } -public Type wellTyped(LoEq(lhs, rhs), ctx) { +public Type wellTyped(Eq(lhs, rhs), ctx) { switch() { case : return TBool(); case : return TBool(); @@ -99,3 +99,64 @@ public Type wellTyped(LoEq(lhs, rhs), ctx) { } } +//Statments TypeChecker + +public Type wellTyped(WhileStmt(c,stmt), ctx){ + switch() { + case : return TBool(); + default : return TError(); + } +} + +public Type wellTyped(IfStmt(c,stmt), ctx){ + switch() { + case : return TBool(); + default : return TError(); + } +} + + +public Type wellTyped(IfElseStmt(c,stmtThen, stmtElse), ctx){ + switch() { + case : return TBool(); + default : return TError(); + } +} + + +public Type wellTyped(Print(e), ctx) { + switch() { + case : return TBool(); + case : return TInt(); + default : return TError(); + } +} + +public Type wellTyped(Return(e), ctx) { + switch() { + case : return TBool(); + case : return TInt(); + default : return TError(); + } +} + +public Type wellTyped(Assignment(n, e), ctx){ + switch() { + case : return TBool(); + case : return TInt(); + case : return TUndef(); + default : return TError(); + } +} + +//public Type wellTyped(VarDecl(v), ctx){ +// switch() { +// case : return TBool(); +// case : return TInt(); +// case : return TUndef(); +// default : return TError(); +// } +//} + + + diff --git a/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc b/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc index d4a20b9..315eb10 100644 --- a/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc +++ b/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc @@ -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); @@ -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); \ No newline at end of file