Skip to content

Commit

Permalink
at calls, need to check function body return value against specified …
Browse files Browse the repository at this point in the history
…type if any
  • Loading branch information
yinwang0 committed Feb 7, 2014
1 parent 48913b1 commit 65afb7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/main/java/org/yinwang/yin/ast/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,19 @@ public Value typecheck(Scope s) {
}
}

TypeChecker.self.callStack.add((FunType) fun);
Value actual = funtype.fun.body.typecheck(funScope);
TypeChecker.self.callStack.remove(fun);

Object retType = funtype.properties.lookupPropertyLocal(Constants.RETURN_ARROW, "type");
if (retType != null) {
if (retType instanceof Node) {
// evaluate the return type because it might be (typeof x)
return ((Node) retType).typecheck(funScope);
Value expected = ((Node) retType).typecheck(funScope);
if (!Type.subtype(actual, expected)) {
_.abort(this, "type error. expected: " + expected + ", actual: " + actual);
}
return actual;
} else {
_.abort("illegal return type: " + retType);
return null;
Expand All @@ -199,10 +207,7 @@ public Value typecheck(Scope s) {
_.abort(func, "You must specify return type for recursive functions: " + func);
return null;
} else {
TypeChecker.self.callStack.add((FunType) fun);
Value ret = funtype.fun.body.typecheck(funScope);
TypeChecker.self.callStack.remove(fun);
return ret;
return actual;
}
}
} else if (fun instanceof RecordType) {
Expand Down
10 changes: 5 additions & 5 deletions tests/typecheck1.elt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
-- true
-- (even (- x 1))))))

-- (record A
-- [x Int]
-- [y String])
(record A
[x Int]
[y String])

-- (define o (A :x 1 :y "hi"))

-- (+ 1 o.x)


(define f (fun ([x Any] [-> x]) x))
(f 1)
(define f (fun ([x Any]) x))
(f "hi")

0 comments on commit 65afb7c

Please sign in to comment.