From 1756f22980afffd139ded7742af489196f928c1a Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 24 Mar 2018 18:25:30 +0200 Subject: [PATCH] Add perlcritic test and make code compliant --- MANIFEST | 6 +++-- lib/App/Scheme79asm.pm | 16 +++++++------ lib/App/Scheme79asm/Compiler.pm | 6 ++--- t/perlcritic.t | 10 ++++++++ t/perlcriticrc | 42 +++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 t/perlcritic.t create mode 100644 t/perlcriticrc diff --git a/MANIFEST b/MANIFEST index bcf5fe1..d94d257 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,9 +1,11 @@ Changes +lib/App/Scheme79asm.pm +lib/App/Scheme79asm/Compiler.pm Makefile.PL MANIFEST README scheme79asm t/App-Scheme79asm.t t/Compiler.t -lib/App/Scheme79asm.pm -lib/App/Scheme79asm/Compiler.pm +t/perlcritic.t +t/perlcriticrc diff --git a/lib/App/Scheme79asm.pm b/lib/App/Scheme79asm.pm index fdbbd21..fb99ee3 100644 --- a/lib/App/Scheme79asm.pm +++ b/lib/App/Scheme79asm.pm @@ -3,6 +3,8 @@ 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/; @@ -66,8 +68,8 @@ sub process { } $addr += (1 << $self->{addr_bits}) if $addr < 0; - die "Type too large: $type\n" unless $type < (1 << $self->{type_bits}); - die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits}); + 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}++; @@ -118,9 +120,9 @@ sub print_binary16 { die "addr_bits + type_bits >= 16\n"if $self->{addr_bits} + $self->{type_bits} > 16; my $length = @{$self->{memory}}; - print $fh pack('n', $length); + print $fh pack 'n', $length or croak "Failed to print memory size: $!"; for (@{$self->{memory}}) { - print $fh pack('n', $_) + print $fh pack 'n', $_ or croak "Failed to print memory: $!" } } @@ -142,9 +144,9 @@ sub print_verilog { my $spaces = ' ' x ($bits + 5 - (length $val)); $index = sprintf $index_format, $index; - print $fh "mem[$index] <= $val;"; - print $fh "$spaces // $comment" if defined $comment; - print $fh "\n"; + my $string = "mem[$index] <= $val;"; + $string .= "$spaces // $comment" if defined $comment; + say $fh $string or croak "Failed to print verilog: $!"; } } diff --git a/lib/App/Scheme79asm/Compiler.pm b/lib/App/Scheme79asm/Compiler.pm index f476262..bf03eea 100644 --- a/lib/App/Scheme79asm/Compiler.pm +++ b/lib/App/Scheme79asm/Compiler.pm @@ -106,7 +106,7 @@ sub new { my %self = ( symbols => ['', '', 'T'], nsymbols => 3, - symbol_map => {} + symbol_map => {}, ); bless \%self, $class; } @@ -187,12 +187,12 @@ sub process_toplevel { } elsif ($func eq 'LAMBDA') { my $func_name = $expr->cdr->car; my $func_args = $expr->cdr->cdr->car; - my $func_body = $expr->cdr->cdr->cdr->car; + my $func_body = $expr->cdr->cdr->cdr->car; ## no critic (ProhibitLongChainsOfMethodCalls) [PROC => $self->process_proc($func_name, $func_args, $func_body, $env)] } elsif ($func eq 'IF') { my ($if_cond, $if_then, $if_else) = map { $self->process_toplevel($_, $env) } - ($expr->cdr->car, $expr->cdr->cdr->car, $expr->cdr->cdr->cdr->car); + ($expr->cdr->car, $expr->cdr->cdr->car, $expr->cdr->cdr->cdr->car); ## no critic (ProhibitLongChainsOfMethodCalls) [IF => [LIST => $if_else, $if_then], $if_cond] } else { $self->process_funcall($expr->car, $expr->cdr, $env) diff --git a/t/perlcritic.t b/t/perlcritic.t new file mode 100644 index 0000000..e005e07 --- /dev/null +++ b/t/perlcritic.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Test::More; + +BEGIN { plan skip_all => '$ENV{RELEASE_TESTING} is false' unless $ENV{RELEASE_TESTING} } +use Test::Perl::Critic -profile => 't/perlcriticrc'; + +all_critic_ok 'lib' diff --git a/t/perlcriticrc b/t/perlcriticrc new file mode 100644 index 0000000..f6a22c0 --- /dev/null +++ b/t/perlcriticrc @@ -0,0 +1,42 @@ +severity = 1 + +[-BuiltinFunctions::ProhibitComplexMappings] +[-CodeLayout::RequireTidyCode] +[-ControlStructures::ProhibitPostfixControls] +[-ControlStructures::ProhibitUnlessBlocks] +[-Documentation::PodSpelling] +[-Documentation::RequirePodLinksIncludeText] +[-InputOutput::RequireBracedFileHandleWithPrint] +[-Modules::ProhibitAutomaticExportation] +[-References::ProhibitDoubleSigils] +[-RegularExpressions::ProhibitEnumeratedClasses] +[-RegularExpressions::ProhibitUnusualDelimiters] +[-RegularExpressions::RequireBracesForMultiline] +[-RegularExpressions::RequireLineBoundaryMatching] +[-Subroutines::ProhibitSubroutinePrototypes] +[-Subroutines::RequireFinalReturn] +[-ValuesAndExpressions::ProhibitConstantPragma] +[-ValuesAndExpressions::ProhibitEmptyQuotes] +[-ValuesAndExpressions::ProhibitLeadingZeros] +[-ValuesAndExpressions::ProhibitMagicNumbers] +[-ValuesAndExpressions::ProhibitNoisyQuotes] +[-Variables::ProhibitLocalVars] +[-Variables::ProhibitPackageVars] +[-Variables::ProhibitPunctuationVars] + +[BuiltinFunctions::ProhibitStringyEval] +allow_includes = 1 + +[RegularExpressions::RequireExtendedFormatting] +minimum_regex_length_to_complain_about = 20 + +[Documentation::RequirePodSections] +lib_sections = NAME | SYNOPSIS | DESCRIPTION | AUTHOR | COPYRIGHT AND LICENSE +script_sections = NAME | SYNOPSIS | DESCRIPTION | AUTHOR | COPYRIGHT AND LICENSE + +[Subroutines::RequireArgUnpacking] +short_subroutine_statements = 5 +allow_subscripts = 1 + +[TestingAndDebugging::ProhibitNoWarnings] +allow_with_category_restriction = 1 -- 2.39.2