HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/share/doc/libparse-recdescent-perl/examples/demo_precalc.pl
#!/usr/bin/perl -ws

BEGIN
{
	unless (-f "Calc.pm")
	{
		print "You first need to build Calc.pm.\n\n",
		       "Try: perl -MParse::RecDescent - calc_grammar Calc\n",
		       " or: demo_buildcalc.pl\n\n";
		exit;
	}
}

use Parse::RecDescent;
use Calc;

sub evalop
{
	# my (@list) = @{[@{$_[0]}]};
	my (@list) = @{$_[0]};
	my $val = shift(@list)->();
	while (@list)
	{
		my ($op, $arg2) = splice @list, 0, 2;
		$op->($val,$arg2->());
	}
	return $val;
}

my $parse = Calc->new() or die "bad grammar";

print "> ";
while (<DATA>) {	# FOR DEMO CHANGE TO: while (<DATA>)
  print $parse->main($_), "\n\n> ";
}

__DATA__
$x = 2
$y = 3
+1-1+1-1+1-1+1-1+1
7*7-6*8
121/(121/11)/121*11
1/(10-1/(1/(10-1)))
$x * $y
foreach $i (1..$y) $x = $x * 2 + $i
$x