Math-SymbolicX-Calculator-Interface-Shell

 view release on metacpan or  search on metacpan

lib/Math/SymbolicX/Calculator/Interface/Shell.pm  view on Meta::CPAN

my $Ident = $Math::SymbolicX::Calculator::Identifier_Regex;

=head1 NAME

Math::SymbolicX::Calculator::Interface::Shell - A Calculator Shell

=head1 SYNOPSIS

  # simplest form of usage:
  use Math::SymbolicX::Calculator::Interface::Shell;
  my $shell = Math::SymbolicX::Calculator::Interface::Shell->new();
  $shell->run();

=head1 DESCRIPTION

This module implements a shell interface to the Math::SymbolicX::Calculator.

=head1 METHODS

=cut


# defined or
sub _dor {
    foreach (@_) {
        return $_ if defined $_;
    }
    return(undef);
}


=head2 new

Returns a new shell application object. Call the C<run()> method on it
to run the application.

Optional parameters: (default in parenthesis)

  calc => a Math::SymbolicX::Calculator object to use
  input_handle => a file handle to read from (\*STDIN)
  prompt => the prompt string to use ('~> ')
  continued_prompt => prompt string to use for continued lines ('>> ')
  app_name => the name of the application ('Symbolic Calculator Shell')

=cut

sub new {
    my $proto = shift;
    my $class = ref($proto)||$proto;

    my %args = @_;

    my $self = {
        calc             => $args{calculator}
                            || Math::SymbolicX::Calculator->new(),
        input_handle     => $args{input_handle} || \*STDIN,
        prompt           => _dor($args{prompt}, '~> '),
        continued_prompt => _dor($args{continued_prompt}, '>> '),
        app_name         => _dor($args{app_name}, 'Symbolic Calculator Shell'),
    };
    bless $self => $class;

    $self->_setup_readline();

    return $self;
}

sub _setup_readline {
    my $self = shift;
    $self->{readline} = Term::ReadLine->new(
        $self->{app_name}, $self->{input_handle}, *STDOUT,
    );
    $self->{readline}->ornaments(0);
}


=head2 run

Runs the main loop of the shell.

=cut

sub run {
    my $self = shift;

    # FIXME refactor
    # Main Loop
    while (1) {
        # get a new expression.
        my $expr = $self->get_expression();
        
        return $self->exit_hook() if not defined $expr;

        my $cmd;
        # What type of command?
        if ($expr =~ /=~~?/) {
            $cmd = $self->_parse_transformation($expr);
        }
        elsif ($expr =~ /=/) {
            $cmd = $self->_parse_assignment($expr);
        }
        else {
            $cmd = $self->_parse_command($expr);
        }
    
        if (not defined $cmd) {
            next;
        }
        elsif (_INSTANCE($cmd, 'Math::SymbolicX::Calculator::Command')) {
            my @output = $self->calc->execute($cmd);
            $self->_generic_out(@output);
        }
        elsif (ref($cmd) eq 'ARRAY') {
            if ($cmd->[0] eq 'print') {
                $self->_generic_out(@{$cmd}[1..$#$cmd]);
            }
        }
        elsif ($cmd eq 'exit') {
            return $self->exit_hook();
        }
        else {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.168 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )