DylanSp / wheel-lang

A small toy imperative language (with some OOP features) for demonstrating and practicing language design/implementation.
MIT License
14 stars 0 forks source link

Add static types #14

Closed DylanSp closed 4 years ago

DylanSp commented 4 years ago

Syntax similar to Typescript:

x: number = 3;
function add(m: number, y: number): number {
}

But how to do syntax for higher-order functions?

DylanSp commented 4 years ago

Type check pass should validate, not parse: returns Either<TypeError, Type> where the Right case represents the type of the top-level function. Evaluation still takes an AST from the parser.

DylanSp commented 4 years ago

Roughly following TS, could use

function makeAdder(x: number): (number => number) {
  function adder(y: number): number {
    return x + y;
  }

  return adder;
}

function doubleApply(func: (number => number), x: number): number {
  return func(func(x));
}
DylanSp commented 4 years ago

Do I want to allow (parametric) polymorphism?

DylanSp commented 4 years ago

When adding type syntax, convert existing parser tests to use .toMatchObject(), so existing tests won't break as I add type-related properties.

DylanSp commented 4 years ago

Lack of separate declaration and assignment could cause problems.

x = 1;
x = x + 1;

Should line 2 have a type annotation, even if it's reusing already-declared variable?

DylanSp commented 4 years ago

Instead of adding syntax: type inference everything? Would probably need to drop polymorphism, but I can live with that.

DylanSp commented 4 years ago

If I go the inference route - how to handle ==, /= (and print if I add that)? They're ad-hoc polymorphic.

DylanSp commented 4 years ago

Rather than full Hindley-Milner inference - treat it as a dataflow/DAG, and run the type checks one way. If I require initialization of variables to a value, which sets the variable's type permanently, I should just be able to work forwards through the DAG of statements/expressions.

DylanSp commented 4 years ago

Three options so far.

Option 1: usual C-style typing, explicit type annotations on everything.

Option 2: Hindley-Milner inference

Option 3: Forward-only dataflow analysis

DylanSp commented 4 years ago

I'm thinking that I'll leave wheel-lang dynamically typed, especially with exploring prototypical inheritance as syntax sugar.