Skip to content

Commit

Permalink
added spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 17, 2024
1 parent cb12a4e commit cd88e3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/typecheck/typecheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ test("infer a variable not present in the context", () => {
});
});

test("typechecking previously defined vars", () => {
const [types, errors] = tc(`
let x = 42
let y = x
`);

expect(errors).toEqual([]);
expect(types).toEqual({
x: "Int",
y: "Int",
});
});

function tc(src: string, context: Context = {}) {
const parsedProgram = unsafeParse(src);
const [typed, errors] = typecheck(parsedProgram, context);
Expand Down
4 changes: 2 additions & 2 deletions src/typecheck/typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export function typecheck<T = {}>(
): [Program<T & TypeMeta>, TypeError<Expr<T & TypeMeta>>[]] {
TVar.resetId();
const errors: TypeError<Expr<T & TypeMeta>>[] = [];
const context: Context = { ...initialContext };
let context: Context = { ...initialContext };

const typedStatements = ast.statements.map<Statement<T & TypeMeta>>(
(decl) => {
const annotated = annotateExpr(decl.value);
errors.push(...typecheckAnnotatedExpr(annotated, context));
// TODO save bindings in context
context[decl.binding.name] = annotated.$.asType();
return {
...decl,
binding: { ...decl.binding, $: annotated.$ },
Expand Down

0 comments on commit cd88e3d

Please sign in to comment.