Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active June 7, 2021 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/edd9b31e11a887b9d3ded6a5c4b76cbf to your computer and use it in GitHub Desktop.
Save Xliff/edd9b31e11a887b9d3ded6a5c4b76cbf to your computer and use it in GitHub Desktop.
RakuAST PseudoCode of a method

So we have this Perl6 we are looking to convert to RakuAST:

  method setICalComponent(ICalComponentAncestry $_) {
    my $to-parent;

    $!icc = do {
      when ICalComponent {
        $to-parent = cast(ICalObject, $_);
        $_;
      }

      default {
        $to-parent = $_;
        cast(ICalComponent, $_);
      }
    }
    self.setICalObject($to-parent);
  }

Given that I have no idea what the actual node names are, nor their intended use¸ this is how I would envision the conversion would look like:

$set-ical-component := RakuAST::MethodCreation.new(
  'setICalComponent',
  RakuAST::ParameterList.new(
    RakuAST::Parameter.new( type => ICalConmponentAncestry, RakuAST::VariableReference.new('$_') )
  ),
  RakuAST::Block.new(
    RakuAST::StatementList.new(
      RakuAST::ScopedVariableDeclaration.new('$to-parent')
    ),
    RakuAST.Statement.new(
      RakuAST.VariableAssignment.new(
        RakuAST::VariableReference.new('$!icc'),
        RakuAST::DoBlock.new(
          RakuAST.Block.new(
            RakuAST::StatementList.new(
              RakuAST::WhenBlock(
                expression => RakuAST::TypeReference('ICalComponent'),
                RakuAST::Block.new(
                  RakuAST::StatementList(
                    RakuAST::VariableAssignment.new(
                      RakuAST::VariableRefernce.new('$to-parent'),
                      RAKUAST::CallRoutine.new(
                        'cast',
                        RakuAST::ParameterList.new(
                          # This, really should be a call to a function
                          # that returns either:
                          #   - The type name of the implementor
                          #   - The imlementor's RakuAST::TypeReference
                          RakuAST::TypeReference('ICalObject'),
                          RakuAST::VariableReference.new('$_')
                        ),
                      ),
                    ),
                    RakuAST::VariableReference('$_')
                  )
                )
              ),
              RakuAST::DefaultBlock.new(
                RakuAST::StatementList.new(
                  RakuAST::VariableAssignment.new(
                    RakuAST::VariableRefernce.new('$to-parent'),
                    RakuAST::VariableRefernce.new('$!');

                  ),
                  RAKUAST::CallRoutine.new(
                    'cast',
                    RakuAST::ParameterList.new(
                      RakuAST::TypeRefernce('ICalComponent'),
                      RakuAST::VariableReference.new('$_')
                    ),
                  ),
                )
              )
            )
          ),
        ),
      )
      RakuAST::CallMethod.new(
        'setICalObject',
          RakuAST::ParameterList.new(
          RakuAST::VariableReference.new('$to-parent')
        )
      )
    )
  )
)
@lizmat
Copy link

lizmat commented Jun 7, 2021

I see several cases of RakuAST.VariableAssignment, which I think you meant RakuAST::VariableAssignment ?

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