Execute SNUSP/Perl: Difference between revisions

m
Fixed syntax highlighting.
(Perl implementation of SNUSP)
 
m (Fixed syntax highlighting.)
 
(2 intermediate revisions by 2 users not shown)
Line 19:
 
Here's the main program:
<langsyntaxhighlight Perllang="perl">#!perl
use strict;
use warnings;
Line 36:
print qq[do "\Q$include\E" or die;\n];
 
my %commands; = (
$commands{ '<'} => q{ --$col; $col < 0 and die; };,
$commands{ '>'} => q{ ++$col };,
$commands{ '+'} => q{ ++$data[$row][$col] };,
$commands{ '-'} => q{ --$data[$row][$col] };,
$commands{ '.'} => q{ print chr $data[$row][$col] };,
$commands{ ','} => q{ goto (schedule(readchar($data[$row][$col]) ? 'NEXT' : 'REDO')) };,
# /, \, and ! are implemented in the main loop
 
$commands{ '?'} => q{ goto (schedule($data[$row][$col] ? 'NEXT' : 'SKIP')) };,
 
$commands{ '@'} => q{ push @st, 'SKIP' };,
$commands{ '#'} => q{ goto (pop @st) };,
 
$commands{ ':'} => q{ --$row; $row < 0 and die };,
$commands{ ';'} => q{ ++$row };,
$commands{ '%'} => q{ $_ = int rand(1 + $_) for $data[$row][$col] };,
# & is implemented in the main loop.
);
 
tr/;{}// or s/(\s*)\z/;$1/ for values %commands;
Line 294 ⟶ 295:
EX
 
__END__</syntaxhighlight>
</lang>
 
The following is the file which is included by each compiled perl program.
Line 302:
the main program.
 
<langsyntaxhighlight Perllang="perl">#!perl
use strict;
use warnings;
Line 358:
 
1;
__END__</syntaxhighlight>
</lang>
 
Here's a basic Hello, World! program, which I found at:
http://www.quirkster.com/iano/snusp/snusp-js.html
 
<langsyntaxhighlight SNUSPlang=snusp"> \/\ /\ /\ /\
+++ ++ ++ ++
+++ ++ /++++\ ++ ++ /++++\
Line 380 ⟶ 379:
. +> + + + + +- - +- -.
\.+/\++/ \++++/ + . +\ /-.+/- --
\ / \< / \ / \/</syntaxhighlight>
</lang>
 
When compiled, the SNUSP code above produces the Perl code below:
 
<langsyntaxhighlight lang="perl">#!perl
use strict;
use warnings;
Line 418 ⟶ 415:
goto (pop @st);# U_40_9 '#'
STACK_UNDERFLOW: ;
EXIT: exit($data[$col] || 0);</syntaxhighlight>
</lang>
 
This code produces:
9,479

edits