-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbsparser.c
4817 lines (4471 loc) · 158 KB
/
bsparser.c
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
/*
* Copyright 2022 Rochus Keller <mailto:[email protected]>
*
* This file is part of the BUSY build system.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
// It's actually not only a parser, but it also directly interprets the syntax with no full AST.
#include "bsparser.h"
#include "bslex.h"
#include "bshost.h"
#include "bsunicode.h"
#include <memory.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "lauxlib.h"
#include <stdarg.h>
typedef enum BS_ParseArgs { BS_PathToSourceRoot = 1, BS_NewModule, BS_Params } BS_ParseArgs;
typedef struct BSScope {
int table; // index to table on the stack
int n; // number of ordered items
} BSScope;
typedef struct BSParserContext {
BSHiLex* lex;
BSScope module;
const char* dirpath; // normalized absolute path to current dir
const char* label; // pointer to internal of dirpath to display in error messages
const char* filepath; // normalized absolute path to BUSY in current dir
unsigned builtins : 8;
unsigned skipMode : 1; // when on, read over tokens without executing
unsigned locInfo : 1; // when on, add #row, #col and #file to AST elements
unsigned fullAst : 1; // when on, also add AST statement and expression elements
unsigned numRefs : 8; // optional index a weak #refs table pointing to all objects with identdef
unsigned xref : 8; // optional index to xref table or 0; if a table is present, build bi-dir xref
lua_State* L;
BSLogger logger;
void* loggerData;
} BSParserContext;
static BSToken nextToken(BSParserContext* ctx)
{
return bslex_hnext(ctx->lex);
}
static BSToken peekToken(BSParserContext* ctx, int off ) // off = 1..
{
return bslex_hpeek(ctx->lex,off);
}
typedef struct BSIdentDef {
const char* name;
int len;
int visi; // BSVisibility
BSRowCol loc;
} BSIdentDef;
static void printLexerStack(BSParserContext* ctx)
{
int i = bslex_hlevelcount(ctx->lex) - 2;
while(i >= 0)
{
BSToken level = bslex_hlevel(ctx->lex,i);
BSRowCol loc;
loc.row = level.loc.row;
loc.col = level.loc.col;
va_list args;
va_end(args);
ctx->logger(BS_Error, ctx->loggerData,level.source,loc, " instantiated from here", args );
i--;
}
}
static void error(BSParserContext* ctx, int row, int col, const char* format, ... )
{
BSRowCol loc;
loc.row = row;
loc.col = col;
va_list args;
va_start(args, format);
ctx->logger(BS_Error, ctx->loggerData,bslex_hfilepath(ctx->lex),loc, format, args );
va_end(args);
printLexerStack(ctx);
lua_pushnil(ctx->L);
lua_error(ctx->L);
}
static void warning(BSParserContext* ctx, int row, int col, const char* format, ... )
{
BSRowCol loc;
loc.row = row;
loc.col = col;
va_list args;
va_start(args, format);
ctx->logger(BS_Warning, ctx->loggerData,bslex_hfilepath(ctx->lex),loc, format, args );
va_end(args);
}
static void information(BSParserContext* ctx, const char* format, ... )
{
BSRowCol loc;
loc.row = 0;
loc.col = 0;
va_list args;
va_start(args, format);
ctx->logger(BS_Info, ctx->loggerData,0,loc, format, args );
va_end(args);
}
static void debugprint(BSLogger out, void* data, const char* format, ... )
{
BSRowCol loc;
loc.row = 0;
loc.col = 0;
va_list args;
va_start(args, format);
out(BS_Debug, data,0,loc, format, args );
va_end(args);
}
static void unexpectedToken(BSParserContext* ctx, BSToken* t, const char* where)
{
error(ctx,t->loc.row, t->loc.col,"unexpected token %s: %s", where, bslex_tostring(t->tok) );
}
#define BS_BEGIN_LUA_FUNC(ctx,diff) const int $stack = lua_gettop((ctx)->L) + diff
#define BS_END_LUA_FUNC(ctx) int $end = lua_gettop((ctx)->L); assert($stack == $end)
static void dumpimp(lua_State* L, int index, BSLogger out, void* data, const char* title)
{
if( index <= 0 )
index += lua_gettop(L) + 1;
if( title && *title != 0 )
debugprint(out,data,title);
switch( lua_type(L,index) )
{
case LUA_TNIL:
debugprint(out,data,"nil");
break;
case LUA_TBOOLEAN:
debugprint(out,data,"bool %d",lua_toboolean(L,index));
break;
case LUA_TNUMBER:
if( ( (int)lua_tonumber(L,index) - lua_tonumber(L,index) ) == 0.0 )
debugprint(out,data,"int %d",lua_tointeger(L,index));
else
debugprint(out,data,"number %f",lua_tonumber(L,index));
break;
case LUA_TSTRING:
debugprint(out,data,"string \"%s\"",lua_tostring(L,index));
break;
case LUA_TTABLE:
debugprint(out,data,"*** table: %p", lua_topointer(L,index));
if( lua_getmetatable(L,index) )
{
debugprint(out,data," metatable %p", lua_topointer(L,-1));
lua_pop(L,1);
}
lua_pushnil(L); /* first key */
while (lua_next(L, index) != 0) {
const int key = lua_gettop(L)-1;
const int value = lua_gettop(L);
if( lua_type(L,key) == LUA_TTABLE )
lua_pushfstring(L,"table %p", lua_topointer(L,key));
else
lua_pushvalue(L,key);
if( lua_type(L,value) == LUA_TTABLE )
lua_pushfstring(L,"table %p", lua_topointer(L,value));
else
lua_pushvalue(L,value);
debugprint(out,data," %s = %s", lua_tostring(L,-2), lua_tostring(L,-1));
lua_pop(L, 3);
}
break;
case LUA_TFUNCTION:
debugprint(out,data,"function %p",lua_topointer(L,index));
break;
default:
debugprint(out,data,"<lua value>");
break;
}
}
static void dump(BSParserContext* ctx, int index)
{
BS_BEGIN_LUA_FUNC(ctx,0);
dumpimp(ctx->L,index,ctx->logger,ctx->loggerData,0);
BS_END_LUA_FUNC(ctx);
}
static void dump2(BSParserContext* ctx, const char* title, int index)
{
BS_BEGIN_LUA_FUNC(ctx,0);
dumpimp(ctx->L,index,ctx->logger,ctx->loggerData,title);
BS_END_LUA_FUNC(ctx);
}
static void checkUnique(BSParserContext* ctx, BSScope* scope, BSIdentDef* id )
{
lua_pushlstring(ctx->L,id->name,id->len);
const char* name = lua_tostring(ctx->L,-1);
lua_rawget(ctx->L,scope->table);
const int isnil = lua_isnil(ctx->L,-1);
lua_pop(ctx->L,1);
if( !isnil )
error(ctx,id->loc.row, id->loc.col,"name is not unique in scope: '%s'", name );
}
static void addLocInfo(BSParserContext* ctx, BSRowCol loc, int table)
{
if( ctx->locInfo )
{
lua_pushinteger(ctx->L, loc.row );
lua_setfield(ctx->L, table, "#row");
lua_pushinteger(ctx->L, loc.col );
lua_setfield(ctx->L, table, "#col");
lua_getfield(ctx->L, ctx->module.table,"#file");
lua_setfield(ctx->L,table,"#file");
}
}
enum { ROW_BIT_LEN = 19, COL_BIT_LEN = 32 - ROW_BIT_LEN };
unsigned int bs_torowcol(int row, int col)
{
return ( row << COL_BIT_LEN ) | ( col & ( ( 1 << COL_BIT_LEN ) -1 ));
}
unsigned int bs_torow(int rowcol)
{
return ( rowcol >> COL_BIT_LEN ) ;
}
unsigned int bs_tocol(int rowcol)
{
return rowcol & ( ( 1 << COL_BIT_LEN ) -1 );
}
static void addXref(BSParserContext* ctx, BSRowCol loc, int decl)
{
if( !ctx->xref )
return;
const int top = lua_gettop(ctx->L);
if( decl <= 0 )
decl += top + 1;
// #xref table: filepath -> list_of_idents
// list_of_idents: rowcol -> set_of_decls
// or: rowcol -> path
// decl.#xref table: filepath -> set of rowcol
lua_pushstring(ctx->L, bs_denormalize_path(ctx->filepath) );
lua_rawget(ctx->L,ctx->xref);
assert( lua_istable(ctx->L,-1) );
const int list_of_idents = lua_gettop(ctx->L);
const unsigned int rowCol = bs_torowcol(loc.row, loc.col);
if( lua_isstring(ctx->L, decl ) )
{
lua_pushvalue(ctx->L,decl);
const int path = lua_gettop(ctx->L);
if( *lua_tostring(ctx->L, path) == '.' )
{
lua_getfield(ctx->L,ctx->module.table,"#dir");
bs_add_path(ctx->L, -1, path );
lua_replace(ctx->L,path);
lua_pop(ctx->L,1);
}
if( bs_exists(lua_tostring(ctx->L, path) ) )
{
lua_pushinteger(ctx->L,rowCol);
lua_pushvalue(ctx->L,path);
lua_rawset(ctx->L,list_of_idents);
}
lua_pop(ctx->L,2); // list_of_idents, path
return;
}
lua_pushinteger(ctx->L,rowCol);
lua_rawget(ctx->L,list_of_idents);
if( !lua_istable(ctx->L,-1) )
{
lua_pop(ctx->L,1); // nil
lua_createtable(ctx->L,0,0);
lua_pushinteger(ctx->L,rowCol);
lua_pushvalue(ctx->L,-2);
lua_rawset(ctx->L,list_of_idents);
}
const int set_of_decls = lua_gettop(ctx->L);
assert( lua_istable(ctx->L,-1) );
lua_pushvalue(ctx->L,decl);
lua_pushboolean(ctx->L,1);
lua_rawset(ctx->L,set_of_decls);
lua_pop(ctx->L,2); // list_of_idents, set_of_decls
lua_getfield(ctx->L, decl, "#xref");
if( !lua_istable(ctx->L,-1) )
{
lua_pop(ctx->L,1); // nil
lua_createtable(ctx->L,0,0);
lua_pushvalue(ctx->L,-1);
lua_setfield(ctx->L,decl, "#xref");
}
const int decl_xref = lua_gettop(ctx->L);
lua_pushstring(ctx->L, bs_denormalize_path(ctx->filepath) );
lua_rawget(ctx->L,decl_xref);
if( !lua_istable(ctx->L,-1) )
{
lua_pop(ctx->L,1); // nil
lua_createtable(ctx->L,0,0);
lua_pushstring(ctx->L, bs_denormalize_path(ctx->filepath) );
lua_pushvalue(ctx->L,-2);
lua_rawset(ctx->L,decl_xref);
}
const int set_of_rowcol = lua_gettop(ctx->L);
lua_pushinteger(ctx->L,rowCol);
lua_pushboolean(ctx->L,1);
lua_rawset(ctx->L, set_of_rowcol);
lua_pop(ctx->L,2); // decl_xref, set_of_rowcol
assert( top == lua_gettop(ctx->L));
}
static void addNumRef(BSParserContext* ctx, int obj)
{
if( !ctx->numRefs )
return;
const int id = lua_objlen(ctx->L,ctx->numRefs) + 1;
lua_pushvalue(ctx->L,obj);
lua_rawseti(ctx->L,ctx->numRefs,id);
lua_pushinteger(ctx->L,id);
lua_setfield(ctx->L,obj,"#ref");
}
static BSIdentDef identdef(BSParserContext* ctx, BSScope* scope)
{
BSToken t = nextToken(ctx);
if( t.tok != Tok_ident )
error(ctx, t.loc.row, t.loc.col , "expecting an ident");
BSIdentDef res;
res.name = t.val;
res.len = t.len;
res.loc = t.loc;
checkUnique(ctx,scope,&res);
t = peekToken(ctx,1);
switch( t.tok )
{
case Tok_Bang:
res.visi = BS_PublicDefault;
nextToken(ctx);
break;
case Tok_Star:
res.visi = BS_Public;
nextToken(ctx);
break;
case Tok_Minus:
res.visi = BS_Protected;
nextToken(ctx);
break;
default:
res.visi = BS_Private;
break;
}
return res;
}
static void addToScope(BSParserContext* ctx, BSScope* scope, BSIdentDef* id, int table )
{
BS_BEGIN_LUA_FUNC(ctx,0);
lua_pushinteger(ctx->L,id->visi);
lua_setfield(ctx->L,table,"#visi");
lua_pushlstring(ctx->L,id->name, id->len);
lua_setfield(ctx->L,table,"#name");
lua_pushvalue(ctx->L,scope->table);
lua_setfield(ctx->L,table,"#owner");
lua_pushlstring(ctx->L,id->name, id->len);
lua_pushvalue(ctx->L,table);
lua_rawset(ctx->L,scope->table);
lua_pushinteger(ctx->L, ++scope->n );
lua_pushvalue(ctx->L,table);
lua_rawset(ctx->L,scope->table);
BS_END_LUA_FUNC(ctx);
}
static int expression(BSParserContext* ctx, BSScope* scope, int lhsType);
static void parampath(lua_State* L, int module, const char* name, int len);
static int /*BSPathStatus*/ caclFsDir(BSParserContext* ctx, const char* path, int pathlen)
{
BS_BEGIN_LUA_FUNC(ctx,1);
lua_pushvalue(ctx->L, ctx->module.table);
const int root = lua_gettop(ctx->L);
lua_getfield(ctx->L,root,"^");
while( !lua_isnil(ctx->L,-1) )
{
lua_replace(ctx->L,root);
lua_getfield(ctx->L,root,"^");
}
lua_pop(ctx->L,1);
lua_getfield(ctx->L,root,"#dir");
lua_replace(ctx->L,root);
if( *path != '/' )
{
// this is a relative path, either normalized or not
// first calc the absolute path of the new subdir (to avoide ../ exhaution)
lua_getfield(ctx->L,ctx->module.table,"#dir");
if( *path != '.' )
{
// not yet normalized
lua_pushstring(ctx->L,"./");
lua_pushlstring(ctx->L,path,pathlen);
lua_concat(ctx->L,2);
}else
lua_pushlstring(ctx->L,path,pathlen);
// stack: root, #dir, relpath
int res = bs_add_path(ctx->L, -2, -1);
if( res != BS_OK )
{
lua_pop(ctx->L,3);
return res;
}
// stack: root, #dir, relpath, abspath
lua_replace(ctx->L,-3);
lua_pop(ctx->L,1);
}else
lua_pushlstring(ctx->L,path,pathlen);
// stack: root, abs submod fspath
// now calc the relative path
int res = bs_makeRelative(lua_tostring(ctx->L,-2),lua_tostring(ctx->L,-1));
lua_pop(ctx->L,2);
if( res == BS_OK )
lua_pushstring(ctx->L, bs_global_buffer() );
BS_END_LUA_FUNC(ctx);
return res;
}
static void submodule(BSParserContext* ctx, int subdir)
{
BS_BEGIN_LUA_FUNC(ctx,0);
nextToken(ctx); // keyword
BSIdentDef id = identdef(ctx,&ctx->module);
lua_getfield(ctx->L,ctx->module.table,"#dummy");
if( !lua_isnil(ctx->L,-1) )
{
va_list args;
va_end(args);
ctx->logger(BS_Error, ctx->loggerData,ctx->filepath,id.loc, "submod declarations not allowed here", args );
lua_pushnil(ctx->L);
lua_error(ctx->L);
}else
lua_pop(ctx->L,1);
const char* path = id.name;
int pathlen = id.len;
if( id.visi == BS_PublicDefault )
error(ctx, id.loc.row, id.loc.col,"'!' is not applicable here" );
BSToken t = peekToken(ctx,1);
BSToken pt = t;
if( t.tok == Tok_Eq || t.tok == Tok_ColonEq )
{
nextToken(ctx); // eat '='
t = nextToken(ctx);
if( t.tok == Tok_path || t.tok == Tok_ident )
{
path = t.val;
pathlen = t.len;
pt = t;
// We now support all paths (no necessity of source-tree and dir-tree correspondence)
// #rdir is made of idents, not dir names
// the following code is only for backward compatibility
if( subdir )
{
if( t.tok == Tok_path )
{
// check path is relative and only one level
if( *path == '\'' )
{
path++;
pathlen -= 2;
}
if( strncmp(path,"//",2) == 0 || strncmp(path,"..",2) == 0)
error(ctx, pt.loc.row, pt.loc.col,"this path is not supported here" );
if( strncmp(path,".",1) == 0 )
{
path += 2;
pathlen -= 2;
}
int i;
for( i = 0; i < pathlen; i++ )
{
if( path[i] == '/' )
error(ctx, pt.loc.row, pt.loc.col,"expecting an immediate subdirectory" );
}
// NOTE: no xref path here because it is a directory and we already have the ident
}
}
}else
error(ctx, pt.loc.row, pt.loc.col,"expecting a path or an ident" );
t = peekToken(ctx,1);
}
const char* altpath = 0;
int altpathlen = 0;
if( t.tok == Tok_else )
{
nextToken(ctx); // eat 'else'
t = nextToken(ctx);
if( t.tok == Tok_path )
{
altpath = t.val;
altpathlen = t.len;
lua_pushlstring(ctx->L,altpath,altpathlen);
addXref(ctx,t.loc,-1);
lua_pop(ctx->L,1);
pt = t;
}else
error(ctx, pt.loc.row, pt.loc.col,"expecting a path after 'else'" );
t = peekToken(ctx,1);
}
if( t.tok == Tok_Lpar )
{
nextToken(ctx); // eat lpar
t = peekToken(ctx,1);
while( t.tok != Tok_Rpar && t.tok != Tok_Eof )
{
if( t.tok != Tok_ident )
error(ctx, t.loc.row, t.loc.col,"expexting an identifier" );
nextToken(ctx); // eat ident
BSToken pname = t;
parampath(ctx->L,ctx->module.table, id.name,id.len);
lua_pop(ctx->L,1);
lua_pushstring(ctx->L,".");
lua_pushlstring(ctx->L,pname.val,pname.len);
lua_concat(ctx->L,3);
const int nm = lua_gettop(ctx->L);
t = peekToken(ctx,1);
if( t.tok == Tok_Eq || t.tok == Tok_ColonEq )
{
nextToken(ctx); // eat eq
t = peekToken(ctx,1);
expression(ctx,&ctx->module,0);
lua_getfield(ctx->L,-1,"#kind");
const int kind = lua_tointeger(ctx->L,-1);
lua_pop(ctx->L,2); // type, kind
if( kind != BS_BaseType )
error(ctx, t.loc.row, t.loc.col,"parameter value must be of basic type" );
switch( lua_type(ctx->L,-1) )
{
case LUA_TBOOLEAN:
lua_pushstring(ctx->L, lua_toboolean(ctx->L,-1) ? "true" : "false");
lua_replace(ctx->L,-2);
break;
case LUA_TNUMBER:
lua_pushstring(ctx->L, lua_tostring(ctx->L,-1));
lua_replace(ctx->L,-2);
break;
case LUA_TSTRING:
break;
}
t = peekToken(ctx,1);
}else
{
lua_pushstring(ctx->L,"true");
}
const int val = lua_gettop(ctx->L);
// check if param is already set, otherwise set it
lua_pushvalue(ctx->L,nm);
lua_rawget(ctx->L,BS_Params);
const int overridden = !lua_isnil(ctx->L,-1);
lua_pop(ctx->L,1);
if( overridden )
{
warning(ctx,pname.loc.row,pname.loc.col,"parameter %s is overridden by outer value", lua_tostring(ctx->L,nm));
lua_pop(ctx->L,2); // nm, val
}else
{
lua_createtable(ctx->L,0,0);
lua_pushvalue(ctx->L,-2);
lua_rawseti(ctx->L,-2,1); // the param value is a table which carries the value in index 1
lua_replace(ctx->L,-2);
// stack: nm, tables
// because value is boxed in a table the module knows not to do visibility check
lua_rawset(ctx->L,BS_Params);
}
if( t.tok == Tok_Comma )
{
nextToken(ctx); // eat comma
t = peekToken(ctx,1);
}
}
if( t.tok == Tok_Eof )
error(ctx, t.loc.row, t.loc.col,"non-terminated module parameter list" );
else
nextToken(ctx); // eat rpar
}
lua_createtable(ctx->L,0,0); // module definition
const int module = lua_gettop(ctx->L);
lua_pushinteger(ctx->L, BS_ModuleDef);
lua_setfield(ctx->L,-2,"#kind");
lua_pushvalue(ctx->L,ctx->module.table);
lua_setfield(ctx->L,-2,"^"); // set reference to outer scope
addToScope(ctx, &ctx->module, &id, module ); // the outer module directly references this module, no adapter
addXref(ctx, id.loc, module);
lua_getfield(ctx->L,ctx->module.table,"#rdir");
lua_pushstring(ctx->L,"/");
lua_pushlstring(ctx->L,id.name,id.len);
lua_pushvalue(ctx->L,-1);
lua_setfield(ctx->L,module,"#dirname");
lua_concat(ctx->L,3);
lua_setfield(ctx->L,module,"#rdir"); // construct rdir from root by concatenating the subdir identdefs
lua_pushinteger(ctx->L,id.loc.row);
lua_setfield(ctx->L,module,"#row");
lua_pushinteger(ctx->L,id.loc.col);
lua_setfield(ctx->L,module,"#col");
if( altpath )
{
if( *altpath == '/' )
lua_pushlstring(ctx->L,altpath,altpathlen); // this is already an absolute path
else if( *altpath == '.' )
{
// already normalized
lua_getfield(ctx->L,ctx->module.table,"#dir");
lua_pushlstring(ctx->L,altpath,altpathlen);
if( bs_add_path(ctx->L, -2, -1) != 0 )
error(ctx, pt.loc.row, pt.loc.col,"cannot convert this path (1)" );
lua_replace(ctx->L,-3);
lua_pop(ctx->L,1);
}else
{
if(*altpath == '\'')
{
altpath++;
altpathlen -= 2;
}
lua_pushstring(ctx->L,ctx->dirpath);
lua_pushstring(ctx->L,"/");
lua_pushlstring(ctx->L,altpath,altpathlen);
lua_concat(ctx->L,3);
}
lua_setfield(ctx->L,module,"#altmod");
}
lua_pushcfunction(ctx->L, bs_parse);
if(*path == '\'')
{
path++;
pathlen -= 2;
}
if( caclFsDir(ctx, path, pathlen) != BS_OK )
{
error(ctx, pt.loc.row, pt.loc.col,"error creating relative file system path" );
} // else
lua_setfield(ctx->L,module,"#fsrdir");
if( *path == '/' )
{
lua_pushlstring(ctx->L,path,pathlen); // this is already an absolute path
}else
{
// submod name = relpath
if( *path == '.' )
{
// already normalized, either ./ or ../
lua_getfield(ctx->L,ctx->module.table,"#dir");
lua_pushlstring(ctx->L,path,pathlen);
if( bs_add_path(ctx->L, -2, -1) != 0 )
error(ctx, pt.loc.row, pt.loc.col,"cannot convert this path (4)" );
lua_replace(ctx->L,-3);
lua_pop(ctx->L,1);
}else
{
lua_pushstring(ctx->L,ctx->dirpath);
lua_pushstring(ctx->L,"/");
lua_pushlstring(ctx->L,path,pathlen);
lua_concat(ctx->L,3);
}
}
const int newPath = lua_gettop(ctx->L);
lua_pushvalue(ctx->L,ctx->module.table);
while( !lua_isnil(ctx->L,-1) )
{
// if path is already present then this module is called recursively, i.e. indefinitely
lua_getfield(ctx->L,-1,"#dir");
if( lua_equal(ctx->L,newPath,-1) )
error(ctx, pt.loc.row, pt.loc.col,"path points to the same directory as current or outer module" );
lua_pop(ctx->L,1);
lua_getfield(ctx->L,-1,"^");
lua_replace(ctx->L,-2);
}
lua_pop(ctx->L,1);
lua_pushvalue(ctx->L,module);
lua_pushvalue(ctx->L,BS_Params);
lua_call(ctx->L,3,1); // call bs_parse
lua_pop(ctx->L,1); // module returned by bs_parse (the same as handed in)
lua_getfield(ctx->L, ctx->module.table, "#inst"); // the instance of the outer table
lua_pushlstring(ctx->L,id.name,id.len);
lua_getfield(ctx->L, module, "#inst"); // the instance of the nested table
lua_rawset(ctx->L, -3 ); // outer inst points to nested inst by name
lua_pop(ctx->L,2); // module, outer inst
BS_END_LUA_FUNC(ctx);
}
static void macrodef(BSParserContext* ctx)
{
BS_BEGIN_LUA_FUNC(ctx,0);
nextToken(ctx); // keyword
BSIdentDef id = identdef(ctx,&ctx->module);
lua_createtable(ctx->L,0,0);
const int decl = lua_gettop(ctx->L);
lua_pushinteger(ctx->L,BS_MacroDef);
lua_setfield(ctx->L,decl,"#kind");
lua_pushstring(ctx->L,ctx->label);
lua_setfield(ctx->L,decl,"#source");
addToScope(ctx, &ctx->module, &id, decl );
addLocInfo(ctx,id.loc,decl);
addXref(ctx, id.loc, decl);
addNumRef(ctx,decl);
BSToken t = nextToken(ctx);
if( t.tok == Tok_Lpar )
{
// args
BSToken lpar = t;
t = nextToken(ctx);
int n = 0;
while( t.tok != Tok_Rpar )
{
if( t.tok == Tok_Eof )
error(ctx, lpar.loc.row, lpar.loc.col,"non-terminated argument list" );
if( t.tok == Tok_ident )
{
lua_pushlstring(ctx->L, t.val, t.len);
const int name = lua_gettop(ctx->L);
lua_getfield(ctx->L,decl,lua_tostring(ctx->L,name));
// check for duplicates
if( !lua_isnil(ctx->L,-1) )
error(ctx, t.loc.row, t.loc.col,"duplicate argument name" );
else
lua_pop(ctx->L,1);
lua_pushvalue(ctx->L,name);
lua_pushinteger(ctx->L,++n);
lua_rawset(ctx->L,decl);
lua_pushvalue(ctx->L,name);
lua_rawseti(ctx->L,decl,n);
lua_pop(ctx->L,1); // name
}else if( t.tok == Tok_Comma )
; // ignore
else
error(ctx, t.loc.row, t.loc.col,"expecting an identifier or ')'" );
t = nextToken(ctx);
}
t = nextToken(ctx);
}
if( t.tok != Tok_Lbrace )
error(ctx, t.loc.row, t.loc.col,"expecting '{'" );
BSToken lbrace = t;
lua_pushinteger(ctx->L,lbrace.loc.row);
lua_setfield(ctx->L,decl,"#brow");
lua_pushinteger(ctx->L,lbrace.loc.col);
lua_setfield(ctx->L,decl,"#bcol");
int n = 0;
while(1)
{
// TODO: should check syntax already here?
// i.e. that only { ( subdirectory | declaration | statement ) [';'] } appears
t = bslex_hnext(ctx->lex);
if( t.tok == Tok_Lbrace )
n++;
else if( t.tok == Tok_Rbrace )
{
if( n == 0 )
break;
n--;
}else if( t.tok == Tok_Eof )
error(ctx, lbrace.loc.row, lbrace.loc.col,"non-terminated macro body" );
else if( t.tok == Tok_Invalid )
{
lua_pushnil(ctx->L);
lua_error(ctx->L);
}
}
assert( t.tok == Tok_Rbrace );
lua_pushlstring(ctx->L, lbrace.val, t.val-lbrace.val+1); // include { and } in code fragment
lua_setfield(ctx->L,decl,"#code");
lua_pop(ctx->L,1); // decl
BS_END_LUA_FUNC(ctx);
}
static void goforthis(BSParserContext* ctx, BSScope* scope)
{
BS_BEGIN_LUA_FUNC(ctx,1);
lua_pushvalue(ctx->L,scope->table);
const int curScope = lua_gettop(ctx->L);
lua_getfield(ctx->L, curScope, "#this" );
// stack: def, this or nil
// continue search to surrounding (local) scope (if present)
while( lua_isnil(ctx->L, -1) )
{
lua_pop(ctx->L,1); // remove nil
// stack: old def
lua_getfield(ctx->L, curScope, "#up");
// stack: old def, new def or nil
if( lua_isnil(ctx->L, -1) )
break;
lua_replace(ctx->L,curScope);
// stack: new def
lua_getfield(ctx->L, curScope, "#this");
// stack: new def, this or nil
}
// stack: def, this or nil
lua_replace(ctx->L,curScope);
BS_END_LUA_FUNC(ctx);
}
// returns 0: read-write, 1: read-only, 2: dot to bound
static int resolveInstance(BSParserContext* ctx, BSScope* scope )
// out: resolved container instance + derefed declaration
// NOTE: the order of the declarations is relevant; a decl cannot be dereferenced before it appears in the text
{
BS_BEGIN_LUA_FUNC(ctx,2);
BSToken t = nextToken(ctx);
int ret = 0;
enum { LocalOnly, LocalOuter, Field };
int method;
switch( t.tok )
{
case Tok_Dot:
method = Field;
//lua_getfield(ctx->L, scope->table, "#this" );
goforthis(ctx,scope);
if( lua_isnil(ctx->L,-1) )
error(ctx, t.loc.row, t.loc.col,"designator cannot start with '.' here" );
// from here we have the instance on the stack
ret = 2;
t = nextToken(ctx);
break;
case Tok_Hat:
method = LocalOuter;
lua_pushvalue(ctx->L,ctx->module.table); // use module instead of scope because outer is only available on mod level
lua_getfield(ctx->L, -1, "^");
lua_remove(ctx->L,-2);
// from here we have the nearest outer module definition on the stack, or nil
t = nextToken(ctx);
break;
case Tok_ident:
method = LocalOnly;
lua_getfield(ctx->L,scope->table,"#inst");
// from here we have the module or block instance on the stack
break;
default:
error(ctx, t.loc.row, t.loc.col, "designator must start with a '^', '.' or identifier" );
break;
}
// here we have the first scope on stack from where we resolve the ident
if( t.tok != Tok_ident )
error(ctx, t.loc.row, t.loc.col, "expecting an identifier here" );
// now resolve the first ident of the desig
if( method == LocalOuter )
{
// start with outer module decl on stack
while( !lua_isnil(ctx->L,-1) )
{
lua_pushlstring(ctx->L, t.val, t.len);
lua_rawget(ctx->L,-2);
if( !lua_isnil(ctx->L, -1) )
{
// we have a hit;
// stack: module def, decl
lua_getfield(ctx->L,-1,"#visi");
const int visi = lua_tointeger(ctx->L,-1);
lua_pop(ctx->L,1);
if( visi == BS_Private )
error(ctx, t.loc.row, t.loc.col, "the identifier is not visible from here" );
else
{
// stack: module def, decl
lua_getfield(ctx->L,-2,"#inst");
// stack: module def, decl, inst
lua_replace(ctx->L,-3); // replace the module def by its instance
// result stack: module inst, decl
addXref(ctx, t.loc, -1);
break;
}
}else
{
// continue search to outer module (if present)
lua_pop(ctx->L,1); // remove nil
lua_getfield(ctx->L, -1, "^");
lua_remove(ctx->L,-2);
}
}
}else
{
// stack: module, body or object instance
// method == LocalOnly or Field
lua_getmetatable(ctx->L,-1);
// stack: instance, def
lua_pushlstring(ctx->L, t.val, t.len);
lua_rawget(ctx->L,-2);
lua_remove(ctx->L,-2);
// stack: instance, derefed decl or nil
if( !lua_isnil(ctx->L, -1) )
{
// we have a hit
// stack: container instance, derefed decl
addXref(ctx, t.loc, -1);
}else if( method != Field )
{
// continue search to surrounding (local) scope (if present)
while( lua_isnil(ctx->L, -1) )
{
lua_pop(ctx->L,1); // remove nil
// stack: old instance
lua_getmetatable(ctx->L,-1);
// stack: old instance, old def
lua_getfield(ctx->L, -1, "#up");
// stack: old instance, old def, new def or nil
lua_replace(ctx->L,-2);
// stack: old instance, new def or nil
if( lua_isnil(ctx->L, -1) )
break;
lua_getfield(ctx->L, -1, "#inst");
// stack: old instance, new def, new inst
lua_replace(ctx->L,-3);
// stack: new instance, new def
lua_pushlstring(ctx->L, t.val, t.len);
lua_rawget(ctx->L,-2);
// stack: new instance, new def, derefed decl or nil
lua_remove(ctx->L,-2);
// stack: new instance, derefed decl or nil
}
if( lua_isnil(ctx->L, -1) )
{
// no hit, directly look in builtins (we don't need to prefix them with ^ to desig them)
lua_pop(ctx->L,2); // instance, nil
lua_getfield(ctx->L,ctx->builtins,"#inst");
lua_pushlstring(ctx->L, t.val, t.len);
lua_rawget(ctx->L,ctx->builtins);
if( !lua_isnil(ctx->L, -1) )
{