Search code examples
ceylon

How to feed ancompilation unit obtained from ceylon.ast to the Ceylon compiler?


The following example found in the ceylon/ceylon.ast project shows how to construct some AST programmatically and have it typechecked by the Ceylon typechecker (com.redhat.ceylon.compiler.typechecker.TypeChecker):

https://github.com/ceylon/ceylon.ast/blob/master/source/test/ceylon/ast/samples/turingMachine/powerOfTwo.ceylon

What would then subsequently be the recommended method to be called with the typed syntax tree to compile the typed syntax tree - considering that compilation units are normally given as files not as typed syntax trees (from the ceylon.ast project) to the typechecker?

This question is related to this one: The Ceylon Typechecker: How to obtain the typed syntax tree?.


Solution

  • Generating an AST and then compiling it apart from the context of a real project doesn't seem to be a viable option. The compiler requires more information than an AST alone provides and providing the compiler with default or fake information (e.g. file names) is still a complicated task.

    On the other hand, using the ASTs of real compilation units in the context of a real project as a sort of a macro system doesn't make sense since ceylon programs may have side effects and transformation has to take that into account. Additionally, transformation rules should be terminating and confluent and should be expressed in form that makes maintaining them as easy as possible as opposed to procedural transformers. Ceylon's switch expression are not the right tool for it. The worst case would be a procedural Ast transformer that transforms procedural a la groovy's AST transformers.

    An exeption would be the transformation of declarative treelike structures in ceylon. And that is exactly what I was thinking of in my question: Reading in data (not necessarily in ceylon syntax) and making them available to evaluation or optionally compilation.