Created an initial pull request adding the source-typer tool to the compiler created here: Add Source Code Typing Tool by Vici37 · Pull Request #15211 · crystal-lang/crystal · GitHub
Some other thoughts / comments that aren’t directly related to this tool, but discovered along the way of building it:
- An implicit
require "prelude"
gets inserted into the beginning of all crystal programs, and will either load this file, or a different crystal file if the--prelude <new-prelude>
build option is provided. - Prelude is responsible for “filling out” all of the methods and behaviors of the base types of the language (such as the
+
operator for Int32). - Prelude isn’t cheap - when compiling a
puts "hello world!"
crystal file, running the semantic on theprelude
takes about 1.5 seconds out of the 2 seconds total for building (my computer is a bit of a potato) - When running
program.semantic
, it’s typical (traditional?) to put all parsed (your file) and constructed (require "prelude"
) ASTNodes into a single expression and then run semantic on that in a single pass - I don’t think this is a required operation, at least I seemed to be able to run
semantic
on therequire "prelude"
and then runsemantic
on whatever the entrypoint file might be, without compiler / semantic errors being thrown (didn’t test the codegen, admittedly) - If the
Program
object could be serialized, then serializing whatever the result is of runningsemantic
on prelude and packaging that into the compiler itself could potentially seriously shorten compile times for smaller programs - A potential POC for this could be with the crystal playground, where a
semantic
on prelude could be pre-run while the user is entering in crystal code. When the user clicks “run” or whatever, it should almost immediately return results (and in the background could preload another new Program withsemantic
running on prelude)
Might be the next random weekend project to try :) I’m having a lot of fun digging into the internals of the compiler! Well done Crystal Team!