Skip to content

Commit

Permalink
(UnB-CIC#37) create and test wellTyped for BlkStmt, WhileStmt, VarDec…
Browse files Browse the repository at this point in the history
…lStmt and fix AssignmentStmt
  • Loading branch information
waltim committed Nov 7, 2019
1 parent cf599d2 commit 0540d4a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions sample-code/oberon/src/lang/oberon/Interpreter.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
31 changes: 23 additions & 8 deletions sample-code/oberon/src/lang/oberon/TypeChecker.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 <false> : 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){
Expand Down Expand Up @@ -161,13 +164,25 @@ public bool wellTyped(Return(e), ctx) {
}

public bool wellTyped(Assignment(n, e), ctx){
switch(<typeOf(e, ctx)>) {
case <TBool()>: return true;
case <TInt()>: return true;
case <TUndef()>: return true;
switch(<typeOf(VarRef(n), ctx), typeOf(e, ctx)>) {
case <TInt(), TInt()> : return true;
case <TBool(), TBool()> : return true;
case <TUndef(), TUndef()>: return true;
default : return false;
}
}


public bool wellTyped(VarDecl(v), ctx) {
switch(<v.varType,typeOf(fromVar(v), ctx)>) {
case <TInt(), TInt()> : return true;
case <TBool(), TBool()> : return true;
case <TUndef(), TUndef()>: return true;
case <TInt(), TUndef()>: return true;
case <TBool(), TUndef()>: return true;
default : return false;
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
6 changes: 4 additions & 2 deletions sample-code/oberon/src/lang/oberon/test/TestTypeChecker.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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);



0 comments on commit 0540d4a

Please sign in to comment.