Acme-Pythonic
view release on metacpan or search on metacpan
t/calculator.t view on Meta::CPAN
# -*- Mode: Python -*-
#
# --- [ A port of the calculator example in Stroustrup's ]--------------
#
use Test::More 'no_plan';
use Acme::Pythonic debug => 0;
use strict
use warnings
use vars qw($cin $curr_tok $number_value $string_value %table)
use constant NAME => 0
use constant NUMBER => 1
use constant STOP => 2 # END is a Perl keyword
use constant PLUS => '+'
use constant MINUS => '-'
use constant MUL => '*'
use constant DIV => '/'
use constant PRINT => ';'
use constant ASSIGN => '='
use constant LP => '('
use constant RP => ')'
%table = (pi => 3.1415926535897932385,
e => 2.7182818284590452354)
$curr_tok = PRINT
sub main:
$cin = shift
while $cin ne '':
get_token()
last if $curr_tok eq STOP
next if $curr_tok eq PRINT
return expr(0)
sub expr:
my $get = shift
my $left = term($get)
while:
$left += term(1), last if $curr_tok eq PLUS
$left -= term(1), last if $curr_tok eq MINUS
return $left
sub term:
my $get = shift
my $left = prim($get)
while:
$left *= prim(1), last if $curr_tok eq MUL
if $curr_tok eq DIV:
if my $d = prim(1):
$left *= $d**(-1) # Filter::Simple eated too much with conventional notation
last
die
return $left
sub prim:
my $get = shift
get_token() if $get
if $curr_tok eq NUMBER:
my $v = $number_value
get_token()
return $v
if $curr_tok eq NAME:
( run in 1.683 second using v1.01-cache-2.11-cpan-e1769b4cff6 )