-
Notifications
You must be signed in to change notification settings - Fork 15
/
NieczaFrontendSTD.pm6
302 lines (246 loc) · 8.39 KB
/
NieczaFrontendSTD.pm6
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
our ($Actions, $Backend);
class NieczaFrontendSTD;
use STD;
use NieczaGrammar;
use MONKEY_TYPING;
augment class STD {
my %term = (:dba('term') , :prec<z=>);
my %methodcall = (:dba('methodcall') , :prec<y=>, :assoc<unary>, :uassoc<left>, :fiddly, :!pure);
my %symbolic_unary = (:dba('symbolic unary') , :prec<v=>, :assoc<unary>, :uassoc<left>, :pure);
my %additive = (:dba('additive') , :prec<t=>, :assoc<left>, :pure);
my %named_unary = (:dba('named unary') , :prec<o=>, :assoc<unary>, :uassoc<left>, :pure);
# TODO: allow variable :dba()s
method cat_O($cat, $sym) {
my $name = "&{$cat}:<$sym>";
self.function_O($name) // self.default_O($cat, $sym);
}
method function_O($name) {
my @lex = self.lookup_lex($name);
if @lex && @lex[0] eq 'dispatch' {
@lex = self.lookup_lex($name ~ ":(!proto)");
}
return Any unless @lex;
my $sub;
if @lex[0] eq 'common' {
$sub = $*unit.get(@lex[4], @lex[5]);
} elsif @lex[0] eq 'sub' {
$sub = @lex[4];
}
if $sub && $sub.kind eq 'sub' {
my %ext = $sub.get_extend('prec');
return %ext || Any;
} else {
return Any;
}
}
method default_O($cat, $sym) {
given $cat {
when 'infix' { return %additive }
when 'prefix' {
return ($sym ~~ /^\W/) ?? %symbolic_unary !! %named_unary
}
when 'postfix' { return %methodcall }
when 'circumfix' { return %term }
when 'postcircumfix' { return %methodcall }
when 'term' { return %term }
default {
self.sorry("Cannot extend category:$cat with subs");
return %additive
}
}
}
sub rolecache($key, $thunk) {
state %cache;
(%cache{$key}:exists) ?? %cache{$key} !! (%cache{$key} := $thunk())
}
method balanced ($start,$stop) { self.mixin( rolecache("B$start\0$stop", {STD::startstop[$start,$stop]}) ); }
method unbalanced ($stop) { self.mixin( rolecache("U$stop", {STD::stop[$stop]}) ); }
method unitstop ($stop) { self.mixin( rolecache("N$stop", {STD::unitstop[$stop]}) ); }
method cat_role($cat,$sym) {
state %cat_cache;
my $name ::= "{$cat}:<{$sym}>";
if %cat_cache{$name}:exists {
return %cat_cache{$name};
}
my $role;
# need these readonly for proper LTM
if $sym ~~ /\s+/ {
my $sym1 ::= $sym.substr(0, $/.from);
my $sym2 ::= $sym.substr($/.to, $sym.chars - $/.to);
my $mname = "{$cat}:sym<$sym1 $sym2>";
my $meth = anon token bracket_categorical () {
:my $*GOAL := $sym2;
$sym1 {}:s
$<name>={$mname}
[ :lang($¢.unbalanced($sym2)) <semilist> ]
[ $sym2 || <.FAILGOAL($sym2, $mname, self.pos)> ]
$<O>={ self.cat_O($cat, "$sym1 $sym2") } $<sym>={ [$sym1,$sym2] }
}
$role = $Backend.make_role($mname, $meth);
} else {
my $mname = "{$cat}:sym<$sym>";
my $meth = anon token sym_categorical () {
$sym $<sym>={$sym} $<name>={$mname}
$<O>={ self.cat_O($cat, $sym) }
}
$role = $Backend.make_role($mname, $meth);
}
return (%cat_cache{$name} := $role);
}
method exists_syntax($cat, $sym) {
return True if self.can("{$cat}:sym<{$sym}>");
return True if self.can("{$cat}:{$sym}");
return True if $sym eq any < ...^ >;
return False;
}
method add_categorical($name) {
# Signature extension, not categorical
if $name ~~ /^\w+\:\(/ {
return self;
}
# CORE names are hardcoded; also, avoid redundant composition
return self if substr($name, chars($name)-1, 1) eq ')'; # multi candidates
return self unless ($name ~~ /^(\w+)\: \< (.*) \> /);
return self if $*UNITNAME eq 'CORE' && self.exists_syntax($0, $1);
%*LANG<MAIN> = $Backend.cached_but(self.WHAT, self.cat_role(~$0, ~$1));
return self.cursor_fresh(%*LANG<MAIN>);
}
method batch_categoricals(@names) {
my @roles;
for @names -> $name {
next if $name ~~ /^\w+\:\(/;
next if substr($name, chars($name)-1, 1) eq ')'; # multi candidates
next unless ($name ~~ /^(\w+)\: \< (.*) \> /);
# XXX: This is maybe not 100% right as it doesn't allow user modules
# to overwrite parsed setting macros with subs
next if self.exists_syntax($0, $1);
push @roles, self.cat_role(~$0, ~$1);
}
%*LANG<MAIN> = self.WHAT but @roles;
self.cursor_fresh(%*LANG<MAIN>);
}
method locmess () {
my $pos = self.pos;
my $line = self.lineof($pos);
if $pos >= chars(self.orig) {
$line = $line ~ " (EOF)";
}
my $pre = substr(self.orig, 0, $pos);
my $prel = chars($pre) min 40;
$pre = substr($pre, chars($pre)-$prel, $prel);
if ($pre ~~ /^.*\n/) {
$pre = substr($pre, $/.to);
}
$pre = '<BOL>' if $pre eq '';
my $post = substr(self.orig, $pos, (chars(self.orig)-$pos) min 40);
if ($post ~~ /\n/) {
$post = substr($post,0,$/.from);
}
$post = '<EOL>' if $post eq '';
" at " ~ $*FILE<name> ~ " line $line:\n------> " ~ $Cursor::GREEN ~
$pre ~ $Cursor::YELLOW ~ "\x23CF" ~ $Cursor::RED ~ $post ~
$Cursor::CLEAR;
}
method line {
self.lineof(self.pos);
}
method FAILGOAL ($stop, $name, $startpos) {
my $s = "'$stop'";
$s = '"\'"' if $s eq "'''";
self.panic("Unable to parse $name" ~ self.cursor($startpos).locmess ~ "\nCouldn't find final $s; gave up");
}
method deb(*@str) { note @str }
method cursor_fresh($k = self) { Q:CgOp {
(ns (cursor_fresh (cast cursor (@ {self})) (@ {$k})))
} }
method cursor_force($pos) {
$*HIGHWATER = $pos;
self.cursor($pos);
}
method mixin($role) { self.cursor_fresh($Backend.cached_but(self.WHAT, $role)) }
method mark_sinks(@sl) { #OK not used
#NYI
self
}
method you_are_here() { self }
method lineof ($p) {
return 1 unless defined $p;
my $line = @*LINEMEMOS[$p];
return $line if $line;
$line = 1; my $pos = 0;
my $lm = @*LINEMEMOS;
self.orig ~~ / :r [ \n { $lm[$pos++] = $line++ } ||
. { $lm[$pos++] = $line } ]* /;
$lm[$pos++] = $line;
return $lm[$p] // 0;
}
method lookup_dynvar($) { Any } # NYI
method check_old_cclass($) { } # NYI
}
# these stash entries were created in STD so they are considered to belong
# to STD, so we have to use an INIT to change them
INIT {
$Cursor::RED = "\e[31m";
$Cursor::GREEN = "\e[32m";
$Cursor::YELLOW = "\e[33m";
$Cursor::CLEAR = "\e[0m";
}
has $.lang;
has $.safemode;
method parse(:$unitname, :$filename, :$source, :$outer, :$run, :$main, :$evalmode, :$repl) {
my $*SAFEMODE = $.safemode;
my $*UNITNAME = $unitname;
my $lang = $!lang;
if $unitname eq 'CORE' {
$lang = 'NULL';
$*SAFEMODE = False;
} elsif $unitname ne 'MAIN' {
$lang = 'CORE';
}
# XXX temp() or should be contextuals
my @save_herestub = @STD::herestub_queue;
@STD::herestub_queue = ();
my $*niecza_outer_ref = $outer;
my @*MEMOS;
my @*LINEMEMOS;
my $*FILE = { name => $filename };
my @*ACTIVE;
my $*HIGHWATER = 0;
my $*HIGHEXPECT = {};
my $*HIGHMESS = "";
my $*LASTSTATE = 0;
my $*IN_PANIC = 0;
my $*IN_SUPPOSE = 0;
my $*FATALS = 0;
my $*LAST_NIBBLE = 0;
my $*LAST_NIBBLE_START = 0;
my $*LAST_NIBBLE_MULTILINE = 0;
my $*LAST_NIBBLE_MULTILINE_START = 0;
my $*GOAL = "(eof)";
my $*UNIT;
my $*CCSTATE; my $*BORG; my %*RX; my $*XACT; my $*VAR; my $*IN_REDUCE;
$*backend.push_compartment unless $evalmode;
LEAVE { $*backend.pop_compartment unless $evalmode };
my $*unit = $*backend.create_unit($unitname, $filename, $source, $main, $run);
my $*settingref = $*niecza_outer_ref ||
($lang ne 'NULL' ?? $*unit.need_unit($lang).bottom !! Any);
$*unit.abs_pkg('GLOBAL', :auto);
$*unit.abs_pkg('PROCESS', :auto);
NieczaGrammar.parse($source, actions => $Actions);
if %*ENV<NIECZA_MEMO_INFO> {
say "Memos used for $*UNITNAME ($source.chars() positions):";
my %memo_count;
for @*MEMOS -> $memo {
$memo // next;
for $memo.keys -> $key {
++%memo_count{$key}
}
}
for %memo_count.kv -> $key, $ct {
say "{$ct.fmt('%05d')} $key";
}
}
@STD::herestub_queue = @save_herestub;
$*backend.accept($*unit, :$filename, :$run, :$evalmode, :$repl);
$evalmode ?? $*unit !! Nil;
}