Debian Codegraph @

t/sl_wall.t

raw
1 #!perl
2 # Copyright 2022 Jeffrey Kegler
3 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
4 # redistribute it and/or modify it under the terms of the GNU Lesser
5 # General Public License as published by the Free Software Foundation,
6 # either version 3 of the License, or (at your option) any later version.
7 #
8 # Marpa::R2 is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser
14 # General Public License along with Marpa::R2. If not, see
15 # http://www.gnu.org/licenses/.
16 #
17 use 5.010001;
18 use strict;
19 use warnings;
20
21 # The Wall Series: a sequence of numbers generated by an especially
22 # ambiguous section of Perl syntax, relaxed to ignore precedence
23 # and lvalue restricitons.
24
25 # This produces numbers in the series A052952 in the literature.
26 # It's a kind of ragtime Fibonacci series. My proof that the
27 # parse counts generated by this grammar and A052952 are identical
28 # is at perlmonks.org: http://perlmonks.org/?node_id=649892
29
30 use Test::More tests => 12;
31 use lib 'inc';
32 use Marpa::R2::Test;
33 use Marpa::R2;
34
35 # The inefficiency (at least some of it) is deliberate.
36 # Passing up a duples of [ string, value ] and then
37 # assembling a final string at the top would be better
38 # than assembling the string then taking it
39 # apart at each step. But I wanted to test having
40 # a start symbol that appears repeatedly on the RHS.
41
42 ## no critic (Subroutines::RequireArgUnpacking)
43
44 sub My_Actions::minus {
45 shift;
46 my ( $right_string, $right_value ) = ( $_[2] =~ /^(.*)==(.*)$/xms );
47 my ( $left_string, $left_value ) = ( $_[0] =~ /^(.*)==(.*)$/xms );
48 my $value = $left_value - $right_value;
49 return '(' . $left_string . q{-} . $right_string . ')==' . $value;
50 } ## end sub minus
51
52 sub My_Actions::postfix_decr {
53 shift;
54 my ( $string, $value ) = ( $_[0] =~ /^(.*)==(.*)$/xms );
55 return '(' . $string . q{--} . ')==' . $value--;
56 }
57
58 sub My_Actions::prefix_decr {
59 shift;
60 my ( $string, $value ) = ( $_[2] =~ /^(.*)==(.*)$/xms );
61 return '(' . q{--} . $string . ')==' . --$value;
62 }
63
64 sub My_Actions::negation {
65 shift;
66 my ( $string, $value ) = ( $_[1] =~ /^(.*)==(.*)$/xms );
67 return '(' . q{-} . $string . ')==' . -$value;
68 }
69
70 sub My_Actions::number {
71 shift;
72 return "$_[0]==$_[0]";
73 }
74
75 sub My_Actions::default_action {
76 shift;
77 my $v_count = scalar @_;
78 return q{} if $v_count <= 0;
79 return $_[0] if $v_count == 1;
80 return '(' . join( q{;}, @_ ) . ')';
81 } ## end sub default_action
82
83 ## use critic
84
85 my $g = Marpa::R2::Scanless::G->new(
86 { source => \(<<'END_OF_SOURCE'),
87 :start ::= E
88 :default ::= action => default_action
89 E ::=
90 E Minus E action => minus
91 | E Minus Minus action => postfix_decr
92 | Minus Minus E action => prefix_decr
93 | Minus E action => negation
94 | Number action => number
95 Number ~ [0-9]
96 Minus ~ '-'
97 END_OF_SOURCE
98 }
99 );
100
101 my @expected = qw(0 1 1 3 4 8 12 21 33 55 88 144 232 );
102
103 for my $n ( 1 .. 12 ) {
104
105 # Set max_parses just in case there's an infinite loop.
106 # This is for debugging, after all
107 my $recce = Marpa::R2::Scanless::R->new(
108 { grammar => $g,
109 semantics_package => 'My_Actions',
110 max_parses => 300
111 }
112 );
113 $recce->read( \'6-', 0, 1 );
114 $recce->resume( 1, 1 ) for 1 .. $n;
115 $recce->resume( 0, 1 );
116 my $parse_count = 0;
117 while ( $recce->value() ) { $parse_count++; }
118 Marpa::R2::Test::is( $expected[$n], $parse_count,
119 "Wall Series Number $n" );
120
121 } ## end for my $n ( 1 .. 12 )
122
123 1; # In case used as "do" file
124
125 # vim: expandtab shiftwidth=4: