| 1 | package Inline::Spew; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | require Exporter; | |||||||||||||
| 8 | 2 | require Inline; | |||||||||||||
| 9 | 2 | require YAML; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | our @ISA = qw(Inline Exporter); | |||||||||||||
| 12 | |||||||||||||||
| 13 | 2 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||
| 14 | |||||||||||||||
| 15 | ) ] ); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | our @EXPORT = qw( | |||||||||||||
| 20 | |||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 2 | our $VERSION = '0.02'; | |||||||||||||
| 24 | |||||||||||||||
| 25 | 0 | ▶ | sub register { | ||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 26 | return { | ||||||||||||||
| 27 | 0 | language => 'Spew', | |||||||||||||
| 28 | type => 'interpreted', | ||||||||||||||
| 29 | suffix => 'spew', | ||||||||||||||
| 30 | }; | ||||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 2 | ▶ | sub validate { | ||||||||||||
|
Subroutine
validate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 34 | ## warn "validate called with:\n", YAML::Dump(@_), "\n\n"; | ||||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 2 | ▶ | sub build { | ||||||||||||
|
Subroutine
build: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 38 | 2 | my $o = shift; | |||||||||||||
| 39 | 2 | my $code = $o->{API}{code}; | |||||||||||||
| 40 | 2 | my $location = "$o->{API}{location}"; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 2 | require File::Basename; | |||||||||||||
| 43 | 2 | my $directory = File::Basename::dirname($location); | |||||||||||||
| 44 | 2 | ▶ | $o->mkpath($directory) unless -d $directory; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 2 | my $spew = spew_compile($code); | |||||||||||||
| 47 | |||||||||||||||
| 48 | 2 | YAML::DumpFile($location, $spew); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | ▶ | sub load { | ||||||||||||
|
Subroutine
load: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 52 | 2 | my $o = shift; | |||||||||||||
| 53 | |||||||||||||||
| 54 | 2 | my $sub = do { | |||||||||||||
| 55 | 2 | ▶ | my $s = $o->{CONFIG}{SUB} || "spew"; | ||||||||||||
|
Condition100%2/2
$o->{'CONFIG'}{'SUB'} || 'spew'
MC/DC100%1/1
$o->{'CONFIG'}{'SUB'} || 'spew'
$o->{'CONFIG'}{'SUB'}
Truth table100%2/2
A
A= $o->{'CONFIG'}{'SUB'}
| |||||||||||||||
| 56 | 2 | ▶ | unless ($s =~ /::/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 2 | $s = $o->{API}{pkg}."::$s"; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | 2 | $s; | |||||||||||||
| 60 | }; | ||||||||||||||
| 61 | 2 | my $location = $o->{API}{location}; | |||||||||||||
| 62 | 2 | my @result = YAML::LoadFile($location); | |||||||||||||
| 63 | |||||||||||||||
| 64 | { | ||||||||||||||
| 65 | 2 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 66 | 2 | ▶ | *$sub = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||
| 67 | 2 | ▶ | my $start = shift || "START"; | ||||||||||||
|
Condition50%1/2
shift() || 'START'
MC/DC0%0/1
shift() || 'START'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||
| 68 | 2 | return spew_show($result[0], $start); | |||||||||||||
| 69 | 2 | }; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 61 | ▶ | sub spew_show { | ||||||||||||
|
Subroutine
spew_show: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||
| 74 | 61 | my ($parsed, $defn) = @_; | |||||||||||||
| 75 | 61 | ▶ | die "missing defn for $defn" unless exists $parsed->{$defn}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | 61 | my @choices = @{$parsed->{$defn}{is}}; | |||||||||||||
| 78 | 61 | my $weight = 0; | |||||||||||||
| 79 | 61 | my @keeper = (); | |||||||||||||
| 80 | 61 | ▶ | while (@choices) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 828 | my ($thisweight, @thisitem) = @{pop @choices}; | |||||||||||||
| 82 | 828 | ▶ | $thisweight = 0 if $thisweight < 0; # no funny stuff | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 828 | $weight += $thisweight; | |||||||||||||
| 84 | 828 | ▶ | @keeper = @thisitem if rand($weight) < $thisweight; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | 61 | my $result; | |||||||||||||
| 87 | 61 | for (@keeper) { | |||||||||||||
| 88 | ## should be a list of ids or defns | ||||||||||||||
| 89 | 144 | ▶ | die "huh $_ in $defn" if ref $defn; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 144 | ▶ | if (/^ (.*)/s) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 91 | 85 | $result .= $1; | |||||||||||||
| 92 | } elsif (/^(\w+)$/) { | ||||||||||||||
| 93 | 59 | $result .= spew_show($parsed, $1); | |||||||||||||
| 94 | } else { | ||||||||||||||
| 95 | 0 | die "Can't show $_ in $defn\n"; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | 61 | return $result; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 2 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 2 | my $parser; | |||||||||||||
| 104 | 2 | my $GRAMMAR = q{ | |||||||||||||
| 105 | ## return hashref | ||||||||||||||
| 106 | ## { ident => { | ||||||||||||||
| 107 | ## is => [ | ||||||||||||||
| 108 | ## [weight => item, item, item, ...], | ||||||||||||||
| 109 | ## [weight => item, item, item, ...], ... | ||||||||||||||
| 110 | ## ], | ||||||||||||||
| 111 | ## defined => { line-number => times } | ||||||||||||||
| 112 | ## used => { line-number => times } | ||||||||||||||
| 113 | ## }, ... | ||||||||||||||
| 114 | ## } | ||||||||||||||
| 115 | ## item is " literal" or ident | ||||||||||||||
| 116 | ## ident is C-symbol or number (internal for nested rules) | ||||||||||||||
| 117 | |||||||||||||||
| 118 | { my %grammar; my $internal = 0; } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | grammar: rule(s) /\Z/ { \%grammar; } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | ## rule returns identifier (not used) | ||||||||||||||
| 123 | rule: identifier ":" defn { | ||||||||||||||
| 124 | push @{$grammar{$item[1]}{is}}, @{$item[3]}; | ||||||||||||||
| 125 | $grammar{$item[1]}{defined}{$itempos[1]{line}{to}}++; | ||||||||||||||
| 126 | $item[1]; | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | | <error> | ||||||||||||||
| 129 | |||||||||||||||
| 130 | ## defn returns listref of choices | ||||||||||||||
| 131 | defn: <leftop: choice "|" choice> | ||||||||||||||
| 132 | |||||||||||||||
| 133 | ## choice returns a listref of [weight => @items] | ||||||||||||||
| 134 | choice: weight unweightedchoice { [ $item[1] => @{$item[2]} ] } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | ## weight returns weight if present, 1 if not | ||||||||||||||
| 137 | weight: /\d+(\.\d+)?/ <commit> /\@/ { $item[1] } | { 1 } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | ## unweightedchoice returns a listref of @items | ||||||||||||||
| 140 | unweightedchoice: item(s) | ||||||||||||||
| 141 | |||||||||||||||
| 142 | ## item returns " literal text" or "identifier" | ||||||||||||||
| 143 | item: | ||||||||||||||
| 144 | { $_ = extract_quotelike($text) and " " . eval } | ||||||||||||||
| 145 | | identifier <commit> ...!/:/ { # must not be followed by colon! | ||||||||||||||
| 146 | $grammar{$item[1]}{used}{$itempos[1]{line}{to}}++; | ||||||||||||||
| 147 | $item[1]; # non-leading space flags an identifier | ||||||||||||||
| 148 | } | ||||||||||||||
| 149 | | "(" defn ")" { # parens for recursion, gensym an internal | ||||||||||||||
| 150 | ++$internal; | ||||||||||||||
| 151 | push @{$grammar{$internal}{is}}, @{$item[2]}; | ||||||||||||||
| 152 | $internal; | ||||||||||||||
| 153 | } | ||||||||||||||
| 154 | | <error> | ||||||||||||||
| 155 | |||||||||||||||
| 156 | identifier: /[^\W\d]\w*/ | ||||||||||||||
| 157 | }; | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 2 | ▶ | sub spew_compile { | ||||||||||||
|
Subroutine
spew_compile: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||
| 160 | 2 | my $source = shift; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 2 | ▶ | unless ($parser) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 2 | require Parse::RecDescent; | |||||||||||||
| 164 | 2 | ▶ | $parser = Parse::RecDescent->new($GRAMMAR) or die "internal bad"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 2 | ▶ | my $parsed = $parser->grammar($source) or die "bad spew grammar"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | |||||||||||||||
| 169 | 2 | for my $id (sort keys %$parsed) { | |||||||||||||
| 170 | 18 | ▶ | next if $id =~ /^\d+$/; # skip internals | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 14 | my $id_ref = $parsed->{$id}; | |||||||||||||
| 172 | 14 | ▶ | unless (exists $id_ref->{defined}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 0 | die "$id used in @{[sort keys %{$id_ref->{used}}]} but not defined"; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | ## unless (exists $id_ref->{used} or $id eq $START) { | ||||||||||||||
| 176 | ## warn "$id defined in @{[sort keys %{$id_ref->{defined}}]} but not used"; | ||||||||||||||
| 177 | ## } | ||||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | 2 | return $parsed; | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | } | ||||||||||||||
| 183 | |||||||||||||||
| 184 | 2 | 1; | |||||||||||||
| 185 | __END__ | ||||||||||||||