forked from UnB-CIC/sle-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(37) fix errors in interpreter functions of eval, implemented and tes…
…ted weelTyped and typeOf functions
- Loading branch information
Showing
4 changed files
with
114 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,173 @@ | ||
module lang::oberon::TypeChecker | ||
|
||
import lang::oberon::AST; | ||
import lang::util::Stack; | ||
import lang::oberon::ExecutionContext; | ||
import List; | ||
import Map; | ||
|
||
//public Type wellTyped(Expression exp, Context ctx) | ||
public Type typeOf(IntValue(v), ctx) = TInt(); | ||
|
||
public Type wellTyped(IntValue(v), ctx) = TInt(); | ||
public Type typeOf(BoolValue(v), ctx) = TBool(); | ||
|
||
public Type wellTyped(BoolValue(v), ctx) = TBool(); | ||
public Type typeOf(Undefined(), ctx) = TUndef(); | ||
|
||
public Type wellTyped(Undefined(), ctx) = TUndef(); | ||
|
||
public Type wellTyped(Add(lhs,rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Add(lhs,rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TInt(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Sub(lhs,rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Sub(lhs,rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TInt(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Mult(lhs,rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Mult(lhs,rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TInt(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Div(lhs,rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Div(lhs,rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TInt(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(And(lhs,rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(And(lhs,rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Or(lhs,rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Or(lhs,rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Not(exp), ctx) { | ||
switch(<wellTyped(exp,ctx)>) { | ||
public Type typeOf(Not(exp), ctx) { | ||
switch(<typeOf(exp,ctx)>) { | ||
case <TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Gt(lhs, rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Gt(lhs, rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TBool(); | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Lt(lhs, rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Lt(lhs, rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TBool(); | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(GoEq(lhs, rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(GoEq(lhs, rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TBool(); | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(LoEq(lhs, rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(LoEq(lhs, rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TBool(); | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type wellTyped(Eq(lhs, rhs), ctx) { | ||
switch(<wellTyped(lhs, ctx), wellTyped(rhs, ctx)>) { | ||
public Type typeOf(Eq(lhs, rhs), ctx) { | ||
switch(<typeOf(lhs, ctx), typeOf(rhs, ctx)>) { | ||
case <TInt(), TInt()> : return TBool(); | ||
case <TBool(), TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
} | ||
|
||
public Type typeOf(VarRef(n), ctx) { | ||
if(n in top(ctx.heap)) { | ||
return typeOf(top(ctx.heap)[n],ctx); | ||
} | ||
else if(n in ctx.global) { | ||
return typeOf(ctx.global[n],ctx); | ||
} | ||
return TError(); | ||
} | ||
|
||
|
||
//Statments TypeChecker | ||
|
||
public Type wellTyped(WhileStmt(c,stmt), ctx){ | ||
switch(<wellTyped(c,ctx)>) { | ||
case <TBool()> : return TBool(); | ||
default : return TError(); | ||
public bool wellTyped(WhileStmt(c,stmt), ctx){ | ||
switch(<typeOf(c,ctx)>) { | ||
case <TBool()> : return wellTyped(stmt,ctx); | ||
default : return false; | ||
} | ||
} | ||
|
||
public Type wellTyped(IfStmt(c,stmt), ctx){ | ||
switch(<wellTyped(c,ctx)>) { | ||
case <TBool()> : return TBool(); | ||
default : return TError(); | ||
} | ||
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; | ||
} | ||
} | ||
|
||
public bool wellTyped(IfStmt(c,stmt), ctx){ | ||
switch(<typeOf(c,ctx)>) { | ||
case <TBool()> : return true; | ||
default : return false; | ||
} | ||
} | ||
|
||
public Type wellTyped(IfElseStmt(c,stmtThen, stmtElse), ctx){ | ||
switch(<wellTyped(c,ctx)>) { | ||
case <TBool()> : return TBool(); | ||
default : return TError(); | ||
public bool wellTyped(IfElseStmt(c,stmtThen, stmtElse), ctx){ | ||
switch(<typeOf(c,ctx)>) { | ||
case <TBool()> : return true; | ||
default : return false; | ||
} | ||
} | ||
|
||
|
||
public Type wellTyped(Print(e), ctx) { | ||
switch(<wellTyped(e, ctx)>) { | ||
case <TBool()>: return TBool(); | ||
case <TInt()>: return TInt(); | ||
default : return TError(); | ||
public bool wellTyped(Print(e), ctx) { | ||
switch(<typeOf(e, ctx)>) { | ||
case <TBool()>: return true; | ||
case <TInt()>: return true; | ||
default : return false; | ||
} | ||
} | ||
|
||
public Type wellTyped(Return(e), ctx) { | ||
switch(<wellTyped(e, ctx)>) { | ||
case <TBool()>: return TBool(); | ||
case <TInt()>: return TInt(); | ||
default : return TError(); | ||
public bool wellTyped(Return(e), ctx) { | ||
switch(<typeOf(e, ctx)>) { | ||
case <TBool()>: return true; | ||
case <TInt()>: return true; | ||
default : return false; | ||
} | ||
} | ||
|
||
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 bool wellTyped(Assignment(n, e), ctx){ | ||
switch(<typeOf(e, ctx)>) { | ||
case <TBool()>: return true; | ||
case <TInt()>: return true; | ||
case <TUndef()>: return true; | ||
default : return false; | ||
} | ||
} | ||
|
||
//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(); | ||
// } | ||
//} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters