[Cursor] Optimize ltm alternation after horizons a bit. Cuts 200 nodes off the state...
[pugs.git] / examples / hq9+.pl
blobbe53fa8f04d6057658ac8a759610be5e597a2eb8
1 class HQ9Plus;
3 # 2006-12-19 (rough) update to current spec
5 my subset HQ9PlusProgram
6 of Str
7 where /^ <[hq9+]>* $/;
9 my subset HQ9PlusStep
10 of HQ9PlusProgram
11 where { .chars == 1 };
13 my subset Error::OutOfBounds of Failure where True;
15 has HQ9PlusProgram $.program;
16 has Int $.accumulator = 0;
17 has Int $position = 0; # twigilless are private
19 has %actions = (
20 'h' => { self.hello },
21 'q' => { self.quine },
22 '9' => { self.nine },
23 '+' => { self.plus },
26 method run () {
27 # Java, anyone? Feel free to fix this.
28 loop {
29 .step;
30 CATCH {
31 when Error::OutOfBounds {
32 return; # end of program
34 default { fail }
39 method step () {
40 given $.program.substr($position++, 1) {
41 %actions{$_}();
45 my method hello () { say "Hello, world!" }
46 my method quine () { say $.program }
47 my method plus () { $.accumulator++ }
48 my method nine () {
49 my Int $i = 99;
51 while $i {
52 say qq:to/END/;
53 $i bottles of beer on the wall
54 $i bottles of beer!
55 Take one down, pass it around
56 END
57 $i--;
58 say "$i bottles of beer on the wall!"
62 =begin END
63 =head1 NAME
65 HQ9Plus - A HQ9+ implementation
67 =head1 SYNOPSIS
69 HQ9Plus.new(program => $foo).run;
71 my $program = HQ9Plus.new(program => $foo);
73 while ... {
74 $program.step;
75 ...inspect the program state...
78 =head1 DESCRIPTION
80 C<HQ9Plus> implements a I<HQ9+> interpreter with stepping.
82 =head1 SEE ALSO
84 http://en.wikipedia.org/wiki/HQ9+
86 =head1 AUTHOR
88 Ilmari Vacklin <ilmari.vacklin@helsinki.fi>
90 =head1 LICENSE
92 Public domain.