-
Notifications
You must be signed in to change notification settings - Fork 15
/
niecza
212 lines (192 loc) · 5.19 KB
/
niecza
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
use Metamodel;
use Op;
use RxOp;
use CClass;
use CgOp;
use Body;
use Unit;
use JSYNC;
use NAMOutput;
use NieczaFrontendSTD;
use NieczaPassBegin;
use NieczaPassBeta;
use NieczaPassSimplifier;
use NieczaBackendNAM;
use NieczaBackendDotnet;
use NieczaBackendClisp;
use NieczaBackendHoopl;
use NieczaPathSearch;
use NieczaCompiler;
use MONKEY_TYPING;
use GetOptLong;
use NieczaActions;
use OpHelpers;
use Operator;
use OptRxSimple;
use STD;
use Sig;
augment class NieczaActions {
method integer($/) {
$<decint> andthen make [10, ~$<decint>];
$<octint> andthen make [8, ~$<octint>];
$<hexint> andthen make [16, ~$<hexint>];
$<binint> andthen make [2, ~$<binint>];
}
method dec_number ($/) {
if $<escale> { make +((~$/).comb(/<-[_]>/).join("")) }
else { make [10, ~$/] }
}
method number($/) {
my $child = $<integer> // $<dec_number> // $<rad_number>;
make (defined($child) ?? $child.ast !!
$child eq 'NaN' ?? NaN !! Inf);
}
}
# XXX should there be a different class for this?
augment class Op::Num { #OK exist
method code($) {
if $.value ~~ Array {
CgOp.const(CgOp.exactnum(|$.value))
} else {
CgOp.const(CgOp.box('Num', CgOp.double($.value)))
}
}
}
augment class Op::Labelled { #OK exist
method statement_level() {
self.new(name => $.name, stmt => $.stmt.statement_level);
}
}
class RxOp::CutBrack is RxOp {
method code($) { CgOp.rxcall('CommitGroup', CgOp.str("BRACK")) }
method lad() { [ 'Null' ]; }
}
augment class CgOp {
method outerlex ($n) { self._cgop("outerlex", $n) }
method newtypedscalar ($t) { self._cgop("newtypedscalar", $t) }
method bif_rand () { self._cgop("bif_rand") }
method exactnum ($base, $digits) { self._cgop("exactnum", $base, $digits) }
method bif_rat_approx ($center, $eps) { self._cgop("bif_rat_approx", $center, $eps) }
}
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:
-B --backend=NAME # select backend (nam, dotnet, clisp, hoopl)
-L --language=NAME # select your setting
-v --verbose # detailed timing info
-c --compile # don't run (implied with -C)
--stop-after=STAGE # stop after STAGE and dump AST
--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;
my $stop = "";
my $aotc = 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 = $_ },
"verbose|v" => sub { $verb++ },
"compile|c" => sub { $comp = True },
"safe" => sub { $safe = True },
"stop=s" => sub { $stop = $_ },
"aot" => sub { $aotc = True },
"include|I=s" => sub { unshift @lib, $_.IO.realpath },
"obj-dir=s" => sub { $odir = $_ },
"help|h" => sub { say $usage; exit 0 },
);
my $backend;
if $bcnd eq 'nam' {
$backend = NieczaBackendNAM.new(obj_dir => $odir);
}
elsif $bcnd eq 'dotnet' || $bcnd eq 'mono' {
$backend = NieczaBackendDotnet.new(obj_dir => $odir, safemode => $safe);
}
elsif $bcnd eq 'clisp' {
$backend = NieczaBackendClisp.new(obj_dir => $odir);
}
elsif $bcnd eq 'hoopl' {
$backend = NieczaBackendHoopl.new(obj_dir => $odir);
}
else {
note "Backend '$bcnd' not supported";
exit 1;
}
my $stages;
if $bcnd eq 'clisp' {
$stages = [
NieczaPassBegin.new,
NieczaPassSimplifier.new,
];
} elsif $bcnd eq 'hoopl' {
$stages = [
NieczaPassBegin.new,
NieczaPassSimplifier.new,
];
} else {
$stages = [
NieczaPassBegin.new,
NieczaPassBeta.new,
NieczaPassSimplifier.new,
];
}
my $c = NieczaCompiler.new(
module_finder => NieczaPathSearch.new(
path => @lib,
),
frontend => NieczaFrontendSTD.new(
lang => $lang,
safemode => $safe,
),
stages => $stages,
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($_, $stop);
}
}
elsif @eval {
$c.backend.run_args = @*ARGS;
for @eval {
$c.compile_string($_, !$comp, $stop);
}
}
elsif @*ARGS {
my $file = shift @*ARGS;
$c.backend.run_args = @*ARGS;
$c.compile_file($file, !$comp, $stop);
}
else {
while True {
print "niecza> ";
my $l = $*IN.get // last;
$c.compile_string("say (" ~ $l ~ ").perl", !$comp, $stop);
}
}