Skip to content
This repository
tree: 149198748f
Fetching contributors…

Cannot retrieve contributors at this time

file 310 lines (280 sloc) 8.929 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
use MONKEY_TYPING;
use CClass;
use CgOp;
use GetOptLong;
use NieczaActions;
use NieczaBackendDotnet;
use NieczaCompiler;
use NieczaFrontendSTD;
use NieczaPassSimplifier;
use NieczaPathSearch;
use Op;
use Operator;
use OpHelpers;
use OptBeta;
use OptRxSimple;
use RxOp;
use Sig;
use STD;

augment grammar STD {
method explain_mystery($nested?) {
    my %post_types;
    my %unk_types;
    my %unk_routines;
    my $m = '';
    for keys(%*MYSTERY) {
        my $p = %*MYSTERY{$_}.<lex>;
        if self.is_name($_, $p) {
            # types may not be post-declared
            %post_types{$_} = %*MYSTERY{$_};
            next;
        }

        next if self.is_known($_, $p) or self.is_known('&' ~ $_, $p);

        # just a guess, but good enough to improve error reporting
        if $_ lt 'a' {
            %unk_types{$_} = %*MYSTERY{$_};
        }
        else {
            %unk_routines{$_} = %*MYSTERY{$_};
        }
    }
    if %post_types {
        my @tmp = sort keys(%post_types);
        $m ~= "Illegally post-declared type" ~ ('s' x (@tmp != 1)) ~ ":\n";
        for @tmp {
            $m ~= "\t'$_' used at line " ~ %post_types{$_}.<line> ~ "\n";
        }
    }
    if %unk_types {
        my @tmp = sort keys(%unk_types);
        $m ~= "Undeclared name" ~ ('s' x (@tmp != 1)) ~ ":\n";
        for @tmp {
            $m ~= "\t'$_' used at line " ~ %unk_types{$_}.<line> ~ "\n";
        }
    }
    if %unk_routines {
        my @tmp = sort keys(%unk_routines);
        $m ~= "Undeclared routine" ~ ('s' x (@tmp != 1)) ~ ":\n";
        for @tmp {
            $m ~= "\t'$_' used at line " ~ %unk_routines{$_}.<line> ~ "\n";
        }
    }
    self.sorry($m) if $m;

    unless $nested {
        for $*unit.stubbed_stashes -> $pos, $type {
            next if $type.closed || $type.kind eq 'package';
            self.cursor($pos).sorry("Package was stubbed but not defined");
        }
    }

    self;
}
method nibbler() {
    my @nibbles;
    my $from = self.pos;
    my $len = self.orig.chars;
    my $to = $from;

    loop {
        my $here = self.cursor($to);
        last if head($here.stopper);

        if head($here.starter) -> $starter {
            push @nibbles, Match.synthetic(:cursor(self), :$from, :$to,
                :method<Str>, :captures()) if $from != $to;

            my $nibbler = head(self.cursor($starter.to).nibbler) or return;
            my $stopper = head(self.cursor($nibbler.to).stopper) or return;

            $from = $to = $stopper.to;
            push @nibbles, $starter;
            push @nibbles, @( $nibbler<nibbles> );
            push @nibbles, $stopper;
        }
        elsif head($here.escape) -> $escape {
            push @nibbles, Match.synthetic(:cursor(self), :$from, :$to,
                :method<Str>, :captures()) if $from != $to;

            $from = $to = $escape.to;
            push @nibbles, $escape;
        }
        elsif $to < $len {
            $to++;
        }
        else { # at end, and not stopper
            return;
        }
    }

    push @nibbles, Match.synthetic(:cursor(self), :$from, :$to,
        :method<Str>, :captures()) if $from != $to || !@nibbles;

    $*LAST_NIBBLE = $to;
    $*LAST_NIBBLE_START = self.pos;
    if defined substr(self.orig, self.pos, $to - self.pos).index("\n") {
        $*LAST_NIBBLE_MULTILINE = $to;
        $*LAST_NIBBLE_MULTILINE_START = self.pos;
    }

    Match.synthetic(:cursor(self), from => self.pos, :$to, :method<nibbler>,
        :captures(nibbles => @nibbles))
}
}

augment class RxOp::Sym {
    method clone(:$captures) {
        self.WHAT.new(text => $!text, igcase => $!igcase, igmark => $!igmark,
            endsym => $!endsym, :$captures);
    }
}

augment class NieczaActions {
method statement_prefix:BEGIN ($/) {
    # MAJOR HACK - allows test code like BEGIN { @*INC.push: ... } to work
    repeat while False {
        my $c = ($<blast><statement> || $<blast><block><blockoid>).ast;

        last unless $c ~~ Op::StatementList;
        last unless $c.children == 1;
        my $d = $c.children.[0];
        last unless $d ~~ Op::CallMethod;
        last unless $d.receiver ~~ Op::ContextVar;
        last unless $d.receiver.name eq '@*INC';
        last if $d.private || $d.ismeta;
        last unless $d.name eq any <push unshift>;
        last unless +$d.getargs == 1;
        last unless defined my $str = self.trivial_eval($/, $d.getargs.[0]);
        @*INC."$d.name()"($str);
        make ::Op::StatementList.new;
        return;
    }

    $*CURLEX<!sub>.create_static_pad;
    my $con = self.make_constant($/, 'anon', 'BEGIN');
    $<blast>.ast.run_BEGIN($con.name);
    $con.init = True;
    make $con;
}
method init_constant($con, $rhs) {
    my $body = self.thunk_sub($rhs, name => "$con.name() init");
    $body.outer.create_static_pad;
    $body.run_BEGIN($con.name);
    $con.init = True;
    $con;
}
method blockoid($/) {
    # XXX horrible cheat, but my data structures aren't up to the task of
    # $::UNIT being a class body &c.
    if $/ eq '{YOU_ARE_HERE}' {
        $*unit.set_bottom($*CURLEX<!sub>);
        $*CURLEX<!sub>.create_static_pad;
        $*CURLEX<!sub>.noninlinable;

        loop (my $l = $*CURLEX<!sub>; $l; $l.=outer) {
            # this isn't *quite* right, as it will cause declaring
            # anything more in the same scope to fail.
            # ... and we have to be careful not to mark anon_0 used
            # or installing this very block will fail!
            substr($_,0,4) ne 'anon' and $/.CURSOR.mark_used($_)
                for $l.lex_names;
        }

        make ::Op::YouAreHere.new(|node($/), unitname => $*UNITNAME);
    } else {
        make $<statementlist>.ast;
    }
}
}

CgOp._register_ops: < who sc_root sc_indir temporize _addmethod _invalidate
>;

my $usage = q:to/EOM/;
niecza -- a command line wrapper for Niecza

usage: niecza -e 'code' # run a one-liner
   OR: niecza file.pl [args] # run a program
   OR: niecza -C MyModule # precompile a module
   OR: niecza # interactive shell

general options:
   -n # short for -L CORN
   -p # short for -L CORP
   -B --backend=NAME # select backend (dotnet)
   -L --language=NAME # select your setting
   -I --include=DIR # add a directory to search for modules
   -v --verbose # detailed timing info
   -c --compile # don't run (implied with -C)
      --safe # disable system interaction
      --help # display this message

backend options:
      --obj-dir=DIR # select output location (all)
EOM

my $runobj = Q:CgOp { (box Str (rawcall get_BaseDirectory (rawscall System.AppDomain.get_CurrentDomain))) };
my $basedir = $runobj.IO.append("..").realpath;

my @lib = $basedir.append("lib"), ".".IO.realpath;
my $lang = "CORE";
my $safe = False;
my $bcnd = "dotnet";
my $odir = $basedir.append("obj");
my $verb = 0;
my @eval;
my $cmod = False;
my $comp = False;

GetOptions(:!permute,
    "evaluate|e=s" => sub { push @eval, $_ },
    "compile-module|C" => sub { $cmod = True },
    "backend|B=s" => sub { $bcnd = $_ },
    "language|L=s" => sub { $lang = $_ },
    "p" => sub { $lang = "CORP" },
    "n" => sub { $lang = "CORN" },
    "verbose|v" => sub { $verb++ },
    "compile|c" => sub { $comp = True },
    "safe" => sub { $safe = True },
    "include|I=s" => sub { unshift @lib, $_.IO.realpath },
    "obj-dir=s" => sub { $odir = $_ },
    "help|h" => sub { say $usage; exit 0 },
);

my @*INC;
my $backend;
if $bcnd eq 'dotnet' || $bcnd eq 'mono' {
    $backend = NieczaBackendDotnet.new(obj_dir => $odir, safemode => $safe);
}
else {
    note "Backend '$bcnd' not supported";
    exit 1;
}

my $c = NieczaCompiler.new(
    module_finder => NieczaPathSearch.new(
        path => @lib,
    ),
    frontend => NieczaFrontendSTD.new(
        lang => $lang,
        safemode => $safe,
    ),
    backend => $backend,
    verbose => $verb,
);

if $cmod {
    if @eval {
        note "Module compilation cannot be used with strings to evaluate";
        exit 1;
    }
    if !@*ARGS {
        say "No modules named to compile!";
        exit 0;
    }
    for @*ARGS {
        $c.compile_module($_);
    }
}
elsif @eval {
    $c.backend.run_args = @*ARGS;
    for @eval {
        $c.compile_string($_, !$comp);
    }
}
elsif @*ARGS {
    my $file = shift @*ARGS;
    $c.backend.run_args = @*ARGS;
    $c.compile_file($file, !$comp);
}
else {
    my $*repl_outer;
    my $*repl_outer_frame;
    $c.compile_string('$PROCESS::OUTPUT_USED ::= True', !$comp, :repl,
        :evalmode);
    while True {
        print "niecza> ";
        my $l = $*IN.get // last;
        my $ok;
        try {
            $c.compile_string($l, !$comp, :repl, :evalmode,
                :outer($*repl_outer), :outer_frame($*repl_outer_frame));
            $ok = True;
        }
        say $! unless $ok;
    }
    say "";
}
Something went wrong with that request. Please try again.