#!/usr/bin/perl

use 5.008;
use strict;
use warnings;

use Getopt::Std;
use Pod::Usage;
use WAP::wmls;

my %opts;
getopts('hivSO:e:', \%opts);

my $flagOutputIntermediaire = $opts{i};
my $flagOutputAsm = $opts{S};
my $flagOptim = 1;
my $flagOptimExpr = 1;
if (exists $opts{O}) {
    if    ($opts{O} eq '2') {
    }
    elsif ($opts{O} eq 'n') {
        $flagOptim = 0;
        $flagOptimExpr = 0;
    }
    elsif ($opts{O} eq q{}) {
        $flagOptim = 1;
        $flagOptimExpr = 0;
    }
}

if ($opts{v}) {
    print "WAP::wmls $WAP::wmls::VERSION\n";
    print "$0\n";
    print "Perl $] on $^O\n";
    exit;
}
if ($opts{h} or !scalar(@ARGV)) {
    pod2usage(-verbose => 1);
}

my $srcname = $ARGV[0];
die "can't access '$srcname' ($!).\n"
        unless (-r $srcname);

my $parser = new WAP::wmls::parser;
if (exists $opts{e}) {
    $parser->YYData->{encoding} = $opts{e};
}
else {
    $parser->YYData->{encoding} = 'iso-8859-1';
}
$parser->YYData->{verbose_error} = 1;       # 0, 1
$parser->YYData->{verbose_warning} = 1;     # 0, 1
$parser->YYData->{verbose_info} = 1;        # 0, 1
$parser->YYData->{verbose_debug} = 0;       # 0, 1 (Optim)
$parser->YYData->{filename} = $srcname;
if ($flagOutputIntermediaire) {
    my $filename = $srcname;
    $filename =~ s/\.wmls$//;
    $filename .= '.i';
    open STDOUT, '>', $filename
            or die "can't open $filename ($!).\n";
}
$parser->Run();

unless (exists $parser->YYData->{nb_error}) {
    if ($flagOptim) {
        $parser->Optimize($flagOptimExpr);
    }
    if ($flagOutputIntermediaire) {
        my $visitor = new WAP::wmls::printVisitor();
        $parser->YYData->{PragmaList}->visit($visitor)
                if (defined $parser->YYData->{PragmaList});
        $parser->YYData->{FunctionList}->visit($visitor)
                if (defined $parser->YYData->{FunctionList});
    }
    else {
        if ($flagOutputAsm) {
            my $filename = $srcname;
            $filename =~ s/\.wmls$//;
            $filename .= '.s';
            open $WAP::wmls::asm::VERBOSE, '>', $filename
                    or die "can't open $filename ($!).\n";
            WAP::wmls::verbose::Init($parser->YYData->{filename});
        }
        $parser->generate();
        if ($flagOutputAsm) {
            close $WAP::wmls::asm::VERBOSE;
            WAP::wmls::verbose::End();
        }
    }
}

__END__

=head1 NAME

wmlsc - WMLScript Compiler

=head1 SYNOPSIS

wmlsc [options] I<file>.wmls

=head1 OPTIONS

=over 8

=item -e

charset encoding (ISO-8859-1 is the default)

=item -i

produce intermediary format in I<file>.i

=item -S

output mixte asm/source in I<file>.s

=item -O

enable simple optimizations

=item -O2

enable expression optimizations (default)

=item -On

disable all optimizations

=item -v

display version

=back

=head1 DESCRIPTION

B<wmlsc> compiles WMLScript file into a binary form.

The parser is generated by Parse::Yapp.

This compiler could be parametrized by the file 'wmlslibs.cfg'
what contains all the description of known libraries.

WAP Specifications are available on L<http://www.wapforum.org/>.

=head1 SEE ALSO

 wmlsd

=head1 COPYRIGHT

(c) 2002-2007 Francois PERRAD, France.

This program is distributed
under the terms of the Artistic Licence.

The WAP Specification are copyrighted by the Wireless Application Protocol Forum Ltd.
See L<http://www.wapforum.org/what/copyright.htm>.

=head1 AUTHOR

Francois PERRAD

=cut

