Exploring the Compiler

Here’s version 0.1.0 of the creatively named cr-source-typer project:

It works on my machine, but buyer beware and all that :) If you try it and find issues, please let me know so I can improve it!

And another mini brain dump along the lines of the one that started this thread:

  • Running the semantic process has a cleanup property; setting this to true will cause it to expand the returned ASTNode to contain all required files and expanded macros. Setting it to false doesn’t.
  • After semantic has run, the program object has a types variable that’s a hash of the type name => Type instance representing that class / struct / whatever.
  • The types hash only contains the top level types - to get to ones within “namespaces” (or subclasses, etc.), a breadth-first-search expansion can be done, using each types’ own types variable to get the subtypes under it.
  • Similarly, the previously described defs and def_instances on the program object only contain the top level methods and its typed definitions. Use the Type#defs and Type#def_instances to get the methods for individual types (keeping in mind not every Type actually has or supports methods)
  • Static / class methods don’t exist on the Type directly (which contains instance level variables and methods), but instead on the *MetaclassType, which is a type specific to capturing class level information. Use type#metaclass to get a metaclass of a given type. This is the difference between String and String.class
  • *MetaclassType also use the defs and def_instances to store the class level methods (like new and allocate, by default).
5 Likes