gracelang / minigrace

Self-hosting compiler for the Grace programming language
39 stars 22 forks source link

Type declarations installed too late #273

Closed apblack closed 5 years ago

apblack commented 5 years ago

In gradualTypesND, the following declarations appear at the module level:

type MethodType = interface {
    name → String
    ...
}

type ObjectType = {
    // the type of an expression as a collection of method types
    methods → Set⟦MethodType⟧
    ...
}

def scope: Scope is public = object {
    // keep track of each kind of expression separately
    def variables is public = stackOfKind ("variable")
    def methods is public = stackOfKind ⟦MethodType⟧("method")
    def types is public = stackOfKind ⟦ObjectType⟧("type")
    ...
}

If written in the order above, all is fine. However, if the type declarations for MethodType and ObjectType are written after the def scope ..., then the initialization of scope fails. The method MethodType is invoked on the module object before to has been installed, and therefor fails.

I'm not clear why this happens. The initialization of scope should take place as part of the module initialization, which should not occur until after the type methods have been installed.

apblack commented 5 years ago

The problem is that modules are treated specially. There is no $build function, because it's not possible to inherit from a module. Instead, the code for a module is compiled by first installing all the methods, and then running the executable code.

Whether an AST node defines a method or not, and is executable or not, is specified by predicates on the various nodes. A typeDecNode should now claim to be non-executable, rather than an executable node, and should be installed along with the methods. Then everything should happen in the correct order.

apblack commented 5 years ago

Fixed in commit b188d81

apblack commented 5 years ago

Test added in commit 38758e4