v6 Parsing: type annotations and signatures

Lesson, slides, and applied problem sets.

View Slides

Lesson

v6 Parsing: type annotations and function signatures

New syntax

let x: number = 1;
fn add(a: number, b: number) -> number { return a + b; }

Type grammar

type       -> simple | fnType
simple     -> number | bool | string | nil
           | array<type>
           | map<type, type>
fnType     -> fn("typeList?") -> type

Parser changes

  • varDecl parses optional : type after name.
  • fnDecl parses param types and requires return type.
  • New AST nodes: Type tree.

AST additions

  • Stmt.TypeAnn *Type for variables.
  • Stmt.ParamTypes []*Type and Stmt.ReturnType *Type for functions.

Module Items