FP::AST::Perl -- abstract syntax tree for representing Perl code
use FP::AST::Perl ":all"; use FP::List; use FP::Equal ":all"; use FP::Ops qw(regex_substitute); is Get(ScalarVar "foo")->string, '$foo'; is Get(ArrayVar "foo")->string, '@foo'; is Get(HashVar "foo::bar")->string, '%foo::bar'; is Ref(HashVar "foo::bar")->string, '\%foo::bar'; my $codefoo = CodeVar("foo"); my $arrayfoo = ArrayVar("foo"); my $lexfoo = ScalarVar("foo"); is App(Get($codefoo), list())->string, '&foo()'; is AppP(Get($codefoo), list())->string, 'foo()'; is AppP(Get($lexfoo), list(Ref($codefoo)))->string, '$foo->(\&foo)'; is eval { App(Get(HashVar "foo"), list())->string } || regex_substitute(sub{s/ at .*//s}, $@), 'HASH var can\'t be called'; is AppP(Get($lexfoo), list(Get($lexfoo), Get($arrayfoo), Ref($arrayfoo)))->string, '$foo->($foo, @foo, \@foo)'; is AppP(Get($codefoo), list(Get(ScalarVar 'foo'), Literal(Number 123)))->string, 'foo($foo, 123)'; is AppP(Get($codefoo), list(Get($lexfoo), Literal(String 123)))->string, 'foo($foo, \'123\')'; # Semicolons are like a compile time (AST level) operator: is Semicolon(Get(ScalarVar "a"), Get(ScalarVar "b"))->string, '$a; $b'; # to end with a semicolon you could use: is Semicolon(Get(ScalarVar "a"), Noop)->string, '$a; '; # The n-ary `semicolons` function builds a Semicolon chain for # you (right-associated): is_equal semicolons(), Noop; is_equal semicolons("x"), "x"; # ^ no `Semicolon` instantiation thus no type failure because # of the string my $sems = semicolons(map {Get ScalarVar $_} qw(a b c)); is_equal $sems, Semicolon(Get(ScalarVar('a')), Semicolon(Get(ScalarVar('b')), Get(ScalarVar('c')))); is $sems->string, '$a; $b; $c'; is commas(map {Get ScalarVar $_} qw(a b c))->string, '$a, $b, $c'; is Let(list(ScalarVar("foo"), ScalarVar("bar")), Get(ArrayVar "baz"), semicolons( AppP(Get(CodeVar "print"), list Literal String "Hello"), Get(ScalarVar("bar"))))->string, 'my ($foo, $bar) = @baz; print(\'Hello\'); $bar'; # Yes, how should print, map etc. be handled?
This is not a parser, and hence should be outside the scope of the "can only parse Perl at runtime" issue.
The longer term aim is to support all of Perl, and to support conversion to and maybe from an op tree.
Implements: FP::Abstract::Pure, FP::Abstract::Show, FP::Abstract::Equal.
This is alpha software! Read the status section in the package README or on the website.
To install FunctionalPerl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm FunctionalPerl
CPAN shell
perl -MCPAN -e shell install FunctionalPerl
For more information on module installation, please visit the detailed CPAN module installation guide.