| 1 | 34 | ▶ | use strict; use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | package Lingy::Common; | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 34 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 34 | ▶ | use Scalar::Util 'refaddr'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | # Base type classes: | ||||||||||||||
| 9 | 34 | ▶ | use constant LISTTYPE => 'Lingy::Lang::ListClass'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 34 | ▶ | use constant SCALARTYPE => 'Lingy::Lang::ScalarClass'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 34 | ▶ | use constant SEQUENTIAL => 'Lingy::Lang::Sequential'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | # Type classes: | ||||||||||||||
| 14 | 34 | ▶ | use constant ATOM => 'Lingy::Lang::Atom'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 34 | ▶ | use constant BOOLEAN => 'Lingy::Lang::Boolean'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 34 | ▶ | use constant CHARACTER => 'Lingy::Lang::Character'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 34 | ▶ | use constant CLASS => 'Lingy::Lang::Class'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 34 | ▶ | use constant COMPILER => 'Lingy::Lang::Compiler'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 34 | ▶ | use constant FUNCTION => 'Lingy::Lang::Fn'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 34 | ▶ | use constant HASHMAP => 'Lingy::Lang::HashMap'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 34 | ▶ | use constant KEYWORD => 'Lingy::Lang::Keyword'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 34 | ▶ | use constant LIST => 'Lingy::Lang::List'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 34 | ▶ | use constant MACRO => 'Lingy::Lang::Macro'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 34 | ▶ | use constant NIL => 'Lingy::Lang::Nil'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 34 | ▶ | use constant NUMBER => 'Lingy::Lang::Number'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 34 | ▶ | use constant REGEX => 'Lingy::Lang::Regex'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 34 | ▶ | use constant STRING => 'Lingy::Lang::String'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 34 | ▶ | use constant SYMBOL => 'Lingy::Lang::Symbol'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 34 | ▶ | use constant UTIL => 'Lingy::Lang::Util'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 34 | ▶ | use constant VECTOR => 'Lingy::Lang::Vector'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 34 | ▶ | use constant VAR => 'Lingy::Lang::Var'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | # Functionality classes: | ||||||||||||||
| 34 | 34 | ▶ | use constant NAMESPACE => 'Lingy::Lang::Namespace'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 34 | ▶ | use constant NUMBERS => 'Lingy::Lang::Numbers'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 34 | ▶ | use constant RT => 'Lingy::Lang::RT'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 34 | ▶ | use constant TERM => 'Lingy::Lang::Term'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 34 | ▶ | use constant THREAD => 'Lingy::Lang::Thread'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | BEGIN { | ||||||||||||||
| 41 | 34 | ▶ | our @EXPORT = qw< | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | READY | ||||||||||||||
| 43 | |||||||||||||||
| 44 | $symbol_re | ||||||||||||||
| 45 | $namespace_re | ||||||||||||||
| 46 | |||||||||||||||
| 47 | refaddr | ||||||||||||||
| 48 | |||||||||||||||
| 49 | LISTTYPE | ||||||||||||||
| 50 | SCALARTYPE | ||||||||||||||
| 51 | SEQUENTIAL | ||||||||||||||
| 52 | |||||||||||||||
| 53 | COMPILER | ||||||||||||||
| 54 | NAMESPACE | ||||||||||||||
| 55 | NUMBERS | ||||||||||||||
| 56 | RT | ||||||||||||||
| 57 | TERM | ||||||||||||||
| 58 | THREAD | ||||||||||||||
| 59 | UTIL | ||||||||||||||
| 60 | |||||||||||||||
| 61 | ATOM | ||||||||||||||
| 62 | BOOLEAN | ||||||||||||||
| 63 | CHARACTER | ||||||||||||||
| 64 | CLASS | ||||||||||||||
| 65 | FUNCTION | ||||||||||||||
| 66 | HASHMAP | ||||||||||||||
| 67 | KEYWORD | ||||||||||||||
| 68 | LIST | ||||||||||||||
| 69 | MACRO | ||||||||||||||
| 70 | NIL | ||||||||||||||
| 71 | NUMBER | ||||||||||||||
| 72 | REGEX | ||||||||||||||
| 73 | STRING | ||||||||||||||
| 74 | SYMBOL | ||||||||||||||
| 75 | VAR | ||||||||||||||
| 76 | VECTOR | ||||||||||||||
| 77 | |||||||||||||||
| 78 | list | ||||||||||||||
| 79 | string | ||||||||||||||
| 80 | symbol | ||||||||||||||
| 81 | |||||||||||||||
| 82 | err | ||||||||||||||
| 83 | box_val | ||||||||||||||
| 84 | unbox_val | ||||||||||||||
| 85 | assert_args | ||||||||||||||
| 86 | comp_pair | ||||||||||||||
| 87 | nil | ||||||||||||||
| 88 | false | ||||||||||||||
| 89 | true | ||||||||||||||
| 90 | |||||||||||||||
| 91 | Dump | ||||||||||||||
| 92 | PPP | ||||||||||||||
| 93 | WWW | ||||||||||||||
| 94 | XXX | ||||||||||||||
| 95 | YYY | ||||||||||||||
| 96 | ZZZ | ||||||||||||||
| 97 | >; | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 34 | ▶ | use Lingy::Printer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | |||||||||||||||
| 102 | 34 | our $namespace_re = qr{(?: | |||||||||||||
| 103 | \w+ | ||||||||||||||
| 104 | (?:\.\w+)* | ||||||||||||||
| 105 | )}x; | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 34 | our $symbol_re = qr{( | |||||||||||||
| 108 | \*?[-\w]+[\?\!\*\#]? | | ||||||||||||||
| 109 | [-+*/<>] | | ||||||||||||||
| 110 | ==? | | ||||||||||||||
| 111 | <= | | ||||||||||||||
| 112 | >=| | ||||||||||||||
| 113 | ->>? | ||||||||||||||
| 114 | )}x; | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | ▶ | sub READY { RT->ready } | ||||||||||||
|
Subroutine
READY: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | ▶ | sub list { LIST->new(@_) } | ||||||||||||
|
Subroutine
list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 119 | 0 | ▶ | sub string { STRING->new(@_) } | ||||||||||||
|
Subroutine
string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 120 | 24 | ▶ | sub symbol { SYMBOL->new(@_) } | ||||||||||||
|
Subroutine
symbol: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 121 | |||||||||||||||
| 122 | sub err { | ||||||||||||||
| 123 | 0 | ▶ | my $msg = shift; | ||||||||||||
|
Subroutine
err: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 124 | 0 | $msg = sprintf $msg, @_; | |||||||||||||
| 125 | 0 | ▶ | die "Error:" . | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | ($msg =~ /\n./ ? "\n" : ' ') . | ||||||||||||||
| 127 | $msg . | ||||||||||||||
| 128 | "\n"; | ||||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | sub box_val { | ||||||||||||||
| 132 | map { | ||||||||||||||
| 133 | 0 | ▶ | my $o = $_; | ||||||||||||
|
Subroutine
box_val: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 134 | 0 | my $type = ref($o); | |||||||||||||
| 135 | 0 | ▶ | if (not($type)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 136 | 0 | ▶ | /^\-?\d+$/ ? NUMBER->new($o) : STRING->new($o); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 137 | } | ||||||||||||||
| 138 | elsif ($type =~ /^(?:ARRAY|HASH|SCALAR|REF|Regexp)$/) { | ||||||||||||||
| 139 | 0 | XXX $o, "Lingy can't box this object yet"; | |||||||||||||
| 140 | } else { | ||||||||||||||
| 141 | 0 | $o; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | } @_; | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | sub unbox_val { | ||||||||||||||
| 147 | 0 | ▶ | my ($obj) = @_; | ||||||||||||
|
Subroutine
unbox_val: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 148 | 0 | ▶ | ref($obj) =~ /^Lingy::Lang::(String|Number|Fn)$/ ? $obj->unbox : $obj; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | sub assert_args { | ||||||||||||||
| 152 | 0 | ▶ | my $args = shift; | ||||||||||||
|
Subroutine
assert_args: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 153 | 0 | ▶ | for (my $i = 0; $i < @_; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 154 | 0 | ▶ | if (ref($args->[$i]) ne $_[$i]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | 0 | my (undef, undef, undef, $fn) = caller(1); | |||||||||||||
| 156 | 0 | err "Arg %d for '%s' must be '%s', not '%s'", | |||||||||||||
| 157 | $i, $fn, $_[$i], ref($args->[$i]); | ||||||||||||||
| 158 | } | ||||||||||||||
| 159 | } | ||||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | sub comp_pair { | ||||||||||||||
| 163 | 0 | ▶ | my ($x, $y) = @_; | ||||||||||||
|
Subroutine
comp_pair: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||
| 164 | 0 | ▶ | if (ref($x) eq NIL) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | 0 | ▶ | return ref($y) eq NIL ? 0 : -1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | } | ||||||||||||||
| 167 | 0 | ▶ | return 1 if ref($y) eq NIL; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | 0 | ▶ | ref($x) eq ref($y) or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | err "Can't compare values of type '%s' and '%s'", | ||||||||||||||
| 170 | ref($x), ref($y); | ||||||||||||||
| 171 | 0 | ▶ | if (ref($x) eq VECTOR) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | 0 | ▶ | return @$x cmp @$y unless @$x == @$y; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 0 | my $i = 0; | |||||||||||||
| 174 | 0 | for my $e (@$x) { | |||||||||||||
| 175 | 0 | ▶ | return 1 if $i > @$y; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | 0 | my $r = comp_pair $x->[$i], $y->[$i]; | |||||||||||||
| 177 | 0 | ▶ | return $r if $r; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | 0 | $i++; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | 0 | return 0; | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | 0 | "$x" cmp "$y"; | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | |||||||||||||||
| 185 | sub Dump { | ||||||||||||||
| 186 | 0 | ▶ | _dump(@_); | ||||||||||||
|
Subroutine
Dump: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 187 | } | ||||||||||||||
| 188 | sub PPP { | ||||||||||||||
| 189 | 0 | ▶ | require Lingy::Printer; | ||||||||||||
|
Subroutine
PPP: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 190 | 0 | die _dump(RT->printer->pr_str(@_)); | |||||||||||||
| 191 | } | ||||||||||||||
| 192 | sub WWW { | ||||||||||||||
| 193 | 0 | ▶ | warn _dump(@_); | ||||||||||||
|
Subroutine
WWW: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 194 | 0 | ▶ | return wantarray ? @_ : $_[0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | } | ||||||||||||||
| 196 | sub XXX { | ||||||||||||||
| 197 | 0 | ▶ | die _dump(@_); | ||||||||||||
|
Subroutine
XXX: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 198 | } | ||||||||||||||
| 199 | sub YYY { | ||||||||||||||
| 200 | 0 | ▶ | print _dump(@_); | ||||||||||||
|
Subroutine
YYY: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 201 | 0 | ▶ | return wantarray ? @_ : $_[0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 202 | } | ||||||||||||||
| 203 | sub ZZZ { | ||||||||||||||
| 204 | 0 | ▶ | require Carp; | ||||||||||||
|
Subroutine
ZZZ: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 205 | 0 | Carp::confess _dump(@_); | |||||||||||||
| 206 | } | ||||||||||||||
| 207 | sub _dump { | ||||||||||||||
| 208 | 0 | ▶ | require YAML::PP; | ||||||||||||
|
Subroutine
_dump: not called CC=1 SCAR=6.9
| |||||||||||||||
| 209 | 0 | YAML::PP->new( | |||||||||||||
| 210 | schema => ['Core', 'Perl', '-dumpcode'] | ||||||||||||||
| 211 | )->dump_string(@_) . "...\n"; | ||||||||||||||
| 212 | } | ||||||||||||||
| 213 | |||||||||||||||
| 214 | 34 | 1; | |||||||||||||