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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
package Elymas;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
popInt popString popArray enstruct arrayAccess $quoted @globalCallStack
interpretCode execute executeString executeFile resolve
);
use Data::Dumper;
our $quoted = 0;
our @globalCallStack;
sub popInt {
my ($data) = @_;
my $i = pop @$data or die "Stack underflow";
die "Not integer " . Dumper($i) unless $i->[1] eq 'int';
return $i->[0];
}
sub popString {
my ($data) = @_;
my $s = pop @$data or die "Stack underflow";
die "Not string " . Dumper($s) unless $s->[1] eq 'string';
return $s->[0];
}
sub popArray {
my ($data) = @_;
my $a = pop @$data or die "Stack underflow";
die "Not array: " . Dumper($a) unless ref($a->[1]) eq 'ARRAY' and $a->[1]->[0] eq 'array';
return $a->[0];
}
sub enstruct {
my ($struct) = @_;
return ($struct, ['struct', { map { my @e = ($_, [@{$struct->{$_}}]); shift @{$e[1]}; @e } keys %$struct }]);
}
sub arrayAccess {
my ($array, $data, $scope) = @_;
my $i = pop @$data or die "Stack underflow";
die "array index must be int" unless $i->[1] eq 'int';
push @$data, $array->[0]->[$i->[0]];
}
sub interpretCode {
my ($code, $data, $scope) = @_;
foreach my $t (@$code) {
eval {
if($t->[1] eq 'tok') {
die "unexpanded token in interpretCode";
} elsif(ref($t->[1]) eq 'ARRAY' and $t->[1]->[0] eq 'func') {
push @$data, $t;
execute($data, $scope);
} else {
push @$data, $t;
}
};
if($@) {
#print "Code: " . Dumper($tokens);
#print "Scope: " . Dumper($scope);
print "Stack: " . Dumper($data);
print "Token: " . Dumper($t);
die;
}
}
}
sub typeStack {
my ($type) = @_;
if(ref($type) eq 'ARRAY') {
if($type->[0] eq 'func' or $type->[0] eq 'array') {
if(not exists $type->[2]) {
die "type analysis incomplete on " . Dumper($type);
}
if(@{$type->[2]} == 1 and @{$type->[3]} == 1) {
my $ret = typeStack($type->[3]->[0]);
unshift @$ret, $type->[2]->[0];
return $ret;
}
}
}
return [$type];
}
sub typeEqual {
my ($a, $b) = @_;
return 0 if(ref($a) xor ref($b));
if(ref($a) and ref($b)) {
return 0 if($a->[0] ne $b->[0]);
if($a->[0] eq 'range') {
return $a->[1] == $b->[1] && $a->[2] == $b->[2];
} elsif($a->[0] eq 'array' or $a->[0] eq 'func') {
return 0 if(@{$a->[2]} != @{$b->[2]});
return 0 if(@{$a->[3]} != @{$b->[3]});
return 0 unless @{$a->[2]} == grep { typeEqual($a->[2]->[$_], $b->[2]->[$_]) } 0 .. $#{$a->[2]};
return 0 unless @{$a->[3]} == grep { typeEqual($a->[3]->[$_], $b->[3]->[$_]) } 0 .. $#{$a->[3]};
return 1;
} else {
die "not yet implemented";
}
}
return $a eq $b;
}
sub canCastTo {
my ($subtype, $supertype) = @_;
return 1 if(typeEqual($subtype, $supertype));
return 1 if($supertype eq '*');
return 1 if($supertype eq 'int' and ref($subtype) eq 'ARRAY' and $subtype->[0] eq 'range');
return 0;
}
sub commonSubType {
my ($a, $b) = @_;
return $a if(canCastTo($a, $b));
return $b if(canCastTo($b, $a));
return undef;
}
sub typeMismatchCount {
my ($formal, $concrete) = @_;
my @rFormal = reverse @$formal;
my @rConcrete = reverse @$concrete;
my $mismatches = 0;
while(@rFormal) {
my $f = shift @rFormal;
if(canCastTo($rConcrete[0], $f)) {
shift @rConcrete;
} else {
++$mismatches;
}
}
return $mismatches;
}
sub isVariableType {
my ($type) = @_;
return 0;
}
sub isIterableType {
my ($type) = @_;
return 1 if(ref($type) eq 'ARRAY' and $type->[0] eq 'range');
return 0;
}
sub getLoopStart {
my ($iterable) = @_;
if(ref($iterable->[1]) eq 'ARRAY' and $iterable->[1]->[0] eq 'array') {
return [0, 'int'];
}
die "Cannot iterate: " . Dumper($iterable);
}
sub isLoopEnd {
my ($iterable, $i) = @_;
if(ref($iterable->[1]) eq 'ARRAY' and $iterable->[1]->[0] eq 'array') {
return $i->[0] == @{$iterable->[0]};
}
die "Cannot iterate: " . Dumper($iterable);
}
sub doLoopStep {
my ($iterable, $i) = @_;
if(ref($iterable->[1]) eq 'ARRAY' and $iterable->[1]->[0] eq 'array') {
return [$i->[0] + 1, 'int'];
}
die "Cannot iterate: " . Dumper($iterable);
}
# Executing a function f: A->B->C (i.e. B A f) on concrete arguments b a.
# Phase 1
# Foreach argument:
# Find the function input type from top of concrete argument type stack,
# increase viewport from top of concrete type stack
# match type from bottom to top, if type cannot be found, create constant function
# final match is that which creates minimal number of constant function layers
# Phase 2
# Foreach argument type:
# Identify the type stack above the match from phase 1.
# Run from right (stacktop) argument to left (stacklow) argument:
# Take topmost type, check whether it can be found in other stacks (from top)
# Eliminate all matching types via function or loop creation
sub execute {
my ($data, $scope) = @_;
my $f = pop @$data or die "Stack underflow";
if(ref($f->[1]) ne 'ARRAY') {
push @$data, $f;
return;
}
if($f->[1]->[0] eq 'array') {
my $ff = $f;
$f = [sub {
my ($data, $scope) = @_;
arrayAccess($ff, $data, $scope);
}, ['func', 'array-to-func-cast', ['int'], [$ff->[1]->[1]]]];
}
die "complex type unsuitable for execution" if($f->[1]->[0] ne 'func');
if(not $f->[1]->[2]) {
# untyped function, just call
push @globalCallStack, $f;
&{$f->[0]}($data, $scope);
pop @globalCallStack;
return;
}
my @concreteArgs;
my @viewPortOffset;
# Phase 1
for(my $argI = $#{$f->[1]->[2]}; $argI >= 0; --$argI) {
# print "Analyzing Arg $argI\n";
my $formalArg = $f->[1]->[2]->[$argI];
my $formalTypeStack = typeStack($formalArg);
my $c = pop @$data;
my $typeStack = typeStack($c->[1]);
# die "Type-Stack: " . Dumper($typeStack);
my $bestViewPortSize = 0;
my $bestViewPortMatch = @$typeStack + 1;
# print "Formal Type Stack: @$formalTypeStack\n";
# print " Type Stack: @$typeStack\n";
if(isVariableType($typeStack->[-1])) {
for(my $viewPortSize = 1; $viewPortSize < @$typeStack + 1; ++$viewPortSize) {
my @typeViewPort;
unshift @typeViewPort, $typeStack->[$_ - 1] for(1 .. $viewPortSize);
# print "@$formalTypeStack vs. @$typeStack\n";
my $viewPortMatch = typeMismatchCount($formalTypeStack, $typeStack);
if($viewPortMatch < $bestViewPortMatch) {
$bestViewPortSize = $viewPortSize;
$bestViewPortMatch = $viewPortMatch;
}
}
} else {
$bestViewPortSize = @$typeStack;
$bestViewPortMatch = 0;
}
# convert concrete argument to exactly matching function
# ... which calls the concrete argument using its relevant args
if($bestViewPortMatch == 0) {
# zero mismatches, can directly use concrete argument
unshift @viewPortOffset, @$typeStack - @$formalTypeStack;
} else {
# if argument is concrete, but we need are construction a function overall, then concrete
# argument needs to be converted to a constant function in whatever domain is necessary
die "concrete argument constant functionification needs to be implemented, mismatch: $bestViewPortMatch";
$c = sub { "magic goes here FIXME" };
}
unshift @concreteArgs, $c;
}
# print "Viewport Offsets: @viewPortOffset\n";
# Phase 2,
my @toBeAbstractedTypes;
foreach my $i (0 .. $#viewPortOffset) {
my @remaining = @{typeStack($concreteArgs[$i]->[1])};
@{$toBeAbstractedTypes[$i]} = @remaining[0 .. $viewPortOffset[$i] - 1];
}
# print "To be abstracted: " . Dumper(@toBeAbstractedTypes);
if(not grep { @$_ } @toBeAbstractedTypes) {
# no types need to be abstracted, function can be called
push @globalCallStack, $f;
&{$f->[0]}(\@concreteArgs, $scope);
pop @globalCallStack;
push @$data, @concreteArgs;
} else {
my @argTypes; # the type stack of the new function
my @stageCalls; # which functions to call in each stage
my @loops; # undef for lambda abstraction, loop bound source for loops
foreach my $i (reverse 0 .. $#toBeAbstractedTypes) {
while(@{$toBeAbstractedTypes[$i]}) {
my $type = shift @{$toBeAbstractedTypes[$i]};
my $stageCalls = [$i];
my $iterationSource = undef; # which concrete argument we'll take the iteration bounds from
if(isIterableType($type)) {
$iterationSource = $i;
}
foreach my $j (reverse 0 .. $i - 1) {
next unless @{$toBeAbstractedTypes[$j]};
my $common = commonSubType($type, $toBeAbstractedTypes[$j]->[0]);
next unless $common;
$type = $common;
if(isIterableType($type) and not defined $iterationSource) {
$iterationSource = $j;
}
shift @{$toBeAbstractedTypes[$j]};
unshift @$stageCalls, $j;
}
if(defined $iterationSource) {
unshift @argTypes, undef;
unshift @loops, $iterationSource;
} else {
unshift @argTypes, $type;
unshift @loops, undef;
}
push @stageCalls, $stageCalls;
}
}
# die Dumper(\@argTypes, \@stageCalls, \@loops);
my $unravel; $unravel = sub {
my ($data, $concreteArgs, $stageCalls, $argTypes, $loops) = @_;
my @stageCallCopy = @$stageCalls;
my @argTypeCopy = @$argTypes;
my @loopCopy = @$loops;
my $stage = pop @stageCallCopy;
my $argType = pop @argTypeCopy;
my $loop = pop @loopCopy;
if($argType) {
my $abstraction = sub {
my ($data, $scope) = @_;
my $v = pop @$data;
my @argCopy = @$concreteArgs;
foreach my $i (@$stage) {
my @s = ($v, $argCopy[$i]);
execute(\@s, $scope);
$argCopy[$i] = $s[0];
}
&$unravel($data, \@argCopy, \@stageCallCopy, \@argTypeCopy, \@loopCopy);
};
push @$data, [$abstraction, ['func', 'autoabstraction of ' . $f->[1]->[1], [grep { $_ } @argTypeCopy], undef]];
# FIXME the undef can be determined
} elsif(defined $loop) {
my @argCopy = @$concreteArgs;
my @results;
for (my $i = getLoopStart($argCopy[$loop]); !isLoopEnd($argCopy[$loop], $i); $i = doLoopStep($argCopy[$loop], $i)) {
my @argCopy2 = @$concreteArgs;
foreach my $j (@$stage) {
my @s = ($i, $argCopy2[$j]);
execute(\@s, $scope);
$argCopy2[$j] = $s[0];
}
my $count = @$data;
&$unravel($data, \@argCopy2, \@stageCallCopy, \@argTypeCopy, \@loopCopy);
push @results, pop @$data;
die "abstracted function produced multiple results (can be handled corretly, needs to be implemented)"
unless $count == @$data;
# by producing two arrays side by side
}
push @$data, [\@results, ['array', '[]', [['range', 0, $#results]], [undef]]];
# FIXME the undef can be determined
} else {
my @argCopy = @$concreteArgs;
push @globalCallStack, $f;
&{$f->[0]}(\@argCopy, $scope);
pop @globalCallStack;
push @$data, @argCopy;
}
};
&$unravel($data, \@concreteArgs, \@stageCalls, \@argTypes, \@loops);
}
}
sub resolve {
my ($scope, $data, $name) = @_;
die "resolution for undefined name attempted" unless defined $name;
return $scope->{$name} if(exists $scope->{$name});
return resolve($scope->{' parent'}, $data, $name) if(exists $scope->{' parent'});
if($name =~ /^(_+)(\d*)$/s) {
my @spec = split //, $2;
@spec = (0) unless @spec;
return [sub {
my ($data, $scope) = @_;
my @new;
foreach my $i (@spec) {
die "Stack underflow" if @$data < $i + 1;
push @new, $data->[-$i - 1];
}
push @$data, @new;
}, ['func', 'auto-created of ' . $name], 'active'];
} elsif($name =~ /^(-+)([0-9*]*)$/s) {
my $max = length($1) - 1;
my @spec = split //, $2;
$max = $_ > $max? $_: $max foreach grep { $_ ne '*' } @spec;
return [sub {
my ($data, $scope) = @_;
my @buffer;
foreach (0 .. $max) {
die "Stack underflow" unless @$data;
push @buffer, pop @$data;
}
foreach my $i (@spec) {
if($i eq '*') {
execute($data, $scope);
} else {
push @$data, $buffer[$i];
}
}
}, ['func', 'auto-created of ' . $name], 'active'];
} elsif($name =~ /^\*(\d*)$/s) {
my @spec = split //, $1;
return [sub {
my ($data, $scope) = @_;
my @buffer;
foreach my $i (@spec) {
die "Stack underflow" if @$data < $i + 2;
push @buffer, $data->[-$i - 2];
}
execute($data, $scope);
push @$data, @buffer;
}, ['func', 'auto-created of ' . $name], 'active'];
}
return undef;
}
sub applyResolvedName {
my ($t, $meaning, $data, $scope, $quoted) = @_;
if(not defined $meaning) {
if($quoted) {
push @$data, [sub {
my ($data, $scope) = @_;
my $meaning = resolve($$scope, $data, $t->[0]);
applyResolvedName($t, $meaning, $data, $scope, 0);
}, ['func', 'quoted late-resolve of ' . $t->[0]], $t->[0]];
} else {
die "could not resolve '$t->[0]'";
}
} elsif($meaning->[2] eq 'passive') {
if($quoted) {
push @$data, [sub { push @{$_[0]}, [$meaning->[0], $meaning->[1]] }, ['func', 'quoted-constant of ' . $t->[0]], $t->[0]];
} else {
push @$data, [$meaning->[0], $meaning->[1]];
}
} elsif($meaning->[2] eq 'active') {
if($quoted) {
push @$data, [$meaning->[0], $meaning->[1], $t->[0]];
} else {
push @$data, [$meaning->[0], $meaning->[1]];
execute($data, $scope);
}
} elsif($meaning->[2] eq 'quote') {
push @$data, [$meaning->[0], $meaning->[1]];
execute($data, $scope);
} else {
die "unknown scope entry meaning for '$t->[0]': " . $meaning->[2];
}
}
sub interpretTokens {
my ($tokens, $data, $scope) = @_;
foreach my $t (@$tokens) {
eval {
if($t->[1] eq 'tok') {
my $meaning = resolve($$scope, $data, $t->[0]);
applyResolvedName($t, $meaning, $data, $scope, $quoted);
} elsif(ref($t->[1]) eq 'ARRAY' and $t->[1]->[0] eq 'func') {
die "function pointer in interpretTokens";
} else {
push @$data, $t;
}
};
if($@) {
#print "Code: " . Dumper($tokens);
#print "Scope: " . Dumper($scope);
print "Stack: " . Dumper($data);
print "Token: " . Dumper($t);
die;
}
}
}
sub executeFile {
my ($file, $data, $scope) = @_;
open my $code, '<', $file or die "cannot open $file: $!";
while(my $line = <$code>) {
chomp $line;
executeString($line, $data, $scope);
}
close $code;
}
sub executeString {
my ($str, $data, $scope) = @_;
my @tokens = tokenize($str);
interpretTokens(\@tokens, $data, $scope);
return $data;
}
sub tokenize {
my ($line) = @_;
$line .= ' ';
my @t;
while($line) {
if($line =~ /^ +(.*)/s) {
$line = $1;
} elsif($line =~ /^#/s) {
$line = '';
} elsif($line =~ /^(\d+) +(.*)/s) {
$line = $2;
push @t, [$1, 'int'];
} elsif($line =~ /^"(.*)/s) {
$line = $1;
my $str = "";
while(1) {
if($line =~ /^"(.*)/s) {
$line = $1;
last;
} elsif($line =~ /^\\(.)(.*)/s) {
if($1 eq '\\') {
$str .= '\\';
} elsif($1 eq 'n') {
$str .= "\n";
} elsif($1 eq '"') {
$str .= "\"";
} else {
die "invalid \\-char in string: '$1', '$line'";
}
$line = $2;
} elsif($line =~ /^([^"\\])(.*)/s) {
$str .= $1;
$line = $2;
} else {
die "cannot tokenize string-like: '$line'";
}
}
push @t, [$str, 'string'];
} elsif($line =~ /^([^a-zA-Z ]+)([a-zA-Z]+) +(.*)/s) {
$line = "$1 $3";
push @t, [$2, 'string'];
} elsif($line =~ /^([a-zA-Z]+|[^a-zA-Z ]+) +(.*)/s) {
$line = $2;
push @t, [$1, 'tok'];
} else {
die "cannot tokenize: '$line'";
}
}
return @t;
}
1;
|