From 0540d4aea777a0906eee039be5d213a3a3aececb Mon Sep 17 00:00:00 2001 From: waltim Date: Wed, 6 Nov 2019 21:52:42 -0300 Subject: [PATCH] (#37) create and test wellTyped for BlkStmt, WhileStmt, VarDeclStmt and fix AssignmentStmt --- .../oberon/src/lang/oberon/Interpreter.rsc | 4 +-- .../oberon/src/lang/oberon/TypeChecker.rsc | 31 ++++++++++++++----- .../src/lang/oberon/test/TestInterpreter.rsc | 2 +- .../src/lang/oberon/test/TestTypeChecker.rsc | 6 ++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/sample-code/oberon/src/lang/oberon/Interpreter.rsc b/sample-code/oberon/src/lang/oberon/Interpreter.rsc index d4e9533..e535646 100644 --- a/sample-code/oberon/src/lang/oberon/Interpreter.rsc +++ b/sample-code/oberon/src/lang/oberon/Interpreter.rsc @@ -10,9 +10,9 @@ import lang::util::Stack; import lang::oberon::ExecutionContext; -private Expression fromVar(Variable v) { +public Expression fromVar(Variable v) { switch(v) { - case variableInit(v, _, val) : return val; + case variableInit(n, _, val) : return val; default: return Undefined(); } } diff --git a/sample-code/oberon/src/lang/oberon/TypeChecker.rsc b/sample-code/oberon/src/lang/oberon/TypeChecker.rsc index 709c9dd..be783c5 100644 --- a/sample-code/oberon/src/lang/oberon/TypeChecker.rsc +++ b/sample-code/oberon/src/lang/oberon/TypeChecker.rsc @@ -3,8 +3,10 @@ module lang::oberon::TypeChecker import lang::oberon::AST; import lang::util::Stack; import lang::oberon::ExecutionContext; +import lang::oberon::Interpreter; import List; import Map; +import IO; public Type typeOf(IntValue(v), ctx) = TInt(); @@ -122,11 +124,12 @@ public bool wellTyped(WhileStmt(c,stmt), ctx){ } public bool wellTyped(BlockStmt(list[Statement] stmts), ctx) { - m = toList((s : wellTyped(s,ctx) | s <- stmts)); - switch(m){ - case : return false; - default : return true; + m = [wellTyped(s,ctx) | s <- stmts]; + println(m); + if(false in m){ + return false; } + return true; } public bool wellTyped(IfStmt(c,stmt), ctx){ @@ -161,13 +164,25 @@ public bool wellTyped(Return(e), ctx) { } public bool wellTyped(Assignment(n, e), ctx){ - switch() { - case : return true; - case : return true; - case : return true; + switch() { + case : return true; + case : return true; + case : return true; default : return false; } } +public bool wellTyped(VarDecl(v), ctx) { + switch() { + case : return true; + case : return true; + case : return true; + case : return true; + case : return true; + default : return false; + } +} + + diff --git a/sample-code/oberon/src/lang/oberon/test/TestInterpreter.rsc b/sample-code/oberon/src/lang/oberon/test/TestInterpreter.rsc index a354b38..0fe1904 100644 --- a/sample-code/oberon/src/lang/oberon/test/TestInterpreter.rsc +++ b/sample-code/oberon/src/lang/oberon/test/TestInterpreter.rsc @@ -20,7 +20,7 @@ Statement mainBlock = BlockStmt([attrib1, whileStmt]); Expression returnExp = Add(VarRef("z"), IntValue(1)); Statement retStmt = Return(returnExp); -FDecl f = FDecl("inc", [Parameter("z", TInt())], retStmt); +FDecl f = FDecl(TInt(),"inc", [Parameter("z", TInt())], retStmt); public Context emptyContext = context((), (), empty()); diff --git a/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc b/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc index 9e1b891..9a9b8c6 100644 --- a/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc +++ b/sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc @@ -59,9 +59,11 @@ test bool testWellTypedPrintStmt() = true == wellTyped(Print(IntValue(10)),empty test bool testWellTypedReturnStmt() = true == wellTyped(Return(IntValue(10)),emptyContext); -test bool testWellTypedAssignmentStmt() = true == wellTyped(Assignment("w",undefined),emptyContext); +test bool testWellTypedAssignmentStmt() = true == wellTyped(attrib1,testContext); -test bool testWellTypedBlockStmt() = true == wellTyped(BlockStmt([stmt, attrib1]),testContext); +test bool testWellTypedBlockStmt() = false == wellTyped(BlockStmt([stmt, Assignment("x", BoolValue(true))]),testContext); + +test bool testWellTypedVarDeclStmt() = true == wellTyped(VarDecl(var),emptyContext);