Rand Stats

RakuAST::Utils

zef:lizmat

Actions Status Actions Status Actions Status

NAME

RakuAST::Utils - provide basic RakuAST utilities

SYNOPSIS

use RakuAST::Utils;

say NameAST("Foo::Bar");

say TypeAST( Array[Int] );

sub foo(Int $a, Str $b) { }
say ParameterAST( &foo.signature.params[0] );

say SignatureAST( &foo.signature );

DESCRIPTION

The RakuAST::Utils provides a number of subroutines that make the programmatical creation of code using RakuAST a lot easier.

SUBROUTINES

NameAST

say NameAST( "Foo" );
say NameAST( "Foo::Bar" );
RakuAST::Name.from-identifier("Foo")
RakuAST::Name.from-identifier-parts("Foo","Bar")

The TypeAST subroutine takes a package-like string and returns the RakUAST representation of that string.

TypeAST

say TypeAST( Array[Int] );
RakuAST::Type::Parameterized.new(
  base-type => RakuAST::Type::Simple.new(
    RakuAST::Name.from-identifier("Array")
  ),
  args      => RakuAST::ArgList.new(
    RakuAST::Type::Simple.new(
      RakuAST::Name.from-identifier("Int")
    )
  )
)

The TypeAST subroutine takes a type object and returns the RakUAST representation of that type object.

ParameterAST

sub foo(Int $a, Str $b --> Str:D) { }
say ParameterAST( &foo.signature.params[0] );
RakuAST::Parameter.new(
  type   => RakuAST::Type::Simple.new(
    RakuAST::Name.from-identifier("Int")
  ),
  target => RakuAST::ParameterTarget::Var.new(
    name => "\$a"
  )
)

The ParameterAST subroutine takes a Parameter object and returns the RakUAST representation of that object.

SignatureAST

sub foo(Int $a, Str $b --> Str:D) { }
say SignatureAST( &foo.signature );
RakuAST::Signature.new(
  parameters => (
    RakuAST::Parameter.new(
      type   => RakuAST::Type::Simple.new(
        RakuAST::Name.from-identifier("Int")
      ),
      target => RakuAST::ParameterTarget::Var.new(
        name => "\$a"
      )
    ),
    RakuAST::Parameter.new(
      type   => RakuAST::Type::Simple.new(
        RakuAST::Name.from-identifier("Str")
      ),
      target => RakuAST::ParameterTarget::Var.new(
        name => "\$b"
      )
    ),
  ),
  returns    => RakuAST::Type::Simple.new(
    RakuAST::Name.from-identifier("Str:D")
  )
)

The SignatureAST subroutine takes a Signature object and returns the RakUAST representation of that object.

ACKNOWLEDGEMENTS

The initial version of this distribution was taken from the work on .assuming, which was sponsored by the Raku Foundation.

AUTHOR

Elizabeth Mattijsen liz@raku.rocks

Source can be located at: https://github.com/lizmat/RakuAST-Utils . Comments and Pull Requests are welcome.

If you like this module, or what I'm doing more generally, committing to a small sponsorship would mean a great deal to me!

COPYRIGHT AND LICENSE

Copyright 2025 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.