Search code examples
macrosmetaprogrammingabstract-syntax-treequotationsnemerle

How do I use quasi-quotations to get an AST in Nemerle?


I try to get the AST of a simple code-snippet in Nemerle, using Nemerle's quasi-quotes.

This is the code I tried:

def ast() : void {
  System.Console.WriteLine(<["Test"]>)
}

I ran it on IdeOne (ncc 0.9.3) and I got this error:

prog.nem:2:30:2:36: error: unbound name `Literal.String'
prog.nem:2:30:2:36: error: unbound name `PExpr.Literal'

How can I solve these issues?


Solution

  • You just need to add Nemerle.Compiler.dll as a reference to your project. Also, some of the more complex quasi-quotes will only work in macros.

    using Nemerle.Compiler;
    using System.Console;
    
    macro Test()
    {
        def x = <[ while (true) WriteLine(4) ]>.ToString();
        <[ WriteLine($x) ]>
    }