/
parser-quirk-file.bas
1304 lines (1069 loc) · 29.3 KB
/
parser-quirk-file.bas
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
'' quirk file statements and functions (PRINT, WRITE, OPEN, ...) parsing
''
'' chng: sep/2004 written [v1ctor]
#include once "fb.bi"
#include once "fbint.bi"
#include once "parser.bi"
#include once "rtl.bi"
#include once "ast.bi"
'':::::
'' PrintStmt = (PRINT|'?') ('#' Expression ',')? (USING Expression{str} ';')? (Expression? ';'|"," )*
''
function cPrintStmt _
( _
byval tk as FB_TOKEN _
) as integer
dim as ASTNODE ptr usingexpr, filexpr, filexprcopy, expr
dim as integer expressions, issemicolon, iscomma, istab, isspc, islprint
function = FALSE
'' (PRINT|'?')
select case tk
case FB_TK_PRINT, CHAR_QUESTION
islprint = FALSE
case FB_TK_LPRINT
if( fbLangOptIsSet( FB_LANG_OPT_OPTION ) = FALSE ) then
errReportNotAllowed( FB_LANG_OPT_OPTION )
else
islprint = TRUE
end if
case else
exit function
end select
lexSkipToken( LEXCHECK_POST_SUFFIX )
if( islprint ) then
filexpr = astNewCONSTi( -1 )
else
'' ('#' Expression)?
if( hMatch( CHAR_SHARP ) ) then
hMatchFileNumberExpression( filexpr, FB_DATATYPE_INTEGER )
hMatchCOMMA( )
else
filexpr = astNewCONSTi( 0 )
end if
end if
'' side-effect?
'' (vars may also cause side-effects if modified by printed expressions)
if( astIsCONST( filexpr ) = FALSE ) then
astAdd( astRemSideFx( filexpr ) )
end if
usingexpr = NULL
'' (Expression?|SPC(Expression)|TAB(Expression) ';'|"," )*
expressions = 0
do
'' (USING Expression{str} ';')?
if( hMatch( FB_TK_USING, LEXCHECK_POST_SUFFIX ) ) then
if( usingexpr <> NULL ) then
#if 1 '' remove this to allow multiple USINGs on one line
errReport( FB_ERRMSG_EXPECTEDEOL )
#endif
filexprcopy = astCloneTree( filexpr )
if( rtlPrintUsingEnd( filexprcopy, _
islprint ) = FALSE ) then
exit function
end if
end if
hMatchExpressionEx( usingexpr, FB_DATATYPE_STRING )
if( hMatch( CHAR_SEMICOLON ) = FALSE ) then
errReport( FB_ERRMSG_EXPECTEDSEMICOLON )
end if
if( rtlPrintUsingInit( usingexpr, islprint ) = FALSE ) then
exit function
end if
end if
'' (Expression?|SPC(Expression)|TAB(Expression)
isspc = FALSE
istab = FALSE
if( hMatch( FB_TK_SPC, LEXCHECK_POST_LANG_SUFFIX ) ) then
isspc = TRUE
hMatchLPRNT( )
hMatchExpressionEx( expr, FB_DATATYPE_INTEGER )
hMatchRPRNT( )
elseif( hMatch( FB_TK_TAB, LEXCHECK_POST_LANG_SUFFIX ) ) then
istab = TRUE
hMatchLPRNT( )
hMatchExpressionEx( expr, FB_DATATYPE_INTEGER )
hMatchRPRNT( )
else
expr = cExpression( )
end if
iscomma = FALSE
issemicolon = FALSE
if( hMatch( CHAR_COMMA ) ) then
if( usingexpr <> NULL ) then
'' QB automatically converted them to semi-colons in the editor.
errReport( FB_ERRMSG_EXPECTEDSEMICOLON )
end if
iscomma = TRUE
elseif( hMatch( CHAR_SEMICOLON ) ) then
issemicolon = TRUE
end if
if( (isspc or istab) and (issemicolon = FALSE) ) then
'' only semicolons are supported after SPC()/TAB() (QB editor autocorrected them)
errReport( FB_ERRMSG_EXPECTEDSEMICOLON )
end if
'' handle PRINT w/o expressions
if( (iscomma = FALSE) and _
(issemicolon = FALSE) and _
(expr = NULL) ) then
exit do
end if
if( isspc or istab ) then
filexprcopy = astCloneTree( filexpr )
if( rtlPrintSPCTab( filexprcopy, _
expr, _
istab, _
islprint ) = FALSE ) then
exit function
end if
else
if( usingexpr = NULL /'or expr = NULL'/ ) then
/' (commented check allows multiple consecutive commas/semicolons in USING statements.
QB doesn't support it though, so I'm not sure we should. '/
filexprcopy = astCloneTree( filexpr )
if( rtlPrint( filexprcopy, _
iscomma, _
issemicolon, _
expr, _
islprint ) = FALSE ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
end if
else
filexprcopy = astCloneTree( filexpr )
if( rtlPrintUsing( filexprcopy, _
expr, _
iscomma, _
issemicolon, _
islprint ) = FALSE ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
end if
end if
end if
expressions += 1
loop while( iscomma or issemicolon )
if( usingexpr = NULL ) then
if( expressions = 0 ) then
filexprcopy = astCloneTree( filexpr )
if( rtlPrint( filexprcopy, _
FALSE, _
FALSE, _
NULL, _
islprint ) = FALSE ) then
exit function
end if
end if
else
filexprcopy = astCloneTree( filexpr )
if( rtlPrintUsingEnd( filexprcopy, _
islprint ) = FALSE ) then
exit function
end if
end if
''
astDelTree( filexpr )
function = TRUE
end function
'':::::
'' WriteStmt = WRITE ('#' Expression)? (Expression? "," )*
''
function cWriteStmt() as integer
dim as ASTNODE ptr filexpr, filexprcopy, expr
dim as integer expressions, iscomma
function = FALSE
'' WRITE
lexSkipToken( LEXCHECK_POST_SUFFIX )
'' ('#' Expression)?
if( hMatch( CHAR_SHARP ) ) then
hMatchFileNumberExpression( filexpr, FB_DATATYPE_INTEGER )
hMatchCOMMA( )
else
filexpr = astNewCONSTi( 0 )
end if
'' side-effect?
'' (vars may also cause side-effects if modified by printed expressions)
if( astIsCONST( filexpr ) = FALSE ) then
astAdd( astRemSideFx( filexpr ) )
end if
'' (Expression? "," )*
expressions = 0
do
expr = cExpression( )
if( expr = NULL ) then
expr = NULL
end if
iscomma = FALSE
if( hMatch( CHAR_COMMA ) ) then
iscomma = TRUE
end if
filexprcopy = astCloneTree( filexpr )
'' handle WRITE w/o expressions
if( (iscomma = FALSE) and (expr = NULL) ) then
if( expressions = 0 ) then
rtlWrite( filexprcopy, FALSE, NULL )
end if
exit do
end if
if( rtlWrite( filexprcopy, iscomma, expr ) = FALSE ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
end if
expressions += 1
loop while( iscomma )
''
astDelTree( filexpr )
function = TRUE
end function
'':::::
'' LineInputStmt = LINE INPUT ';'? ('#' Expression| Expression{str}?) (','|';')? Variable? .
''
function cLineInputStmt _
( _
_
) as integer
dim as ASTNODE ptr filestrexpr = NULL, dstexpr = NULL, maxlenexpr = NULL
dim as integer isfile = FALSE, addnewline = FALSE, issep = FALSE, addquestion = FALSE
dim as integer dtype = any
function = FALSE
'' INPUT
if( lexGetLookAhead( 1 ) <> FB_TK_INPUT ) then
exit function
end if
'' LINE
lexSkipToken( LEXCHECK_POST_SUFFIX )
'' INPUT
lexSkipToken( LEXCHECK_POST_SUFFIX )
'' ';'?
addnewline = (hMatch( CHAR_SEMICOLON ) = FALSE)
'' '#'?
if( hMatch( CHAR_SHARP ) ) then
isfile = TRUE
'' Expression
hMatchFileNumberExpression( filestrexpr, FB_DATATYPE_INTEGER )
else
'' could be a prompt or variable, we don't know until we get the next delimiter
dstexpr = cExpression( )
end if
'' ','|';'?
if( hMatch( CHAR_COMMA ) = FALSE ) then
if( hMatch( CHAR_SEMICOLON ) = FALSE ) then
'' only show an error if we didn't get a dstexpr, which means we may not
'' expect a delimiter here if we read in a variable below
if( dstexpr = NULL ) then
'' fbc allows both comma and semicolon following the file number
'' but should probably only be comma
if( isfile ) then
errReport( FB_ERRMSG_EXPECTEDCOMMA )
else
errReport( FB_ERRMSG_EXPECTEDCOMMAORSEMICOLON )
end if
end if
else
addquestion = TRUE
issep = TRUE
end if
else
issep = TRUE
end if
'' if we got a separator and dstexpr, then it must be a prompt
if( issep = TRUE ) then
if( dstexpr ) then
assert( isfile = FALSE )
filestrexpr = dstexpr
dstexpr = NULL
end if
end if
'' Variable? - but only if we don't have it yet
if( dstexpr = NULL ) then
dstexpr = cVarOrDeref( )
if( dstexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER )
hSkipStmt( )
return TRUE
end if
end if
'' dest can't be a top-level const
if( typeIsConst( astGetFullType( dstexpr ) ) ) then
errReport( FB_ERRMSG_CONSTANTCANTBECHANGED )
elseif( astGetStrLitSymbol( dstexpr ) ) then
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER, TRUE )
end if
'' string variable length unknown? allow specifying a max length
if( rtlCalcStrLen( dstexpr, astGetDataType( dstexpr ) ) = 0 ) then
'' ',' max_chars
if( hMatch( CHAR_COMMA ) ) then
maxlenexpr = cExpression( )
if( maxlenexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDEXPRESSION )
'' error recovery: none rtlFileLineInput[Wstr] will handle it
end if
else
errReport( FB_ERRMSG_EXPECTEDCOMMA )
end if
end if
select case astGetDataType( dstexpr )
case FB_DATATYPE_STRING, FB_DATATYPE_FIXSTR, FB_DATATYPE_CHAR
function = rtlFileLineInput( isfile, filestrexpr, dstexpr, maxlenexpr, addquestion, addnewline )
case FB_DATATYPE_WCHAR
function = rtlFileLineInputWstr( isfile, filestrexpr, dstexpr, maxlenexpr, addquestion, addnewline )
'' not a string?
case else
astDelTree( dstexpr )
errReport( FB_ERRMSG_INVALIDDATATYPES )
return TRUE
end select
end function
'':::::
'' InputStmt = INPUT ';'? (('#' Expression| STRING_LIT) (','|';'))? Variable (',' Variable)*
''
function cInputStmt _
( _
_
) as integer
dim as ASTNODE ptr filestrexpr = NULL, dstexpr = NULL
dim as integer islast = FALSE, isfile = FALSE, addnewline = FALSE, addquestion = FALSE
function = FALSE
'' INPUT
lexSkipToken( LEXCHECK_POST_SUFFIX )
'' ';'?
addnewline = (hMatch( CHAR_SEMICOLON ) = FALSE)
'' '#'?
if( hMatch( CHAR_SHARP ) ) then
isfile = TRUE
'' Expression
hMatchFileNumberExpression( filestrexpr, FB_DATATYPE_INTEGER )
else
'' STRING_LIT?
if( lexGetClass( ) = FB_TKCLASS_STRLITERAL ) then
filestrexpr = astNewVAR( symbAllocStrConst( *lexGetText( ), lexGetTextLen( ) ) )
lexSkipToken( )
else
filestrexpr = NULL
addquestion = TRUE
end if
end if
'' ','|';'?
if( (isfile) or (filestrexpr <> NULL) ) then
if( hMatch( CHAR_COMMA ) = FALSE ) then
if( hMatch( CHAR_SEMICOLON ) = FALSE ) then
'' fbc allows both comma and semicolon following the file number
'' but should probably only be comma
if( isfile ) then
errReport( FB_ERRMSG_EXPECTEDCOMMA )
else
errReport( FB_ERRMSG_EXPECTEDCOMMAORSEMICOLON )
end if
else
addquestion = TRUE
end if
end if
end if
''
if( rtlFileInput( isfile, filestrexpr, addquestion, addnewline ) = FALSE ) then
exit function
end if
'' Variable (',' Variable)*
do
dstexpr = cVarOrDeref( )
if( dstexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER )
dstexpr = NULL
hSkipUntil( CHAR_COMMA )
end if
if( dstexpr <> NULL ) then
'' dest can't be a top-level const
if( typeIsConst( astGetFullType( dstexpr ) ) ) then
errReport( FB_ERRMSG_CONSTANTCANTBECHANGED )
elseif( astGetStrLitSymbol( dstexpr ) ) then
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER, TRUE )
end if
end if
if( hMatch( CHAR_COMMA ) ) then
islast = FALSE
else
islast = TRUE
end if
if( dstexpr <> NULL ) then
if( rtlFileInputGet( dstexpr ) = FALSE ) then
exit function
end if
end if
loop until( islast )
function = TRUE
end function
'':::::
private function hFileClose _
( _
byval isfunc as integer _
) as ASTNODE ptr
dim as integer cnt
dim as ASTNODE ptr filenum, proc
function = NULL
'' CLOSE
lexSkipToken( LEXCHECK_POST_SUFFIX )
if( isfunc ) then
'' '('
hMatchLPRNT( )
end if
cnt = 0
do
hMatch( CHAR_SHARP )
filenum = cExpression( )
if( filenum = NULL ) then
if( cnt = 0 ) then
'' pass NULL to rtlFileClose to get close-all function
else
errReport( FB_ERRMSG_EXPECTEDFILENUMBEREXPRESSION )
filenum = astNewCONSTi( 0 )
end if
end if
proc = rtlFileClose( filenum, isfunc )
if( proc = NULL ) then
exit function
end if
if( isfunc ) then
exit do
end if
cnt += 1
loop while( hMatch( CHAR_COMMA ) )
if( isfunc ) then
'' ')'
hMatchRPRNT( )
end if
function = proc
end function
'':::::
'' Put = PUT '#' Expression ',' Expression? ',' Expression{str|int|float|array} (',' Expression)?
''
private function hFilePut _
( _
byval isfunc as integer _
) as ASTNODE ptr
dim as ASTNODE ptr fileexpr, posexpr, srcexpr, elmexpr
dim as integer isarray
dim as FBSYMBOL ptr s
function = NULL
'' '#'?
if( lexGetToken( ) = CHAR_SHARP ) then
lexSkipToken( )
end if
hMatchFileNumberExpression( fileexpr, FB_DATATYPE_INTEGER )
'' ',' offset
hMatchCOMMA( )
posexpr = cExpression( )
if( posexpr = NULL ) then
posexpr = NULL
end if
'' ',' source
hMatchCOMMA( )
hMatchExpressionEx( srcexpr, FB_DATATYPE_INTEGER )
'' don't allow literal values, due the way "byref as
'' any" args work (ie, the VB-way: literals are passed by value)
if( astIsCONST( srcexpr ) or astIsOFFSET( srcexpr ) ) then
astDelTree( srcexpr )
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER, TRUE )
if( isfunc ) then
hSkipUntil( CHAR_RPRNT )
else
hSkipStmt( )
end if
return astNewCONSTi( 0 )
end if
isarray = FALSE
if( lexGetToken( ) = CHAR_LPRNT ) then
if( lexGetLookAhead( 1 ) = CHAR_RPRNT ) then
s = astGetSymbol( srcexpr )
if( s <> NULL ) then
isarray = symbIsArray( s )
if( isarray ) then
'' don't allow var-len strings
if( symbGetType( s ) = FB_DATATYPE_STRING ) then
astDelTree( srcexpr )
errReport( FB_ERRMSG_INVALIDDATATYPES, TRUE )
if( isfunc ) then
hSkipUntil( CHAR_RPRNT )
else
hSkipStmt( )
end if
return astNewCONSTi( 0 )
end if
lexSkipToken( )
lexSkipToken( )
end if
end if
end if
end if
'' (',' elements)?
if( hMatch( CHAR_COMMA ) ) then
if( isarray ) then
errReport( FB_ERRMSG_ELEMENTSMUSTBEEMPTY )
'' error recovery: skip elements
elmexpr = cExpression( )
if( elmexpr <> NULL ) then
astDelTree( elmexpr )
elmexpr = NULL
end if
else
'' don't allow elements if source is string type
select case astGetDataType( srcexpr )
case FB_DATATYPE_STRING, FB_DATATYPE_FIXSTR
errReport( FB_ERRMSG_ELEMENTSMUSTBEEMPTY )
'' error recovery: skip elements
elmexpr = cExpression( )
if( elmexpr <> NULL ) then
astDelTree( elmexpr )
elmexpr = NULL
end if
case else
elmexpr = cExpression( )
if( elmexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDEXPRESSION )
end if
end select
end if
'' elems has to be an integer or able to be converted
if( elmexpr ) then
if( typeIsPtr( astGetDatatype( elmexpr ) ) ) then
errReportWarn( FB_WARNINGMSG_PASSINGPTRTOSCALAR )
end if
if( astGetDatatype( elmexpr ) <> FB_DATATYPE_INTEGER ) then
elmexpr = astNewCONV( FB_DATATYPE_INTEGER, NULL, elmexpr )
if( elmexpr = NULL ) then
errReport( FB_ERRMSG_SYNTAXERROR, TRUE )
end if
end if
end if
else
elmexpr = NULL
end if
if( isarray = FALSE ) then
function = rtlFilePut( fileexpr, posexpr, srcexpr, elmexpr, isfunc )
else
function = rtlFilePutArray( fileexpr, posexpr, srcexpr, isfunc )
end if
end function
'':::::
'' Get = GET '#' Expression ',' Expression? ',' Variable{str|int|float|array}
'' (',' Expression)? (',' variable)?
''
private function hFileGet _
( _
byval isfunc as integer _
) as ASTNODE ptr
dim as ASTNODE ptr fileexpr, posexpr, dstexpr, elmexpr, iobexpr
dim as integer isarray
dim as FBSYMBOL ptr s
function = NULL
'' '#'?
if( lexGetToken( ) = CHAR_SHARP ) then
lexSkipToken( )
end if
hMatchFileNumberExpression( fileexpr, FB_DATATYPE_INTEGER )
'' ',' offset
hMatchCOMMA( )
posexpr = cExpression( )
'' ',' destine
hMatchCOMMA( )
dstexpr = cVarOrDeref( )
if( dstexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER )
if( isfunc ) then
hSkipUntil( CHAR_RPRNT )
else
hSkipStmt( )
end if
return astNewCONSTi( 0 )
end if
isarray = FALSE
if( astIsNIDXARRAY( dstexpr ) ) then
s = astGetSymbol( dstexpr )
isarray = TRUE
'' don't allow var-len strings
if( symbGetType( s ) = FB_DATATYPE_STRING ) then
errReport( FB_ERRMSG_INVALIDDATATYPES, TRUE )
if( isfunc ) then
hSkipUntil( CHAR_RPRNT )
else
hSkipStmt( )
end if
return astNewCONSTi( 0 )
end if
end if
'' (',' elements)?
if( hMatch( CHAR_COMMA ) ) then
elmexpr = cExpression( )
if( isarray ) then
'' elems must be NULL
if( elmexpr <> NULL ) then
errReport( FB_ERRMSG_ELEMENTSMUSTBEEMPTY )
astDelTree( elmexpr )
elmexpr = NULL
end if
else
if( elmexpr ) then
'' don't allow elements if destine is string type
select case astGetDataType( dstexpr )
case FB_DATATYPE_STRING, FB_DATATYPE_FIXSTR
errReport( FB_ERRMSG_ELEMENTSMUSTBEEMPTY )
'' error recovery: skip elements
astDelTree( elmexpr )
elmexpr = NULL
case else
'' elems has to be an integer or able to be converted
if( typeIsPtr( astGetDatatype( elmexpr ) ) ) then
errReportWarn( FB_WARNINGMSG_PASSINGPTRTOSCALAR )
end if
if( astGetDatatype( elmexpr ) <> FB_DATATYPE_INTEGER ) then
elmexpr = astNewCONV( FB_DATATYPE_INTEGER, NULL, elmexpr )
if( elmexpr = NULL ) then
errReport( FB_ERRMSG_SYNTAXERROR, TRUE )
end if
end if
end select
end if
end if
else
elmexpr = NULL
end if
'' (',' iobytes)?
if( hMatch( CHAR_COMMA ) ) then
iobexpr = cVarOrDeref( )
if( iobexpr <> NULL ) then
s = astGetSymbol( iobexpr )
if( s <> NULL ) then
'' It's for a BYREF AS INTEGER parameter, so it
'' must be an integer type of the same size to
'' be allowed by astNewARG().
'' (Note: normally astNewARG() would do these
'' checks already, but this is a quirk RTL function,
'' and astNewARG()'s error reports may be confusing
'' if the RTL function parameters and the quirk syntax
'' arguments don't match up)
var dtype = symbGetType( s )
if( (typeGetSize( dtype ) <> env.pointersize) or _
(typeGetClass( dtype ) <> FB_DATACLASS_INTEGER) or _
typeIsPtr( dtype ) ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
end if
end if
else
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER )
end if
else
iobexpr = NULL
end if
'' dest can't be a top-level const
if( typeIsConst( astGetFullType( dstexpr ) ) ) then
errReport( FB_ERRMSG_CONSTANTCANTBECHANGED )
end if
'' iobytes can't be a top-level const
if( iobexpr ) then
if( typeIsConst( astGetFullType( iobexpr ) ) ) then
errReport( FB_ERRMSG_CONSTANTCANTBECHANGED )
end if
end if
if( isarray = FALSE ) then
function = rtlFileGet( fileexpr, posexpr, dstexpr, elmexpr, iobexpr, isfunc )
else
function = rtlFileGetArray( fileexpr, posexpr, dstexpr, iobexpr, isfunc )
end if
end function
'':::::
'' FileOpen = OPEN Expression{str}
'' (FOR Expression)? (ENCODING Expression)?
'' (ACCESS Expression)?
'' (SHARED|LOCK (READ|WRITE|READ WRITE))?
'' AS '#'? Expression
'' (LEN '=' Expression)?
''
'' OPEN ("O"|"I"|"B"|"R"|"A")',' '#'? Expression{int}',' Expression{str} (',' Expression{int})?
''
private function hFileOpen _
( _
byval isfunc as integer _
) as ASTNODE ptr
dim as ASTNODE ptr filenum, filename, fmode, faccess, flock, flen, fencoding
dim as integer short_form
dim as integer file_mode, access_mode, lock_mode, record_len
dim as FBOPENKIND open_kind
function = NULL
open_kind = FB_FILE_TYPE_FILE
short_form = FALSE
'' if it's a qb-style open, we only get an identifier, or a literal
if( fbLangIsSet( FB_LANG_QB ) = FALSE ) then
'' special devices
select case ucase( *lexGetText( ) )
case "CONS"
'' not a symbol?
if( lexGetSymChain( ) = NULL ) then
lexSkipToken( LEXCHECK_POST_SUFFIX )
open_kind = FB_FILE_TYPE_CONS
end if
case "ERR"
lexSkipToken( LEXCHECK_POST_SUFFIX )
open_kind = FB_FILE_TYPE_ERR
case "PIPE"
'' not a symbol?
if( lexGetSymChain( ) = NULL ) then
lexSkipToken( LEXCHECK_POST_SUFFIX )
open_kind = FB_FILE_TYPE_PIPE
end if
case "SCRN"
'' not a symbol?
if( lexGetSymChain( ) = NULL ) then
lexSkipToken( LEXCHECK_POST_SUFFIX )
open_kind = FB_FILE_TYPE_SCRN
end if
case "LPT"
'' not a symbol?
if( lexGetSymChain( ) = NULL ) then
lexSkipToken( LEXCHECK_POST_SUFFIX )
open_kind = FB_FILE_TYPE_LPT
end if
case "COM"
'' not a symbol?
if( lexGetSymChain( ) = NULL ) then
lexSkipToken( LEXCHECK_POST_SUFFIX )
open_kind = FB_FILE_TYPE_COM
end if
end select
end if
if( isfunc ) then
'' '('
hMatchLPRNT( )
end if
'' if it's a qb-style open, we only get an identifier, or a literal
if( fbLangIsSet( FB_LANG_QB ) ) then
open_kind = FB_FILE_TYPE_QB
end if
select case as const open_kind
case FB_FILE_TYPE_FILE, FB_FILE_TYPE_PIPE, FB_FILE_TYPE_LPT, _
FB_FILE_TYPE_COM, FB_FILE_TYPE_QB
'' a filename is only valid for some file types
hMatchExpressionEx( filename, FB_DATATYPE_STRING )
if( isfunc ) then
'' ','?
hMatch( CHAR_COMMA )
end if
' only test for short OPEN form when using the "normal" OPEN
select case open_kind
case FB_FILE_TYPE_FILE, FB_FILE_TYPE_QB
if( isfunc ) then
select case lexGetToken( )
case FB_TK_FOR, FB_TK_ACCESS, FB_TK_AS
case else
short_form = TRUE
end select
else
if( hMatch( CHAR_COMMA ) ) then
'' ',' -> indicates the short form
short_form = TRUE
end if
end if
end select
case else
' no file name provided for this kind of OPEN statmenets
filename = astNewCONSTstr( "" )
end select
if( short_form ) then
'' file mode ("I"|"O"|"A"|"B"|"R")
fmode = filename
filename = NULL
'' '#'? file number
hMatch( CHAR_SHARP )
hMatchFileNumberExpression( filenum, FB_DATATYPE_INTEGER )
hMatchCOMMA( )
'' file name
hMatchExpressionEx( filename, FB_DATATYPE_STRING )
'' record length
if( hMatch( CHAR_COMMA ) ) then
if( lexGetToken( ) <> CHAR_COMMA ) then
hMatchExpressionEx( flen, FB_DATATYPE_INTEGER )
end if
'' access mode
if( hMatch( CHAR_COMMA ) ) then
if( lexGetToken( ) <> CHAR_COMMA ) then
hMatchExpressionEx( faccess, FB_DATATYPE_STRING )
end if
'' lock mode
if( hMatch( CHAR_COMMA ) ) then
hMatchExpressionEx( flock, FB_DATATYPE_STRING )
end if
end if
end if
if( flen = NULL ) then
flen = astNewCONSTi( 0 )
end if
if( faccess = NULL ) then
faccess = astNewCONSTstr( "" )
end if
if( flock = NULL ) then
flock = astNewCONSTstr( "" )
end if
if( isfunc ) then
'' ')'
hMatchRPRNT( )
end if
return rtlFileOpenShort( filename, fmode, faccess, flock, _
filenum, flen, isfunc )
end if
'' long form..
'' (FOR (INPUT|OUTPUT|BINARY|RANDOM|APPEND))?
if( hMatch( FB_TK_FOR, LEXCHECK_POST_SUFFIX ) ) then
select case ucase( *lexGetText( ) )
case "INPUT"
file_mode = FB_FILE_MODE_INPUT
case "OUTPUT"
file_mode = FB_FILE_MODE_OUTPUT
case "BINARY"
file_mode = FB_FILE_MODE_BINARY
case "RANDOM"
file_mode = FB_FILE_MODE_RANDOM
case "APPEND"
file_mode = FB_FILE_MODE_APPEND
case else
exit function
end select
lexSkipToken( LEXCHECK_POST_SUFFIX )
else
file_mode = FB_FILE_MODE_RANDOM
end if
fmode = astNewCONSTi( file_mode )
if( isfunc ) then
'' ','?
hMatch( CHAR_COMMA )
end if
fencoding = NULL