-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathruntime.cpp
1324 lines (1142 loc) · 38.2 KB
/
runtime.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
// *****************************************************************************
// runtime.cpp XL project
// *****************************************************************************
//
// File description:
//
// Runtime functions necessary to execute XL programs
//
//
//
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU General Public License v3+
// (C) 2012, Baptiste Soulisse <baptiste.soulisse@taodyne.com>
// (C) 2011-2013, Catherine Burvelle <catherine@taodyne.com>
// (C) 2009-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 "runtime.h"
#include "tree.h"
#include "parser.h"
#include "scanner.h"
#include "renderer.h"
#include "context.h"
#include "options.h"
#include "opcodes.h"
#include "main.h"
#include "save.h"
#include "tree-clone.h"
#include "utf8_fileutils.h"
#include "interpreter.h"
#include "winglob.h"
#ifndef INTERPRETER_ONLY
#include "compiler.h"
#include "native.h"
#else // !INTERPRETER_ONLY
#define XL_NATIVE(fn)
#endif // INTERPRETER_ONLY
#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <sstream>
#include <iostream>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
XL_BEGIN
Tree * xl_evaluate(Scope *scope, Tree *tree)
// ----------------------------------------------------------------------------
// Dispatch evaluation to the main entry point
// ----------------------------------------------------------------------------
{
return MAIN->Evaluate(scope, tree);
}
XL_NATIVE(evaluate);
Tree * xl_identity(Scope *scope, Tree *tree)
// ----------------------------------------------------------------------------
// No op, returns the input tree
// ----------------------------------------------------------------------------
{
return tree;
}
Tree * xl_typecheck(Scope *scope, Tree *type, Tree *value)
// ----------------------------------------------------------------------------
// Dispatch a type check to the current evaluator
// ----------------------------------------------------------------------------
{
return MAIN->TypeCheck(scope, type, value);
}
Tree *xl_call(Scope *scope, text prefix, TreeList &argList)
// ----------------------------------------------------------------------------
// Generate a call to the given prefix
// ----------------------------------------------------------------------------
{
XLCall call(prefix);
for (Tree *arg : argList)
call.Arg(arg);
return call(scope);
}
Tree *xl_assign(Scope *scope, Tree *ref, Tree *value)
// ----------------------------------------------------------------------------
// Perform an assignment in the scope (temporary? interpreter only?)
// ----------------------------------------------------------------------------
{
Context context(scope);
return context.Assign(ref, value);
}
Tree *xl_form_error(Scope *scope, Tree *what)
// ----------------------------------------------------------------------------
// Raise an error if we have a form error
// ----------------------------------------------------------------------------
{
bool quickExit = false; // For debugging purpose
if (quickExit)
return what;
static bool recursive = false;
if (recursive)
{
std::cerr << "ABORT - Recursive error during error handling\n"
<< "Error tree: " << what << "\n";
return XL::xl_false;
}
Save<bool> saveRecursive(recursive, true);
Ooops("No pattern match $1 at runtime", what);
return what;
}
Tree *xl_stack_overflow(Tree *what)
// ----------------------------------------------------------------------------
// Return an error evaluating a tree
// ----------------------------------------------------------------------------
{
Ooops("Stack overflow evaluating $1", what);
return what;
}
XL_NATIVE(stack_overflow);
bool xl_same_text(Tree *what, const char *ref)
// ----------------------------------------------------------------------------
// Compile the tree if necessary, then evaluate it
// ----------------------------------------------------------------------------
{
Text *tval = what->AsText(); assert(tval);
return tval->value == text(ref);
}
XL_NATIVE(same_text);
bool xl_same_shape(Tree *left, Tree *right)
// ----------------------------------------------------------------------------
// Check equality of two trees
// ----------------------------------------------------------------------------
{
return Tree::Equal(left, right);
}
kstring xl_infix_name(Infix *infix)
// ----------------------------------------------------------------------------
// Return the name of an infix as a C string
// ----------------------------------------------------------------------------
{
return infix->name.c_str();
}
// ========================================================================
//
// Creating entities (callbacks for compiled code)
//
// ========================================================================
Natural *xl_new_natural(TreePosition pos, ulonglong value)
// ----------------------------------------------------------------------------
// Called by generated code to build a new Natural
// ----------------------------------------------------------------------------
{
Natural *result = new Natural(value, pos);
return result;
}
Real *xl_new_real(TreePosition pos, double value)
// ----------------------------------------------------------------------------
// Called by generated code to build a new Real
// ----------------------------------------------------------------------------
{
Real *result = new Real (value, pos);
return result;
}
Text *xl_new_character(TreePosition pos, char value)
// ----------------------------------------------------------------------------
// Called by generated code to build a new single-quoted Text
// ----------------------------------------------------------------------------
{
Text *result = new Text(text(&value, 1), "'", "'", pos);
return result;
}
Text *xl_new_text(TreePosition pos, text value)
// ----------------------------------------------------------------------------
// Called by generated code to build a new double-quoted Text
// ----------------------------------------------------------------------------
{
Text *result = new Text(value, pos);
return result;
}
Text *xl_new_text_ptr(TreePosition pos, text *value)
// ----------------------------------------------------------------------------
// Called by generated code to build a new double-quoted Text
// ----------------------------------------------------------------------------
{
Text *result = new Text(*value, pos);
return result;
}
Text *xl_new_ctext(TreePosition pos, kstring value)
// ----------------------------------------------------------------------------
// Called by generated code to build a new double-quoted Text
// ----------------------------------------------------------------------------
{
Text *result = new Text(text(value), pos);
return result;
}
Text *xl_new_xtext(TreePosition pos, kstring value, longlong len,
kstring open, kstring close)
// ----------------------------------------------------------------------------
// Called by generated code to build a new arbitrarily-quoted Text
// ----------------------------------------------------------------------------
{
Text *result = new Text(text(value, len), open, close, pos);
return result;
}
Block *xl_new_block(Block *source, Tree *child)
// ----------------------------------------------------------------------------
// Called by generated code to build a new block
// ----------------------------------------------------------------------------
{
Block *result = new Block(source, child);
return result;
}
Prefix *xl_new_prefix(Prefix *source, Tree *left, Tree *right)
// ----------------------------------------------------------------------------
// Called by generated code to build a new Prefix
// ----------------------------------------------------------------------------
{
Prefix *result = new Prefix(source, left, right);
return result;
}
Postfix *xl_new_postfix(Postfix *source, Tree *left, Tree *right)
// ----------------------------------------------------------------------------
// Called by generated code to build a new Postfix
// ----------------------------------------------------------------------------
{
Postfix *result = new Postfix(source, left, right);
return result;
}
Infix *xl_new_infix(Infix *source, Tree *left, Tree *right)
// ----------------------------------------------------------------------------
// Called by generated code to build a new Infix
// ----------------------------------------------------------------------------
{
Infix *result = new Infix(source, left, right);
return result;
}
// ============================================================================
//
// Parsing trees
//
// ============================================================================
Tree *xl_parse_tree_inner(Scope *scope, Tree *tree)
// ----------------------------------------------------------------------------
// Build a parse tree in the current scope
// ----------------------------------------------------------------------------
{
switch(tree->Kind())
{
case NATURAL:
case REAL:
case TEXT:
case NAME:
return tree;
case INFIX:
{
Infix *infix = (Infix *) tree;
Tree *left = xl_parse_tree_inner(scope, infix->left);
Tree *right = xl_parse_tree_inner(scope, infix->right);
Infix *result = new Infix(infix, left, right);
return result;
}
case PREFIX:
{
Prefix *prefix = (Prefix *) tree;
Tree *left = xl_parse_tree_inner(scope, prefix->left);
Tree *right = xl_parse_tree_inner(scope, prefix->right);
Prefix *result = new Prefix(prefix, left, right);
return result;
}
case POSTFIX:
{
Postfix *postfix = (Postfix *) tree;
Tree *left = xl_parse_tree_inner(scope, postfix->left);
Tree *right = xl_parse_tree_inner(scope, postfix->right);
Postfix *result = new Postfix(postfix, left, right);
return result;
}
case BLOCK:
{
Block *block = (Block *) tree;
Tree *result = block->child;
if (block->opening == "{" && block->closing == "}")
{
Block *child = result->AsBlock();
if (child && child->opening == "{" && child->closing == "}")
{
// Case where we have parse_tree {{x}}: Return {x}
result = xl_parse_tree_inner(scope, child->child);
result = new Block(block, result);
return result;
}
// Name or expression in { }
result = xl_evaluate(scope, result);
return result;
}
result = xl_parse_tree_inner(scope, result);
result = new Block(block, result);
return result;
}
}
return tree;
}
Tree *xl_parse_tree(Scope *scope, Tree *code)
// ----------------------------------------------------------------------------
// Entry point for parse_tree
// ----------------------------------------------------------------------------
{
if (Block *block = code->AsBlock())
code = block->child;
return xl_parse_tree_inner(scope, code);
}
Tree *xl_parse_text(text source)
// ----------------------------------------------------------------------------
// Generate a tree from text
// ----------------------------------------------------------------------------
{
std::istringstream input(source);
Parser parser(input, MAIN->syntax,MAIN->positions,*MAIN->errors, "<text>");
return parser.Parse();
}
// ============================================================================
//
// Write a tree to standard output (temporary hack)
//
// ============================================================================
static bool isAbsolute(text path)
// ----------------------------------------------------------------------------
// Test absolute/relative path
// ----------------------------------------------------------------------------
{
if (path == "")
return false;
#ifdef HAVE_WINDOWS_H
if (path[0] == '/' || path[0] == '\\')
return true;
if (path.length() >= 2 && path[1] == ':')
return true;
return false;
#else
return (path[0] == '/');
#endif
}
static void xl_list_files(Scope *scope, Tree *patterns, Tree_p *&parent)
// ----------------------------------------------------------------------------
// Append all files found in the parent
// ----------------------------------------------------------------------------
{
if (Block *block = patterns->AsBlock())
{
xl_list_files(scope, block->child, parent);
return;
}
if (Infix *infix = patterns->AsInfix())
{
if (IsCommaList(infix))
{
xl_list_files(scope, infix->left, parent);
xl_list_files(scope, infix->right, parent);
return;
}
}
patterns = xl_evaluate(scope, patterns);
if (Text *regexp = patterns->AsText())
{
glob_t files;
text filename = regexp->value;
glob(filename.c_str(), GLOB_MARK, nullptr, &files);
for (uint i = 0; i < files.gl_pathc; i++)
{
std::string entry(files.gl_pathv[i]);
Text *listed = new Text(entry);
if (*parent)
{
Infix *added = new Infix(",", *parent, listed);
*parent = added;
parent = &added->right;
}
else
{
*parent = listed;
}
}
globfree(&files);
return;
}
Ooops("Malformed files list $1", patterns);
}
// ============================================================================
//
// File utilities
//
// ============================================================================
Tree *xl_list_files(Scope *scope, Tree *patterns)
// ----------------------------------------------------------------------------
// List all files in the given pattern
// ----------------------------------------------------------------------------
{
Tree_p result = nullptr;
Tree_p *parent = &result;
xl_list_files(scope, patterns, parent);
if (!result)
result = xl_nil;
return result;
}
bool xl_file_exists(Scope *scope, Tree_p self, text path)
// ----------------------------------------------------------------------------
// Check if a file exists
// ----------------------------------------------------------------------------
{
if (!isAbsolute(path))
{
Context context(scope);
path = context.ResolvePrefixedPath(path);
if (!isAbsolute(path))
{
// Relative path: look in same directory as parent
if (Tree * dir = context.Named("module_dir"))
{
if (Text * txt = dir->AsText())
path = txt->value + "/" + path;
}
}
}
utf8_filestat_t st;
return (utf8_stat (path.c_str(), &st) < 0) ? false : true;
}
// ============================================================================
//
// Loading trees from external files
//
// ============================================================================
struct ImportedFileInfo : Info
// ----------------------------------------------------------------------------
// Information about a file that was imported (save full path)
// ----------------------------------------------------------------------------
{
ImportedFileInfo(text path)
: path(path) {}
text path;
};
Tree *xl_use(Scope *scope, Tree *self)
// ----------------------------------------------------------------------------
// Load a file from disk without evaluating it
// ----------------------------------------------------------------------------
{
Infix *infix = self->AsInfix();
if (!infix)
{
Ooops("Unexpected use: $1", self);
return self;
}
text modname = "";
Name *name = infix->right->AsName();
if (name)
{
modname = name->value;
}
else
{
Tree *value = xl_evaluate(scope, infix->right);
if (Text *text = value->AsText())
{
modname = text->value;
}
else
{
Ooops("Invalid use name $1", infix->right);
return self;
}
}
text path = "";
ImportedFileInfo *info = self->GetInfo<ImportedFileInfo>();
if (info)
{
path = info->path;
}
else
{
if (path == "")
path = MAIN->SearchFile(modname);
if (path == "" && !isAbsolute(modname))
{
// Relative path: look in same directory as parent
static Name_p module_dir = new Name("module_dir");
Context context(scope);
if (Tree * dir = context.Bound(module_dir))
{
if (Text * txt = dir->AsText())
{
path = txt->value + "/" + modname;
utf8_filestat_t st;
if (utf8_stat (path.c_str(), &st) < 0)
path = "";
}
}
}
if (path == "")
{
Ooops("Source file $2 not found for $1", self).Arg(modname);
return XL::xl_false;
}
info = new ImportedFileInfo(path);
self->SetInfo<ImportedFileInfo> (info);
}
// Check if the file has already been loaded somehwere.
// If so, return the loaded file
bool exists = MAIN->files.count(path);
if (!exists)
{
record(fileload, "Loading %s", path.c_str());
bool hadError = MAIN->LoadFile(path);
if (hadError)
{
Ooops("Unable to load file $2 for $1", self).Arg(path);
return XL::xl_false;
}
}
SourceFile &sf = MAIN->files[path];
Tree *result = sf.tree;
return result;
}
Tree *xl_load(Scope *scope, Tree *self)
// ----------------------------------------------------------------------------
// Load a file from disk
// ----------------------------------------------------------------------------
{
return xl_use(scope, self);
}
struct LoadDataInfo : Info
// ----------------------------------------------------------------------------
// Information about the data that was loaded
// ----------------------------------------------------------------------------
{
LoadDataInfo(): files() {}
struct PerFile
{
PerFile(): data(), loaded(), mtime(0) {}
struct Row
{
TreeList args;
};
typedef std::vector<Row> Data;
Data data;
Tree_p loaded;
time_t mtime;
};
std::map<text, PerFile> files;
};
Tree *xl_load_data(Scope *scope, Tree *self,
text name, text prefix, text fieldSeps, text recordSeps,
Tree *body)
// ----------------------------------------------------------------------------
// Load a comma-separated or tab-separated file from disk
// ----------------------------------------------------------------------------
{
// Open data file
text path = MAIN->SearchFile(name);
if (path == "")
{
Ooops("CSV file $2 not found in $1", self).Arg(name);
return XL::xl_false;
}
utf8_ifstream input(path.c_str(), std::ifstream::in);
if (!input.good())
{
Ooops("Unable to load data for $1.\n"
"(Accessing $2 resulted in the following error: $3)",
self).Arg(path).Arg(strerror(errno));
return XL::xl_nil;
}
return xl_load_data(scope, self, path,
input, true, true,
prefix, fieldSeps, recordSeps,
body);
}
Tree *xl_load_data(Scope *scope, Tree *self, text inputName,
std::istream &input, bool cached, bool statTime,
text prefix, text fieldSeps, text recordSeps,
Tree *body)
// ----------------------------------------------------------------------------
// Variant reading from a stream directly
// ----------------------------------------------------------------------------
{
// Check if the file has already been loaded somehwere.
// If so, return the loaded data
LoadDataInfo *info = self->GetInfo<LoadDataInfo>();
if (!info)
{
// Create cache
info = new LoadDataInfo();
self->SetInfo<LoadDataInfo> (info);
}
// Load per-file cached data
LoadDataInfo::PerFile &perFile = info->files[inputName];
if (perFile.loaded)
{
if (statTime)
{
// Check if we need to reload the contents of the file
// because it has been modified
utf8_filestat_t st;
utf8_stat (inputName.c_str(), &st);
if (perFile.mtime != st.st_mtime)
cached = false;
perFile.mtime = st.st_mtime;
}
if (cached)
{
Tree_p result = xl_false;
if (prefix.size())
{
ulong i, max = perFile.data.size();
for (i = 0; i < max; i++)
{
LoadDataInfo::PerFile::Row &row = perFile.data[i];
result = xl_call(scope, prefix, row.args);
}
return result;
}
return perFile.loaded;
}
// Restart with clean data
perFile.data.clear();
perFile.loaded = nullptr;
}
// Read data from file
char buffer[256];
char *ptr = buffer;
char *end = buffer + sizeof(buffer) - 1;
Tree_p tree = nullptr;
Tree_p line = nullptr;
Tree_p *treePtr = &tree;
Tree_p *linePtr = &line;
bool hasQuote = false;
bool hasRecord = false;
bool hasField = false;
bool hasPrefix = prefix.size() != 0;
LoadDataInfo::PerFile::Row row;
*end = 0;
while (input.good())
{
int c = input.get();
if (!hasQuote && ptr == buffer && c != '\n' && isspace(c))
continue;
if (hasQuote)
{
hasRecord = false;
hasField = false;
}
else
{
hasRecord = recordSeps.find(c) != recordSeps.npos || c == EOF;
hasField = fieldSeps.find(c) != fieldSeps.npos;
}
if (hasRecord || hasField)
{
c = 0;
}
else if (c == '"')
{
int next = input.peek();
bool escapedQuote = (hasQuote && next == '"');
if (escapedQuote)
input.get();
else
hasQuote = !hasQuote;
}
if (ptr < end)
*ptr++ = c;
if (!c)
{
text token;
Tree *child = nullptr;
if (isdigit(buffer[0]) ||
((buffer[0] == '-' || buffer[0] == '+') && isdigit(buffer[1])))
{
char *ptr2 = nullptr;
longlong l = strtoll(buffer, &ptr2, 10);
if (ptr2 == ptr-1)
{
child = new Natural(l);
}
else
{
double d = strtod (buffer, &ptr2);
if (ptr2 == ptr-1)
child = new Real(d);
}
}
if (child == nullptr)
{
if (buffer[0] == '"' && ptr > buffer+2 && ptr[-2] == '"')
token = text(buffer+1, ptr - buffer - 3);
else
token = text(buffer, ptr - buffer - 1);
child = new Text(token);
}
// Combine data that we just read to current line
if (hasPrefix)
{
row.args.push_back(child);
}
else
{
if (*linePtr)
{
Infix *infix = new Infix(",", *linePtr, child);
*linePtr = infix;
linePtr = &infix->right;
}
else
{
*linePtr = child;
}
}
// Combine as line if necessary
if (hasRecord)
{
if (hasPrefix)
{
if (body)
row.args.push_back(body);
perFile.data.push_back(row);
tree = xl_call(scope, prefix, row.args);
row.args.clear();
}
else
{
if (body)
{
if (*linePtr)
{
Infix *infix = new Infix(",", *linePtr, body);
*linePtr = infix;
linePtr = &infix->right;
}
else
{
*linePtr = body;
}
}
if (*treePtr)
{
Infix *infix = new Infix("\n", *treePtr, line);
*treePtr = infix;
treePtr = &infix->right;
}
else
{
*treePtr = line;
}
line = nullptr;
linePtr = &line;
}
}
ptr = buffer;
}
}
if (!tree)
tree = xl_false;
perFile.loaded = tree;
return tree;
}
// ============================================================================
//
// C functions called from builtins.xl
//
// ============================================================================
extern "C"
{
bool xl_write_natural(ulonglong x)
// ----------------------------------------------------------------------------
// Write a natural value
// ----------------------------------------------------------------------------
{
// HACK: Until type system is more robust, print as signed value
std::cout << (longlong) x;
return true;
}
XL_NATIVE(write_natural);
bool xl_write_integer(longlong x)
// ----------------------------------------------------------------------------
// Write an integer value
// ----------------------------------------------------------------------------
{
std::cout << x;
return true;
}
XL_NATIVE(write_integer);
bool xl_write_real(double x)
// ----------------------------------------------------------------------------
// Write a double value
// ----------------------------------------------------------------------------
{
std::cout << x;
return true;
}
XL_NATIVE(write_real);
bool xl_write_text(kstring x)
// ----------------------------------------------------------------------------
// Write a text value
// ----------------------------------------------------------------------------
{
std::cout << x;
return true;
}
XL_NATIVE(write_text);
bool xl_write_tree(Tree *tree)
// ----------------------------------------------------------------------------
// Write a text value
// ----------------------------------------------------------------------------
{
std::cout << tree;
return true;
}
bool xl_write_character(char x)
// ----------------------------------------------------------------------------
// Write a character value
// ----------------------------------------------------------------------------
{
std::cout << x;
return true;
}
bool xl_write_cr()
// ----------------------------------------------------------------------------
// Write a line separator
// ----------------------------------------------------------------------------
{
std::cout << '\n';
return true;
}
bool xl_text_eq(kstring x, kstring y)
// ----------------------------------------------------------------------------
// Compare two texts
// ----------------------------------------------------------------------------
{
return strcmp(x, y) == 0;
}
bool xl_text_ne(kstring x, kstring y)
// ----------------------------------------------------------------------------
// Compare two texts
// ----------------------------------------------------------------------------
{
return strcmp(x, y) != 0;
}
bool xl_text_lt(kstring x, kstring y)
// ----------------------------------------------------------------------------
// Compare two texts
// ----------------------------------------------------------------------------
{
return strcmp(x, y) < 0;
}
bool xl_text_le(kstring x, kstring y)
// ----------------------------------------------------------------------------