snirk-lang / protosnirk

The beginnings of a programming language
MIT License
1 stars 1 forks source link

Do not use `RefCell` in the AST #41

Open SnirkImmington opened 6 years ago

SnirkImmington commented 6 years ago

Currently (on the typeck branch), there are a few passes of the parsed AST which are done:

  1. Construction of AST

  2. Identify Pass

    • ASTIdentifier sets each expression Identifier's ScopedId (a kind of De Brujin Index) to match the binding it refers to (taking into account i.e. recursive functions and shadowing).
    • It will also set ScopedIds of type expressions with a table of Type ScopedId -> ConcreteType. This is the basis for type inference.
  3. Check pass

    • Type inference runs, accumulating equations into a TypeGraph, producing a more complete mapping of types of expressions which is given to the next passes.
  4. Lint Pass

    • A simple linter runs through the AST and accumulates basic linting errors.
  5. Compile pass

    • The ModuleCompiler constructs an LLVM Module using the AST and type information.

The "identification pass" is the only pass which modifies the AST: it causes nodes to have specific Ids to allow other passes to accumulate information about them.

Code in the visit module should be expanded to allow for a "mutating AST visitor."

SnirkImmington commented 5 years ago

I opened mutable-visitor to do this and after writing all the _mut methods the borrow checker complains wherever it can. The main problem is that an entire mode is mutably borrowed while its children are being visited or iterated over. This means that I can't even log the name of a function while iterating through its arguments.

I'm marking this as blocked on a better AST representation. If we used i.e. petgraph then we would be able to do iteration without borrowing the core data structure. If we build a CST before building an AST, we could possibly construct the initial AST with symbol IDs from the start.