#!/usr/bin/perl

# Scheme Evaluator

# Convert infix to prefix
# Assume there are always spaces between elements


sub convert
{
    my $e = $_[0]; # expression to be converted
    if ($e =~ /(\d+)([-+*\/])(\d)+/)
	{
	    my $a = convert($1);
	    my $b = convert($3);
	    "($2 $a $b)";
	}
   
    else {if ($e =~ /\(.+\)/)  # compound expression
    {
	chop $e;  # gets rid of last char
	$e =~ s/\(//;   # s///g will replace all
#	$e =~ s/([\+])4/${1}5/;
#	print "e is now $e\n";
	convert($e);
    }
    else # simple expression
    {
	$e;    # leave alone
    }}  # ifelse
}

print convert("3"), "\n";
print convert("(3+4)"), "\n";
print convert("((3+x)-(4*y))"), "\n";

print "\ndone\n";

