-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsoulfu_script.c
More file actions
3042 lines (2740 loc) · 108 KB
/
soulfu_script.c
File metadata and controls
3042 lines (2740 loc) · 108 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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include "soulfu_script.h"
#ifndef FALSE
# define FALSE 0
#endif
#ifndef TRUE
# define TRUE 1
#endif
#define log_message
#define debug printf
#define repeat(A, B) for(A=0; A<B; A++)
#define SRC_PERMANENT (0) // Define is permanent
#define SRC_TEMPORARY_FILE (1) // Define goes away when done with file
#define SRC_TEMPORARY_FUNCTION (2) // Define goes away when done with function
#define SRC_MAX_TOKEN 64 // The maximum number of pieces per line of a SRC file
#define SRC_MAX_DEFINE 2048 // The maximum number of defines
#define SRC_MAX_TOKEN_SIZE 128 // The maximum size of each piece
#define SRC_MAX_INDENT 256 // Allow up to 256 indentation levels...
#define SRC_JUMP_INVALID 0 // Marked as unused...
#define SRC_JUMP_IF 1 // Used when an if is found
#define SRC_JUMP_ELSE 2 // Used when an else is found
#define SRC_JUMP_WHILE 3 // Used when a while is found
// For direct code->script calls...
# define MAX_FAST_FUNCTION 16 // For predefined script functions like Spawn
# define FAST_FUNCTION_SPAWN 0 // Offset for the Spawn() function
# define FAST_FUNCTION_REFRESH 2 // Offset for the Refresh() function
# define FAST_FUNCTION_EVENT 4 // Offset for the Event() function
# define FAST_FUNCTION_AISCRIPT 6 // Offset for the AIScript() function
# define FAST_FUNCTION_BUTTONEVENT 8 // Offset for the ButtonEvent() function
# define FAST_FUNCTION_GETNAME 10 // Offset for the GetName() function
# define FAST_FUNCTION_UNPRESSED 12 // Offset for the Unpressed() function
# define FAST_FUNCTION_FRAMEEVENT 14 // Offset for the FrameEvent() function
# define FAST_FUNCTION_MODELSETUP 16 // Offset for the ModelSetup() function...
# define FAST_FUNCTION_DEFENSERATING 18 // Offset for the DefenseRating() function...
# define FAST_FUNCTION_SETUP 20 // Offset for the Setup() function...
# define FAST_FUNCTION_DIRECTUSAGE 22 // Offset for the DirectUsage() function...
# define FAST_FUNCTION_ENCHANTUSAGE 24 // Offset for the EnchantUsage() function...
#define MAX_VARIABLE 32 // I00 - I31, F00 - F31...
#define MAX_ARGUMENT 16
#define OPCODE_EQUALS 0
#define OPCODE_ADD 1
#define OPCODE_SUBTRACT 2
#define OPCODE_MULTIPLY 3
#define OPCODE_DIVIDE 4
#define OPCODE_INCREMENT 5
#define OPCODE_DECREMENT 6
#define OPCODE_ISEQUAL 7
#define OPCODE_ISNOTEQUAL 8
#define OPCODE_ISGREATEREQUAL 9
#define OPCODE_ISLESSEREQUAL 10
#define OPCODE_ISGREATER 11
#define OPCODE_ISLESSER 12
#define OPCODE_LOGICALAND 13
#define OPCODE_LOGICALOR 14
#define OPCODE_LOGICALNOT 15
#define OPCODE_NEGATE 16
// !!!BAD!!!
// !!!BAD!!!
#define OPCODE_BITWISEAND 19
#define OPCODE_BITWISEOR 20
#define OPCODE_BITWISELEFT 21
#define OPCODE_BITWISERIGHT 22
#define OPCODE_MODULUS 23
#define OPCODE_LOCALMESSAGE 24
#define OPCODE_LOGMESSAGE 25
#define OPCODE_FINDSELF 26
#define OPCODE_SYSTEMSET 27
#define OPCODE_SYSTEMGET 28
#define OPCODE_DEBUGMESSAGE 29
#define OPCODE_TOFLOAT 30
#define OPCODE_TOINT 31
#define OPCODE_F_EQUALS 32
#define OPCODE_F_ADD 33
#define OPCODE_F_SUBTRACT 34
#define OPCODE_F_MULTIPLY 35
#define OPCODE_F_DIVIDE 36
#define OPCODE_F_INCREMENT 37
#define OPCODE_F_DECREMENT 38
#define OPCODE_F_ISEQUAL 39
#define OPCODE_F_ISNOTEQUAL 40
#define OPCODE_F_ISGREATEREQUAL 41
#define OPCODE_F_ISLESSEREQUAL 42
#define OPCODE_F_ISGREATER 43
#define OPCODE_F_ISLESSER 44
#define OPCODE_F_LOGICALAND 45
#define OPCODE_F_LOGICALOR 46
#define OPCODE_F_LOGICALNOT 47
#define OPCODE_F_NEGATE 48
#define OPCODE_STRING 49
#define OPCODE_STRINGGETNUMBER 50
#define OPCODE_STRINGCLEAR 51
#define OPCODE_STRINGCLEARALL 52
#define OPCODE_STRINGAPPEND 53
#define OPCODE_STRINGCOMPARE 54
#define OPCODE_STRINGLENGTH 55
#define OPCODE_STRINGCHOPLEFT 56
#define OPCODE_STRINGCHOPRIGHT 57
#define OPCODE_STRINGRANDOMNAME 58
#define OPCODE_STRINGSANITIZE 59
#define OPCODE_NETWORKMESSAGE 60
#define OPCODE_STRINGLANGUAGE 61
#define OPCODE_STRINGUPPERCASE 62
#define OPCODE_STRINGAPPENDNUMBER 63
#define OPCODE_CALLFUNCTION 64
#define OPCODE_RETURNINT 65
#define OPCODE_RETURNFLOAT 66
#define OPCODE_IFFALSEJUMP 67
#define OPCODE_JUMP 68
#define OPCODE_SQRT 69
#define OPCODE_FILEOPEN 70
#define OPCODE_FILEREADBYTE 71
#define OPCODE_FILEWRITEBYTE 72
#define OPCODE_FILEINSERT 73
#define OPCODE_SPAWN 74
#define OPCODE_GOPOOF 75
#define OPCODE_DISMOUNT 76
#define OPCODE_ROLLDICE 77
#define OPCODE_PLAYSOUND 78
#define OPCODE_PLAYMEGASOUND 79
#define OPCODE_DISTANCESOUND 80
#define OPCODE_PLAYMUSIC 81
#define OPCODE_UPDATEFILES 82
#define OPCODE_SIN 83
#define OPCODE_ACQUIRETARGET 84
#define OPCODE_FINDPATH 85
#define OPCODE_BUTTONPRESS 86
#define OPCODE_AUTOAIM 87
#define OPCODE_ROOMHEIGHTXY 88
// !!!BAD!!!
// !!!BAD!!!
// !!!BAD!!!
// !!!BAD!!!
// !!!BAD!!!
// !!!BAD!!!
#define OPCODE_WINDOWBORDER 96
#define OPCODE_WINDOWSTRING 97
#define OPCODE_WINDOWMINILIST 98
#define OPCODE_WINDOWSLIDER 99
#define OPCODE_WINDOWIMAGE 100
#define OPCODE_WINDOWTRACKER 101
#define OPCODE_WINDOWBOOK 102
#define OPCODE_WINDOWINPUT 103
#define OPCODE_WINDOWEMACS 104
#define OPCODE_WINDOWMEGAIMAGE 105
#define OPCODE_WINDOW3DSTART 106
#define OPCODE_WINDOW3DEND 107
#define OPCODE_WINDOW3DPOSITION 108
#define OPCODE_WINDOW3DMODEL 109
#define OPCODE_MODELASSIGN 110
#define OPCODE_PARTICLEBOUNCE 111
#define OPCODE_WINDOWEDITKANJI 112
#define OPCODE_WINDOW3DROOM 113
#define OPCODE_INDEXISLOCALPLAYER 114
#define OPCODE_FINDBINDING 115
#define OPCODE_FINDTARGET 116
#define OPCODE_FINDOWNER 117
#define OPCODE_FINDINDEX 118
#define OPCODE_FINDBYINDEX 119
#define OPCODE_FINDWINDOW 120
#define OPCODE_FINDPARTICLE 121
// !!!BAD!!!
#define OPCODE_ATTACHTOTARGET 123
#define OPCODE_GETDIRECTION 124
#define OPCODE_DAMAGETARGET 125
#define OPCODE_EXPERIENCEFUNCTION 126
// !!!BAD!!!
// !!!BAD!!!
// !!!BAD!!!
// Last basic function is 127...
#define VAR_INVALID '?' // Used a bunch
#define VAR_INT 'I' // Used a bunch
#define VAR_FLOAT 'F' // Used a bunch
#define VAR_STRING 'S' // For property extensions only
#define VAR_TEXT 'T' // For property extensions only
#define VAR_BYTE 'B' // For property extensions only
#define VAR_WORD 'W' // For property extensions only
#define SRC_REQUEST_ENTRY 0 // Used for mega_find... Return pointer to function header
#define SRC_REQUEST_OFFSET 1 // Tells us where a CallFunction should take us
#define SRC_REQUEST_ARGUMENTS 2 // Return pointer to a function's argument/returncode string
#define SRC_REQUEST_FILESTART 3 // Return pointer to the start of the function's run file data
#define TEMPBUFF_NUM 16
static char *working_dir = NULL;
// workaround for buffer overwrite in src_mega_find_function
// if multiple files are traversed during a single line parsing
struct Buffer tempbuff[TEMPBUFF_NUM];
int tempbuff_idx = 0;
static enum SSError error = SSE_NONE;
static char define_token[SRC_MAX_DEFINE][SRC_MAX_TOKEN_SIZE]; // ex. "TRUE"
static char define_value[SRC_MAX_DEFINE][SRC_MAX_TOKEN_SIZE]; // ex. "1"
static char define_temporary_level[SRC_MAX_DEFINE]; // 0 is global, 1 is file, 2 is function
static int src_num_define = 0; // The number of defined values... May be higher than actual...
static char *sdf_read_file = NULL; // A pointer to the current read position for sdf_open
static int sdf_read_first_line; // FALSE until sdf_read_line has been called...
static int sdf_read_line_number = 0; // The current line number
static int sdf_read_remaining; // The number of bytes left to read
static unsigned char token_buffer[SRC_MAX_TOKEN][SRC_MAX_TOKEN_SIZE]; // A place to put the pieces
static signed char next_token_may_be_negative; // For reading -5 as negative 5 instead of minus 5
// for compilerizing only
static unsigned char last_function_returns_integer;
unsigned char last_return_type;
static signed char float_variable_set[MAX_VARIABLE]; // Make sure variables are set before they're
static signed char int_variable_set[MAX_VARIABLE]; // used in expressions... x = 0 before y = x...
#define SRC_BUFFER_SIZE 65550 //16384 extra big so src_buffer_used doesn't write other stuff... Size of the RUN (!!!) file...
static unsigned char src_buffer[SRC_BUFFER_SIZE]; // Stick the RUN file here while building it...
static unsigned short src_buffer_used = 0; // Current size
unsigned char token_order; // For determining RPN order/priority...
// Property stuff...
#define MAX_PROPERTY 256 // Must be 256... Or else script props need to have 2 byte extensions...
char property_token[MAX_PROPERTY][8]; // Tags for the properties... the x of window.x...
char property_type[MAX_PROPERTY]; // F or I or others...
unsigned short property_offset[MAX_PROPERTY]; // Offset of data for this property
int obj_num_property; // The number of registered properties
unsigned short put_jump_offset_here[SRC_MAX_INDENT]; // For figurin' out indentation jumps
unsigned short while_jumps_back_to_here[SRC_MAX_INDENT]; // For figurin' out indentation jumps
unsigned char last_jump_type_found[SRC_MAX_INDENT]; // Not used, If, Else, While...
unsigned char token_priority[SRC_MAX_TOKEN]; // For figurin' out the RPN order
unsigned char token_priority_list[SRC_MAX_TOKEN]; // List of tokens in order...
unsigned char token_is_operand[SRC_MAX_TOKEN]; // For figurin' out the RPN order
unsigned char token_is_operator[SRC_MAX_TOKEN]; // For figurin' out the RPN order
unsigned char token_is_function[SRC_MAX_TOKEN]; // For figurin' out the RPN order
unsigned char token_opcode[SRC_MAX_TOKEN]; // For figurin' out the RPN data
unsigned char token_variable_type[SRC_MAX_TOKEN]; // F or I or ?...
signed char token_number_to_destroy[SRC_MAX_TOKEN]; // The number of arguments to a function
unsigned char token_change_type[SRC_MAX_TOKEN]; // For variable type conversions
unsigned char token_is_destroyed[SRC_MAX_TOKEN]; // For variable type conversions
unsigned char token_is_string[SRC_MAX_TOKEN]; // 'Cause string pointers are filled in during functionize...
unsigned char* token_arg_list[SRC_MAX_TOKEN]; // Points to somethin' like "FII" for a function...
unsigned char line_is_a_conditional; // While, If, or Else on line...
int token_extension[SRC_MAX_TOKEN]; // For figurin' out the RPN data
char arg_list_none[]=""; // Argument lists for basic functions
char arg_list_int[]="I"; //
char arg_list_int_int[]="II"; //
char arg_list_float[]="F"; //
char arg_list_float_float[]="FF"; //
char arg_list_iii[] = "III"; //
char arg_list_iif[] = "IIF"; //
char arg_list_iiii[] = "IIII"; //
char arg_list_iffi[] = "IFFI"; //
char arg_list_ffii[] = "FFII"; //
char arg_list_iiiii[] = "IIIII"; //
char arg_list_ifffi[] = "IFFFI"; //
char arg_list_ffiii[] = "FFIII"; //
char arg_list_iiiiii[] = "IIIIII"; //
char arg_list_iffiii[] = "IFFIII"; //
char arg_list_ffiiii[] = "FFIIII"; //
char arg_list_ffiiiii[] = "FFIIIII"; //
char arg_list_fffi[] = "FFFI"; //
char arg_list_fffiiii[] = "FFFIIII"; //
char arg_list_ffffi[] = "FFFFI"; //
char arg_list_ffffiii[] = "FFFFIII"; //
char arg_list_ffffiiiiii[] = "FFFFIIIIII"; //
char arg_list_ffffffffffffiii[] = "FFFFFFFFFFFFIII";
// ---
static void src_undefine_level(char temporary_level);
static float sdf_read_float(unsigned char* location);
static unsigned int sdf_read_unsigned_int(unsigned char* location);
static void sdf_write_unsigned_int(unsigned char* location, unsigned int value);
static unsigned short sdf_read_unsigned_short(unsigned char* location);
static void sdf_write_unsigned_short(unsigned char* location, unsigned short value);
static signed char sdf_read_line(void);
static signed char src_read_token(unsigned char* buffer);
static int count_indentation(char *string);
static void src_add_define(char* token, char* value, char temporary_level);
static int src_get_define(char* token);
static void make_uppercase(char *string);
// compilerize only
int obj_get_property(char* token);
void src_close_jumps(unsigned char indent, unsigned char last_indent);
void src_add_return_opcode(void);
int src_find_function_entry(unsigned char* filedata, char* functionname);
void src_make_arrays(int token_count, char* filename);
void src_find_priority(int start, int end, signed char change_signs, signed char first_check);
void src_figure_variable_types(int token_count);
void src_generate_opcodes(int token_count);
int src_find_string_entry(unsigned char* filedata, char* stringname);
unsigned char* src_mega_find_function(unsigned char* functionstring, unsigned char* filename, unsigned char request);
void src_set_priority(int start, int i, int end, signed char any_type);
unsigned char* sdf_find_filetype(char *filename, char *filetype);
void obj_reset_property(void);
void obj_add_property(char* tag, char type, char* offset);
signed char sdf_open(char* filename);
// ---
enum SSError src_headerize(struct Buffer *script, struct Buffer *run)
{
// This function runs through an SRC file that has been stored in memory and finds
// function information to store in the header. This must be done for every file
// before compilation can occur.
error = SSE_NONE;
// Undefine any variables and defines used by the last file
src_undefine_level(SRC_TEMPORARY_FILE);
// Just like sdf_open...
sdf_read_first_line = FALSE;
sdf_read_remaining = script->used;
sdf_read_file = (unsigned char *)script->mem;
sdf_read_line_number = 0;
next_token_may_be_negative = TRUE;
// Pad the start of the header with fast function offsets...
run->used = 0;
for (int i = 0; i < MAX_FAST_FUNCTION; ++i)
{
// 2 bytes each...
run->mem[run->used] = 0;
run->used++;
run->mem[run->used] = 0;
run->used++;
}
// Something like this in the run_buffer...
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// Make a list of all the functions in this file... Figure out offsets in another pass...
run->used += 4; // Skip a word for the string count... Filled in after counting...
//debug("Headerizing functions...\n");
unsigned short number_of_functions = 0;
while (sdf_read_line())
{
// Count the indentation (by 2's), and skip over any whitespace...
unsigned char indent = count_indentation(sdf_read_file);
sdf_read_file += indent;
sdf_read_remaining -= indent;
indent = indent >> 1;
// Skip #defines, we're only interested in functions...
if (sdf_read_file[0] != '#' && indent == 0)
{
// Read all of the tokens on this line...
int token_count = 0;
unsigned short keepgoing = TRUE;
while (keepgoing)
{
keepgoing = FALSE;
if (token_count < SRC_MAX_TOKEN)
{
if (src_read_token(token_buffer[token_count]))
{
//debug("Line %d, %s\n", sdf_read_line_number, token_buffer[token_count]);
token_count++;
keepgoing = TRUE;
}
}
}
// Did we read anything?
if (token_count > 0)
{
// Get the return type for the function...
int i = 0;
char returncode = 'I';
if (strcmp(token_buffer[0], "INT") == 0) { returncode = 'I'; i++; }
if (strcmp(token_buffer[0], "FLOAT") == 0) { returncode = 'F'; i++; }
// Make sure the function has parentheses after it
if (token_buffer[i+1][0] == '(')
{
// Now stick the function address and name in our RUN file...
sprintf((run->mem + run->used), "%c%c%s%c", 0, 0, token_buffer[i], 0);
run->used += strlen(token_buffer[i]) + 3;
//debug("function... %s()\n", token_buffer[i]);
// Now store the return code type...
sprintf((run->mem + run->used), "%c", returncode);
run->used++;
// Now store any arguments types...
i = i + 2;
while (i < token_count)
{
if (strcmp(token_buffer[i], "INT") == 0)
{
sprintf((run->mem + run->used), "I");
run->used++;
}
else if (strcmp(token_buffer[i], "FLOAT") == 0)
{
sprintf((run->mem + run->used), "F");
run->used++;
}
i++;
}
// And zero terminate the string...
sprintf((run->mem + run->used), "%c", 0);
run->used++;
number_of_functions++;
}
else
{
debug("Error: (line %d), function symbol '%s' not followed by ()\n", sdf_read_line_number, token_buffer[i]);
error = SSE_HEADERIZE;
goto src_headerize_error_handle;
}
}
}
}
sdf_write_unsigned_short(run->mem + (MAX_FAST_FUNCTION << 1), number_of_functions);
// Something like this in the run_buffer...
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 03 00 00
// 0 0 D E A T H 00 I F F F I 00
// 0 0 B L A H 00 V 00
// 0 0 A T T A C K 00 V I 00
// Address FunctionName ReturnAndArgumentTypes
// Check for errors too...
if (number_of_functions == 0)
{
debug("Error: No functions found.\n");
error = SSE_HEADERIZE;
goto src_headerize_error_handle;
}
// Now search for any #defines... Just in case someone #define'd a string...
//debug("Headerizing #define's...\n");
sdf_read_first_line = FALSE;
sdf_read_remaining = script->used;
sdf_read_file = (unsigned char *)script->mem;
sdf_read_line_number = 0;
next_token_may_be_negative = TRUE;
// Make a list of all the functions in this file... Figure out offsets in another pass...
while (sdf_read_line())
{
// Count the indentation (by 2's), and skip over any whitespace...
unsigned char indent = count_indentation(sdf_read_file);
sdf_read_file += indent;
sdf_read_remaining -= indent;
indent = indent >> 1;
// Check for a #define...
if (sdf_read_file[0] == '#')
{
if (sdf_read_file[1] == 'D')
{
if (sdf_read_file[2] == 'E')
{
if (sdf_read_file[3] == 'F')
{
if (sdf_read_file[4] == 'I')
{
if (sdf_read_file[5] == 'N')
{
if (sdf_read_file[6] == 'E')
{
sdf_read_file+=7;
sdf_read_remaining-=7;
if (src_read_token(token_buffer[0]))
{
src_add_define(token_buffer[0], sdf_read_file, SRC_TEMPORARY_FILE);
}
}
}
}
}
}
}
}
}
// Now search for strings to append to the header... Just like above...
sdf_read_first_line = FALSE;
sdf_read_remaining = script->used;
sdf_read_file = (unsigned char *)script->mem;
sdf_read_line_number = 0;
next_token_may_be_negative = TRUE;
// Make a list of all the functions in this file... Figure out offsets in another pass...
//debug("Headerizing strings...\n");
unsigned short number_of_strings = 0;
while (sdf_read_line())
{
// Count the indentation (by 2's), and skip over any whitespace...
unsigned char indent = count_indentation(sdf_read_file);
sdf_read_file += indent;
sdf_read_remaining -= indent;
indent = indent >> 1;
// Skip #defines, we're only interested in strings...
if (sdf_read_file[0] != '#' && indent != 0)
{
// Read all of the tokens on this line...
int token_count = 0;
unsigned short keepgoing = TRUE;
while (keepgoing)
{
keepgoing = FALSE;
if (token_count < SRC_MAX_TOKEN)
{
if (src_read_token(token_buffer[token_count]))
{
token_count++;
keepgoing = TRUE;
}
}
}
// Look through all of the tokens for a string token...
for (int i = 0; i < token_count; ++i)
{
// Look for string tokens...
//debug("Token %s\n", token_buffer[i]);
if(token_buffer[i][0] == '"')
{
// Found a string, so put it in the header
sdf_write_unsigned_short(run->mem + run->used, 65535);
run->used += 2;
int length = strlen(token_buffer[i]+1)-1;
for (int j = 0; j < length; ++j)
{
*(run->mem + run->used) = token_buffer[i][j+1];
run->used++;
}
*(run->mem + run->used) = 0;
run->used++;
number_of_strings++;
}
else
{
// Check for #define'd strings...
int def = src_get_define(token_buffer[i]);
if (def >= 0)
{
//debug("Sneaky define... %s\n", define_token[def]);
// It was a sneaky #define... Search for " characters...
char read_char = define_value[def][0];
int k = 0;
while (read_char != 0)
{
if (read_char == '"')
{
// Start of a string, so write it into the header...
sdf_write_unsigned_short(run->mem + run->used, 65535);
run->used += 2;
k++;
read_char = define_value[def][k];
while(read_char != 0 && read_char != '"')
{
*(run->mem + run->used) = read_char;
run->used++;
k++;
read_char = define_value[def][k];
}
*(run->mem + run->used) = 0;
run->used++;
number_of_strings++;
if(read_char == 0) k--;
}
k++;
read_char = define_value[def][k];
}
}
}
}
}
}
sdf_write_unsigned_short(run->mem + (MAX_FAST_FUNCTION << 1) + 2, number_of_strings);
// Something like this in the run_buffer...
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// 00 03 00 02
// 0 0 D E A T H 00 I F F F I 00
// 0 0 B L A H 00 V 00
// 0 0 A T T A C K 00 V I 00
// 0 0 B i g B o b i s c o o l 00
// FF FF T e s t s t r i n g 00
// FF FF F I L E : T E S T . T X T 00
// String address String
src_headerize_error_handle:
return error;
}
enum SSError src_compilerize(struct Buffer *script, struct Buffer *run, char *filename)
{
// <ZZ> This function compiles an SRC file that has been stored in memory.
int i;
unsigned char indent;
unsigned char last_indent;
unsigned char* data; // Original, Headerized data
signed char keepgoing;
int token_count;
int num_int_variables = 0;
int num_float_variables = 0;
int define;
int tempint;
int tempnum;
signed char tempnext;
unsigned char* tempptr;
signed char stillgoing;
char operation_line;
unsigned short number_of_functions;
int function_offset;
char tempstring[16];
// Don't create the RUN file if we find any errors...
error = SSE_NONE;
// Log what we're doing
//debug("Compilerizing...\n");
// Undefine any variables and defines used by the last file
src_undefine_level(SRC_TEMPORARY_FILE);
// Clear out our jump data...
repeat(i, SRC_MAX_INDENT)
{
last_jump_type_found[i] = SRC_JUMP_INVALID;
}
last_indent = 0;
last_function_returns_integer = TRUE;
// Remember what variables have been set (for generating errors)
repeat(i, MAX_VARIABLE)
{
float_variable_set[i] = FALSE;
int_variable_set[i] = FALSE;
}
// Copy all of the data from the header (that we already compiled)...
src_buffer_used = run->used;
memcpy(src_buffer, run->mem, src_buffer_used);
// Just like sdf_open...
sdf_read_first_line = FALSE;
sdf_read_remaining = script->used;
sdf_read_file = (unsigned char *)script->mem;
sdf_read_line_number = 0;
next_token_may_be_negative = TRUE;
// Make a list of all the functions in this file... Figure out offsets in another pass...
while (sdf_read_line())
{
// Count the indentation (by 2's), and skip over any whitespace...
indent = count_indentation(sdf_read_file);
sdf_read_file += indent;
sdf_read_remaining -= indent;
indent = indent >> 1;
// Check for a #define...
if (sdf_read_file[0] == '#')
{
if (sdf_read_file[1] == 'd')
{
if (sdf_read_file[2] == 'e')
{
if (sdf_read_file[3] == 'f')
{
if (sdf_read_file[4] == 'i')
{
if (sdf_read_file[5] == 'n')
{
if (sdf_read_file[6] == 'e')
{
sdf_read_file += 7;
sdf_read_remaining -= 7;
if (src_read_token(token_buffer[0]))
{
src_add_define(token_buffer[0], sdf_read_file, SRC_TEMPORARY_FILE);
}
}
}
}
}
}
}
}
}
// Just like sdf_open...
sdf_read_first_line = FALSE;
sdf_read_remaining = script->used;
sdf_read_file = (unsigned char *)script->mem;
sdf_read_line_number = 0;
// For each line in the file...
number_of_functions = 0;
while (sdf_read_line())
{
// Count the indentation (by 2's), and skip over any whitespace...
indent = count_indentation(sdf_read_file);
sdf_read_file += indent;
sdf_read_remaining -= indent;
indent = indent >> 1;
next_token_may_be_negative = TRUE;
if (sdf_read_file[0] != '#')
{
// Get all of the tokens in the line...
token_count = 0;
keepgoing = TRUE;
while (keepgoing)
{
keepgoing = FALSE;
if (token_count < SRC_MAX_TOKEN)
{
if (src_read_token(token_buffer[token_count]))
{
// Undefine any variables used by the last function
if (indent == 0 && token_count == 0)
{
src_undefine_level(SRC_TEMPORARY_FUNCTION);
num_int_variables = 0;
num_float_variables = 0;
// Remember what variables have been set (for generating errors)
repeat(i, MAX_VARIABLE)
{
float_variable_set[i] = FALSE;
int_variable_set[i] = FALSE;
}
}
// See if the token is defined
define = src_get_define(token_buffer[token_count]);
if (define > -1)
{
// Save the read line stuff...
tempint = sdf_read_remaining;
tempptr = sdf_read_file;
tempnum = sdf_read_line_number;
tempnext = next_token_may_be_negative;
// Read in all of the tokens in the #define...
next_token_may_be_negative = TRUE;
sdf_read_remaining = SRC_MAX_TOKEN_SIZE;
sdf_read_file = define_value[define];
stillgoing = TRUE;
while (stillgoing)
{
stillgoing = FALSE;
if (token_count < SRC_MAX_TOKEN)
{
if (src_read_token(token_buffer[token_count]))
{
token_count++;
stillgoing = TRUE;
}
}
}
token_count--;
// Restore the read line stuff...
next_token_may_be_negative = tempnext;
sdf_read_line_number = tempnum;
sdf_read_file = tempptr;
sdf_read_remaining = tempint;
}
// See if the token has a dot style property
tempptr = strpbrk(token_buffer[token_count], ".");
if (((token_buffer[token_count][0] >= 'A' && token_buffer[token_count][0] <= 'Z') || token_buffer[token_count][0] == '_') && tempptr)
{
// Looks like it is... Need to break it into two parts...
// First find the property extension...
*tempptr = 0;
tempint = obj_get_property(tempptr+1);
define = src_get_define(token_buffer[token_count]);
if(tempint > -1 && define > -1)
{
// Okay, we found the property and variable... Now reguritate 'em
// If we didn't find anything, it may be an external function...
sprintf(token_buffer[token_count], "%s.%d", define_value[define], tempint);
}
else
{
// Must be an external function call... Replace the period we removed...
*tempptr = '.';
}
}
token_count++;
keepgoing = TRUE;
}
}
}
// Did we actually read anything on this line?
if (token_count > 0)
{
// Check for errors with if and else and while with nothing after...
if (line_is_a_conditional == TRUE && indent <= last_indent)
{
debug("Error: (line %d), Conditional without a nest\n", sdf_read_line_number);
error = SSE_COMPILERIZE;
}
// Is this line the start of a function?
operation_line = TRUE;
if (indent == 0)
{
// It is... Let's write down the offset so we can find it again later...
operation_line = FALSE;
if(last_indent > 0)
{
// Close out all jumps...
src_close_jumps(indent, last_indent);
// Slap in a return 1, just to be on the safe side...
src_add_return_opcode();
last_indent = 0;
}
// Skip over the return value...
i = 0;
last_function_returns_integer = TRUE;
if (strcmp(token_buffer[0], "INT") == 0) i++;
if (strcmp(token_buffer[0], "FLOAT") == 0) { last_function_returns_integer = FALSE; i++; }
// Find the function in the RUN header...
function_offset = src_find_function_entry(src_buffer, token_buffer[i]);
if (function_offset)
{
// Then write down the offset...
sdf_write_unsigned_short(src_buffer + function_offset, src_buffer_used);
number_of_functions++;
i++;
// Now define any arguments... #define value i0
i++; // Skip the parentheses
while (i < token_count)
{
if (strcmp(token_buffer[i], "INT") == 0 && num_int_variables < MAX_VARIABLE)
{
i++;
if (i < token_count)
{
sprintf(tempstring, "I%d", num_int_variables);
src_add_define(token_buffer[i], tempstring, SRC_TEMPORARY_FUNCTION);
int_variable_set[num_int_variables] = TRUE;
num_int_variables++;
}
}
else if (strcmp(token_buffer[i], "FLOAT") == 0 && num_float_variables < MAX_VARIABLE)
{
i++;
if (i < token_count)
{
sprintf(tempstring, "F%d", num_float_variables);
src_add_define(token_buffer[i], tempstring, SRC_TEMPORARY_FUNCTION);
float_variable_set[num_float_variables] = TRUE;
num_float_variables++;
}
}
i++;
}
}
else
{
debug("Error: (line %d), Problem with function %s (corrupt header?)\n", sdf_read_line_number, token_buffer[i]);
error = SSE_COMPILERIZE;
}
}
// Check for local int and float declarations... Use temporary defines... I0-I31, F0-F31
if (indent == 1)
{
if (strcmp(token_buffer[0], "INT") == 0)
{
operation_line = FALSE;
i = 1;
while (i < token_count)
{
if (token_buffer[i][0] != ',' && num_int_variables < MAX_VARIABLE)
{
sprintf(tempstring, "I%d", num_int_variables);
src_add_define(token_buffer[i], tempstring, SRC_TEMPORARY_FUNCTION);
num_int_variables++;
}
i++;
}
}
else if (strcmp(token_buffer[0], "FLOAT") == 0)
{
operation_line = FALSE;
i = 1;
while (i < token_count)
{
if (token_buffer[i][0] != ',' && num_float_variables < MAX_VARIABLE)
{
sprintf(tempstring, "F%d", num_float_variables);
src_add_define(token_buffer[i], tempstring, SRC_TEMPORARY_FUNCTION);
num_float_variables++;
}
i++;
}
}
}
if (operation_line)
{
// Must have a conditional to change indentation levels...
if (indent > last_indent && last_indent != 0)
{
if (last_jump_type_found[last_indent] == SRC_JUMP_INVALID)
{
// Throw an error
debug("Error: (line %d), Must have a conditional statement to indent\n", sdf_read_line_number);
error = SSE_COMPILERIZE;
}
}
// Going down an indentation level allows us to fill in jump locations...
src_close_jumps(indent, last_indent);
// Remember where while's start, because we need to jump back to 'em...
if (strcmp(token_buffer[0], "WHILE") == 0)
{
while_jumps_back_to_here[indent] = src_buffer_used;
}
// // Going down an indentation level allows us to fill in jump locations...
// src_close_jumps(indent, last_indent);
// Moved this up a couple lines because two whiles in a row would break it...
// Fixed that problem, but something else mighta broken in the process...
// Remember indentation level for next time
last_indent = indent;
// Clear out the RPN data and fill in some of the helper arrays...
src_make_arrays(token_count, filename);
// Figure out the RPN order...
token_order = 1;
src_find_priority(0, token_count - 1, FALSE, TRUE);
// Figure out type casting based on RPN data...
src_figure_variable_types(token_order - 1);
// Write out the opcode data...
src_generate_opcodes(token_order - 1);
// Figure out where to write jump locations and stuff for indentation levels
if (error == SSE_NONE)
{
if (strcmp(token_buffer[0], "IF") == 0)
{
// Make sure there's an if opcode where it should be...
if (src_buffer_used >= 5)
{
if (src_buffer[src_buffer_used - 3] == OPCODE_IFFALSEJUMP)
{
put_jump_offset_here[indent] = src_buffer_used - 2;
last_jump_type_found[indent] = SRC_JUMP_IF;
}
else
{
debug("Error: (line %d), Praxis syllabication error\n", sdf_read_line_number);
error = SSE_COMPILERIZE;
}
}
else
{
error = SSE_COMPILERIZE;
}
}
if (strcmp(token_buffer[0], "WHILE") == 0)
{
// Make sure there's an if opcode where it should be...
if (src_buffer_used >= 5)
{
if (src_buffer[src_buffer_used - 3] == OPCODE_IFFALSEJUMP)