| 1 | package App::Scheme79asm::Compiler; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use 5.014000; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | our $VERSION = '1.000'; | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw/croak/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 1 | ▶ | use Data::Dumper qw/Dumper/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 1 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 1 | ▶ | use Data::SExpression qw/cons consp scalarp/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 1 | ▶ | use List::MoreUtils qw/firstidx/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 1 | our @PRIMOPS = qw/car cdr cons atom progn reverse-list/; | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 20 | ▶ | sub make_symbol { Data::SExpression::Symbol->new(shift) } | ||||||||||||||||
|
Subroutine
make_symbol: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | # types | ||||||||||||||||||
| 21 | 1 | our $LIST = make_symbol 'LIST'; | |||||||||||||||||
| 22 | 1 | our $SYMBOL = make_symbol 'SYMBOL'; | |||||||||||||||||
| 23 | 1 | our $VAR = make_symbol 'VAR'; | |||||||||||||||||
| 24 | # no need for closures | ||||||||||||||||||
| 25 | 1 | our $PROC = make_symbol 'PROC'; | |||||||||||||||||
| 26 | 1 | our $IF = make_symbol 'IF'; | |||||||||||||||||
| 27 | 1 | our $CALL = make_symbol 'CALL'; | |||||||||||||||||
| 28 | # no need for quoted constants | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | # primitives | ||||||||||||||||||
| 31 | 1 | our $MORE = make_symbol 'MORE'; | |||||||||||||||||
| 32 | 1 | our $FUNCALL = make_symbol 'FUNCALL'; | |||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 1 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 35 | 1 | *cons = *Data::SExpression::cons; | |||||||||||||||||
| 36 | 1 | *consp = *Data::SExpression::consp; | |||||||||||||||||
| 37 | 1 | *scalarp = *Data::SExpression::scalarp; | |||||||||||||||||
| 38 | } | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | # list processing routines | ||||||||||||||||||
| 41 | 30 | ▶ | sub append { | ||||||||||||||||
|
Subroutine
append: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 42 | 30 | my ($expr, $rest) = @_; | |||||||||||||||||
| 43 | 30 | ▶ | if (defined $expr) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 20 | cons $expr->car, append($expr->cdr, $rest) | |||||||||||||||||
| 45 | } else { | ||||||||||||||||||
| 46 | 10 | $rest | |||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | sub mapcar (&@); | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 41 | ▶ | sub mapcar (&@) { | ||||||||||||||||
|
Subroutine
mapcar: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 53 | 41 | my ($block, $expr) = @_; | |||||||||||||||||
| 54 | 41 | ▶ | if (defined $expr) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | 24 | my $result; | |||||||||||||||||
| 56 | 24 | do { | |||||||||||||||||
| 57 | 24 | local $_ = $expr->car; | |||||||||||||||||
| 58 | 24 | $result = $block->() | |||||||||||||||||
| 59 | }; | ||||||||||||||||||
| 60 | 24 | ▶ | cons $result, mapcar { $block->($_) } $expr->cdr | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 61 | } else { | ||||||||||||||||||
| 62 | undef | ||||||||||||||||||
| 63 | 17 | } | |||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 14 | ▶ | sub revacc { | ||||||||||||||||
|
Subroutine
revacc: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 67 | 14 | my ($expr, $acc) = @_; | |||||||||||||||||
| 68 | 14 | ▶ | if (defined $expr) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 8 | revacc ($expr->cdr, cons($expr->car, $acc)) | |||||||||||||||||
| 70 | } else { | ||||||||||||||||||
| 71 | 6 | $acc | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 6 | ▶ | sub rev { | ||||||||||||||||
|
Subroutine
rev: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 76 | 6 | my ($expr) = @_; | |||||||||||||||||
| 77 | 6 | revacc $expr, undef; | |||||||||||||||||
| 78 | } | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 37 | ▶ | sub positionacc { | ||||||||||||||||
|
Subroutine
positionacc: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 81 | 37 | my ($expr, $list, $acc) = @_; | |||||||||||||||||
| 82 | 37 | ▶ | if (!defined $list) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 83 | undef | ||||||||||||||||||
| 84 | 1 | } elsif ($list->car eq $expr) { | |||||||||||||||||
| 85 | 16 | $acc | |||||||||||||||||
| 86 | } else { | ||||||||||||||||||
| 87 | 20 | positionacc($expr, $list->cdr, $acc + 1) | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 17 | ▶ | sub position { | ||||||||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 92 | 17 | my ($expr, $list) = @_; | |||||||||||||||||
| 93 | 17 | positionacc $expr, $list, 0 | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | # end list processing routines | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 28 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 98 | 28 | my ($class) = @_; | |||||||||||||||||
| 99 | 28 | my %self = ( | |||||||||||||||||
| 100 | symbols => ['', '', 'T'], | ||||||||||||||||||
| 101 | nsymbols => 3, | ||||||||||||||||||
| 102 | symbol_map => {T => 2}, | ||||||||||||||||||
| 103 | ); | ||||||||||||||||||
| 104 | 28 | bless \%self, $class; | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 48 | ▶ | sub process_quoted { | ||||||||||||||||
|
Subroutine
process_quoted: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 108 | 48 | my ($self, $expr) = @_; | |||||||||||||||||
| 109 | 48 | ▶ | if (!defined $expr) { # nil | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 110 | 9 | [$LIST => 0] | |||||||||||||||||
| 111 | } elsif (scalarp $expr) { | ||||||||||||||||||
| 112 | 25 | $expr = uc $expr; | |||||||||||||||||
| 113 | 25 | ▶ | if ($expr eq 'NIL') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 114 | 1 | return [$LIST => 0] | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | 24 | ▶ | if (!exists $self->{symbol_map}{$expr}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 117 | 22 | $self->{symbol_map}{$expr} = $self->{nsymbols}; | |||||||||||||||||
| 118 | 22 | $self->{nsymbols}++; | |||||||||||||||||
| 119 | 22 | push @{$self->{symbols}}, $expr; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | 24 | [$SYMBOL => $self->{symbol_map}{$expr}] | |||||||||||||||||
| 122 | } elsif (consp $expr) { | ||||||||||||||||||
| 123 | 13 | [$LIST => $self->process_quoted($expr->cdr), $self->process_quoted($expr->car)] | |||||||||||||||||
| 124 | } else { | ||||||||||||||||||
| 125 | 1 | croak 'argument to process_quoted is not a scalar, cons, or nil: ', Dumper($expr); | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 6 | ▶ | sub process_proc { | ||||||||||||||||
|
Subroutine
process_proc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 130 | 6 | my ($self, $func_name, $func_args, $func_body, $env) = @_; | |||||||||||||||||
| 131 | 6 | my $new_env = append cons($func_name, rev $func_args), $env; | |||||||||||||||||
| 132 | 6 | $self->process_toplevel($func_body, $new_env) | |||||||||||||||||
| 133 | } | ||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | 28 | ▶ | sub rest_of_funcall { | ||||||||||||||||
|
Subroutine
rest_of_funcall: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 136 | 28 | my ($self, $func, $args) = @_; | |||||||||||||||||
| 137 | 28 | ▶ | if (!defined $args) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 138 | 16 | $func | |||||||||||||||||
| 139 | } else { | ||||||||||||||||||
| 140 | 12 | [$MORE => $self->rest_of_funcall($func, $args->cdr), $args->car] | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | } | ||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 17 | ▶ | sub process_funcall { | ||||||||||||||||
|
Subroutine
process_funcall: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 145 | 17 | my ($self, $func_name, $func_args, $env) = @_; | |||||||||||||||||
| 146 | 17 | ▶ | my $prim_idx = firstidx { uc $_ eq uc $func_name } @PRIMOPS; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 147 | my $processed_args = | ||||||||||||||||||
| 148 | 17 | ▶ | mapcar { $self->process_toplevel($_, $env) } $func_args; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 149 | 17 | ▶ | if ($prim_idx > -1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 150 | 13 | ▶ | if (!defined $processed_args) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 151 | 1 | croak "Cannot call primitive $func_name with no arguments"; | |||||||||||||||||
| 152 | } | ||||||||||||||||||
| 153 | 12 | [$CALL => $self->rest_of_funcall([make_symbol(uc $func_name), 0], $processed_args->cdr), $processed_args->car] | |||||||||||||||||
| 154 | } else { | ||||||||||||||||||
| 155 | 4 | my $final_args = append $processed_args, cons ($self->process_toplevel($func_name, $env), undef); | |||||||||||||||||
| 156 | 4 | [$CALL => $self->rest_of_funcall([$FUNCALL => 0], $final_args->cdr), $final_args->car] | |||||||||||||||||
| 157 | } | ||||||||||||||||||
| 158 | } | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | 68 | ▶ | sub process_toplevel { | ||||||||||||||||
|
Subroutine
process_toplevel: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||
| 161 | 68 | my ($self, $expr, $env) = @_; | |||||||||||||||||
| 162 | 68 | ▶ | if (!defined $expr) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 163 | 2 | [$LIST => 0] | |||||||||||||||||
| 164 | } elsif (scalarp $expr) { | ||||||||||||||||||
| 165 | 25 | ▶ | if (looks_like_number $expr) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 166 | 2 | $self->process_quoted($expr); | |||||||||||||||||
| 167 | } elsif (uc $expr eq 'T') { | ||||||||||||||||||
| 168 | 4 | [$SYMBOL => 2] | |||||||||||||||||
| 169 | } elsif (uc $expr eq 'NIL') { | ||||||||||||||||||
| 170 | 2 | [$LIST => 0] | |||||||||||||||||
| 171 | } else { | ||||||||||||||||||
| 172 | 17 | my $position = position $expr, $env; | |||||||||||||||||
| 173 | 17 | ▶ | if (defined $position) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 174 | 16 | [$VAR => -1 - $position] | |||||||||||||||||
| 175 | } else { | ||||||||||||||||||
| 176 | 1 | croak "Variable $expr not in environment"; | |||||||||||||||||
| 177 | } | ||||||||||||||||||
| 178 | } | ||||||||||||||||||
| 179 | } else { | ||||||||||||||||||
| 180 | 41 | my $func = uc $expr->car; | |||||||||||||||||
| 181 | 41 | ▶ | if ($func eq 'QUOTE') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 182 | 14 | $self->process_quoted($expr->cdr->car) | |||||||||||||||||
| 183 | } elsif ($func eq 'LAMBDA') { | ||||||||||||||||||
| 184 | 6 | my $func_name = $expr->cdr->car; | |||||||||||||||||
| 185 | 6 | my $func_args = $expr->cdr->cdr->car; | |||||||||||||||||
| 186 | 6 | my $func_body = $expr->cdr->cdr->cdr->car; ## no critic (ProhibitLongChainsOfMethodCalls) | |||||||||||||||||
| 187 | 6 | [$PROC => $self->process_proc($func_name, $func_args, $func_body, $env)] | |||||||||||||||||
| 188 | } elsif ($func eq 'IF') { | ||||||||||||||||||
| 189 | my ($if_cond, $if_then, $if_else) = | ||||||||||||||||||
| 190 | 4 | map { $self->process_toplevel($_, $env) } | |||||||||||||||||
| 191 | ($expr->cdr->car, $expr->cdr->cdr->car, $expr->cdr->cdr->cdr->car); ## no critic (ProhibitLongChainsOfMethodCalls) | ||||||||||||||||||
| 192 | 4 | [$IF => [$LIST => $if_else, $if_then], $if_cond] | |||||||||||||||||
| 193 | } else { | ||||||||||||||||||
| 194 | 17 | $self->process_funcall($expr->car, $expr->cdr, $env) | |||||||||||||||||
| 195 | } | ||||||||||||||||||
| 196 | } | ||||||||||||||||||
| 197 | } | ||||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | 10 | ▶ | sub compile_sexp { | ||||||||||||||||
|
Subroutine
compile_sexp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 200 | 10 | my ($self, $expr) = @_; | |||||||||||||||||
| 201 | 10 | $self->process_toplevel($expr, undef) | |||||||||||||||||
| 202 | } | ||||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | 10 | ▶ | sub compile_string { | ||||||||||||||||
|
Subroutine
compile_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 205 | 10 | my ($self, $string) = @_; | |||||||||||||||||
| 206 | 10 | my $sexp = Data::SExpression->new( | |||||||||||||||||
| 207 | {fold_lists => 0, use_symbol_class => 1} | ||||||||||||||||||
| 208 | ); | ||||||||||||||||||
| 209 | 10 | my $expr = $sexp->read($string); | |||||||||||||||||
| 210 | 10 | $self->compile_sexp($expr) | |||||||||||||||||
| 211 | } | ||||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | 1 | 1; | |||||||||||||||||
| 214 | __END__ | ||||||||||||||||||