-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcontext.cpp
More file actions
1115 lines (958 loc) · 34.2 KB
/
context.cpp
File metadata and controls
1115 lines (958 loc) · 34.2 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
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
// *****************************************************************************
// context.cpp XL project
// *****************************************************************************
//
// File description:
//
// The execution environment for XL
//
// This defines both the compile-time environment (Context), where we
// keep symbolic information, e.g. how to rewrite trees, and the
// runtime environment (Runtime), which we use while executing trees
//
// See comments at beginning of context.h for details
//
//
//
// *****************************************************************************
// This software is licensed under the GNU General Public License v3+
// (C) 2003-2004,2009-2012,2014-2020, Christophe de Dinechin <christophe@dinechin.org>
// (C) 2010-2013, Jérôme Forissier <jerome@taodyne.com>
// *****************************************************************************
// 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 "context.h"
#include "tree.h"
#include "errors.h"
#include "options.h"
#include "renderer.h"
#include "basics.h"
#include "runtime.h"
#include "main.h"
#include "opcodes.h"
#include "save.h"
#include "cdecls.h"
#include "interpreter.h"
#include "bytecode.h"
#ifndef INTERPRETER_ONLY
#include "compiler.h"
#endif // INTERPRETER_ONLY
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <sys/stat.h>
RECORDER(scope_enter, 16, "Entering items in the scope");
RECORDER(scope_lookup, 16, "Lookup items from a scope");
XL_BEGIN
// ============================================================================
//
// Context: Representation of execution context
//
// ============================================================================
uint Context::hasRewritesForKind = 0;
Context::Context()
// ----------------------------------------------------------------------------
// Constructor for a top-level evaluation context
// ----------------------------------------------------------------------------
: symbols()
{
symbols = new Scope(xl_nil, xl_nil);
}
Context::Context(Context *parent, TreePosition pos)
// ----------------------------------------------------------------------------
// Constructor creating a child context in a parent context
// ----------------------------------------------------------------------------
: symbols(parent->symbols)
{
CreateScope(pos);
}
Context::Context(const Context &source)
// ----------------------------------------------------------------------------
// Constructor for a top-level evaluation context
// ----------------------------------------------------------------------------
: symbols(source.symbols)
{}
Context::Context(Scope *symbols)
// ----------------------------------------------------------------------------
// Constructor from a known symbol table
// ----------------------------------------------------------------------------
: symbols(symbols)
{}
Context::~Context()
// ----------------------------------------------------------------------------
// Destructor for execution context
// ----------------------------------------------------------------------------
{}
// ============================================================================
//
// High-level evaluation functions
//
// ============================================================================
Scope *Context::CreateScope(TreePosition pos)
// ----------------------------------------------------------------------------
// Add a local scope to the current context
// ----------------------------------------------------------------------------
{
symbols = new Scope(symbols, xl_nil, pos);
return symbols;
}
void Context::PopScope()
// ----------------------------------------------------------------------------
// Remove the innermost local scope
// ----------------------------------------------------------------------------
{
if (Scope *enclosing = Enclosing(symbols))
symbols = enclosing;
}
Context *Context::Parent()
// ----------------------------------------------------------------------------
// Return the parent context
// ----------------------------------------------------------------------------
{
if (Scope *psyms = Enclosing(symbols))
return new Context(psyms);
return nullptr;
}
// ============================================================================
//
// Entering symbols
//
// ============================================================================
RECORDER(xl2c, 64, "XL to C translation");
bool Context::ProcessDeclarations(Tree *what)
// ----------------------------------------------------------------------------
// Process all declarations, return true if there are instructions
// ----------------------------------------------------------------------------
{
Tree_g next = nullptr;
bool result = false;
while (what)
{
bool isInstruction = true;
if (Infix *infix = what->AsInfix())
{
if (IsDeclaration(infix))
{
Enter(infix);
isInstruction = false;
}
else if (IsSequence(infix))
{
// Chain of declarations, avoiding recursing if possible.
if (Infix *left = infix->left->AsInfix())
{
isInstruction = false;
if (IsDeclaration(left))
Enter(left);
else
isInstruction = ProcessDeclarations(left);
}
else if (Prefix *left = infix->left->AsPrefix())
{
isInstruction = ProcessDeclarations(left);
}
next = infix->right;
}
}
else if (Prefix *prefix = what->AsPrefix())
{
if (Name *pname = prefix->left->AsName())
{
if (pname->value == "data")
{
Define(prefix->right, xl_self);
isInstruction = false;
}
else if (pname->value == "extern")
{
CDeclaration *pcd = new CDeclaration;
Infix *normalForm = pcd->Declaration(prefix->right);
record(xl2c, "C: %t is XL: %t", prefix, normalForm);
if (normalForm)
{
// Process C declarations only in optimized mode
Define(normalForm->left, normalForm->right);
prefix->SetInfo<CDeclaration>(pcd);
isInstruction = false;
}
else
{
delete pcd;
}
}
}
}
// Check if we see instructions
result |= isInstruction;
// Consider next in chain
what = next;
next = nullptr;
}
return result;
}
static void ValidateNames(Tree *pattern)
// ----------------------------------------------------------------------------
// Check that we have only valid names in the pattern
// ----------------------------------------------------------------------------
{
switch(pattern->Kind())
{
case NATURAL:
case REAL:
case TEXT:
break;
case NAME:
{
Name *name = (Name *) pattern;
if (name->value.length() && !isalpha(name->value[0]))
Ooops("The pattern variable $1 is not a name", name);
break;
}
case INFIX:
{
Infix *infix = (Infix *) pattern;
ValidateNames(infix->left);
ValidateNames(infix->right);
break;
}
case PREFIX:
{
Prefix *prefix = (Prefix *) pattern;
if (prefix->left->Kind() != NAME)
ValidateNames(prefix->left);
ValidateNames(prefix->right);
break;
}
case POSTFIX:
{
Postfix *postfix = (Postfix *) pattern;
if (postfix->right->Kind() != NAME)
ValidateNames(postfix->right);
ValidateNames(postfix->left);
break;
}
case BLOCK:
{
Block *block = (Block *) pattern;
ValidateNames(block->child);
break;
}
}
}
Rewrite *Context::Define(Tree *pattern, Tree *value, bool overwrite)
// ----------------------------------------------------------------------------
// Enter a rewrite in the current context
// ----------------------------------------------------------------------------
{
Infix *decl = new Infix("is", pattern, value, pattern->Position());
return Enter(decl, overwrite);
}
Rewrite *Context::Define(text name, Tree *value, bool overwrite)
// ----------------------------------------------------------------------------
// Enter a rewrite in the current context
// ----------------------------------------------------------------------------
{
Name *nameTree = new Name(name, value->Position());
return Define(nameTree, value, overwrite);
}
Rewrite *Context::Enter(Infix *rewrite, bool overwrite)
// ----------------------------------------------------------------------------
// Enter a known declaration
// ----------------------------------------------------------------------------
{
// If the rewrite is not good, just exit
if (!IsDeclaration(rewrite))
return nullptr;
// In interpreted mode, just skip any C declaration
#ifndef INTERPRETER_ONLY
if (Opt::optimize <= 1)
#endif
if (Prefix *cdecl = rewrite->right->AsPrefix())
if (Name *cname = cdecl->left->AsName())
if (cname->value == "C")
return nullptr;
// Find 'from', 'to' and 'hash' for the rewrite
Tree *from = rewrite->left;
// Check what we are really defining, and verify if it's a name
Tree *defined = PatternBase(from);
Name *name = defined->AsName();
ulong h = Hash(defined);
// Record which kinds we have rewrites for
HasOneRewriteFor(defined->Kind());
// Validate form names, emit errors in case of problem.
ValidateNames(from);
// Find locals symbol table, populate it
// The context always has the locals on the left and enclosing on the right.
// In order to allow for log(N) lookup in the locals, we maintain a
// structure matching the old Rewrite class, which has a declaration
// and two or more possible children (currently two).
// That structure has the following layout: (A->B ; (L; R)), where
// A->B is the local declaration, L and R are the possible children.
// Children are initially nil.
Scope *scope = symbols;
Tree_g &locals = ScopeLocals(scope);
Tree_g *parent = &locals;
Rewrite *result = nullptr;
while (!result)
{
// If we have found a nil spot, that's where we can insert
if (*parent == xl_nil)
{
// Initialize the local entry with nil children
RewriteChildren *nil_children =
new RewriteChildren(REWRITE_CHILDREN_NAME,
xl_nil, xl_nil,
rewrite->Position());
// Create the local entry
Rewrite *entry = new Rewrite(REWRITE_NAME, rewrite, nil_children,
rewrite->Position());
// Insert the entry in the parent
*parent = entry;
// We are done
result = entry;
break;
}
// This should be a rewrite entry, follow it
Rewrite *entry = (*parent)->As<Rewrite>();
// If we are definig a name, signal if we redefine it
if (name)
{
Infix *decl = RewriteDeclaration(entry);
Tree *declDef = PatternBase(decl->left);
if (Name *declName = declDef->AsName())
{
if (declName->value == name->value)
{
if (overwrite)
{
decl->right = rewrite->right;
return entry;
}
else
{
Ooops("Name $1 is redefined", name);
Ooops("Previous definition was in $1", decl);
}
}
}
}
RewriteChildren *children = RewriteNext(entry);
if (h & 1)
parent = &children->right;
else
parent = &children->left;
h = Rehash(h);
}
// Return the entry we created
return result;
}
Tree *Context::Assign(Tree *ref, Tree *value)
// ----------------------------------------------------------------------------
// Perform an assignment in the given context
// ----------------------------------------------------------------------------
{
// Check if the reference already exists
Infix *decl = Reference(ref);
if (!decl)
{
// The reference does not exist: we need to create it.
// Strip outermost block if there is one
if (Block *block = ref->AsBlock())
ref = block->child;
// Enter in the symbol table
Define(ref, value);
}
else
{
// Check if the declaration has a type, i.e. it is 'X as natural'
if (Tree *type = AnnotatedType(decl->left))
{
Scope *scope = Symbols();
Tree *castedValue = xl_typecheck(scope, type, value);
if (castedValue)
{
value = castedValue;
}
else
{
Ooops("New value $1 does not match existing type", value);
Ooops("for declaration $1", decl);
value = decl->right; // Preserve existing value
}
}
// Update existing value in place
decl->right = value;
}
// Return evaluated assigned value
return value;
}
// ============================================================================
//
// Tree attributes
//
// ============================================================================
// Set and get per-context tree attributes
Tree *Context::Info(text key, Tree *what, bool recurse)
// ----------------------------------------------------------------------------
// Return the information associated with the given attribute
// ----------------------------------------------------------------------------
{
std::ostringstream out;
out << key << "@" << (void *) what;
return Named(out.str(), recurse);
}
Rewrite *Context::SetInfo(text key, Tree *what, Tree *value)
// ----------------------------------------------------------------------------
// Set the information associated with the tree
// ----------------------------------------------------------------------------
{
std::ostringstream out;
out << key << "@" << (void *) what;
return Define(out.str(), value, false);
}
Tree *Context::Type(Tree *what)
// ----------------------------------------------------------------------------
// Return the type associated with the value
// ----------------------------------------------------------------------------
{
return Info("type", what);
}
Rewrite *Context::SetType(Tree *what, Tree *type)
// ----------------------------------------------------------------------------
// Set the type associated with the value
// ----------------------------------------------------------------------------
{
return SetInfo("type", what, type);
}
// ============================================================================
//
// Closure management (keeping scoping information with values)
//
// ============================================================================
Tree *Context::Closure(Tree *value)
// ----------------------------------------------------------------------------
// Create a closure encapsulating the current context
// ----------------------------------------------------------------------------
{
Context_g context = this;
retry:
kind valueKind = value->Kind();
if (valueKind >= NAME || context->HasRewritesFor(valueKind))
{
if (valueKind == NAME)
{
if (Tree *bound = context->Bound(value))
{
if (Tree *inside = bound->IsClosure(&context))
{
if (value != inside)
{
value = inside;
goto retry;
}
}
if (value != bound)
{
value = bound;
goto retry;
}
}
}
if (valueKind != PREFIX || !value->GetInfo<ClosureInfo>())
{
Scope *scope = context->Symbols();
value = new Prefix(scope, value);
ClosureInfo *closureMarker = new ClosureInfo;
value->SetInfo(closureMarker);
}
}
return value;
}
// ============================================================================
//
// Context attributes
//
// ============================================================================
Rewrite *Context::SetAttribute(text attribute, Tree *value, bool owr)
// ----------------------------------------------------------------------------
// Set an attribute for the innermost context
// ----------------------------------------------------------------------------
{
return Define(attribute, value, owr);
}
Rewrite *Context::SetAttribute(text attribute, longlong value, bool owr)
// ----------------------------------------------------------------------------
// Set an attribute for the innermost context
// ----------------------------------------------------------------------------
{
return SetAttribute(attribute,new Natural(value,symbols->Position()),owr);
}
Rewrite *Context::SetAttribute(text attribute, double value, bool owr)
// ----------------------------------------------------------------------------
// Set the override priority for the innermost scope in the context
// ----------------------------------------------------------------------------
{
return Define(attribute, new Real(value, symbols->Position()), owr);
}
Rewrite *Context::SetAttribute(text attribute, text value, bool owr)
// ----------------------------------------------------------------------------
// Set the file name the innermost scope in the context
// ----------------------------------------------------------------------------
{
return Define(attribute, new Text(value, symbols->Position()), owr);
}
// ============================================================================
//
// Path management
//
// ============================================================================
void Context::SetPrefixPath(text prefix, text path)
// ----------------------------------------------------------------------------
// Set the path for the given prefix
// ----------------------------------------------------------------------------
{
}
text Context::ResolvePrefixedPath(text path)
// ----------------------------------------------------------------------------
// Resolve the file name in the current paths
// ----------------------------------------------------------------------------
{
return path;
}
// ============================================================================
//
// Looking up symbols
//
// ============================================================================
Tree *Context::Lookup(Tree *what, lookup_fn lookup, void *info, bool recurse)
// ----------------------------------------------------------------------------
// Lookup a tree using the given lookup function
// ----------------------------------------------------------------------------
{
// Quick exit if we have no rewrite for that tree kind
if (!HasRewritesFor(what->Kind()))
return nullptr;
Scope * scope = symbols;
ulong h0 = Hash(what);
while (scope)
{
// Initialize local scope
Tree_g &locals = ScopeLocals(scope);
Tree_g *parent = &locals;
Tree *result = nullptr;
ulong h = h0;
while (true)
{
// If we have found a nil spot, we are done with current scope
if (*parent == xl_nil)
break;
// This should be a rewrite entry, follow it
Rewrite *entry = (*parent)->As<Rewrite>();
XL_ASSERT(entry && entry->name == REWRITE_NAME);
Infix *decl = RewriteDeclaration(entry);
XL_ASSERT(!decl || IsDeclaration(decl));
RewriteChildren *children = RewriteNext(entry);
XL_ASSERT(children && children->name == REWRITE_CHILDREN_NAME);
// Check that hash matches
Tree *defined = PatternBase(decl->left);
ulong declHash = Hash(defined);
if (declHash == h0)
{
result = lookup(symbols, scope, what, decl, info);
if (result)
return result;
}
// Keep going in local symbol table
if (h & 1)
parent = &children->right;
else
parent = &children->left;
h = Rehash(h);
}
// Not found in this scope. Keep going with next scope if recursing
// The last top-level global will be nil, so we will end with scope=NULL
if (!recurse)
break;
scope = Enclosing(scope);
}
// Return NULL if all evaluations failed
return nullptr;
}
static Tree *findReference(Scope *, Scope *, Tree *what, Infix *decl, void *)
// ----------------------------------------------------------------------------
// Return the reference we found
// ----------------------------------------------------------------------------
{
return decl;
}
Rewrite *Context::Reference(Tree *pattern, bool recurse)
// ----------------------------------------------------------------------------
// Find an existing definition in the symbol table that matches the pattern
// ----------------------------------------------------------------------------
{
if (Tree *result = Lookup(pattern, findReference, nullptr, recurse))
if (Rewrite *decl = result->As<Rewrite>())
return decl;
return nullptr;
}
Tree *Context::DeclaredPattern(Tree *pattern)
// ----------------------------------------------------------------------------
// Find the original declaration associated to a given form
// ----------------------------------------------------------------------------
{
if (Tree *result = Lookup(pattern, findReference, nullptr, true))
if (Rewrite *decl = result->As<Rewrite>())
return PatternBase(decl->left);
return nullptr;
}
static Tree *findValue(Scope *, Scope *, Tree *what, Infix *decl, void *info)
// ----------------------------------------------------------------------------
// Return the value bound to a given pattern
// ----------------------------------------------------------------------------
{
if (what->IsLeaf())
if (!Tree::Equal(what, PatternBase(decl->left)))
return nullptr;
return decl->right;
}
static Tree *findValueX(Scope *, Scope *scope,
Tree *what, Infix *decl, void *info)
// ----------------------------------------------------------------------------
// Return the value bound to a given pattern, as well as its scope and decl
// ----------------------------------------------------------------------------
{
if (what->IsLeaf())
if (!Tree::Equal(what, PatternBase(decl->left)))
return nullptr;
Prefix *rewriteInfo = (Prefix *) info;
rewriteInfo->left = scope;
rewriteInfo->right = decl;
return decl->right;
}
Tree *Context::Bound(Tree *pattern, bool recurse)
// ----------------------------------------------------------------------------
// Return the value bound to a given declaration
// ----------------------------------------------------------------------------
{
Tree *result = Lookup(pattern, findValue, nullptr, recurse);
return result;
}
Tree *Context::Bound(Tree *pattern,
bool recurse,
Rewrite_g *rewrite,
Scope_g *ctx)
// ----------------------------------------------------------------------------
// Return the value bound to a given declaration
// ----------------------------------------------------------------------------
{
Prefix info(nullptr, nullptr);
Tree *result = Lookup(pattern, findValueX, &info, recurse);
if (result)
{
if (ctx)
*ctx = info.left->As<Scope>();
if (rewrite)
*rewrite = info.right->As<Rewrite>();
}
return result;
}
Tree *Context::Named(text name, bool recurse)
// ----------------------------------------------------------------------------
// Return the value bound to a given name
// ----------------------------------------------------------------------------
{
Name nameTree(name);
return Bound(&nameTree, recurse);
}
bool Context::IsEmpty()
// ----------------------------------------------------------------------------
// Return true if the context has no declaration inside
// ----------------------------------------------------------------------------
{
return symbols->right == xl_nil;
}
static ulong listNames(Rewrite *where, text begin, RewriteList &list, bool pfx)
// ----------------------------------------------------------------------------
// List names in the given tree
// ----------------------------------------------------------------------------
{
ulong count = 0;
while (where)
{
Infix *decl = RewriteDeclaration(where);
if (decl && IsDeclaration(decl))
{
Tree *declared = decl->left;
Name *name = declared->AsName();
if (!name && pfx)
{
Prefix *prefix = declared->AsPrefix();
if (prefix)
name = prefix->left->AsName();
}
if (name && name->value.find(begin) == 0)
{
list.push_back(decl);
count++;
}
}
if (IsSequence(where))
count += listNames(where->left->As<Rewrite>(), begin, list, pfx);
where = decl->right->As<Rewrite>();
}
return count;
}
ulong Context::ListNames(text begin, RewriteList &list,
bool recurse, bool includePrefixes)
// ----------------------------------------------------------------------------
// List names in a context
// ----------------------------------------------------------------------------
{
Scope *scope = symbols;
ulong count = 0;
while (scope)
{
Rewrite *locals = ScopeRewrites(scope);
count += listNames(locals, begin, list, includePrefixes);
if (!recurse)
scope = nullptr;
else
scope = Enclosing(scope);
}
return count;
}
// ============================================================================
//
// Hash functions to balance things in the symbol table
//
// ============================================================================
static inline ulong HashText(const text &t)
// ----------------------------------------------------------------------------
// Compute the has for some text
// ----------------------------------------------------------------------------
{
ulong h = 0;
uint l = t.length();
kstring ptr = t.data();
if (l > 8)
l = 8;
for (uint i = 0; i < l; i++)
h = (h * 0x301) ^ *ptr++;
return h;
}
static inline longlong hashRealToNatural(double value)
// ----------------------------------------------------------------------------
// Force a static conversion without "breaking strict aliasing rules"
// ----------------------------------------------------------------------------
{
union { double d; longlong i; } u;
u.d = value;
return u.i;
}
ulong Context::Hash(Tree *what)
// ----------------------------------------------------------------------------
// Compute the hash code in the rewrite table
// ----------------------------------------------------------------------------
{
kind k = what->Kind();
ulong h = 0xC0DEDUL + 0x29912837UL*k;
switch(k)
{
case NATURAL:
h += ((Natural *) what)->value;
break;
case REAL:
h += hashRealToNatural(((Real *) what)->value);
break;
case TEXT:
h += HashText(((Text *) what)->value);
break;
case NAME:
h += HashText(((Name *) what)->value);
break;
case BLOCK:
h += HashText(((Block *) what)->opening);
break;
case INFIX:
h += HashText(((Infix *) what)->name);
break;
case PREFIX:
if (Name *name = ((Prefix *) what)->left->AsName())
h += HashText(name->value);
break;
case POSTFIX:
if (Name *name = ((Postfix *) what)->right->AsName())
h += HashText(name->value);
break;
}
return h;
}
// ============================================================================
//
// Utility functions
//
// ============================================================================
void Context::Clear()
// ----------------------------------------------------------------------------
// Clear the symbol table
// ----------------------------------------------------------------------------
{
symbols->right = xl_nil;
}
void Context::Dump(std::ostream &out, Scope *scope, bool recurse)
// ----------------------------------------------------------------------------
// Dump the symbol table to the given stream
// ----------------------------------------------------------------------------
{
while (scope)
{
Scope *parent = Enclosing(scope);
Rewrite *rw = ScopeRewrites(scope);
Dump(out, rw);
if (!recurse)
break;
if (parent)
out << "// === Parent " << (void *) parent << " ================\n";
scope = parent;
}
}
void Context::Dump(std::ostream &out, Rewrite *rw)
// ----------------------------------------------------------------------------
// Dump the symbol table to the given stream
// ----------------------------------------------------------------------------
{
while (rw)
{
Infix * decl = RewriteDeclaration(rw);
Rewrite *next = RewriteNext(rw);
if (rw->name != REWRITE_NAME && rw->name != REWRITE_CHILDREN_NAME)
{
out << "SCOPE?" << rw << "\n";
}
if (decl)
{
if (IsDeclaration(decl))
out << decl << "\n";
else
out << "DECL?" << decl << "\n";
}
else if (rw->left != xl_nil)