#!/usr/bin/perl -w

# Simple INTERCAL desk calculator

# This file is part of CLC-INTERCAL 1.-94.-6

# Copyright (c) 2006 Claudio Calvelli, all rights reserved.

# CLC-INTERCAL is copyrighted software. However, permission to use, modify,
# and distribute it is granted provided that the conditions set out in the
# licence agreement are met. See files README and COPYING in the distribution.

require 5.005;

use strict;
use Getopt::Long;

use vars qw($PERVERSION);
$PERVERSION = "CLC-INTERCAL bin/intercalc 1.-94.-6";

use Language::INTERCAL::Sick '1.-94.-6';
use Language::INTERCAL::GenericIO '1.-94.-5', qw($stdsplat);

if (defined &Getopt::Long::Configure) {
    Getopt::Long::Configure qw(no_ignore_case auto_abbrev permute bundling);
} else {
    $Getopt::Long::ignorecase = 0;
    $Getopt::Long::autoabbrev = 1;
    $Getopt::Long::order = $Getopt::Long::PERMUTE;
    $Getopt::Long::bundling = 1;
}

my $compiler = new Language::INTERCAL::Sick;
my $setoption = sub { $compiler->setoption(@_) };
my $language = 'sick';
my $user_interface = '';
my $verbhandle = $stdsplat;

GetOptions(
    # User Interface Options
    'graphic|X'     => sub { $user_interface = 'X' },
    'curses|c'      => sub { $user_interface = 'Curses' },
    'line'          => sub { $user_interface = 'Line' },
    'batch'         => sub { $user_interface = 'None' },
    'interface|i=s' => \$user_interface,
    # source language and compile options
    'bug=i'         => $setoption,
    'ubug=i'        => $setoption,
    'include|I=s'   => $setoption,
    'language|l=s'  => \$language,
    # misc options
    'verbose|v'     => sub { &$setoption('verbose', $verbhandle) },
    'quiet|q'       => sub { &$setoption('verbose', 0) },
    'stdverb=s'     => sub {
			       my ($opt, $file) = @_;
			       my $mode = $file =~ s/^([ra]),// ? lc($1) : 'r';
			       $verbhandle =
				   new Language::INTERCAL::GenericIO
				       ('FILE', $mode, $file);
			   },
) or usage();

$compiler->setoption('default_backend', 'Run');

sub usage {
    (my $p = $0) =~ s#^.*/##;
    die "Usage: $p [-alphabet] files...\n";
}

__END__

=pod

=head1 NAME

intercalc - CLC-INTERCAL desk calculator

=head1 SYNOPSIS

B<intercalc> [options]

=head1 DESCRIPTION

B<intercalc> is a simple desk calculator, allowing the user to
enter INTERCAL statements (to see what they do) and expressions
(to see what value they produce); it uses an interpreter object
from CLC-INTERCAL to provide immediate feedback.

The desk calculator accepts several options, some of which are documented here.

=head2 User Interface Options

=over 4

=item B<-X> / B<--graphic>

Enters X-based graphical user interface. Requires Perl-GTK. This is the
default if Perl-GTK is installed, the environment variable I<$DISPLAY> is
set and the opening of the X display succeeds.

=item B<-c> / B<--curses>

Enters full screen, curses-based interface. This is the default if the
X based interface cannot be started, the environment variable I<$TERM>
is set and the terminal name is known.

=item B<--line>

Enters the line-mode user interface. This is the default if the X based
and the curses based interfaces do not work.

=item B<--batch>

Avoids entering interactive mode. This is the default if the standard
input and output are not connected to a terminal and the X based interface
cannot be started.

=item B<-i>I<type> / B<--interface=>I<type>

Selects the user interface I<type>. Currently, only I<X>, I<Curses>,
I<Line> and I<None> are defined, but more can be installed as compiler
plug-ins. If the interface selected is I<None>, B<intercalc> will work in
batch mode. In addition, an empty string will reinstate the default
behaviour.

=back

=head2 Source language and compilation options

=over 4

=item B<--bug=>I<number>

Selects a different probability for the compiler bug. The compiler bug is
implemented by initialising the compiler's state with the required probability:
when a statement is compiled (usually at runtime), a "BUG" instruction is
emitted with the required probability. The default is 1%.

=item B<--ubug=>I<number>

Selects a probability for the unexplainable compiler bug. This is the compiler
bug which occurs when the probability of a (explainable) compiler bug is zero.
Only wimps would use this option. The default is 0.01%.

=item B<-I>I<path> / B<--include=>I<path>

Adds a directory before the standard search path for compiler objects
and source code. If a file is accessible from the current directory,
it is never searched in any include path.

If this option is repeated, the given paths will be searched in reverse
order (last specified searched first), followed by the standard paths,
in direct order.

=item B<-l>I<language> / B<--language=>I<language>

Selects the language to use when interpreting user input. This should
correspond to the name of a compiler, which is an INTERCAL object
which was originally built by I<iacc>. Only the expression and
statement parsers are used, so it is possible to test incomplete
compilers by loading them into I<intercalc> even if they don't
work with I<sick>.

=back

=head2 Misc Options

=over 4

=item B<-v> / B<--verbose>

Tells everything it's doing on Standard Error.

=item B<--stdverb=>I<file>

Sends verbose output to I<file> rather than standard error

=item B<-q> / B<--quiet>

Does not chatter to Standard Error or whatever.

=back

=head1 SEE ALSO

The INTERCAL on-line documentation, by entering B<sick>'s interactive mode
and finding the "help" menu (X), key (Curses) or command (Line).

