File Coverage

blib/lib/Pegex/Forth.pm
Criterion Covered Total %
statement 29 43 67.4
branch 4 10 40.0
condition n/a
subroutine 9 10 90.0
pod 0 2 0.0
total 42 65 64.6


line stmt bran cond sub pod time code
1             package Pegex::Forth;
2             our $VERSION = '0.15';
3              
4 4     4   122331 use Pegex::Base;
  4         10085  
  4         28  
5 4     4   14885 use Pegex::Parser;
  4         62026  
  4         1316  
6              
7             has 'args' => [];
8              
9             sub command {
10 0     0 0 0 my ($self) = @_;
11 0         0 my $args = $self->args;
12 0         0 my $input;
13 0 0       0 if (@$args) {
14 0 0       0 if (-f $args->[0]) {
15 0 0       0 open my $fh, $args->[0] or die;
16 0         0 $input = do { local $/; <$fh> };
  0         0  
  0         0  
17             }
18             else {
19 0         0 die "Unknown args";
20             }
21             }
22             else {
23 0         0 $input = do { local $/; <> };
  0         0  
  0         0  
24             }
25 0         0 $self->run($input);
26             }
27              
28             sub run {
29 40     40 0 44860 my ($self, $input) = @_;
30 40         270 my $exec = Pegex::Forth::Exec->new;
31 40         3802 my $parser = Pegex::Parser->new(
32             grammar => Pegex::Forth::Grammar->new,
33             receiver => $exec,
34             # debug => 1,
35             );
36 40         8631 $parser->parse($input);
37 37         11088 my $values = $exec->runtime->stack;
38 37 100       488 return unless @$values;
39 33 100       5586 wantarray ? @$values : $values->[-1];
40             }
41              
42             #------------------------------------------------------------------------------
43             package Pegex::Forth::Grammar;
44 4     4   40 use Pegex::Base;
  4         13  
  4         22  
45             extends 'Pegex::Grammar';
46 4     4   10753 use constant text => <<'...';
  4         9  
  4         395  
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     4   22 use Pegex::Base;
  4         7  
  4         18  
64             extends 'Pegex::Tree';
65              
66 4     4   14034 use Pegex::Forth::Runtime;
  4         14  
  4         2250  
67              
68             has runtime => sub { Pegex::Forth::Runtime->new };
69              
70             sub got_number {
71 82     82   1287461 my ($self, $number) = @_;
72 82         347 $self->runtime->push($number);
73             }
74              
75             sub got_word {
76 41     41   128750 my ($self, $word) = @_;
77 41         158 $self->runtime->call($word);
78             }
79              
80             1;