-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinterpreter.cpp
1048 lines (898 loc) · 33.1 KB
/
interpreter.cpp
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// *****************************************************************************
// interpreter.cpp XL project
// *****************************************************************************
//
// File description:
//
// An interpreter for XL that does not rely on LLVM at all
//
//
//
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU General Public License v3+
// (C) 2015-2020, Christophe de Dinechin <[email protected]>
// *****************************************************************************
// This file is part of XL
//
// XL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// XL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with XL, in a file named COPYING.
// If not, see <https://www.gnu.org/licenses/>.
// *****************************************************************************
#include "interpreter.h"
#include "gc.h"
#include "info.h"
#include "errors.h"
#include "renderer.h"
#include "basics.h"
#include "runtime.h"
#include <cmath>
#include <algorithm>
RECORDER(interpreter, 128, "Interpreted evaluation of XL code");
RECORDER(interpreter_lazy, 64, "Interpreter lazy evaluation");
RECORDER(interpreter_eval, 128, "Primary evaluation entry point");
RECORDER(interpreter_typecheck, 64, "Type checks");
XL_BEGIN
// ============================================================================
//
// Options
//
// ============================================================================
namespace Opt
{
NaturalOption stackDepth("stack_depth",
"Maximum stack depth for interpreter",
1000, 25, 25000);
}
// ============================================================================
//
// Evaluation cache - Recording what has been evaluated and how
//
// ============================================================================
typedef std::map<Tree_p, Tree_p> EvalCache;
// ============================================================================
//
// Interpeter main entry points
//
// ============================================================================
Interpreter::Interpreter()
// ----------------------------------------------------------------------------
// Constructor for interpreter
// ----------------------------------------------------------------------------
{
record(interpreter, "Created interpreter %p", this);
}
Interpreter::~Interpreter()
// ----------------------------------------------------------------------------
// Destructor for interpreter
// ----------------------------------------------------------------------------
{
record(interpreter, "Destroyed interpreter %p", this);
}
Tree *Interpreter::Evaluate(Scope *scope, Tree *what)
// ----------------------------------------------------------------------------
// Evaluate 'what', finding the final, non-closure result
// ----------------------------------------------------------------------------
{
Context_p context = new Context(scope);
Tree *result = EvaluateClosure(context, what);
if (Tree *inside = IsClosure(result, nullptr))
result = inside;
return result;
}
// ============================================================================
//
// Primitive cache for 'opcode' and 'C' bindings
//
// ============================================================================
Opcode *Interpreter::SetInfo(Infix *decl, Opcode *opcode)
// ----------------------------------------------------------------------------
// Create a new info for the given callback
// ----------------------------------------------------------------------------
{
decl->right->SetInfo<Opcode>(opcode);
return opcode;
}
Opcode *Interpreter::OpcodeInfo(Infix *decl)
// ----------------------------------------------------------------------------
// Check if we have an opcode in the definition
// ----------------------------------------------------------------------------
{
Tree *right = decl->right;
Opcode *info = right->GetInfo<Opcode>();
if (info)
return info;
// Check if the declaration is something like 'X -> opcode Foo'
// If so, lookup 'Foo' in the opcode table the first time to record it
if (Prefix *prefix = right->AsPrefix())
if (Name *name = prefix->left->AsName())
if (name->value == "builtin")
if (Name *opName = prefix->right->AsName())
if (Opcode *opcode = Opcode::Find(prefix, opName->value))
return SetInfo(decl, opcode->Clone());
return nullptr;
}
// ============================================================================
//
// Parameter binding
//
// ============================================================================
struct Bindings
// ----------------------------------------------------------------------------
// Structure used to record bindings
// ----------------------------------------------------------------------------
{
typedef bool value_type;
Bindings(Context *context, Context *locals,
Tree *test, EvalCache &cache, TreeList &args)
: context(context), locals(locals),
test(test), cache(cache), resultType(nullptr), args(args) {}
// Tree::Do interface
bool Do(Natural *what);
bool Do(Real *what);
bool Do(Text *what);
bool Do(Name *what);
bool Do(Prefix *what);
bool Do(Postfix *what);
bool Do(Infix *what);
bool Do(Block *what);
// Evaluation and binding of values
void MustEvaluate(bool updateContext = false);
Tree *MustEvaluate(Context *context, Tree *what);
void Bind(Name *name, Tree *value);
void BindClosure(Name *name, Tree *value);
private:
Context_p context;
Context_p locals;
Tree_p test;
EvalCache &cache;
public:
Tree_p resultType;
TreeList &args;
};
inline bool Bindings::Do(Natural *what)
// ----------------------------------------------------------------------------
// The pattern contains an natural: check we have the same
// ----------------------------------------------------------------------------
{
MustEvaluate();
if (Natural *ival = test->AsNatural())
if (ival->value == what->value)
return true;
Ooops("Natural $1 does not match $2", what, test);
return false;
}
inline bool Bindings::Do(Real *what)
// ----------------------------------------------------------------------------
// The pattern contains a real: check we have the same
// ----------------------------------------------------------------------------
{
MustEvaluate();
if (Real *rval = test->AsReal())
if (rval->value == what->value)
return true;
Ooops("Real $1 does not match $2", what, test);
return false;
}
inline bool Bindings::Do(Text *what)
// ----------------------------------------------------------------------------
// The pattern contains a real: check we have the same
// ----------------------------------------------------------------------------
{
MustEvaluate();
if (Text *tval = test->AsText())
if (tval->value == what->value) // Do delimiters matter?
return true;
Ooops("Text $1 does not match $2", what, test);
return false;
}
inline bool Bindings::Do(Name *what)
// ----------------------------------------------------------------------------
// The pattern contains a name: bind it as a closure, no evaluation
// ----------------------------------------------------------------------------
{
Save<Context_p> saveContext(context, context);
// The test value may have been evaluated
EvalCache::iterator found = cache.find(test);
if (found != cache.end())
test = (*found).second;
// If there is already a binding for that name, value must match
// This covers both a pattern with 'pi' in it and things like 'X+X'
if (Tree *bound = locals->Bound(what))
{
MustEvaluate(true);
bool result = Tree::Equal(bound, test);
record(interpreter, "Arg check %t vs %t: %+s",
test, bound, result ? "match" : "failed");
if (!result)
Ooops("Name $1 does not match $2", bound, test);
return result;
}
record(interpreter, "In closure %t: %t = %t",
context->Symbols(), what, test);
BindClosure(what, test);
return true;
}
bool Bindings::Do(Block *what)
// ----------------------------------------------------------------------------
// The pattern contains a block: look inside
// ----------------------------------------------------------------------------
{
if (Block *testBlock = test->AsBlock())
if (testBlock->opening == what->opening &&
testBlock->closing == what->closing)
test = testBlock->child;
return what->child->Do(this);
}
bool Bindings::Do(Prefix *what)
// ----------------------------------------------------------------------------
// The pattern contains prefix: check that the left part matches
// ----------------------------------------------------------------------------
{
// The test itself should be a prefix
if (Prefix *pfx = test->AsPrefix())
{
// If we call 'sin X' and match 'sin 3', check if names match
if (Name *name = what->left->AsName())
{
if (Name *testName = pfx->left->AsName())
{
if (name->value == testName->value)
{
test = pfx->right;
return what->right->Do(this);
}
else
{
Ooops("Prefix name $1 does not match $2", name, testName);
return false;
}
}
}
// For other cases, we must go deep inside each prefix to check
test = pfx->left;
if (!what->left->Do(this))
return false;
test = pfx->right;
if (what->right->Do(this))
return true;
}
// All other cases are a mismatch
Ooops("Prefix $1 does not match $2", what, test);
return false;
}
bool Bindings::Do(Postfix *what)
// ----------------------------------------------------------------------------
// The pattern contains posfix: check that the right part matches
// ----------------------------------------------------------------------------
{
// The test itself should be a postfix
if (Postfix *pfx = test->AsPostfix())
{
// If we call 'X!' and match '3!', check if names match
if (Name *name = what->right->AsName())
{
if (Name *testName = pfx->right->AsName())
{
if (name->value == testName->value)
{
test = pfx->left;
return what->left->Do(this);
}
else
{
Ooops("Postfix name $1 does not match $2", name, testName);
return false;
}
}
}
// For other cases, we must go deep inside each prefix to check
test = pfx->right;
if (!what->right->Do(this))
return false;
test = pfx->left;
if (what->left->Do(this))
return true;
}
// All other cases are a mismatch
Ooops("Postfix $1 does not match $2", what, test);
return false;
}
bool Bindings::Do(Infix *what)
// ----------------------------------------------------------------------------
// The complicated case: various declarations
// ----------------------------------------------------------------------------
{
Save<Context_p> saveContext(context, context);
// Check if we have typed arguments, e.g. X:natural
if (what->name == ":")
{
Name *name = what->left->AsName();
if (!name)
{
Ooops("Invalid declaration, $1 is not a name", what->left);
return false;
}
// Typed name: evaluate type and check match
Scope *scope = context->Symbols();
Tree *type = MustEvaluate(context, what->right);
Tree *checked = xl_typecheck(scope, type, test);
if (!checked || type == XL::value_type)
{
MustEvaluate(type != XL::value_type);
checked = xl_typecheck(scope, type, test);
}
if (checked)
{
Bind(name, checked);
return true;
}
// Type mismatch
Ooops("Type $1 does not contain $2", type, test);
return false;
}
// Check if we have typed declarations, e.g. X+Y as natural
if (IsTypeAnnotation(what))
{
if (resultType)
{
Ooops("Duplicate return type declaration $1", what);
Ooops("Previously declared type was $1", resultType);
}
resultType = MustEvaluate(context, what->right);
return what->left->Do(this);
}
// Check if we have a guard clause
if (what->name == "when")
{
// It must pass the rest (need to bind values first)
if (!what->left->Do(this))
return false;
// Here, we need to evaluate in the local context, not eval one
Tree *check = MustEvaluate(locals, what->right);
if (check == xl_true)
return true;
else if (check != xl_false)
Ooops ("Invalid guard clause, $1 is not a boolean", check);
else
Ooops("Guard clause $1 is not verified", what->right);
return false;
}
// In all other cases, we need an infix with matching name
Infix *ifx = test->AsInfix();
if (!ifx)
{
// Try to get an infix by evaluating what we have
MustEvaluate(true);
ifx = test->AsInfix();
}
if (ifx)
{
if (ifx->name != what->name)
{
Ooops("Infix names $1 and $2 don't match", what->Position())
.Arg(ifx->name).Arg(what->name);
return false;
}
test = ifx->left;
if (!what->left->Do(this))
return false;
test = ifx->right;
if (what->right->Do(this))
return true;
}
// Mismatch
Ooops("Infix $1 does not match $2", what, test);
return false;
}
void Bindings::MustEvaluate(bool updateContext)
// ----------------------------------------------------------------------------
// Evaluate 'test', ensuring that each bound arg is evaluated at most once
// ----------------------------------------------------------------------------
{
Tree *evaluated = cache[test];
if (!evaluated)
{
evaluated = Interpreter::EvaluateClosure(context, test);
cache[test] = evaluated;
record(interpreter_lazy, "Test %t = new %t", test, evaluated);
}
else
{
record(interpreter_lazy, "Test %t = old %t", test, evaluated);
}
test = evaluated;
if (updateContext)
{
if (Tree *inside = Interpreter::IsClosure(test, &context))
{
record(interpreter_lazy, "Encapsulate %t in closure %t",
test, inside);
test = inside;
}
}
}
Tree *Bindings::MustEvaluate(Context *context, Tree *tval)
// ----------------------------------------------------------------------------
// Ensure that each bound arg is evaluated at most once
// ----------------------------------------------------------------------------
{
Tree *evaluated = cache[tval];
if (!evaluated)
{
evaluated = Interpreter::EvaluateClosure(context, tval);
cache[tval] = evaluated;
record(interpreter_lazy, "Evaluate %t in context %t is new %t",
tval, context, evaluated);
}
else
{
record(interpreter_lazy, "Evaluate %t in context %t is old %t",
tval, context, evaluated);
}
if (Tree *inside = Interpreter::IsClosure(evaluated, nullptr))
{
record(interpreter_lazy, "Encapsulate %t in closure %t",
evaluated, inside);
evaluated = inside;
}
return evaluated;
}
RECORDER(bind, 64, "Bind values to names");
void Bindings::Bind(Name *name, Tree *value)
// ----------------------------------------------------------------------------
// Enter a new binding in the current context, remember left and right
// ----------------------------------------------------------------------------
{
record(bind, "Bind %t = %t", name, value);
args.push_back(value);
locals->Define(name, value);
}
void Bindings::BindClosure(Name *name, Tree *value)
// ----------------------------------------------------------------------------
// Enter a new binding in the current context, preserving its environment
// ----------------------------------------------------------------------------
{
value = Interpreter::MakeClosure(context, value);
Bind(name, value);
}
// ============================================================================
//
// Main evaluation loop for the interpreter
//
// ============================================================================
static Tree *error_result = nullptr;
static Tree *evalLookup(Scope *evalScope, Scope *declScope,
Tree *self, Infix *decl, void *ec)
// ----------------------------------------------------------------------------
// Calllback function to check if the candidate matches
// ----------------------------------------------------------------------------
{
static uint depth = 0;
Save<uint> saveDepth(depth, depth+1);
record(interpreter_eval, "Eval%u %t from %t", depth, self, decl->left);
if (depth > Opt::stackDepth)
{
Ooops("Stack depth exceeded evaluating $1", self);
return error_result = xl_error;
}
else if (error_result)
{
return error_result;
}
// Create the scope for evaluation
Context_p context = new Context(evalScope);
Context_p locals = nullptr;
Tree *result = nullptr;
// Check if the decl is an opcode or C binding
Errors *errors = MAIN->errors;
uint errCount = errors->Count();
Opcode *opcode = Interpreter::OpcodeInfo(decl);
if (errors->Count() != errCount)
return nullptr;
// If we lookup a name or a number, just return it
Tree *defined = PatternBase(decl->left);
Tree *resultType = tree_type;
TreeList args;
if (defined->IsLeaf())
{
// Must match literally, or we don't have a candidate
if (!Tree::Equal(defined, self))
{
record(interpreter_eval, "Eval%u %t from constant %t: mismatch",
depth, self, decl->left);
return nullptr;
}
locals = context;
}
else
{
// Retrieve the evaluation cache for arguments
EvalCache *cache = (EvalCache *) ec;
// Create the scope for evaluation and local bindings
locals = new Context(declScope);
locals->CreateScope();
// Check bindings of arguments to declaration, exit if fails
Bindings bindings(context, locals, self, *cache, args);
if (!decl->left->Do(bindings))
{
record(interpreter_eval, "Eval%u %t from %t: mismatch",
depth, self, decl->left);
return nullptr;
}
if (bindings.resultType)
resultType = bindings.resultType;
}
// Check if the right is "self"
if (result == xl_self)
{
record(interpreter_eval, "Eval%u %t from %t is self",
depth, self, decl->left);
return self;
}
// Check if we have builtins (opcode or C bindings)
if (opcode)
{
// Cached callback
uint offset = args.size();
std::reverse(args.begin(), args.end());
args.push_back(decl->right);
args.push_back(context->Symbols());
Data data = &args[offset];
opcode->Run(data);
result = DataResult(data);
record(interpreter_eval, "Eval%u %t opcode %+s, result %t",
depth, self, opcode->OpID(), result);
return result;
}
// Normal case: evaluate body of the declaration in the new context
result = decl->right;
if (resultType != tree_type)
result = new Infix("as", result, resultType, self->Position());
result = Interpreter::MakeClosure(locals, result);
record(interpreter_eval, "Eval%u %t in context %t = %t",
depth, locals->Symbols(), self, result);
return result;
}
inline Tree *encloseResult(Context *context, Scope *old, Tree *what)
// ----------------------------------------------------------------------------
// Encapsulate result with a closure if context is not evaluation context
// ----------------------------------------------------------------------------
{
if (context->Symbols() != old)
what = Interpreter::MakeClosure(context, what);
return what;
}
Tree *Interpreter::Instructions(Context_p context, Tree_p what)
// ----------------------------------------------------------------------------
// Evaluate the input tree once declarations have been processed
// ----------------------------------------------------------------------------
{
Tree_p result = what;
Scope_p originalScope = context->Symbols();
// Loop to avoid recursion for a few common cases, e.g. sequences, blocks
while (what)
{
// First attempt to look things up
EvalCache cache;
if (Tree *eval = context->Lookup(what, evalLookup, &cache))
{
if (eval == xl_error)
return eval;
MAIN->errors->Clear();
result = eval;
if (Tree *inside = IsClosure(eval, &context))
{
what = inside;
continue;
}
return encloseResult(context, originalScope, eval);
}
kind whatK = what->Kind();
switch (whatK)
{
case NATURAL:
case REAL:
case TEXT:
return what;
case NAME:
Ooops("No name matches $1", what);
return encloseResult(context, originalScope, what);
case BLOCK:
{
// Evaluate child in a new context
context->CreateScope();
what = ((Block *) (Tree *) what)->child;
bool hasInstructions = context->ProcessDeclarations(what);
if (context->IsEmpty())
context->PopScope();
if (hasInstructions)
continue;
return encloseResult(context, originalScope, what);
}
case PREFIX:
{
// If we have a prefix on the left, check if it's a closure
if (Tree *closed = IsClosure(what, &context))
{
what = closed;
continue;
}
// If we have a name on the left, lookup name and start again
Prefix *pfx = (Prefix *) (Tree *) what;
Tree *callee = pfx->left;
// Check if we had something like '(X->X+1) 31' as closure
Context_p calleeContext = nullptr;
if (Tree *inside = IsClosure(callee, &calleeContext))
callee = inside;
if (Name *name = callee->AsName())
// A few cases where we don't interpret the result
if (name->value == "matching" ||
name->value == "extern")
return what;
// This variable records if we evaluated the callee
Tree *newCallee = nullptr;
Tree *arg = pfx->right;
// If we have an infix on the left, check if it's a single rewrite
if (Infix *lifx = IsDefinition(callee))
{
// If we have a single name on the left, like (X->X+1)
// interpret that as a lambda function
if (Name *lfname = lifx->left->AsName())
{
// Case like '(X->X+1) Arg':
// Bind arg in new context and evaluate body
context = new Context(context);
context->Define(lfname, arg);
what = lifx->right;
continue;
}
// Otherwise, enter declaration and retry, e.g.
// '(X,Y->X+Y) (2,3)' should evaluate as 5
context = new Context(context);
context->Define(lifx->left, lifx->right);
what = arg;
continue;
}
// Other cases: evaluate the callee, and if it changed, retry
if (!newCallee)
{
Context_p newContext = new Context(context);
newCallee = EvaluateClosure(newContext, callee);
}
if (newCallee != callee)
{
// We need to evaluate argument in current context
arg = Instructions(context, arg);
// We built a new context if left was a block
if (Tree *inside = IsClosure(newCallee, &context))
{
what = arg;
// Check if we have a single definition on the left
if (IsDefinition(inside))
what = new Prefix(newCallee, arg, pfx->Position());
}
else
{
// Other more regular cases
what = new Prefix(newCallee, arg, pfx->Position());
}
continue;
}
// If we get there, we didn't find anything interesting to do
Ooops("No prefix matches $1", what);
return encloseResult(context, originalScope, what);
}
case POSTFIX:
{
// Check if there is a pattern that matches
Ooops("No postifx matches $1", what);
return encloseResult(context, originalScope, what);
}
case INFIX:
{
Infix *infix = (Infix *) (Tree *) what;
text name = infix->name;
// Check sequences
if (name == ";" || name == "\n")
{
// Sequences: evaluate left, then right
Context *leftContext = context;
Tree *left = Instructions(leftContext, infix->left);
if (left != infix->left)
result = left;
what = infix->right;
continue;
}
// Check declarations
if (name == "is")
{
// Declarations evaluate last non-declaration result, or self
return encloseResult(context, originalScope, result);
}
// Check type matching
if (name == "as")
{
Scope *scope = context->Symbols();
result = xl_typecheck(scope, infix->right, infix->left);
if (!result)
{
Ooops("Value $1 does not match type $2",
infix->left, infix->right);
result = infix->left;
}
return encloseResult(context, originalScope, result);
}
// Check scoped reference
if (name == ".")
{
Tree *left = Instructions(context, infix->left);
IsClosure(left, &context);
what = infix->right;
continue;
}
// All other cases: failure
Ooops("No infix matches $1", what);
return encloseResult(context, originalScope, what);
}
} // switch
}// while(what)
return encloseResult(context, originalScope, result);
}
Tree *Interpreter::EvaluateClosure(Context *context, Tree *what)
// ----------------------------------------------------------------------------
// Evaluate 'what', possibly returned as a closure in case not in 'context'
// ----------------------------------------------------------------------------
{
// Create scope for declarations, and evaluate in this context
Tree_p result = what;
Errors *errors = MAIN->errors;
uint errCount = errors->Count();
if (context->ProcessDeclarations(what) && errCount == errors->Count())
result = Instructions(context, what);
// This is a safe point for checking collection status
GarbageCollector::SafePoint();
return result;
}
// ============================================================================
//
// Type checking
//
// ============================================================================
struct Expansion
// ----------------------------------------------------------------------------
// A structure to expand a type-matched structure
// ----------------------------------------------------------------------------
{
Expansion(Context *context): context(context) {}
typedef Tree *value_type;
Tree * Do(Tree *what)
{
return what;
}
Tree * Do(Name *what)
{
if (Tree *bound = context->Bound(what))
{
if (Tree *eval = Interpreter::IsClosure(bound, nullptr))
bound = eval;
return bound;
}
return what;
}
Tree * Do(Prefix *what)
{
Tree *left = what->left->Do(this);
Tree *right = what->right->Do(this);
if (left != what->left || right != what->right)
return new Prefix(left, right, what->Position());
return what;
}
Tree * Do(Postfix *what)
{
Tree *left = what->left->Do(this);
Tree *right = what->right->Do(this);
if (left != what->left || right != what->right)
return new Postfix(left, right, what->Position());
return what;
}
Tree * Do(Infix *what)
{
if (IsTypeAnnotation(what) || IsPatternCondition(what))
return what->left->Do(this);
Tree *left = what->left->Do(this);
Tree *right = what->right->Do(this);
if (left != what->left || right != what->right)
return new Infix(what->name, left, right, what->Position());
return what;
}
Tree * Do(Block *what)
{
Tree *chld = what->child->Do(this);
if (chld != what->child)
return new Block(chld,what->opening,what->closing,what->Position());
return what;
}
Context_p context;
};
static Tree *formTypeCheck(Scope *scope, Tree *shape, Tree *value)
// ----------------------------------------------------------------------------
// Check a value against a type shape
// ----------------------------------------------------------------------------
{
// Strip outermost block if there is one
if (Block *block = shape->AsBlock())
shape = block->child;
// Check if the shape matches
Context_p context = new Context(scope);
Context_p locals = new Context(context);
EvalCache cache;
TreeList args;
Bindings bindings(context, locals, value, cache, args);
if (!shape->Do(bindings))
{
record(interpreter_typecheck, "Shape of tree %t does not match %t",
value, shape);
return nullptr;
}
// Reconstruct the resulting value from the shape
Expansion expand(locals);
value = shape->Do(expand);
// The value is associated to the symbols we extracted
record(interpreter_typecheck, "Shape of tree %t matches %t", value, shape);
return Interpreter::MakeClosure(locals, value);
}
Tree *Interpreter::TypeCheck(Scope *scope, Tree *type, Tree *value)