| #! /usr/bin/perl |
| use strict; |
| use Getopt::Long; |
| |
| # options |
| my @optlist = ("h|help!","v|verbose!","d=s","J=s"); |
| my $result = GetOptions @optlist; |
| our ($opt_h, $opt_v, $opt_d, $opt_J); |
| |
| # -h option || check the number of arguments |
| if ($opt_h || @ARGV != 1 ) { |
| usage(); |
| exit 1; |
| } |
| |
| # local variables |
| my $echo = "/bin/echo"; |
| my $am_file = $ARGV[0]; |
| |
| my %env; |
| &init(); |
| |
| # -d option |
| if ($opt_d && (! -d $opt_d)) { |
| &error("directory $opt_d does not exist."); |
| } |
| |
| # -J option |
| if ($opt_J) { |
| if ($opt_J =~ /(-cp|-classpath)\s+/) { |
| &error("can not use $1 in -J option"); |
| } |
| $env{"system_opts"} .= " $opt_J"; |
| } |
| |
| # main |
| if (! -e $am_file) { |
| &error("file $am_file does not exist."); |
| } |
| |
| my $arg; |
| if ($env{"goal"}) { |
| $arg .= $env{"goal"} . " "; |
| } |
| $arg .= "[\'$am_file\'"; |
| if ($opt_d) { # check -d option |
| $arg .= ", \'$opt_d\'"; |
| } |
| $arg .= "]."; |
| |
| my $cmd = "$echo \"$arg\" | " . $env{"system"}; |
| if ($env{"system_opts"}) { |
| $cmd .= " " . $env{"system_opts"}; |
| } |
| if ($env{"system_args"}) { |
| $cmd .= " " . $env{"system_args"}; |
| } |
| if (! $opt_v) {# check -v option |
| $cmd .= " 2> /dev/null"; |
| } |
| |
| &message("{START translating $am_file --> Java}"); |
| &message($cmd); |
| system($cmd) && error("translation fails"); |
| &message("{END translating $am_file --> Java}\n"); |
| |
| exit 0; |
| |
| # sub |
| sub usage { |
| print "\nUsage: $0 [-options] am_file\n"; |
| print "\n where options include:\n"; |
| print "\t-h -help : print this help message\n"; |
| print "\t-v -verbose : enable verbose output\n"; |
| print "\t-d directory : set the destination directory for am_file.\n"; |
| print "\t : The destination directory must already exist\n"; |
| print "\t-J option : option must be enclosed by '.\n"; |
| print "\t : pass option to the Java Virtual Machine\n"; |
| print "\t : (ex. -J '-Xmx100m -verbose:gc')\n"; |
| print "\n"; |
| } |
| |
| sub message { |
| my ($x) = @_; |
| if ($opt_v) { # check -v option |
| print "\% $x\n"; |
| } |
| } |
| |
| sub error { |
| my ($x) = @_; |
| print "\% ERROR: $x: $0\n"; |
| exit 1; |
| } |
| |
| #sub init { |
| # %env = ( |
| # "goal", "", |
| # "system", "/Users/banbara/prog/plcafe/PrologCafe095/src/compiler/am2j.sav", |
| # "system_opts", "", |
| # "system_args", "", |
| # ); |
| # %env = ( |
| # "goal", "load('/Users/banbara/prog/plcafe/PrologCafe095/src/compiler/pl2am.ql'), main. ", |
| # "system", "sicstus", |
| # "system_opts", "", |
| # "system_args", "", |
| # ); |
| # %env = ( |
| # "goal", "", |
| # "system", "java", |
| # "system_opts", "-cp \$PLCAFEDIR/plcafe.jar:\$CLASSPATH", |
| # "system_args", "com.googlecode.prolog_cafe.lang.PrologMain com.googlecode.prolog_cafe.compiler.pl2am:main", |
| # ); |
| #} |
| |