| 1 | package Pegex::Forth; | ||||||||||
| 2 | 4 | our $VERSION = '0.15'; | |||||||||
| 3 | |||||||||||
| 4 | 4 | ▶ | use Pegex::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use Pegex::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | has 'args' => []; | |||||||||
| 8 | |||||||||||
| 9 | 0 | ▶ | sub command { | ||||||||
|
Subroutine
command: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 10 | 0 | my ($self) = @_; | |||||||||
| 11 | 0 | my $args = $self->args; | |||||||||
| 12 | 0 | my $input; | |||||||||
| 13 | 0 | ▶ | if (@$args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 0 | ▶ | if (-f $args->[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | ▶ | open my $fh, $args->[0] or die; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | $input = do { local $/; <$fh> }; | |||||||||
| 17 | } | ||||||||||
| 18 | else { | ||||||||||
| 19 | 0 | die "Unknown args"; | |||||||||
| 20 | } | ||||||||||
| 21 | } | ||||||||||
| 22 | else { | ||||||||||
| 23 | 0 | $input = do { local $/; <> }; | |||||||||
| 24 | } | ||||||||||
| 25 | 0 | $self->run($input); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 40 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 29 | 40 | my ($self, $input) = @_; | |||||||||
| 30 | 40 | my $exec = Pegex::Forth::Exec->new; | |||||||||
| 31 | 40 | my $parser = Pegex::Parser->new( | |||||||||
| 32 | grammar => Pegex::Forth::Grammar->new, | ||||||||||
| 33 | receiver => $exec, | ||||||||||
| 34 | # debug => 1, | ||||||||||
| 35 | ); | ||||||||||
| 36 | 40 | $parser->parse($input); | |||||||||
| 37 | 37 | my $values = $exec->runtime->stack; | |||||||||
| 38 | 37 | ▶ | return unless @$values; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 33 | ▶ | wantarray ? @$values : $values->[-1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | #------------------------------------------------------------------------------ | ||||||||||
| 43 | package Pegex::Forth::Grammar; | ||||||||||
| 44 | 4 | ▶ | use Pegex::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 4 | extends 'Pegex::Grammar'; | |||||||||
| 46 | 4 | ▶ | use constant text => <<'...'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | forth: - token* | ||||||||||
| 48 | |||||||||||
| 49 | token: | ||||||||||
| 50 | | number | ||||||||||
| 51 | | comment | ||||||||||
| 52 | | word | ||||||||||
| 53 | |||||||||||
| 54 | number: /( DASH? DIGIT+ ) +/ | ||||||||||
| 55 | comment: /'(' + ALL*? ')' +/ | ||||||||||
| 56 | word: /( NS+ ) +/ | ||||||||||
| 57 | |||||||||||
| 58 | ws: / (: WS | EOS ) / | ||||||||||
| 59 | ... | ||||||||||
| 60 | |||||||||||
| 61 | #------------------------------------------------------------------------------ | ||||||||||
| 62 | package Pegex::Forth::Exec; | ||||||||||
| 63 | 4 | ▶ | use Pegex::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 4 | extends 'Pegex::Tree'; | |||||||||
| 65 | |||||||||||
| 66 | 4 | ▶ | use Pegex::Forth::Runtime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | |||||||||||
| 68 | 40 | ▶ | has runtime => sub { Pegex::Forth::Runtime->new }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | |||||||||||
| 70 | 82 | ▶ | sub got_number { | ||||||||
|
Subroutine
got_number: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 82 | my ($self, $number) = @_; | |||||||||
| 72 | 82 | $self->runtime->push($number); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 41 | ▶ | sub got_word { | ||||||||
|
Subroutine
got_word: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 41 | my ($self, $word) = @_; | |||||||||
| 77 | 41 | $self->runtime->call($word); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 4 | 1; | |||||||||