-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdParser.cpp
More file actions
executable file
·3447 lines (3039 loc) · 123 KB
/
EdParser.cpp
File metadata and controls
executable file
·3447 lines (3039 loc) · 123 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//============================================================================
//----------------------------------------------------------------------------
// ScParser.cpp
//----------------------------------------------------------------------------
//============================================================================
// All functions having to do with the parser for the scenario objects.
// Enjoy.
// Some of this stuff is actually code from the game, and is commented out.
// Might make interesting reading.
#include "stdafx.h"
/*
#include <stdio.h>
#include "string.h"
#include "stdlib.h"
*/
#include <ctype.h>
using namespace std;
#include "global.h"
#include "tokntype.h"
// Gloabl varialbes
// external gloabal variables
extern scen_item_data_type scen_data;
extern char store_editor_path[_MAX_PATH + 1];
extern char scenario_path[_MAX_PATH];
// local variables
// stacks for evaluating numerical expressions
#define MAX_NUMERICAL_EXPR_LENGTH 100
token_type tokenList[MAX_NUMERICAL_EXPR_LENGTH];
token_type tokenStack[MAX_NUMERICAL_EXPR_LENGTH];
short stack[MAX_NUMERICAL_EXPR_LENGTH];
char number_string[100]; // used for loding numbers into scripts
// SCRIPT STORAGE
// HERE, ALL CREATURE, SCENARIO, OBJECT SCRIPTS ARE STORED
//script_type *creature_scripts[NUM_CHARS];
//script_type *object_scripts[NUM_OBJ];
//script_type *zone_script = NULL;
//script_type *zone_dialogue_script = NULL;
// function prototype
// void load_town_script();
// void load_tokens_for_initialized_script(script_type *script_data);
// Boolean load_scenario_data(char *scenario_name);
Boolean set_char_variable(short which_char_type,short which_value,short new_value);
Boolean set_char_array_variable(short which_char_type,short which_member,short which_value,short new_value);
Boolean set_char_string(short which_char_type,short which_value,char *new_str);
Boolean set_floor_variable(short which_floor_type,short which_value,short new_value);
Boolean set_floor_string(short which_floor_type,short which_value,char *new_str);
Boolean set_terrain_variable(short which_ter_type,short which_value,short new_value);
Boolean set_terrain_string(short which_ter_type,short which_value,char *new_str);
Boolean set_item_variable(short which_item_type,short which_value,short new_value);
// Boolean set_item_array_variable(short which_item_type,short which_member,short which_value,short new_value);
Boolean set_item_string(short which_item_type,short which_value,char *new_str);
// short evaluate_operator(short operator_type,short op1,short op2);
short value_limit(short start_value,short min,short max,Boolean *error);
Boolean check_script_exists(char *check_file_name,short file_location);
void patch_corescendata2( void );
void init_scripts()
{
//for (short i = 0; i < NUM_CHARS; i++)
// creature_scripts[i] = NULL;
//for (short i = 0; i < NUM_OBJ; i++)
// object_scripts[i] = NULL;
}
// METHODS FOR text_block_type
text_block_type::text_block_type()
{
text_block = NULL;
block_length = 0;
}
text_block_type::~text_block_type()
{
if (text_block != NULL) {
delete[] text_block;
}
}
// Goes to filename, assumed to be a text file, and loads said text file into text string
// buffer.
// file_location: 0 - in main data folder, 1 - in scenario folder
Boolean text_block_type::load_text_file_into_buffer(char *file_name_to_load,short file_location)
{
char file_name[256];
long file_length = 0;
short error;
FILE *file_id;
HANDLE hFile_sizecount ;
char slash[3] = " ";
slash[0] = 92;
switch (file_location) {
case 0: sprintf((char *) file_name,"%s%s%s",store_editor_path,slash,file_name_to_load); break;
case 1: sprintf((char *) file_name,"%s%s%s",scenario_path,slash,file_name_to_load); break;
}
// first, figure out length of file
hFile_sizecount = CreateFile (file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;
file_length = GetFileSize(hFile_sizecount,NULL);
CloseHandle (hFile_sizecount) ;
if ((file_length == 0) || (file_length == 0xFFFFFFFF)) {
return TRUE;
}
text_block = new char[file_length + 25];
// now, load in text file
if (NULL == (file_id = fopen(file_name, "rb"))) {
return TRUE;
}
long paranoid_file_length = (LONG) file_length;
if ((error = FSRead(file_id, ¶noid_file_length, (char *) text_block)) != 0)
{return FALSE;}
error = FSClose(file_id);
if (error != 0) {return FALSE;}
text_block[file_length] = 0;
block_length = file_length;
return TRUE;
}
// Goes through text and eliminates all commented text and makes all text not in quotes
// lowercase
void text_block_type::preprocess_text()
{
if (block_length <= 0)
return;
Boolean in_comments = FALSE;
for (long i = 0; i < block_length; i++)
if (text_block[i] == 10)
text_block[i] = 13;
for (long i = 0; i < block_length; i++) {
if ((i < block_length - 1) && (text_block[i] == '/') && (text_block[i + 1] == '/'))
in_comments = TRUE;
else if (text_block[i] == 13)
in_comments = FALSE;
if (in_comments == TRUE)
text_block[i] = ' ';
}
Boolean in_quotes = FALSE;
for (long i = 0; i < block_length; i++) {
if (text_block[i] == '"')
in_quotes = !(in_quotes);
if ((in_quotes == FALSE) && (text_block[i] >= 65) && (text_block[i] <= 90))
text_block[i] = text_block[i] - 'A' + 'a';
}
}
// Gives an UPPER bound for the number of tokens in the script. It MUST
// be >= the number of actual tokens
short text_block_type::estimate_num_of_tokens()
{
if (block_length <= 0)
return 0;
long num_tokens = 0;
long current_mode = -1; // 0 - space, 1 - letter, 2 - number, 3 - other
Boolean space;
//char dbug[5];
for (long i = 0; i < block_length; i++) {
long old_mode = current_mode;
space = FALSE;
if ((text_block[i] == ' ') || (text_block[i] == '\r') || (text_block[i] == '\t')) {
current_mode = 0;
space = TRUE;
}
else if ((text_block[i] >= '0') && (text_block[i] <= '9'))
current_mode = 2;
else if ((text_block[i] >= 'a') && (text_block[i] <= 'z'))
current_mode = 1;
else if ((text_block[i] >= 'A') && (text_block[i] <= 'Z'))
current_mode = 1;
else if (text_block[i] == '_')
current_mode = 1;
else current_mode = 3;
if ((space == FALSE) && ((old_mode != current_mode) || (current_mode == 3))) {
//dbug[0] = text_block[i];
//dbug[1] = text_block[i + 1];
//dbug[2] = text_block[i + 2];
//dbug[3] = text_block[i + 3];
//dbug[4] = 0;
num_tokens++;
}
}
return (short)(num_tokens + 20);
}
token_type::token_type()
{
type = 0;
line = 0;
what_sort = 0;
}
script_type::script_type()
{
script_name[0] = 0;
type_of_script = -1;
token_list = NULL;
num_tokens = 0;
script_killed = FALSE;
which_object = 0;
for (short i = 0; i < NUM_SCRIPT_STRINGS; i++)
string_data[i] = NULL;
for (short i = 0; i < NUM_SCRIPT_INTS; i++) {
int_var_names[i][0] = 0;
int_var_values[i] = 0;
}
for (short i = 0; i < NUM_SCRIPT_LOCATIONS; i++)
location_var_names[i][0] = 0;
for (short i = 0; i < NUM_SCRIPT_STRING_VARS; i++) {
string_var_names[i][0] = 0;
string_var_values[i] = NULL;
}
for (short i = 0; i < NUM_PROCEDURE_PASS_VARS; i++) {
procedure_passed_variable_types[i] = 0;
procedure_passed_values[i] = 0;
procedure_passed_locations[i].x = procedure_passed_locations[i].y = 0;
}
}
script_type::~script_type()
{
flush_data();
}
void script_type::flush_data ()
{
for (short i = 0; i < NUM_SCRIPT_STRINGS; i++)
if (string_data[i] != NULL) {
delete[] string_data[i];
string_data[i] = NULL;
}
if (token_list != NULL)
delete[] token_list;
token_list = NULL;
for (short i = 0; i < NUM_SCRIPT_STRING_VARS; i++)
if (string_var_values[i] != NULL) {
delete[] string_var_values[i];
string_var_values[i] = NULL;
string_var_names[i][0] = 0;
}
for (short i = 0; i < NUM_PROCEDURE_PASS_VARS; i++) {
procedure_passed_variable_types[i] = 0;
procedure_passed_values[i] = 0;
procedure_passed_locations[i].x = procedure_passed_locations[i].y = 0;
}
script_killed = FALSE;
}
Boolean script_type::IsWhiteSpace (char c)
{
if (c == 10)
return TRUE;
if (c == 13)
return TRUE;
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
Boolean script_type::IsIdentifier (char c)
{
return isalpha(c) || c == '_';
}
Boolean script_type::IsNumber (char c)
{
return (Boolean)isdigit(c);
//return isdigit(c) || c == '.';
}
Boolean script_type::IsOperatorCharacter (char c)
{
return c == '+' || c == '-' || c == '/' || c == '*' || c == '=' || c == '|' || c == '&' || c == '>' || c == '<' || c == '!';
}
short script_type::IsVariable (char * string, int length, token_type *token)
{
for (short i = 0; i < NUM_SCRIPT_INTS; i++)
if (((int)strlen(int_var_names[i]) == length) &&
(strncmp(string,int_var_names[i],length) == 0)) {
token->type = INT_VARIABLE_TYPE;
token->what_sort = i;
return i;
}
for (short i = 0; i < NUM_SCRIPT_LOCATIONS; i++)
if (((int)strlen(location_var_names[i]) == length) &&
(strncmp(string,location_var_names[i],length) == 0)) {
token->type = LOCATION_VARIABLE_TYPE;
token->what_sort = i;
return i;
}
for (short i = 0; i < NUM_SCRIPT_STRING_VARS; i++)
if (((int)strlen(string_var_names[i]) == length) &&
(strncmp(string,string_var_names[i],length) == 0)) {
token->type = STRING_VARIABLE_TYPE;
token->what_sort = i;
return i;
}
return -1;
}
//---------------------------------------------------------------------------
short script_type::IsBlockDefiner (char *string, int length, short *value)
{
for (short i = 0; i < NUM_BLOCK_DEFINERS; i++)
if (((int)strlen(block_definers[i].token_text) == length) &&
(strncmp(string,block_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsVarDefiner (char * string, int length, short *value)
{
/// This is only in Scenario data files
if (type_of_script != 0)
return -1;
for (short i = 0; i < NUM_VAR_DEFINERS; i++)
if (((int)strlen(variable_definers[i].token_text) == length) &&
(strncmp(string,variable_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsVarArrayDefiner (char * string, int length, short *value)
{
/// This is only in Scenario data files
if (type_of_script != 0)
return -1;
for (short i = 0; i < NUM_ARRAY_VAR_DEFINERS; i++)
if (((int)strlen(variable_array_definers[i].token_text) == length) &&
(strncmp(string,variable_array_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsVarStringDefiner (char * string, int length, short *value)
{
/// This is only in Scenario data files
if (type_of_script != 0)
return -1;
for (short i = 0; i < NUM_STRING_VAR_DEFINERS; i++)
if (((int)strlen(variable_string_definers[i].token_text) == length) &&
(strncmp(string,variable_string_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsNewVariableDefiner (char * string, int length, short *value)
{
for (short i = 0; i < NUM_VAR_TYPE_DEFINERS; i++)
if (((int)strlen(new_variable_definers[i].token_text) == length) &&
(strncmp(string,new_variable_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsConstant (char * string, int length, short *value)
{
for (short i = 0; i < NUM_CONST_TYPE_DEFINERS; i++)
if (((int)strlen(constant_definers[i].token_text) == length) &&
(strncmp(string,constant_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsOperator (char * string, int length, short *value)
{
for (short i = 0; i < NUM_OPERATORS; i++)
//if (same_string(string,(char *) operator_definers[i].token_text)) {
if (((int)strlen(operator_definers[i].token_text) == length) &&
(strncmp(string,operator_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsNoParameterFunction (char * string, int length, short *value)
{
for (short i = 0; i < NUM_NO_PARAM_FUNCTIONS; i++)
//if (same_string(string,(char *) no_parameter_function_definers[i].token_text)) {
if (((int)strlen(no_parameter_function_definers[i].token_text) == length) &&
(strncmp(string,no_parameter_function_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsFunction (char * string, int length, short *value)
{
/// This is never in Scenario data files
if (type_of_script == 0)
return -1;
for (short i = 0; i < NUM_UNARY_FUNCTIONS; i++)
//if (same_string(string,(char *) unary_function_definers[i].token_text)) {
if (((int)strlen(unary_function_definers[i].token_text) == length) &&
(strncmp(string,unary_function_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsProcedure (char * string, int length, short *value)
{
/// This is never in Scenario data files
if (type_of_script == 0)
return -1;
for (short i = 0; i < NUM_PROCEDURES; i++)
//if (same_string(string,(char *) procedure_definers[i].token_text)) {
if ((strlen(procedure_definers[i].token_text) > 0) &&
(strncmp(string,procedure_definers[i].token_text,length) == 0) &&
((int)strlen(procedure_definers[i].token_text) == length)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsFlowController (char * string, int length, short *value)
{
for (short i = 0; i < NUM_FLOW_CONTROLLERS; i++)
if (((int)strlen(flow_controller_definers[i].token_text) == length) &&
(strncmp(string,flow_controller_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsBinaryFunction (char * string, int length, short *value)
{
/// This is never in Scenario data files
if (type_of_script == 0)
return -1;
for (short i = 0; i < NUM_BINARY_FUNCTIONS; i++)
//if (same_string(string,(char *) binary_function_definers[i].token_text)) {
if (((int)strlen(binary_function_definers[i].token_text) == length) &&
(strncmp(string,binary_function_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsTrinaryFunction (char * string, int length, short *value)
{
/// This is never in Scenario data files
if (type_of_script == 0)
return -1;
for (short i = 0; i < NUM_TRINARY_FUNCTIONS; i++)
//if (same_string(string,(char *) trinary_function_definers[i].token_text)) {
if (((int)strlen(trinary_function_definers[i].token_text) == length) &&
(strncmp(string,trinary_function_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
short script_type::IsLocationFunction (char * string, int length, short *value)
{
/// This is never in Scenario data files
if (type_of_script == 0)
return -1;
for (short i = 0; i < NUM_LOCATION_FUNCTIONS; i++)
//if (same_string(string,(char *) location_returning_function_definers[i].token_text)) {
if (((int)strlen(location_returning_function_definers[i].token_text) == length) &&
(strncmp(string,location_returning_function_definers[i].token_text,length) == 0)) {
*value = i;
return i;
}
return -1;
}
// Loads in the text of the script. Tokenizes it. Sets up all variables and initializes them. Gets rid of loaded text.
// type_of_script_to_load:
// 0 - Scenario data
// 1 - Scenario data file
// 2 - Town - specials
// 3 - Town - dialogue
// 4 - Outdoor - specials
// 5 - Outdoor - dialogue
// 6 - Creature
// 7 - Terrain Spot
// file_location: 0 - in main data folder, 1 - in scenario folder
Boolean script_type::load_script(short type_of_script_to_load,char *script_to_load,short file_location)
{
// first, flush out old crap.
type_of_script = -1;
flush_data();
num_tokens = 0;
// Check script name
if ((strlen(script_to_load) <= 0) || (strlen(script_to_load) >= 100)){
ASB_big("Script name empty or too long.","","","",-1,"");
return FALSE;
}
//strcpy(script_name,script_to_load);
sprintf(script_name,"%s.txt",script_to_load);
// load in text
text_block_type *script = new text_block_type;
if (script == NULL) {
ASB("Failed to allocate script memory.");
return FALSE;
}
if (script->load_text_file_into_buffer(script_name,file_location) == FALSE) {
ASB("Failed to load script text.");
delete script;
script = NULL;
return FALSE;
}
script->preprocess_text();
// estimate tokens and make token list
num_tokens = script->estimate_num_of_tokens() + 2;
if (num_tokens < 0) {
ASB_big("The script ",script_name," is too long.","",-1,"");
return FALSE;
}
token_list = new token_type[num_tokens];
if (token_list == NULL) {
ASB("Failed to allocate memory for token list.");
return FALSE;
}
// Everything worked out. So far.
type_of_script = type_of_script_to_load;
// tokenize script
token_type token;
char *s = script->text_block;
short cur_line = 1,cur_list_position = 0;
Boolean defining_new_variables = FALSE;
short defining_new_variable_type = -1;
short current_variable_created = -1;
for (char *f = s; *f; f = s)
{
int length;
if ((*s == 13) || (*s == 10))
cur_line++;
// get token from input string
if ( IsWhiteSpace(*s) )
{
s++;
continue;
}
if ( *s == '(' ) {
token.type = 18;
s++;
}
else if ( *s == ')' ) {
token.type = 19;
s++;
}
//else if ( *s == '=' ) {
// token.type = 40;
// s++;
//}
else if ( *s == ';' ) {
token.type = 41;
s++;
defining_new_variables = FALSE;
}
else if ( *s == '{' ) {
token.type = 42;
s++;
}
else if ( *s == '}' ) {
token.type = 43;
s++;
}
else if ( *s == ',' ) {
token.type = 20;
if (defining_new_variables)
current_variable_created = -1;
s++;
}
else if ( IsIdentifier(*s) )
{
short length;
for (length = 0; IsIdentifier(*s) || isdigit(*s) ; length++)
s++;
if (length >= MAX_DESCRIBER_LEN) {
ASB_big(script_to_load," script failure: Token longer than max length in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
if ( IsBlockDefiner(f,length,&token.what_sort) >= 0) {
token.type = 1;
}
else if ( IsVarDefiner(f,length,&token.what_sort) >= 0) {
token.type = 44;
}
else if ( IsVarArrayDefiner(f,length,&token.what_sort) >= 0) {
token.type = 45;
}
else if ( IsVarStringDefiner(f,length,&token.what_sort) >= 0) {
token.type = 46;
}
else if ( IsNewVariableDefiner(f,length,&token.what_sort) >= 0) {
token.type = 48;
defining_new_variables = TRUE;
defining_new_variable_type = token.what_sort;
current_variable_created = -1;
}
else if ( IsVariable(f,length,&token) >= 0) {
//token.type = ttVariable;
}
else if ( IsConstant(f,length,&token.what_sort) >= 0) {
token.type = 17;
token.what_sort = constant_values[token.what_sort];
}
else if ( IsProcedure(f,length,&token.what_sort) >= 0)
{
token.type = PROCEDURE_TYPE;
}
else if ( IsNoParameterFunction(f,length,&token.what_sort) >= 0)
{
token.type = NO_PARAM_FUNCTION_TYPE;
}
else if ( IsFunction(f,length,&token.what_sort) >= 0)
{
token.type = UNARY_FUNCTION_TYPE;
}
else if ( IsFlowController(f,length,&token.what_sort) >= 0)
{
token.type = FLOW_CONTROLLER_TYPE;
}
else if ( IsBinaryFunction(f,length,&token.what_sort) >= 0)
{
token.type = BINARY_FUNCTION_TYPE;
}
else if ( IsTrinaryFunction(f,length,&token.what_sort) >= 0)
{
token.type = TRINARY_FUNCTION_TYPE;
}
else if ( IsLocationFunction(f,length,&token.what_sort) >= 0)
{
token.type = LOCATION_FUNCTION_TYPE;
}
else {
// We don't recognize the string. So. Either it's maybe a new variable type
// or it's crap.
if (defining_new_variables) { // maybe a new variable
short i;
Boolean no_room_in_list = FALSE;
if (length >= SCRIPT_VAR_NAME_LEN - 1) {
ASB_big(script_to_load," script failure: Variable name too long in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
switch (defining_new_variable_type) {
case 0: case 1: // int
for (i = 0; i < NUM_SCRIPT_INTS; i++)
if (strlen(int_var_names[i]) == 0) {
strncpy(int_var_names[i],f,length);
int_var_names[i][length] = 0;
current_variable_created = i;
token.type = INT_VARIABLE_TYPE;
token.what_sort = i;
i = NUM_SCRIPT_INTS + 10;
}
if (i < NUM_SCRIPT_INTS + 10)
no_room_in_list = TRUE;
break;
case 2: // string
for (i = 0; i < NUM_SCRIPT_STRING_VARS; i++)
if (strlen(string_var_names[i]) == 0) {
strncpy(string_var_names[i],f,length);
string_var_names[i][length] = 0;
current_variable_created = i;
token.type = STRING_VARIABLE_TYPE;
token.what_sort = i;
i = NUM_SCRIPT_STRING_VARS + 10;
}
if (i < NUM_SCRIPT_STRING_VARS + 10)
no_room_in_list = TRUE;
break;
case 3: // location
for (i = 0; i < NUM_SCRIPT_LOCATIONS; i++)
if (strlen(location_var_names[i]) == 0) {
strncpy(location_var_names[i],f,length);
location_var_names[i][length] = 0;
current_variable_created = i;
token.type = LOCATION_VARIABLE_TYPE;
token.what_sort = i;
i = NUM_SCRIPT_LOCATIONS + 10;
}
if (i < NUM_SCRIPT_LOCATIONS + 10)
no_room_in_list = TRUE;
break;
}
if (no_room_in_list) {
ASB_big(script_to_load," script failure: Too many variables defined in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
}
else { // it's crap. error out
char* save = new char[length+1];
strncpy(save,f,length);
save[length] = 0;
ASB_big(script_to_load," script failure: Invalid identifier ",save," in line ",cur_line,".");
delete[] save;
delete[] token_list;
token_list = NULL;
return FALSE;
}
}
}
else if ((IsNumber(*s)) || ((*s == '-') && (IsNumber(*(s + 1))))) {
Boolean minus_present = FALSE;
if (*s == '-') {
minus_present = TRUE;
s++;
}
for (length = 0; IsNumber(*s); length++)
s++;
//char* number = new char[length+1];
if (minus_present == TRUE)
strncpy(number_string,f + 1,length);
else strncpy(number_string,f,length);
number_string[length] = 0;
token.type = 17;
token.what_sort = (short) strtol(number_string,NULL,0);
if (minus_present == TRUE)
token.what_sort = token.what_sort * -1;
//delete[] number;
}
else if ( IsOperatorCharacter(*s) )
{
for (length = 0; IsOperatorCharacter(*s); length++)
s++;
if (length > 2) {
ASB_big(script_to_load," script failure: Overlong operator in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
if ( IsOperator(f,length,&token.what_sort) >= 0) {
token.type = BINARY_OPERATOR_TYPE;
}
else if ((length == 1) && (*f == '=')) {
token.type = EQUALS_TYPE;
}
else {
ASB_big(script_to_load," script failure: Invalid operator ",""," in line ",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
}
else if ( *s == '"' ) { // create string data
s++;
for (length = 0; (*s != '"') && (*s != 0) && (length < 255); length++)
s++;
if ((length >= 255) || (*s == 0)) {
ASB_big(script_to_load," script failure: Overlong or unending string in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
short i = 0;
// put string in either string storage (constants) or variables
if ((defining_new_variables) && (defining_new_variable_type == 2) && (current_variable_created >= 0)) {
if (string_var_values[current_variable_created] == NULL) {
string_var_values[current_variable_created] = new char[length + 1];
strncpy(string_var_values[current_variable_created],f + 1,length);
string_var_values[current_variable_created][length] = 0;
if (length >= 255)
string_var_values[current_variable_created][255] = 0;
}
} else {
for (i = 0; i < NUM_SCRIPT_STRINGS; i++) {
if (string_data[i] == NULL) {
string_data[i] = new char[length + 1];
strncpy(string_data[i],f + 1,length);
string_data[i][length] = 0;
if (length >= 255)
string_data[i][255] = 0;
token.what_sort = i;
i = 2 * NUM_SCRIPT_STRINGS;
}
}
}
if (i == NUM_SCRIPT_STRINGS) {
ASB_big(script_to_load," script failure: Too many strings in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
token.type = 47;
s++;
}
else {
if ( *s )
{
ASB_big(script_to_load," script failure: Invalid symbol in line ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
}
// add token to list
if (cur_list_position >= num_tokens) {
ASB_big(script_to_load," internal error: Token stack overflow in ","","",cur_line,".");
delete[] token_list;
token_list = NULL;
return FALSE;
}
if (token.type == 17) {
// Now, we may want to take this number and use it to initialize a variable
if ((defining_new_variables) && (current_variable_created >= 0)) {
switch (defining_new_variable_type) {
case 0: case 1: // int
int_var_values[current_variable_created] = token.what_sort;
break;
case 2: // string
ASB_big(script_to_load," internal error: Bad initialization in ","","",cur_line,".");
break;
case 3: // location
if (token_list[cur_list_position - 1].type == 17)
location_var_values[current_variable_created].y = (t_coord)token.what_sort;
else location_var_values[current_variable_created].x = (t_coord)token.what_sort;
break;
}
}
}
token.line = cur_line;
token_list[cur_list_position] = token;
cur_list_position++;
}
delete script;
script = NULL;
return TRUE;
}
Boolean script_type::token_type_match(short which_token,short type)
{
if (which_token >= num_tokens)
return FALSE;
if (token_list[which_token].type == type)
return TRUE;
return FALSE;
}
/*
Boolean script_type::token_sort_match(short which_token,short what_sort)
{
if (which_token >= num_tokens)
return FALSE;
if (token_list[which_token].what_sort == what_sort)
return TRUE;
return FALSE;
}
*/
Boolean script_type::token_match(short which_token,short type,short what_sort)
{
if (which_token >= num_tokens)
return FALSE;
if (token_list[which_token].what_sort != what_sort)
return FALSE;
if (token_list[which_token].type != type)
return FALSE;
return TRUE;
}
// given a token type and sort, finds the first token in the script matching it.
// if what_sort is -1, just find first token with type matching type_of_token
/*
short script_type::find_first_matching_token(short type_of_token,short what_sort)
{
short i;
for (i = 0; i < num_tokens; i++)
if ((token_list[i].type == type_of_token) &&
((what_sort < 0) || (what_sort == token_list[i].what_sort)))
return i;
return -1;
}
*/
// given a token type and sort and a start_token from which to begin search, finds the
// next token in the script matching it.
// if what_sort is -1, just find first token with type matching type_of_token
/*
short script_type::find_next_matching_token(short start_token,short type_of_token,short what_sort)
{
if (start_token >= num_tokens)
return -1;
for (short i = start_token; i < num_tokens; i++)
if ((token_list[i].type == type_of_token) &&
((what_sort < 0) || (what_sort == token_list[i].what_sort)))
return i;
return -1;
}
*/
// given a token type and sort and a start_token from which to begin search, finds the