#!/usr/bin/perl -w
# Daniel Reeves
# TODO: quoted or escaped chars/strings will get converted.


use strict;
use diagnostics;

my $sexp = "";
while(<>) { $sexp .= $_; }
my $orig = $sexp;

# remove stupid dos carriage-returns and remove trailing whitespace...
$sexp =~ s/\r//g;
$sexp =~ s/\s*$//s;

# separate expressions with commas instead of whitespace:
$sexp =~ s/(\s+)([^\)\s])/\,$1$2/g;

# parens to curly braces...
$sexp =~ s/\(/\{/g;
$sexp =~ s/\)/\}/g;

# functions to their mathematica equivalents...
#   (TODO: eg, Names["+"] gives {"Plus"})
$sexp =~ s/\+/Plus/g;
$sexp =~ s/\-\,/Subtract\,/g;
$sexp =~ s/\*/Times/g;
$sexp =~ s/\//Divide/g;
$sexp =~ s/if/If/g;
$sexp =~ s/\<\=/LessEqual/g;
$sexp =~ s/\>\=/GreaterEqual/g;
$sexp =~ s/\</Less/g;
$sexp =~ s/\>/Greater/g;
$sexp =~ s/\=\=/Equal/g;
$sexp =~ s/\!\=/Unequal/g;
$sexp =~ s/\=/Equal/g;
$sexp =~ s/or/Or/g;
$sexp =~ s/and/And/g;
$sexp =~ s/not/Not/g;


open(MMA, "echo \"$sexp\" | ./lisp-to-mma-helper.m |")
  or die("Error opening pipe to/from mma script: $!\n");
while(<MMA>) { print; }
close(MMA);

