Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

RPN comparisons and documentation updates

  • Loading branch information...
commit db5b207e5cc75d24f94548818d59fd343966e80d 1 parent 4b19b9d
Scott Penrose scottp authored
14 README
View
@@ -30,12 +30,13 @@ Goals
Directories
-----------
-games - Games should go in here :)
-euler - Answers for Project Euler <http://projecteuler.net>
-perlmonks - Answers to perlmonks.org questions
-wsg - Answers for Winter Scripting Games, divided by difficulty and year
-bin - utility scripts
-lib - utility modules
+99-problems - Based on lisp 99 problems
+games - Games should go in here :)
+euler - Answers for Project Euler <http://projecteuler.net>
+perlmonks - Answers to perlmonks.org questions
+wsg - Answers for Winter Scripting Games, divided by difficulty and year
+bin - utility scripts
+lib - utility modules
License Information
@@ -47,4 +48,3 @@ License 1.0, version 2.0 is GPL compatible by itself, hence there is no benefit
to having an Artistic 2.0 / GPL disjunction.) See the file LICENSE for
details.
-just testing commits
48 bin/rpn1
View
@@ -0,0 +1,48 @@
+#!/usr/local/bin/perl6
+
+# FROM: http://blog.afoolishmanifesto.com/archives/341 - fREW Schmidt
+
+use v6;
+
+my %op_dispatch_table = {
+ '+' => sub (@stack) {
+ @stack.push(@stack.pop + @stack.pop);
+ },
+ '-' => sub (@stack) {
+ # this should probably be:
+ # @stack.push(@stack.pop R- @stack.pop);
+ my $s = @stack.pop;
+ @stack.push(@stack.pop - $s);
+ },
+ '*' => sub (@stack) {
+ @stack.push(@stack.pop * @stack.pop);
+ },
+ '/' => sub (@stack) {
+ # this should probably be:
+ # @stack.push(@stack.pop R/ @stack.pop);
+ my $s = @stack.pop;
+ @stack.push(@stack.pop / $s);
+ },
+ 'sqrt' => sub (@stack) {
+ @stack.push(@stack.pop.sqrt);
+ },
+};
+
+sub evaluate (%odt, $expr) {
+ my @stack;
+ my @tokens = $expr.split(/\s+/);
+ for @tokens -> $token {
+ if $token ~~ /^\d+$/ {
+ @stack.push($token);
+ } else {
+ if my &fn = %odt{$token} {
+ &fn(@stack);
+ } else {
+ die "Unrecognized token '$token'; aborting";
+ }
+ }
+ }
+ return @stack.pop;
+}
+
+say "Result: { evaluate(%op_dispatch_table, @*ARGS[0]) }";
39 bin/rpn2
View
@@ -0,0 +1,39 @@
+#!/usr/local/bin/perl6
+
+# FROM - http://daniel.carrera.bz/2009/06/rpn-calculator-in-perl-6/
+use v6;
+
+token Op {'/' || '*' || '+' || '-'};
+token Value { \d+[\.\d+]? };
+token Item { <Value> || <Op> };
+token Expr { [<ws> <Item> <ws>]+ };
+
+# Read from the command line.
+my $str = @*ARGS[0];
+
+if $str ~~ /^ <Expr> $/ {
+ my @stack;
+ for $/<Expr><Item> -> $item {
+ if $item<Value> {
+ @stack.push($item<Value>);
+ } else {
+ my $v1 = @stack.pop;
+ my $v0 = @stack.pop;
+ @stack.push(do_op($v0,$v1,$item<Op>));
+ }
+ }
+ say @stack[0];
+}
+else {
+ say "This is not an RPN expression.";
+}
+
+sub do_op($lhs, $rhs, $op) {
+ given $op {
+ when '*' { $lhs * $rhs }
+ when '+' { $lhs + $rhs }
+ when '-' { $lhs - $rhs }
+ when '/' { $lhs / $rhs }
+ }
+}
+
23 bin/rpn3
View
@@ -0,0 +1,23 @@
+#!/usr/local/bin/perl6
+use v6;
+
+# FROM: http://daniel.ruoso.com/categoria/perl/rpn-calculator-perl6 by daniel@ruoso.com
+
+multi infix:<rpn> (@stack, $num where /^ \d+[\.\d+]? $/) {
+ [ @stack, $num ];
+}
+multi infix:<rpn> (@stack, $op where /^ '+' | '-' | '*' | '/' $/) {
+ my $lhs = @stack.pop;
+ my $rhs = @stack.pop;
+ my $res = do given $op {
+ when '+' { $rhs + $lhs }
+ when '-' { $rhs - $lhs }
+ when '*' { $rhs * $lhs }
+ when '/' { $rhs / $lhs }
+ }
+ [ @stack, $res ]
+}
+multi infix:<rpn> ($any, $unknown) {
+ die "Unkown expression near $any $unknown";
+}
+say [rpn] [], (~@*ARGS).words;
13 doc/README
View
@@ -0,0 +1,13 @@
+Perl 6 Examples Documentation
+=============================
+
+Documentation� where possible, should be inline in the example.
+There are always other documents required. Create a sensible name,
+where possible include in the form:
+
+ DirectoryName-ProgramName.pod (.txt, .html)
+
+
+License Information
+-------------------
+See ../README
17 doc/bin-rpn.pod
View
@@ -0,0 +1,17 @@
+=begin pod
+=head1 NAME
+RPN[1..3] - command line reverse polish notation calculators
+=head1 DESCRIPTION
+The three examples provided here are implmented in three very different ways.
+They provide an excellent example of TMTOWTDI
+=head1 COMPARISON
+=head2 rpn1
+This is done via a loop and dispatch table. It is a direct translation of the
+Perl 5 version (see web site) - http://blog.afoolishmanifesto.com/archives/341
+=head2 rpn2
+This version uses a token parser and is a good example of the power in Perl 6
+pattern matching.
+=head2 rpn3
+This version uses the infix operator and demonstrates writing your own
+operators, or overloading existing ones.
+=end pod
Please sign in to comment.
Something went wrong with that request. Please try again.