forked from ZDoom/acc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
4845 lines (4503 loc) · 107 KB
/
parse.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
//**************************************************************************
//**
//** parse.c
//**
//**************************************************************************
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <assert.h>
#include "common.h"
#include "parse.h"
#include "symbol.h"
#include "pcode.h"
#include "token.h"
#include "error.h"
#include "misc.h"
#include "strlist.h"
// MACROS ------------------------------------------------------------------
#define MAX_STATEMENT_DEPTH 128
#define MAX_BREAK 128
#define MAX_CONTINUE 128
#define MAX_CASE 128
#define EXPR_STACK_DEPTH 64
// TYPES -------------------------------------------------------------------
typedef enum
{
STMT_SCRIPT,
STMT_IF,
STMT_ELSE,
STMT_DO,
STMT_WHILEUNTIL,
STMT_SWITCH,
STMT_FOR
} statement_t;
typedef struct
{
int level;
int addressPtr;
} breakInfo_t;
typedef struct
{
int level;
int addressPtr;
} continueInfo_t;
typedef struct
{
int level;
int value;
boolean isDefault;
int address;
} caseInfo_t;
typedef struct prefunc_s
{
struct prefunc_s *next;
symbolNode_t *sym;
int address;
int argcount;
int line;
char *source;
} prefunc_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void CountScript(int type);
static void Outside(void);
static void OuterScript(void);
static void OuterFunction(void);
static void OuterMapVar(boolean local);
static void OuterWorldVar(boolean isGlobal);
static void OuterSpecialDef(void);
static void OuterDefine(boolean force);
static void OuterInclude(void);
static void OuterImport(void);
static boolean ProcessStatement(statement_t owner);
static void LeadingCompoundStatement(statement_t owner);
static void LeadingVarDeclare(void);
static void LeadingLineSpecial(boolean executewait);
static void LeadingFunction(boolean executewait);
static void LeadingIdentifier(void);
static void BuildPrintString(void);
static void ActionOnCharRange(boolean write);
static void LeadingStrcpy(void);
static void LeadingPrint(void);
static void LeadingHudMessage(void);
static void LeadingMorphActor(void);
static void LeadingVarAssign(symbolNode_t *sym);
static pcd_t GetAssignPCD(tokenType_t token, symbolType_t symbol);
static void LeadingInternFunc(symbolNode_t *sym);
static void LeadingScriptFunc(symbolNode_t *sym);
static void LeadingSuspend(void);
static void LeadingTerminate(void);
static void LeadingRestart(void);
static void LeadingReturn(void);
static void LeadingIf(void);
static void LeadingFor(void);
static void LeadingWhileUntil(void);
static void LeadingDo(void);
static void LeadingSwitch(void);
static void LeadingCase(void);
static void LeadingDefault(void);
static void LeadingBreak(void);
static void LeadingContinue(void);
static void LeadingCreateTranslation(void);
static void LeadingIncDec(int token);
static void PushCase(int value, boolean isDefault);
static caseInfo_t *GetCaseInfo(void);
static int CaseInfoCmp(const void *a, const void *b);
static boolean DefaultInCurrent(void);
static void PushBreak(void);
static void WriteBreaks(void);
static boolean BreakAncestor(void);
static void PushContinue(void);
static void WriteContinues(int address);
static boolean ContinueAncestor(void);
static void ProcessInternFunc(symbolNode_t *sym);
static void ProcessScriptFunc(symbolNode_t *sym, boolean discardReturn);
static void EvalExpression(void);
static void ExprLevX(int level);
static void ExprLevA(void);
static void ExprFactor(void);
static void ExprTernary(void);
static void ConstExprFactor(void);
static void SendExprCommand(pcd_t pcd);
static void PushExStk(int value);
static int PopExStk(void);
static pcd_t TokenToPCD(tokenType_t token);
static pcd_t GetPushVarPCD(symbolType_t symType);
static pcd_t GetIncDecPCD(tokenType_t token, symbolType_t symbol);
static int EvalConstExpression(void);
static void ParseArrayDims(int *size, int *ndim, int dims[MAX_ARRAY_DIMS]);
static void SymToArray(int symtype, symbolNode_t *sym, int index, int ndim, int size, int dims[MAX_ARRAY_DIMS]);
static void ParseArrayIndices(symbolNode_t *sym, int requiredIndices);
static void InitializeArray(symbolNode_t *sym, int dims[MAX_ARRAY_DIMS], int size);
static void InitializeScriptArray(symbolNode_t *sym, int dims[MAX_ARRAY_DIMS]);
static symbolNode_t *DemandSymbol(char *name);
static symbolNode_t *SpeculateSymbol(char *name, boolean hasReturn);
static symbolNode_t *SpeculateFunction(const char *name, boolean hasReturn);
static void UnspeculateFunction(symbolNode_t *sym);
static void AddScriptFuncRef(symbolNode_t *sym, int address, int argcount);
static void CheckForUndefinedFunctions(void);
static void SkipBraceBlock(int depth);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
int pa_ScriptCount;
struct ScriptTypes *pa_TypedScriptCounts;
int pa_MapVarCount;
int pa_WorldVarCount;
int pa_GlobalVarCount;
int pa_WorldArrayCount;
int pa_GlobalArrayCount;
enum ImportModes ImportMode = IMPORT_None;
boolean ExporterFlagged;
boolean pa_ConstExprIsString;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static int ScriptVarCount;
static int ScriptArrayCount;
static int ScriptArraySize[MAX_SCRIPT_ARRAYS];
static statement_t StatementHistory[MAX_STATEMENT_DEPTH];
static int StatementIndex;
static breakInfo_t BreakInfo[MAX_BREAK];
static int BreakIndex;
static continueInfo_t ContinueInfo[MAX_CONTINUE];
static int ContinueIndex;
static caseInfo_t CaseInfo[MAX_CASE];
static int CaseIndex;
static int StatementLevel;
static int ExprStack[EXPR_STACK_DEPTH];
static int ExprStackIndex;
static boolean ConstantExpression;
static symbolNode_t *InsideFunction;
static prefunc_t *FillinFunctions;
static prefunc_t **FillinFunctionsLatest = &FillinFunctions;
static boolean ArrayHasStrings;
static int AdjustStmtLevel[] =
{
0, // STMT_SCRIPT
0, // STMT_IF
0, // STMT_ELSE
1, // STMT_DO
1, // STMT_WHILEUNTIL
1, // STMT_SWITCH
1 // STMT_FOR
};
static boolean IsBreakRoot[] =
{
NO, // STMT_SCRIPT
NO, // STMT_IF
NO, // STMT_ELSE
YES, // STMT_DO
YES, // STMT_WHILEUNTIL
YES, // STMT_SWITCH
YES // STMT_FOR
};
static boolean IsContinueRoot[] =
{
NO, // STMT_SCRIPT
NO, // STMT_IF
NO, // STMT_ELSE
YES, // STMT_DO
YES, // STMT_WHILEUNTIL
NO, // STMT_SWITCH
YES // STMT_FOR
};
static tokenType_t LevAOps[] =
{
TK_TERNARY,
TK_NONE
};
static tokenType_t LevBOps[] =
{
TK_ORLOGICAL,
TK_NONE
};
static tokenType_t LevCOps[] =
{
TK_ANDLOGICAL,
TK_NONE
};
static tokenType_t LevDOps[] =
{
TK_ORBITWISE,
TK_NONE
};
static tokenType_t LevEOps[] =
{
TK_EORBITWISE,
TK_NONE
};
static tokenType_t LevFOps[] =
{
TK_ANDBITWISE,
TK_NONE
};
static tokenType_t LevGOps[] =
{
TK_EQ,
TK_NE,
TK_NONE
};
static tokenType_t LevHOps[] =
{
TK_LT,
TK_LE,
TK_GT,
TK_GE,
TK_NONE
};
static tokenType_t LevIOps[] =
{
TK_LSHIFT,
TK_RSHIFT,
TK_NONE
};
static tokenType_t LevJOps[] =
{
TK_PLUS,
TK_MINUS,
TK_NONE
};
static tokenType_t LevKOps[] =
{
TK_ASTERISK,
TK_SLASH,
TK_PERCENT,
TK_NONE
};
static tokenType_t *OpsList[] =
{
LevAOps,
LevBOps,
LevCOps,
LevDOps,
LevEOps,
LevFOps,
LevGOps,
LevHOps,
LevIOps,
LevJOps,
LevKOps,
NULL
};
static tokenType_t AssignOps[] =
{
TK_ASSIGN,
TK_ADDASSIGN,
TK_SUBASSIGN,
TK_MULASSIGN,
TK_DIVASSIGN,
TK_MODASSIGN,
TK_ANDASSIGN,
TK_EORASSIGN,
TK_ORASSIGN,
TK_LSASSIGN,
TK_RSASSIGN,
TK_NONE
};
static struct ScriptTypes ScriptCounts[] =
{
{ "closed", 0, 0 },
{ "open", OPEN_SCRIPTS_BASE, 0 },
{ "respawn", RESPAWN_SCRIPTS_BASE, 0 },
{ "death", DEATH_SCRIPTS_BASE, 0 },
{ "enter", ENTER_SCRIPTS_BASE, 0 },
{ "pickup", PICKUP_SCRIPTS_BASE, 0 },
{ "bluereturn", BLUE_RETURN_SCRIPTS_BASE, 0 },
{ "redreturn", RED_RETURN_SCRIPTS_BASE, 0 },
{ "whitereturn", WHITE_RETURN_SCRIPTS_BASE, 0 },
{ "lightning", LIGHTNING_SCRIPTS_BASE, 0 },
{ "disconnect", DISCONNECT_SCRIPTS_BASE, 0 },
{ "unloading", UNLOADING_SCRIPTS_BASE, 0 },
{ "return", RETURN_SCRIPTS_BASE, 0 },
{ "event", EVENT_SCRIPTS_BASE, 0 },
{ "kill", KILL_SCRIPTS_BASE, 0 },
{ "reopen", REOPEN_SCRIPTS_BASE, 0 },
{ NULL, -1, 0 }
};
// CODE --------------------------------------------------------------------
//==========================================================================
//
// PA_Parse
//
//==========================================================================
void PA_Parse(void)
{
int i;
pa_ScriptCount = 0;
pa_TypedScriptCounts = ScriptCounts;
for (i = 0; ScriptCounts[i].TypeName != NULL; i++)
{
ScriptCounts[i].TypeCount = 0;
}
pa_MapVarCount = 0;
pa_WorldVarCount = 0;
pa_GlobalVarCount = 0;
pa_WorldArrayCount = 0;
pa_GlobalArrayCount = 0;
TK_NextToken();
Outside();
CheckForUndefinedFunctions();
ERR_Finish();
}
//==========================================================================
//
// CountScript
//
//==========================================================================
static void CountScript(int type)
{
int i;
for (i = 0; ScriptCounts[i].TypeName != NULL; i++)
{
if (ScriptCounts[i].TypeBase == type)
{
if (type != 0)
{
MS_Message(MSG_DEBUG, "Script type: %s\n",
ScriptCounts[i].TypeName);
}
ScriptCounts[i].TypeCount++;
return;
}
}
return;
}
//==========================================================================
//
// Outside
//
//==========================================================================
static void Outside(void)
{
boolean done;
int outertokencount;
done = NO;
outertokencount = 0;
while(done == NO)
{
outertokencount++;
switch(tk_Token)
{
case TK_EOF:
done = YES;
break;
case TK_SCRIPT:
OuterScript();
break;
case TK_FUNCTION:
OuterFunction();
break;
case TK_INT:
case TK_STR:
case TK_BOOL:
OuterMapVar(NO);
break;
case TK_WORLD:
OuterWorldVar(NO);
break;
case TK_GLOBAL:
OuterWorldVar(YES);
break;
case TK_SPECIAL:
OuterSpecialDef();
break;
case TK_NUMBERSIGN:
TK_NextToken();
switch(tk_Token)
{
case TK_DEFINE:
OuterDefine(NO);
break;
case TK_LIBDEFINE:
OuterDefine(YES);
break;
case TK_INCLUDE:
OuterInclude();
break;
case TK_NOCOMPACT:
if(ImportMode != IMPORT_Importing)
{
if(pc_Address != 8)
{
ERR_Error(ERR_NOCOMPACT_NOT_HERE, YES);
}
MS_Message(MSG_DEBUG, "Forcing NoShrink\n");
pc_NoShrink = TRUE;
}
TK_NextToken();
break;
case TK_WADAUTHOR:
if(ImportMode != IMPORT_Importing)
{
MS_Message(MSG_DEBUG, "Will write WadAuthor-compatible object\n");
MS_Message(MSG_NORMAL, "You don't need to use #wadauthor anymore.\n");
pc_WadAuthor = TRUE;
}
TK_NextToken();
break;
case TK_NOWADAUTHOR:
if(ImportMode != IMPORT_Importing)
{
MS_Message(MSG_DEBUG, "Will write WadAuthor-incompatible object\n");
pc_WadAuthor = FALSE;
}
TK_NextToken();
break;
case TK_ENCRYPTSTRINGS:
if(ImportMode != IMPORT_Importing)
{
MS_Message(MSG_DEBUG, "Strings will be encrypted\n");
pc_EncryptStrings = TRUE;
if(pc_EnforceHexen)
{
ERR_Error(ERR_HEXEN_COMPAT, YES);
}
}
TK_NextToken();
break;
case TK_IMPORT:
OuterImport();
outertokencount = 0;
break;
case TK_LIBRARY:
if (outertokencount != 1)
{
ERR_Error(ERR_LIBRARY_NOT_FIRST, YES);
}
TK_NextTokenMustBe(TK_STRING, ERR_STRING_LIT_NOT_FOUND);
if(ImportMode == IMPORT_None)
{
MS_Message(MSG_DEBUG, "Allocations modified for exporting\n");
ImportMode = IMPORT_Exporting;
if(pc_EnforceHexen)
{
ERR_Error(ERR_HEXEN_COMPAT, YES);
}
}
else if(ImportMode == IMPORT_Importing)
{
PC_AddImport(tk_String);
ExporterFlagged = YES;
}
TK_NextToken();
break;
case TK_REGION: // [mxd]
case TK_ENDREGION:
outertokencount--; // #region markers should not count as "real" tokens
TK_SkipLine();
break;
default:
ERR_Error(ERR_INVALID_DIRECTIVE, YES);
TK_SkipLine();
break;
}
break;
default:
ERR_Exit(ERR_INVALID_DECLARATOR, YES, NULL);
break;
}
}
}
//==========================================================================
//
// OuterScript
//
//==========================================================================
static void OuterScript(void)
{
int scriptNumber;
symbolNode_t *sym;
int scriptType, scriptFlags;
MS_Message(MSG_DEBUG, "---- OuterScript ----\n");
BreakIndex = 0;
CaseIndex = 0;
StatementLevel = 0;
ScriptVarCount = 0;
ScriptArrayCount = 0;
SY_FreeLocals();
TK_NextToken();
if(ImportMode == IMPORT_Importing)
{
// When importing, the script number is not recorded, because
// it might be a #define that is not included by the main .acs
// file, so processing it would generate a syntax error.
SkipBraceBlock(0);
TK_NextToken();
return;
}
// [RH] If you want to use script 0, it must be written as <<0>>.
// This is to avoid using it accidentally, since ZDoom uses script
// 0 to implement many of the Strife-specific line specials.
if(tk_Token == TK_LSHIFT)
{
TK_NextTokenMustBe(TK_NUMBER, ERR_SCRIPT_OUT_OF_RANGE);
if(tk_Number != 0)
{
ERR_Exit(ERR_SCRIPT_OUT_OF_RANGE, YES, NULL);
}
TK_NextTokenMustBe(TK_RSHIFT, ERR_SCRIPT_OUT_OF_RANGE);
TK_NextToken();
scriptNumber = 0;
}
else if(tk_Token == TK_STRING)
{ // Named scripts start counting at -1 and go down from there.
if(strcasecmp("None", tk_String) == 0)
{
ERR_Error(ERR_SCRIPT_NAMED_NONE, YES, NULL);
}
scriptNumber = -1 - STR_FindInListInsensitive(STRLIST_NAMEDSCRIPTS, tk_String);
TK_NextToken();
}
else
{
scriptNumber = EvalConstExpression();
if(scriptNumber < 1 || scriptNumber > 32767)
{
TK_Undo();
ERR_Error(ERR_SCRIPT_OUT_OF_RANGE, YES, NULL);
SkipBraceBlock(0);
TK_NextToken();
return;
}
}
if (scriptNumber >= 0)
{
MS_Message(MSG_DEBUG, "Script number: %d\n", scriptNumber);
}
else
{
MS_Message(MSG_DEBUG, "Script name: %s (%d)\n",
STR_GetString(STRLIST_NAMEDSCRIPTS, -scriptNumber - 1),
scriptNumber);
}
scriptType = 0;
scriptFlags = 0;
if(tk_Token == TK_LPAREN)
{
if(TK_NextToken() == TK_VOID)
{
TK_NextTokenMustBe(TK_RPAREN, ERR_MISSING_RPAREN);
}
else
{
TK_Undo();
do
{
TK_NextTokenMustBe(TK_INT, ERR_BAD_VAR_TYPE);
TK_NextTokenMustBe(TK_IDENTIFIER, ERR_INVALID_IDENTIFIER);
if(ScriptVarCount == 4)
{
ERR_Error(ERR_TOO_MANY_SCRIPT_ARGS, YES);
}
if(SY_FindLocal(tk_String) != NULL)
{ // Redefined
ERR_Error(ERR_REDEFINED_IDENTIFIER, YES, tk_String);
}
else if(ScriptVarCount < 4)
{
sym = SY_InsertLocal(tk_String, SY_SCRIPTVAR);
sym->info.var.index = ScriptVarCount;
ScriptVarCount++;
}
TK_NextToken();
} while(tk_Token == TK_COMMA);
TK_TokenMustBe(TK_RPAREN, ERR_MISSING_RPAREN);
}
TK_NextToken();
switch(tk_Token)
{
case TK_DISCONNECT:
scriptType = DISCONNECT_SCRIPTS_BASE;
if(ScriptVarCount != 1)
{
ERR_Error(ERR_DISCONNECT_NEEDS_1_ARG, YES);
}
break;
case TK_OPEN:
case TK_RESPAWN:
case TK_DEATH:
case TK_ENTER:
case TK_PICKUP:
case TK_BLUERETURN:
case TK_REDRETURN:
case TK_WHITERETURN:
case TK_LIGHTNING:
case TK_UNLOADING:
case TK_RETURN:
case TK_KILL:
case TK_REOPEN:
ERR_Error(ERR_UNCLOSED_WITH_ARGS, YES);
break;
case TK_EVENT:
scriptType = EVENT_SCRIPTS_BASE;
if(ScriptVarCount != 3)
{
ERR_Error(ERR_EVENT_NEEDS_3_ARG, YES);
}
break;
default:
TK_Undo();
}
MS_Message(MSG_DEBUG, "Script type: %s (%d %s)\n",
scriptType == 0 ? "closed" : "disconnect",
ScriptVarCount, ScriptVarCount == 1 ? "arg" : "args");
}
else switch (tk_Token)
{
case TK_OPEN:
scriptType = OPEN_SCRIPTS_BASE;
break;
case TK_RESPAWN: // [BC]
scriptType = RESPAWN_SCRIPTS_BASE;
break;
case TK_DEATH: // [BC]
scriptType = DEATH_SCRIPTS_BASE;
break;
case TK_ENTER: // [BC]
scriptType = ENTER_SCRIPTS_BASE;
break;
case TK_RETURN:
scriptType = RETURN_SCRIPTS_BASE;
break;
case TK_PICKUP: // [BC]
scriptType = PICKUP_SCRIPTS_BASE;
break;
case TK_BLUERETURN: // [BC]
scriptType = BLUE_RETURN_SCRIPTS_BASE;
break;
case TK_REDRETURN: // [BC]
scriptType = RED_RETURN_SCRIPTS_BASE;
break;
case TK_WHITERETURN: // [BC]
scriptType = WHITE_RETURN_SCRIPTS_BASE;
break;
case TK_LIGHTNING:
scriptType = LIGHTNING_SCRIPTS_BASE;
break;
case TK_UNLOADING:
scriptType = UNLOADING_SCRIPTS_BASE;
break;
case TK_DISCONNECT:
scriptType = DISCONNECT_SCRIPTS_BASE;
ERR_Error (ERR_DISCONNECT_NEEDS_1_ARG, YES);
break;
case TK_EVENT: // [BB]
scriptType = EVENT_SCRIPTS_BASE;
ERR_Error (ERR_EVENT_NEEDS_3_ARG, YES);
break;
case TK_KILL: // [JM]
scriptType = KILL_SCRIPTS_BASE;
break;
case TK_REOPEN: // [Nash]
scriptType = REOPEN_SCRIPTS_BASE;
break;
default:
ERR_Error(ERR_BAD_SCRIPT_DECL, YES);
SkipBraceBlock(0);
TK_NextToken();
return;
}
TK_NextToken();
if(tk_Token == TK_NET)
{
scriptFlags |= NET_SCRIPT_FLAG;
TK_NextToken();
}
// [BB] If NET and CLIENTSIDE are specified, this construction can only parse
// "NET CLIENTSIDE" but not "CLIENTSIDE NET".
if(tk_Token == TK_CLIENTSIDE)
{
scriptFlags |= CLIENTSIDE_SCRIPT_FLAG;
TK_NextToken();
}
CountScript(scriptType);
PC_AddScript(scriptNumber, scriptType, scriptFlags, ScriptVarCount);
pc_LastAppendedCommand = PCD_NOP;
if(ProcessStatement(STMT_SCRIPT) == NO)
{
ERR_Error(ERR_INVALID_STATEMENT, YES);
}
if(pc_LastAppendedCommand != PCD_TERMINATE)
{
PC_AppendCmd(PCD_TERMINATE);
}
PC_SetScriptVarCount(scriptNumber, scriptType, ScriptVarCount, ScriptArrayCount, ScriptArraySize);
pa_ScriptCount++;
}
//==========================================================================
//
// OuterFunction
//
//==========================================================================
static void OuterFunction(void)
{
enum ImportModes importing;
boolean hasReturn;
symbolNode_t *sym;
int defLine;
MS_Message(MSG_DEBUG, "---- OuterFunction ----\n");
importing = ImportMode;
BreakIndex = 0;
CaseIndex = 0;
StatementLevel = 0;
ScriptVarCount = 0;
ScriptArrayCount = 0;
SY_FreeLocals();
TK_NextToken();
if(tk_Token != TK_STR && tk_Token != TK_INT &&
tk_Token != TK_VOID && tk_Token != TK_BOOL)
{
ERR_Error(ERR_BAD_RETURN_TYPE, YES);
tk_Token = TK_VOID;
}
hasReturn = tk_Token != TK_VOID;
TK_NextTokenMustBe(TK_IDENTIFIER, ERR_INVALID_IDENTIFIER);
sym = SY_FindGlobal(tk_String);
if(sym != NULL)
{
if(sym->type != SY_SCRIPTFUNC)
{ // Redefined
ERR_Error(ERR_REDEFINED_IDENTIFIER, YES, tk_String);
SkipBraceBlock(0);
TK_NextToken();
return;
}
if(!sym->info.scriptFunc.predefined)
{
ERR_Error(ERR_FUNCTION_ALREADY_DEFINED, YES);
ERR_ErrorAt(sym->info.scriptFunc.sourceName, sym->info.scriptFunc.sourceLine);
ERR_Error(ERR_NONE, YES, "Previous definition was here.");
SkipBraceBlock(0);
TK_NextToken();
return;
}
if(sym->info.scriptFunc.hasReturnValue && !hasReturn)
{
ERR_Error(ERR_PREVIOUS_NOT_VOID, YES);
ERR_ErrorAt(sym->info.scriptFunc.sourceName, sym->info.scriptFunc.sourceLine);
ERR_Error(ERR_NONE, YES, "Previous use was here.");
}
}
else
{
sym = SY_InsertGlobal(tk_String, SY_SCRIPTFUNC);
if (importing == IMPORT_Importing)
{
sym->info.scriptFunc.address = 0;
sym->info.scriptFunc.predefined = NO;
}
else
{
sym->info.scriptFunc.address = pc_Address;
sym->info.scriptFunc.predefined = YES;
// only for consistency with other speculated functions and pretty logs
sym->info.scriptFunc.funcNumber = 0;
}
}
defLine = tk_Line;
TK_NextTokenMustBe(TK_LPAREN, ERR_MISSING_LPAREN);
if(TK_NextToken() == TK_VOID)
{
TK_NextTokenMustBe(TK_RPAREN, ERR_MISSING_RPAREN);
}
else
{
TK_Undo();
do
{
symbolType_t type;
symbolNode_t *local;
TK_NextToken();
/* if(tk_Token != TK_INT && tk_Token != TK_STR && tk_Token != TK_BOOL)
{
ERR_Error(ERR_BAD_VAR_TYPE, YES);
tk_Token = TK_INT;
}
*/ if(tk_Token == TK_INT || tk_Token == TK_BOOL)
{
if(TK_NextToken() == TK_LBRACKET)
{
TK_NextTokenMustBe(TK_RBRACKET, ERR_BAD_VAR_TYPE);
type = SY_SCRIPTALIAS;
}
else
{
TK_Undo();
type = SY_SCRIPTVAR;
}
}
else if(tk_Token == TK_STR)
{
type = SY_SCRIPTVAR;
}
else
{
type = SY_SCRIPTVAR;
ERR_Error(ERR_BAD_VAR_TYPE, YES);
tk_Token = TK_INT;
}
TK_NextTokenMustBe(TK_IDENTIFIER, ERR_INVALID_IDENTIFIER);
if(SY_FindLocal(tk_String) != NULL)
{ // Redefined
ERR_Error(ERR_REDEFINED_IDENTIFIER, YES, tk_String);
}
else
{
local = SY_InsertLocal(tk_String, type);
local->info.var.index = ScriptVarCount;
ScriptVarCount++;
}
TK_NextToken();
} while(tk_Token == TK_COMMA);
TK_TokenMustBe(TK_RPAREN, ERR_MISSING_RPAREN);
}
if(pc_EnforceHexen)
{
ERR_Error(ERR_HEXEN_COMPAT, YES);
}
sym->info.scriptFunc.sourceLine = defLine;
sym->info.scriptFunc.sourceName = tk_SourceName;
sym->info.scriptFunc.argCount = ScriptVarCount;
sym->info.scriptFunc.address = (importing == IMPORT_Importing) ? 0 : pc_Address;
sym->info.scriptFunc.hasReturnValue = hasReturn;
if(importing == IMPORT_Importing)
{
SkipBraceBlock(0);
TK_NextToken();
sym->info.scriptFunc.predefined = NO;
sym->info.scriptFunc.varCount = ScriptVarCount;
return;
}
TK_NextToken();
InsideFunction = sym;
pc_LastAppendedCommand = PCD_NOP;
// If we just call ProcessStatement(STMT_SCRIPT), and this function
// needs to return a value but the last pcode output was not a return,
// then the line number given in the error can be confusing because it
// is beyond the end of the function. To avoid this, we process the
// compound statement ourself and check if it returned something
// before checking for the '}'. If a return is required, then the error
// line will be shown as the one that contains the '}' (if present).
TK_TokenMustBe(TK_LBRACE, ERR_MISSING_LBRACE);
TK_NextToken();
do {} while(ProcessStatement(STMT_SCRIPT) == YES);
if(pc_LastAppendedCommand != PCD_RETURNVOID &&
pc_LastAppendedCommand != PCD_RETURNVAL)
{
if(hasReturn)
{
TK_Undo();
ERR_Error(ERR_MUST_RETURN_A_VALUE, YES, NULL);
}
PC_AppendCmd(PCD_RETURNVOID);
}
TK_TokenMustBe(TK_RBRACE, ERR_INVALID_STATEMENT);
TK_NextToken();
sym->info.scriptFunc.predefined = NO;
sym->info.scriptFunc.varCount = ScriptVarCount -
sym->info.scriptFunc.argCount;
PC_AddFunction(sym, ScriptArrayCount, ScriptArraySize);
UnspeculateFunction(sym);
InsideFunction = NULL;
}
//==========================================================================
//
// OuterMapVar
//
//==========================================================================
static void OuterMapVar(boolean local)
{
symbolNode_t *sym = NULL;
int index;