Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created May 18, 2025 22:26
Show Gist options
  • Save Xliff/8e921aac99eb8496787438d76dd07a7e to your computer and use it in GitHub Desktop.
Save Xliff/8e921aac99eb8496787438d76dd07a7e to your computer and use it in GitHub Desktop.
Automatically Generating a List of `need` Statements from a List of CompUnits

I have a list of compunits that I place in an array, and it needs to generate sometihng like the following:

need GLib::Raw::Debug;
need GLib::Raw::Definitions;
need GLib::Raw::Enums;
need GLib::Raw::Exceptions;
need GLib::Raw::Object;
need GLib::Raw::Structs;
need GLib::Raw::Subs;
need GLib::Raw::Struct_Subs;
need GLib::Raw::Traits;
need GLib::Roles::Pointers;
need GLib::Roles::Implementor;

BEGIN {
  glib-re-export($_) for @GLib::Raw::Exports::glib-exports;
}

I have code that generates the RakuAST. It looks like this:

RakuAST::StatementList.new(
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("Pango::Raw::Definitions"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("Pango::Raw::Enums"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("Pango::Raw::Structs"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("Pango::Raw::Subs"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Debug"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Definitions"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Object"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Enums"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Exceptions"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Structs"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Subs"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Raw::Struct_Subs"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Roles::Pointers"),
    )
  ),
  RakuAST::Statement::Need.new(
    module-names => (
      RakuAST::Name.from-identifier("GLib::Roles::Implementor"),
    )
  ),
  RakuAST::Statement::Expression.new(
    expression => RakuAST::StatementPrefix::Phaser::Begin.new(
      RakuAST::Block.new(
        body => RakuAST::Blockoid.new(
          RakuAST::StatementList.new(
            RakuAST::Statement::Expression.new(
              expression    => RakuAST::Call::Name.new(
                name => RakuAST::Name.from-identifier("glib-re-export"),
                args => RakuAST::ArgList.new(
                  RakuAST::Var::Lexical.new("\$_")
                )
              ),
              loop-modifier => RakuAST::StatementModifier::For.new(
                RakuAST::ApplyListInfix.new(
                  infix    => RakuAST::Infix.new(","),
                  operands => (
                    RakuAST::ApplyPrefix.new(
                      prefix  => RakuAST::Prefix.new("|"),
                      operand => RakuAST::Var::Lexical.new("\@pango-exports")
                    ),
                    RakuAST::ApplyPrefix.new(
                      prefix  => RakuAST::Prefix.new("|"),
                      operand => RakuAST::Var::Lexical.new("\@glib-exports")
                    ),
                  )
                )
              )
            )
          )
        )
      )
    )
  )
)

However when I try to EVAL the above AST, I get the following error:

Unknown compilation input 'qast'

Can someone tell me what I'm missing? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment