Skip to content

Commit

Permalink
Partially implemented typechecking for calls
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTimms committed Jul 21, 2020
1 parent a9d4ab4 commit 2fadd5f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/typechecker/TypeChecker.ts
Expand Up @@ -36,6 +36,8 @@ import AnyType from "./AnyType";
import TypeExpr, { TypeExprVisitor, VariableTypeExpr } from "../TypeExpr";
import { zip } from "../helpers";
import CallableType from "./CallableType";
import { type } from "os";
import { cachedDataVersionTag } from "v8";

class LoxError {
constructor(
Expand Down Expand Up @@ -390,9 +392,23 @@ implements ExprVisitor<Type>, StmtVisitor<void>, TypeExprVisitor<Type> {
}

visitCallExpr(expr: CallExpr): Type {
throw "Not Implemented Yet";
// this.resolve(expr.callee);
// this.resolveAll(expr.args);
const calleeType = this.checkExpr(expr.callee);

if (calleeType.tag !== "CALLABLE") {
if (calleeType !== types.PreviousTypeError) {
this.error(`Type '${calleeType}' is not callable.`);
}
for (const arg of expr.args) {
this.checkExpr(arg);
}
return types.PreviousTypeError;
}

// TODO check arity
// TODO check params

return calleeType.returns ?? types.Nil;

}

visitGetExpr(expr: GetExpr): Type {
Expand Down
7 changes: 7 additions & 0 deletions tests/functions/callPreviousTypeError.lox
@@ -0,0 +1,7 @@
// Because the callee is not defined, we cannot infer the type of
// func. This means the subsequent call cannot be meaningfully checked,
// so it should not show any errors.
var func = notDefined();
func(1, 2, 3);
-- ERROR --
[line 4] Error at 'notDefined': The name 'notDefined' is not defined.
4 changes: 4 additions & 0 deletions tests/functions/callUncallable.lox
@@ -0,0 +1,4 @@
var x = "hello";
x();
-- ERROR --
[line 0] Error: Type 'String' is not callable.
6 changes: 6 additions & 0 deletions tests/functions/callUndefined.lox
@@ -0,0 +1,6 @@
// The typechecker should only complain about foo being undefined, and
// not further complain that it isn't callable or that the parameter types
// are incorrect.
foo(1, 2, 3);
-- ERROR --
[line 4] Error at 'foo': The name 'foo' is not defined.
4 changes: 2 additions & 2 deletions tests/functions/closure.lox
@@ -1,6 +1,6 @@
fun makeCounter(): Number {
fun makeCounter(): fun(): Number {
var i = 0;
fun count() {
fun count(): Number {
i = i + 1;
return i;
}
Expand Down
1 change: 0 additions & 1 deletion tests/functions/fibonacci.lox
Expand Up @@ -2,7 +2,6 @@ fun fibonacci(n: Number): Number {
if (n <= 1) return n;
return fibonacci(n - 2) + fibonacci(n - 1);
}

for (var i = 0; i < 6; i = i + 1) {
print fibonacci(i);
}
Expand Down

0 comments on commit 2fadd5f

Please sign in to comment.