in reply to rough start of an axml compiler
You say in your "offtopic epiphony" that you believe
to be unrepresentable in any kind of data structure, perl or otherwise. Here's a simple solution:<<a>b</a>>c</<a>b</a>>
The definition of the action to be performed on data 'c' is postponed until operation 'a' is performed on data 'b'.my @nodes = ( bless( { 'data' => 'c', 'tag' => bless( { 'data' => 'b', 'tag' => 'a' }, 'Node' ) }, 'Node' ) );
I think you wrote a parser already, so I'm sure you can adapt it to produce a structure like above. Once you have the structure, generating the output is also straightforward:
If you run it (say for map { $_->as_text } @nodes), you'll see that instead of sub b being called, super_b is called.package Node; use Moose; has [ qw<data tag> ] => ( is => 'rw', isa => 'Any' ); sub as_text { my ($self) = shift; my $tag = $self->tag; my $tag_processing_method = ref $tag ? $tag->as_text : $tag; return $self->$tag_processing_method( $self->data ); } # Tag Processing Methods here: sub a { "super_$_[1]" } # prepend "super_" sub b { "b_$_[1]" } # prepend "b_" sub super_b { "B_$_[1]" } # prepend "B_"
I'd be very inclined to add string overloading to the Node package so:
which could make the calls even simpler, (with a possible cost to debugging and maintainability). as_text becomesuse overload q{""} => 'as_text', fallback => 1, ;
and generating output once you have your @nodes array is simply stringification. print @nodes; update fixed some typossub as_text { my ($self) = shift; my $processing_method = $self->tag; return $self->$processing_method( $self->data ); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: rough start of an axml compiler
by Logicus (Initiate) on Aug 01, 2011 at 19:29 UTC | |
Re^2: rough start of an axml compiler
by Logicus (Initiate) on Aug 02, 2011 at 12:40 UTC | |
by Boldra (Deacon) on Aug 02, 2011 at 13:06 UTC | |
by Logicus (Initiate) on Aug 02, 2011 at 15:03 UTC | |
by Logicus (Initiate) on Aug 02, 2011 at 18:13 UTC | |
Re^2: rough start of an axml compiler
by Anonymous Monk on Aug 02, 2011 at 02:34 UTC | |
by Boldra (Deacon) on Aug 02, 2011 at 13:24 UTC | |
by Anonymous Monk on Aug 02, 2011 at 13:46 UTC |
In Section
Seekers of Perl Wisdom