App-Scheme79asm
view release on metacpan or search on metacpan
lib/App/Scheme79asm.pm view on Meta::CPAN
package App::Scheme79asm;
use 5.014000;
use strict;
use warnings;
use re '/s';
use Carp qw/croak/;
use Data::Dumper qw/Dumper/;
use Data::SExpression qw/consp scalarp/;
use Scalar::Util qw/looks_like_number/;
our $VERSION = '1.000';
our %TYPES = (
LIST => 0,
SYMBOL => 1,
NUMBER => 1,
VAR => 2,
VARIABLE => 2,
CLOSURE => 3,
PROC => 4,
PROCEDURE => 4,
IF => 5,
COND => 5,
CONDITIONAL => 5,
CALL => 6,
QUOTE => 7,
QUOTED => 7,
MORE => 0,
CAR => 1,
CDR => 2,
CONS => 3,
ATOM => 4,
PROGN => 5,
'REVERSE-LIST' => 6,
FUNCALL => 7,
);
*consp = *Data::SExpression::consp;
*scalarp = *Data::SExpression::scalarp;
sub process {
my ($self, $sexp, $location) = @_;
die 'Toplevel is not a list: ', Dumper($sexp), "\n" unless ref $sexp eq 'ARRAY';
my ($type, @addrs) = @$sexp;
my $addr;
die 'Type of toplevel is not atom: '. Dumper($type), "\n" unless scalarp($type);
if (@addrs > 1) {
$addr = $self->{freeptr} + 1;
$self->{freeptr} += @addrs;
$self->process($addrs[$_], $addr + $_) for 0 .. $#addrs;
} else {
$addr = $addrs[0];
}
$addr = $self->process($addr) if ref $addr eq 'ARRAY';
die 'Addr of toplevel is not atom: ', Dumper($addr), "\n" unless scalarp($addr);
my ($comment_type, $comment_addr) = ($type, $addr);
die 'Computed addr is not a number: ', Dumper($addr), "\n" unless looks_like_number $addr;
if (!looks_like_number $type) {
die "No such type: $type\n" unless exists $TYPES{$type};
$type = $TYPES{$type};
}
$addr += (1 << $self->{addr_bits}) if $addr < 0;
die "Type too large: $type\n" if $type >= (1 << $self->{type_bits});
die "Addr too large: $addr\n" if $addr >= (1 << $self->{addr_bits});
my $result = ($type << $self->{addr_bits}) + $addr;
unless ($location) {
$self->{freeptr}++;
$location = $self->{freeptr}
}
$self->{memory}[$location] = $result;
$self->{comment}[$location] = "$comment_type $comment_addr";
$location
}
sub parse {
my ($self, $string) = @_;
my $ds = Data::SExpression->new({symbol_case => 'up', use_symbol_class => 1, fold_lists => 1});
my $sexp;
while () {
last if $string =~ /^\s*$/;
($sexp, $string) = $ds->read($string);
$self->process($sexp)
}
}
sub finish {
my ($self) = @_;
$self->{memory}[5] = $self->{memory}[$self->{freeptr}];
$self->{comment}[5] = $self->{comment}[$self->{freeptr}];
$self->{memory}[4] = $self->{freeptr};
delete $self->{memory}[$self->{freeptr}]
}
sub new {
my ($class, %args) = @_;
$args{type_bits} //= 3;
$args{addr_bits} //= 8;
$args{freeptr} //= 6;
$args{memory} //= [0, 0, (1<<$args{addr_bits}), (1<<$args{addr_bits}), 0, 0, 0];
my @default_comments = ('(cdr part of NIL)', '(car part of NIL)', '(cdr part of T)', '(car part of T)', '(free storage pointer)', '', '(result of computation)');
for (0 .. $#default_comments) {
$args{comment}[$_] = $default_comments[$_]
}
bless \%args, $class
}
sub print_binary16 {
my ($self, $fh) = @_;
$fh //= \*STDOUT; # uncoverable condition right
die "addr_bits + type_bits >= 16\n"if $self->{addr_bits} + $self->{type_bits} > 16;
my $length = @{$self->{memory}};
print $fh pack 'n', $length or croak "Failed to print memory size: $!"; # uncoverable branch true
for (@{$self->{memory}}) {
( run in 1.084 second using v1.01-cache-2.11-cpan-39bf76dae61 )