Skip to content
This repository
tag: v0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 100 lines (87 sloc) 2.479 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#! /usr/bin/env perl
use warnings;
use strict;
use 5.010;

BEGIN {
    use FindBin;
    use File::Spec;
    unshift @INC, File::Spec->catdir($FindBin::RealBin, "src");
}

use CompilerDriver ':all';
use Getopt::Long;
use autodie ':all';

my @evaluate;
my $module;
my $stagestats;
my $stopafter;
my $aot;
my $lang = 'CORE';
my $setting;
my $safe;
my $genapp;

sub usage {
    my ($fh, $ex) = @_;
    print $fh <<EOM;
niecza -- a command line wrapper for Niecza

usage: niecza -e 'code' # run a one-liner
OR: niecza file.pl # run a program
OR: niecza -c My::Module # precompile a module
OR: niecza # interactive shell

general options:
-L --language=NAME # select your setting
--gen-app=EXEFILE # make self-contained binary (not with -c)
--setting # precompile target is a setting
-v --stage-stats # detailed timing info
--stop-after=STAGE # stop after STAGE and dump AST
--aot # run ahead-of-time compiler
--safe # disable system interaction, implies -L SAFE
EOM
    exit $ex;
}

GetOptions('evaluate|e=s' => \@evaluate, 'aot' => \$aot,
        'gen-app=s' => \$genapp, 'compile|c' => \$module,
        'language|L=s' => \$lang, 'stage-stats|v' => \$stagestats,
        'stop-after=s' => \$stopafter, 'safe' => \$safe, 'setting' => \$setting)
    or usage(\*STDERR, 1);

my $excl = 0;
$excl++ if @evaluate;
$excl++ if @ARGV;
if ($excl > 1 || $module && !@ARGV || $safe && ($lang ne 'CORE')) {
    usage(\*STDERR, 1);
}

if (defined($genapp) && $module) {
    usage(\*STDERR, 1);
}

$lang = 'SAFE' if $safe;

sub run {
    compile(stopafter => $stopafter, aot => $aot, selfcontained => $genapp,
        stagetime => $stagestats, lang => $lang, safe => $safe,
        setting => $setting, @_);
    system 'mono', CompilerDriver::build_file('MAIN.exe')
        if !({@_}->{name}) && !$stopafter && !$genapp;
}

if ($module) {
    for (@ARGV) {
        run(name => $_);
    }
} elsif (@ARGV) {
    require File::Slurp;
    for (@ARGV) {
        run(file => $_);
    }
} elsif (@evaluate) {
    for (@evaluate) {
        run(code => $_);
    }
} else {
    require Term::ReadLine;
    my $term = Term::ReadLine->new('niecza');
    while (defined ($_ = $term->readline("> ")) ) {
        /^\s*[^\s#]/ or next;
        eval {
            run(code => "say do $_");
        };
        say $@ if $@;
        $term->addhistory($_) if /\S/;
    }
}
Something went wrong with that request. Please try again.