package RakuAST { }
The RakuAST
package serves as a common namespace for all classes that provide RakuAST functionality.
Support for RakuAST
functionality is available in language version 6.e+
and was added in Rakudo compiler release 2023.02. In earlier language versions it is only available when specifying:
use experimental :rakuast;
Classes§
Documentation of RakuAST
classes is ongoing, while the development of RakuAST is still ongoing as well.
Useful methods§
To make developing in RakuAST easier, several helper methods are available.
The AST
method can be called on a string containing Raku source code, and it will return the RakuAST object tree needed to create the AST of the given source code.
$ raku -e 'say Q/"Hello World"/.AST.^name' RakuAST::StatementList
And the gist of such a RakuAST object tree shows the Raku source code to create such a tree:
$ raku -e 'say Q/"Hello World"/.AST' RakuAST::StatementList.new( RakuAST::Statement::Expression.new( expression => RakuAST::Call::Name.new( name => RakuAST::Name.from-identifier("say"), args => RakuAST::ArgList.new( RakuAST::QuotedString.new( segments => ( RakuAST::StrLiteral.new("Hello World"), ) ) ) ) ) )
This can be used as a base to create your own RakuAST object trees.
It is also possible to create a Raku source representation of a RakuAST object tree, by calling the .DEPARSE
method on it:
$ raku -e 'say Q/"Hello World"/.AST.DEPARSE' say("Hello World")
Please note that the .AST
method depends on the Raku grammar, which may not yet support all of the Raku Programming Language features that you want to use. And vice-versa: the .DEPARSE
method may not be able to create a valid, executable Raku source representation, especially if the RakuAST object tree has been built "manually".