Debian Codegraph @

code/botlib/l_precomp.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23
24 /*****************************************************************************
25 * name: l_precomp.c
26 *
27 * desc: pre compiler
28 *
29 * $Archive: /MissionPack/code/botlib/l_precomp.c $
30 *
31 *****************************************************************************/
32
33 //Notes: fix: PC_StringizeTokens
34
35 //#define SCREWUP
36 //#define BOTLIB
37 //#define QUAKE
38 //#define QUAKEC
39 //#define MEQCC
40
41 #ifdef SCREWUP
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <limits.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <time.h>
48 #include "l_memory.h"
49 #include "l_script.h"
50 #include "l_precomp.h"
51
52 typedef enum {qfalse, qtrue} qboolean;
53 #endif //SCREWUP
54
55 #ifdef BOTLIB
56 #include "../qcommon/q_shared.h"
57 #include "botlib.h"
58 #include "be_interface.h"
59 #include "l_memory.h"
60 #include "l_script.h"
61 #include "l_precomp.h"
62 #include "l_log.h"
63 #endif //BOTLIB
64
65 #ifdef MEQCC
66 #include "qcc.h"
67 #include "time.h" //time & ctime
68 #include "math.h" //fabs
69 #include "l_memory.h"
70 #include "l_script.h"
71 #include "l_precomp.h"
72 #include "l_log.h"
73
74 #define qtrue true
75 #define qfalse false
76 #endif //MEQCC
77
78 #ifdef BSPC
79 //include files for usage in the BSP Converter
80 #include "../bspc/qbsp.h"
81 #include "../bspc/l_log.h"
82 #include "../bspc/l_mem.h"
83 #include "l_precomp.h"
84
85 #define qtrue true
86 #define qfalse false
87 #define Q_stricmp stricmp
88
89 #endif //BSPC
90
91 #if defined(QUAKE) && !defined(BSPC)
92 #include "l_utils.h"
93 #endif //QUAKE
94
95 //#define DEBUG_EVAL
96
97 #define MAX_DEFINEPARMS 128
98
99 #define DEFINEHASHING 1
100
101 //directive name with parse function
102 typedef struct directive_s
103 {
104 char *name;
105 int (*func)(source_t *source);
106 } directive_t;
107
108 #define DEFINEHASHSIZE 1024
109
110 #define TOKEN_HEAP_SIZE 4096
111
112 int numtokens;
113 /*
114 int tokenheapinitialized; //true when the token heap is initialized
115 token_t token_heap[TOKEN_HEAP_SIZE]; //heap with tokens
116 token_t *freetokens; //free tokens from the heap
117 */
118
119 //list with global defines added to every source loaded
120 define_t *globaldefines;
121
122 //============================================================================
123 //
124 // Parameter: -
125 // Returns: -
126 // Changes Globals: -
127 //============================================================================
128 void QDECL SourceError(source_t *source, char *str, ...)
129 {
130 char text[1024];
131 va_list ap;
132
133 va_start(ap, str);
134 Q_vsnprintf(text, sizeof(text), str, ap);
135 va_end(ap);
136 #ifdef BOTLIB
137 botimport.Print(PRT_ERROR, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
138 #endif //BOTLIB
139 #ifdef MEQCC
140 printf("error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
141 #endif //MEQCC
142 #ifdef BSPC
143 Log_Print("error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
144 #endif //BSPC
145 } //end of the function SourceError
146 //===========================================================================
147 //
148 // Parameter: -
149 // Returns: -
150 // Changes Globals: -
151 //===========================================================================
152 void QDECL SourceWarning(source_t *source, char *str, ...)
153 {
154 char text[1024];
155 va_list ap;
156
157 va_start(ap, str);
158 Q_vsnprintf(text, sizeof(text), str, ap);
159 va_end(ap);
160 #ifdef BOTLIB
161 botimport.Print(PRT_WARNING, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
162 #endif //BOTLIB
163 #ifdef MEQCC
164 printf("warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
165 #endif //MEQCC
166 #ifdef BSPC
167 Log_Print("warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
168 #endif //BSPC
169 } //end of the function ScriptWarning
170 //============================================================================
171 //
172 // Parameter: -
173 // Returns: -
174 // Changes Globals: -
175 //============================================================================
176 void PC_PushIndent(source_t *source, int type, int skip)
177 {
178 indent_t *indent;
179
180 indent = (indent_t *) GetMemory(sizeof(indent_t));
181 indent->type = type;
182 indent->script = source->scriptstack;
183 indent->skip = (skip != 0);
184 source->skip += indent->skip;
185 indent->next = source->indentstack;
186 source->indentstack = indent;
187 } //end of the function PC_PushIndent
188 //============================================================================
189 //
190 // Parameter: -
191 // Returns: -
192 // Changes Globals: -
193 //============================================================================
194 void PC_PopIndent(source_t *source, int *type, int *skip)
195 {
196 indent_t *indent;
197
198 *type = 0;
199 *skip = 0;
200
201 indent = source->indentstack;
202 if (!indent) return;
203
204 //must be an indent from the current script
205 if (source->indentstack->script != source->scriptstack) return;
206
207 *type = indent->type;
208 *skip = indent->skip;
209 source->indentstack = source->indentstack->next;
210 source->skip -= indent->skip;
211 FreeMemory(indent);
212 } //end of the function PC_PopIndent
213 //============================================================================
214 //
215 // Parameter: -
216 // Returns: -
217 // Changes Globals: -
218 //============================================================================
219 void PC_PushScript(source_t *source, script_t *script)
220 {
221 script_t *s;
222
223 for (s = source->scriptstack; s; s = s->next)
224 {
225 if (!Q_stricmp(s->filename, script->filename))
226 {
227 SourceError(source, "%s recursively included", script->filename);
228 return;
229 } //end if
230 } //end for
231 //push the script on the script stack
232 script->next = source->scriptstack;
233 source->scriptstack = script;
234 } //end of the function PC_PushScript
235 //============================================================================
236 //
237 // Parameter: -
238 // Returns: -
239 // Changes Globals: -
240 //============================================================================
241 void PC_InitTokenHeap(void)
242 {
243 /*
244 int i;
245
246 if (tokenheapinitialized) return;
247 freetokens = NULL;
248 for (i = 0; i < TOKEN_HEAP_SIZE; i++)
249 {
250 token_heap[i].next = freetokens;
251 freetokens = &token_heap[i];
252 } //end for
253 tokenheapinitialized = qtrue;
254 */
255 } //end of the function PC_InitTokenHeap
256 //============================================================================
257 //
258 // Parameter: -
259 // Returns: -
260 // Changes Globals: -
261 //============================================================================
262 token_t *PC_CopyToken(token_t *token)
263 {
264 token_t *t;
265
266 // t = (token_t *) malloc(sizeof(token_t));
267 t = (token_t *) GetMemory(sizeof(token_t));
268 // t = freetokens;
269 if (!t)
270 {
271 #ifdef BSPC
272 Error("out of token space");
273 #else
274 Com_Error(ERR_FATAL, "out of token space");
275 #endif
276 return NULL;
277 } //end if
278 // freetokens = freetokens->next;
279 Com_Memcpy(t, token, sizeof(token_t));
280 t->next = NULL;
281 numtokens++;
282 return t;
283 } //end of the function PC_CopyToken
284 //============================================================================
285 //
286 // Parameter: -
287 // Returns: -
288 // Changes Globals: -
289 //============================================================================
290 void PC_FreeToken(token_t *token)
291 {
292 //free(token);
293 FreeMemory(token);
294 // token->next = freetokens;
295 // freetokens = token;
296 numtokens--;
297 } //end of the function PC_FreeToken
298 //============================================================================
299 //
300 // Parameter: -
301 // Returns: -
302 // Changes Globals: -
303 //============================================================================
304 int PC_ReadSourceToken(source_t *source, token_t *token)
305 {
306 token_t *t;
307 script_t *script;
308 int type, skip;
309
310 //if there's no token already available
311 while(!source->tokens)
312 {
313 //if there's a token to read from the script
314 if (PS_ReadToken(source->scriptstack, token)) return qtrue;
315 //if at the end of the script
316 if (EndOfScript(source->scriptstack))
317 {
318 //remove all indents of the script
319 while(source->indentstack &&
320 source->indentstack->script == source->scriptstack)
321 {
322 SourceWarning(source, "missing #endif");
323 PC_PopIndent(source, &type, &skip);
324 } //end if
325 } //end if
326 //if this was the initial script
327 if (!source->scriptstack->next) return qfalse;
328 //remove the script and return to the last one
329 script = source->scriptstack;
330 source->scriptstack = source->scriptstack->next;
331 FreeScript(script);
332 } //end while
333 //copy the already available token
334 Com_Memcpy(token, source->tokens, sizeof(token_t));
335 //free the read token
336 t = source->tokens;
337 source->tokens = source->tokens->next;
338 PC_FreeToken(t);
339 return qtrue;
340 } //end of the function PC_ReadSourceToken
341 //============================================================================
342 //
343 // Parameter: -
344 // Returns: -
345 // Changes Globals: -
346 //============================================================================
347 int PC_UnreadSourceToken(source_t *source, token_t *token)
348 {
349 token_t *t;
350
351 t = PC_CopyToken(token);
352 t->next = source->tokens;
353 source->tokens = t;
354 return qtrue;
355 } //end of the function PC_UnreadSourceToken
356 //============================================================================
357 //
358 // Parameter: -
359 // Returns: -
360 // Changes Globals: -
361 //============================================================================
362 int PC_ReadDefineParms(source_t *source, define_t *define, token_t **parms, int maxparms)
363 {
364 token_t token, *t, *last;
365 int i, done, lastcomma, numparms, indent;
366
367 if (!PC_ReadSourceToken(source, &token))
368 {
369 SourceError(source, "define %s missing parms", define->name);
370 return qfalse;
371 } //end if
372 //
373 if (define->numparms > maxparms)
374 {
375 SourceError(source, "define with more than %d parameters", maxparms);
376 return qfalse;
377 } //end if
378 //
379 for (i = 0; i < define->numparms; i++) parms[i] = NULL;
380 //if no leading "("
381 if (strcmp(token.string, "("))
382 {
383 PC_UnreadSourceToken(source, &token);
384 SourceError(source, "define %s missing parms", define->name);
385 return qfalse;
386 } //end if
387 //read the define parameters
388 for (done = 0, numparms = 0, indent = 0; !done;)
389 {
390 if (numparms >= maxparms)
391 {
392 SourceError(source, "define %s with too many parms", define->name);
393 return qfalse;
394 } //end if
395 if (numparms >= define->numparms)
396 {
397 SourceWarning(source, "define %s has too many parms", define->name);
398 return qfalse;
399 } //end if
400 parms[numparms] = NULL;
401 lastcomma = 1;
402 last = NULL;
403 while(!done)
404 {
405 //
406 if (!PC_ReadSourceToken(source, &token))
407 {
408 SourceError(source, "define %s incomplete", define->name);
409 return qfalse;
410 } //end if
411 //
412 if (!strcmp(token.string, ","))
413 {
414 if (indent <= 0)
415 {
416 if (lastcomma) SourceWarning(source, "too many comma's");
417 break;
418 } //end if
419 } //end if
420 lastcomma = 0;
421 //
422 if (!strcmp(token.string, "("))
423 {
424 indent++;
425 continue;
426 } //end if
427 else if (!strcmp(token.string, ")"))
428 {
429 if (--indent <= 0)
430 {
431 if (!parms[define->numparms-1])
432 {
433 SourceWarning(source, "too few define parms");
434 } //end if
435 done = 1;
436 break;
437 } //end if
438 } //end if
439 //
440 if (numparms < define->numparms)
441 {
442 //
443 t = PC_CopyToken(&token);
444 t->next = NULL;
445 if (last) last->next = t;
446 else parms[numparms] = t;
447 last = t;
448 } //end if
449 } //end while
450 numparms++;
451 } //end for
452 return qtrue;
453 } //end of the function PC_ReadDefineParms
454 //============================================================================
455 //
456 // Parameter: -
457 // Returns: -
458 // Changes Globals: -
459 //============================================================================
460 int PC_StringizeTokens(token_t *tokens, token_t *token)
461 {
462 token_t *t;
463
464 token->type = TT_STRING;
465 token->whitespace_p = NULL;
466 token->endwhitespace_p = NULL;
467 token->string[0] = '\0';
468 strcat(token->string, "\"");
469 for (t = tokens; t; t = t->next)
470 {
471 snprintf(token->string + strlen(token->string),
472 MAX_TOKEN - strlen(token->string), "%s", t->string);
473 } //end for
474 strncat(token->string, "\"", MAX_TOKEN - strlen(token->string) - 1);
475 return qtrue;
476 } //end of the function PC_StringizeTokens
477 //============================================================================
478 //
479 // Parameter: -
480 // Returns: -
481 // Changes Globals: -
482 //============================================================================
483 int PC_MergeTokens(token_t *t1, token_t *t2)
484 {
485 //merging of a name with a name or number
486 if (t1->type == TT_NAME && (t2->type == TT_NAME || t2->type == TT_NUMBER))
487 {
488 strcat(t1->string, t2->string);
489 return qtrue;
490 } //end if
491 //merging of two strings
492 if (t1->type == TT_STRING && t2->type == TT_STRING)
493 {
494 //remove trailing double quote
495 t1->string[strlen(t1->string)-1] = '\0';
496 //concat without leading double quote
497 strcat(t1->string, &t2->string[1]);
498 return qtrue;
499 } //end if
500 //FIXME: merging of two number of the same sub type
501 return qfalse;
502 } //end of the function PC_MergeTokens
503 //============================================================================
504 //
505 // Parameter: -
506 // Returns: -
507 // Changes Globals: -
508 //============================================================================
509 /*
510 void PC_PrintDefine(define_t *define)
511 {
512 printf("define->name = %s\n", define->name);
513 printf("define->flags = %d\n", define->flags);
514 printf("define->builtin = %d\n", define->builtin);
515 printf("define->numparms = %d\n", define->numparms);
516 // token_t *parms; //define parameters
517 // token_t *tokens; //macro tokens (possibly containing parm tokens)
518 // struct define_s *next; //next defined macro in a list
519 } //end of the function PC_PrintDefine*/
520 #if DEFINEHASHING
521 //============================================================================
522 //
523 // Parameter: -
524 // Returns: -
525 // Changes Globals: -
526 //============================================================================
527 void PC_PrintDefineHashTable(define_t **definehash)
528 {
529 int i;
530 define_t *d;
531
532 for (i = 0; i < DEFINEHASHSIZE; i++)
533 {
534 Log_Write("%4d:", i);
535 for (d = definehash[i]; d; d = d->hashnext)
536 {
537 Log_Write(" %s", d->name);
538 } //end for
539 Log_Write("\n");
540 } //end for
541 } //end of the function PC_PrintDefineHashTable
542 //============================================================================
543 //
544 // Parameter: -
545 // Returns: -
546 // Changes Globals: -
547 //============================================================================
548 //char primes[16] = {1, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41, 43, 47};
549
550 int PC_NameHash(char *name)
551 {
552 int hash, i;
553
554 hash = 0;
555 for (i = 0; name[i] != '\0'; i++)
556 {
557 hash += name[i] * (119 + i);
558 //hash += (name[i] << 7) + i;
559 //hash += (name[i] << (i&15));
560 } //end while
561 hash = (hash ^ (hash >> 10) ^ (hash >> 20)) & (DEFINEHASHSIZE-1);
562 return hash;
563 } //end of the function PC_NameHash
564 //============================================================================
565 //
566 // Parameter: -
567 // Returns: -
568 // Changes Globals: -
569 //============================================================================
570 void PC_AddDefineToHash(define_t *define, define_t **definehash)
571 {
572 int hash;
573
574 hash = PC_NameHash(define->name);
575 define->hashnext = definehash[hash];
576 definehash[hash] = define;
577 } //end of the function PC_AddDefineToHash
578 //============================================================================
579 //
580 // Parameter: -
581 // Returns: -
582 // Changes Globals: -
583 //============================================================================
584 define_t *PC_FindHashedDefine(define_t **definehash, char *name)
585 {
586 define_t *d;
587 int hash;
588
589 hash = PC_NameHash(name);
590 for (d = definehash[hash]; d; d = d->hashnext)
591 {
592 if (!strcmp(d->name, name)) return d;
593 } //end for
594 return NULL;
595 } //end of the function PC_FindHashedDefine
596 #endif //DEFINEHASHING
597 //============================================================================
598 //
599 // Parameter: -
600 // Returns: -
601 // Changes Globals: -
602 //============================================================================
603 define_t *PC_FindDefine(define_t *defines, char *name)
604 {
605 define_t *d;
606
607 for (d = defines; d; d = d->next)
608 {
609 if (!strcmp(d->name, name)) return d;
610 } //end for
611 return NULL;
612 } //end of the function PC_FindDefine
613 //============================================================================
614 //
615 // Parameter: -
616 // Returns: number of the parm
617 // if no parm found with the given name -1 is returned
618 // Changes Globals: -
619 //============================================================================
620 int PC_FindDefineParm(define_t *define, char *name)
621 {
622 token_t *p;
623 int i;
624
625 i = 0;
626 for (p = define->parms; p; p = p->next)
627 {
628 if (!strcmp(p->string, name)) return i;
629 i++;
630 } //end for
631 return -1;
632 } //end of the function PC_FindDefineParm
633 //============================================================================
634 //
635 // Parameter: -
636 // Returns: -
637 // Changes Globals: -
638 //============================================================================
639 void PC_FreeDefine(define_t *define)
640 {
641 token_t *t, *next;
642
643 //free the define parameters
644 for (t = define->parms; t; t = next)
645 {
646 next = t->next;
647 PC_FreeToken(t);
648 } //end for
649 //free the define tokens
650 for (t = define->tokens; t; t = next)
651 {
652 next = t->next;
653 PC_FreeToken(t);
654 } //end for
655 //free the define
656 FreeMemory(define->name);
657 FreeMemory(define);
658 } //end of the function PC_FreeDefine
659 //============================================================================
660 //
661 // Parameter: -
662 // Returns: -
663 // Changes Globals: -
664 //============================================================================
665 void PC_AddBuiltinDefines(source_t *source)
666 {
667 int i;
668 define_t *define;
669 struct builtin
670 {
671 char *string;
672 int builtin;
673 } builtin[] = {
674 { "__LINE__", BUILTIN_LINE },
675 { "__FILE__", BUILTIN_FILE },
676 { "__DATE__", BUILTIN_DATE },
677 { "__TIME__", BUILTIN_TIME },
678 // { "__STDC__", BUILTIN_STDC },
679 { NULL, 0 }
680 };
681
682 for (i = 0; builtin[i].string; i++)
683 {
684 define = (define_t *) GetMemory(sizeof(define_t));
685 Com_Memset(define, 0, sizeof(define_t));
686 define->name = (char *) GetMemory(strlen(builtin[i].string) + 1);
687 strcpy(define->name, builtin[i].string);
688 define->flags |= DEFINE_FIXED;
689 define->builtin = builtin[i].builtin;
690 //add the define to the source
691 #if DEFINEHASHING
692 PC_AddDefineToHash(define, source->definehash);
693 #else
694 define->next = source->defines;
695 source->defines = define;
696 #endif //DEFINEHASHING
697 } //end for
698 } //end of the function PC_AddBuiltinDefines
699 //============================================================================
700 //
701 // Parameter: -
702 // Returns: -
703 // Changes Globals: -
704 //============================================================================
705 int PC_ExpandBuiltinDefine(source_t *source, token_t *deftoken, define_t *define,
706 token_t **firsttoken, token_t **lasttoken)
707 {
708 token_t *token;
709 time_t t;
710
711 char *curtime;
712
713 token = PC_CopyToken(deftoken);
714 switch(define->builtin)
715 {
716 case BUILTIN_LINE:
717 {
718 sprintf(token->string, "%d", deftoken->line);
719 #ifdef NUMBERVALUE
720 token->intvalue = deftoken->line;
721 token->floatvalue = deftoken->line;
722 #endif //NUMBERVALUE
723 token->type = TT_NUMBER;
724 token->subtype = TT_DECIMAL | TT_INTEGER;
725 *firsttoken = token;
726 *lasttoken = token;
727 break;
728 } //end case
729 case BUILTIN_FILE:
730 {
731 strcpy(token->string, source->scriptstack->filename);
732 token->type = TT_NAME;
733 token->subtype = strlen(token->string);
734 *firsttoken = token;
735 *lasttoken = token;
736 break;
737 } //end case
738 case BUILTIN_DATE:
739 {
740 t = time(NULL);
741 curtime = ctime(&t);
742 strcpy(token->string, "\"");
743 strncat(token->string, curtime+4, 7);
744 strncat(token->string+7, curtime+20, 4);
745 strcat(token->string, "\"");
746 free(curtime);
747 token->type = TT_NAME;
748 token->subtype = strlen(token->string);
749 *firsttoken = token;
750 *lasttoken = token;
751 break;
752 } //end case
753 case BUILTIN_TIME:
754 {
755 t = time(NULL);
756 curtime = ctime(&t);
757 strcpy(token->string, "\"");
758 strncat(token->string, curtime+11, 8);
759 strcat(token->string, "\"");
760 free(curtime);
761 token->type = TT_NAME;
762 token->subtype = strlen(token->string);
763 *firsttoken = token;
764 *lasttoken = token;
765 break;
766 } //end case
767 case BUILTIN_STDC:
768 default:
769 {
770 *firsttoken = NULL;
771 *lasttoken = NULL;
772 break;
773 } //end case
774 } //end switch
775 return qtrue;
776 } //end of the function PC_ExpandBuiltinDefine
777 //============================================================================
778 //
779 // Parameter: -
780 // Returns: -
781 // Changes Globals: -
782 //============================================================================
783 int PC_ExpandDefine(source_t *source, token_t *deftoken, define_t *define,
784 token_t **firsttoken, token_t **lasttoken)
785 {
786 token_t *parms[MAX_DEFINEPARMS] = { NULL }, *dt, *pt, *t;
787 token_t *t1, *t2, *first, *last, *nextpt, token;
788 int parmnum, i;
789
790 //if it is a builtin define
791 if (define->builtin)
792 {
793 return PC_ExpandBuiltinDefine(source, deftoken, define, firsttoken, lasttoken);
794 } //end if
795 //if the define has parameters
796 if (define->numparms)
797 {
798 if (!PC_ReadDefineParms(source, define, parms, MAX_DEFINEPARMS)) return qfalse;
799 #ifdef DEBUG_EVAL
800 for (i = 0; i < define->numparms; i++)
801 {
802 Log_Write("define parms %d:", i);
803 for (pt = parms[i]; pt; pt = pt->next)
804 {
805 Log_Write("%s", pt->string);
806 } //end for
807 } //end for
808 #endif //DEBUG_EVAL
809 } //end if
810 //empty list at first
811 first = NULL;
812 last = NULL;
813 //create a list with tokens of the expanded define
814 for (dt = define->tokens; dt; dt = dt->next)
815 {
816 parmnum = -1;
817 //if the token is a name, it could be a define parameter
818 if (dt->type == TT_NAME)
819 {
820 parmnum = PC_FindDefineParm(define, dt->string);
821 } //end if
822 //if it is a define parameter
823 if (parmnum >= 0)
824 {
825 for (pt = parms[parmnum]; pt; pt = pt->next)
826 {
827 t = PC_CopyToken(pt);
828 //add the token to the list
829 t->next = NULL;
830 if (last) last->next = t;
831 else first = t;
832 last = t;
833 } //end for
834 } //end if
835 else
836 {
837 //if stringizing operator
838 if (dt->string[0] == '#' && dt->string[1] == '\0')
839 {
840 //the stringizing operator must be followed by a define parameter
841 if (dt->next) parmnum = PC_FindDefineParm(define, dt->next->string);
842 else parmnum = -1;
843 //
844 if (parmnum >= 0)
845 {
846 //step over the stringizing operator
847 dt = dt->next;
848 //stringize the define parameter tokens
849 if (!PC_StringizeTokens(parms[parmnum], &token))
850 {
851 SourceError(source, "can't stringize tokens");
852 return qfalse;
853 } //end if
854 t = PC_CopyToken(&token);
855 } //end if
856 else
857 {
858 SourceWarning(source, "stringizing operator without define parameter");
859 continue;
860 } //end if
861 } //end if
862 else
863 {
864 t = PC_CopyToken(dt);
865 } //end else
866 //add the token to the list
867 t->next = NULL;
868 if (last) last->next = t;
869 else first = t;
870 last = t;
871 } //end else
872 } //end for
873 //check for the merging operator
874 for (t = first; t; )
875 {
876 if (t->next)
877 {
878 //if the merging operator
879 if (t->next->string[0] == '#' && t->next->string[1] == '#')
880 {
881 t1 = t;
882 t2 = t->next->next;
883 if (t2)
884 {
885 if (!PC_MergeTokens(t1, t2))
886 {
887 SourceError(source, "can't merge %s with %s", t1->string, t2->string);
888 return qfalse;
889 } //end if
890 PC_FreeToken(t1->next);
891 t1->next = t2->next;
892 if (t2 == last) last = t1;
893 PC_FreeToken(t2);
894 continue;
895 } //end if
896 } //end if
897 } //end if
898 t = t->next;
899 } //end for
900 //store the first and last token of the list
901 *firsttoken = first;
902 *lasttoken = last;
903 //free all the parameter tokens
904 for (i = 0; i < define->numparms; i++)
905 {
906 for (pt = parms[i]; pt; pt = nextpt)
907 {
908 nextpt = pt->next;
909 PC_FreeToken(pt);
910 } //end for
911 } //end for
912 //
913 return qtrue;
914 } //end of the function PC_ExpandDefine
915 //============================================================================
916 //
917 // Parameter: -
918 // Returns: -
919 // Changes Globals: -
920 //============================================================================
921 int PC_ExpandDefineIntoSource(source_t *source, token_t *deftoken, define_t *define)
922 {
923 token_t *firsttoken, *lasttoken;
924
925 if (!PC_ExpandDefine(source, deftoken, define, &firsttoken, &lasttoken)) return qfalse;
926
927 if (firsttoken && lasttoken)
928 {
929 lasttoken->next = source->tokens;
930 source->tokens = firsttoken;
931 return qtrue;
932 } //end if
933 return qfalse;
934 } //end of the function PC_ExpandDefineIntoSource
935 //============================================================================
936 //
937 // Parameter: -
938 // Returns: -
939 // Changes Globals: -
940 //============================================================================
941 void PC_ConvertPath(char *path)
942 {
943 char *ptr;
944
945 //remove double path seperators
946 for (ptr = path; *ptr;)
947 {
948 if ((*ptr == '\\' || *ptr == '/') &&
949 (*(ptr+1) == '\\' || *(ptr+1) == '/'))
950 {
951 memmove(ptr, ptr+1, strlen(ptr));
952 } //end if
953 else
954 {
955 ptr++;
956 } //end else
957 } //end while
958 //set OS dependent path seperators
959 for (ptr = path; *ptr;)
960 {
961 if (*ptr == '/' || *ptr == '\\') *ptr = PATHSEPERATOR_CHAR;
962 ptr++;
963 } //end while
964 } //end of the function PC_ConvertPath
965 //============================================================================
966 //
967 // Parameter: -
968 // Returns: -
969 // Changes Globals: -
970 //============================================================================
971 int PC_Directive_include(source_t *source)
972 {
973 script_t *script;
974 token_t token;
975 char path[MAX_QPATH];
976 #ifdef QUAKE
977 foundfile_t file;
978 #endif //QUAKE
979
980 if (source->skip > 0) return qtrue;
981 //
982 if (!PC_ReadSourceToken(source, &token))
983 {
984 SourceError(source, "#include without file name");
985 return qfalse;
986 } //end if
987 if (token.linescrossed > 0)
988 {
989 SourceError(source, "#include without file name");
990 return qfalse;
991 } //end if
992 if (token.type == TT_STRING)
993 {
994 StripDoubleQuotes(token.string);
995 PC_ConvertPath(token.string);
996 script = LoadScriptFile(token.string);
997 if (!script)
998 {
999 Q_strncpyz(path, source->includepath, sizeof(path));
1000 Q_strcat(path, sizeof(path), token.string);
1001 script = LoadScriptFile(path);
1002 } //end if
1003 } //end if
1004 else if (token.type == TT_PUNCTUATION && *token.string == '<')
1005 {
1006 Q_strncpyz(path, source->includepath, sizeof(path));
1007 while(PC_ReadSourceToken(source, &token))
1008 {
1009 if (token.linescrossed > 0)
1010 {
1011 PC_UnreadSourceToken(source, &token);
1012 break;
1013 } //end if
1014 if (token.type == TT_PUNCTUATION && *token.string == '>') break;
1015 Q_strcat(path, sizeof(path), token.string);
1016 } //end while
1017 if (*token.string != '>')
1018 {
1019 SourceWarning(source, "#include missing trailing >");
1020 } //end if
1021 if (!strlen(path))
1022 {
1023 SourceError(source, "#include without file name between < >");
1024 return qfalse;
1025 } //end if
1026 PC_ConvertPath(path);
1027 script = LoadScriptFile(path);
1028 } //end if
1029 else
1030 {
1031 SourceError(source, "#include without file name");
1032 return qfalse;
1033 } //end else
1034 #ifdef QUAKE
1035 if (!script)
1036 {
1037 Com_Memset(&file, 0, sizeof(foundfile_t));
1038 script = LoadScriptFile(path);
1039 if (script) Q_strncpyz(script->filename, path, sizeof(script->filename));
1040 } //end if
1041 #endif //QUAKE
1042 if (!script)
1043 {
1044 #ifdef SCREWUP
1045 SourceWarning(source, "file %s not found", path);
1046 return qtrue;
1047 #else
1048 SourceError(source, "file %s not found", path);
1049 return qfalse;
1050 #endif //SCREWUP
1051 } //end if
1052 PC_PushScript(source, script);
1053 return qtrue;
1054 } //end of the function PC_Directive_include
1055 //============================================================================
1056 // reads a token from the current line, continues reading on the next
1057 // line only if a backslash '\' is encountered.
1058 //
1059 // Parameter: -
1060 // Returns: -
1061 // Changes Globals: -
1062 //============================================================================
1063 int PC_ReadLine(source_t *source, token_t *token)
1064 {
1065 int crossline;
1066
1067 crossline = 0;
1068 do
1069 {
1070 if (!PC_ReadSourceToken(source, token)) return qfalse;
1071
1072 if (token->linescrossed > crossline)
1073 {
1074 PC_UnreadSourceToken(source, token);
1075 return qfalse;
1076 } //end if
1077 crossline = 1;
1078 } while(!strcmp(token->string, "\\"));
1079 return qtrue;
1080 } //end of the function PC_ReadLine
1081 //============================================================================
1082 //
1083 // Parameter: -
1084 // Returns: -
1085 // Changes Globals: -
1086 //============================================================================
1087 int PC_WhiteSpaceBeforeToken(token_t *token)
1088 {
1089 return token->endwhitespace_p - token->whitespace_p > 0;
1090 } //end of the function PC_WhiteSpaceBeforeToken
1091 //============================================================================
1092 //
1093 // Parameter: -
1094 // Returns: -
1095 // Changes Globals: -
1096 //============================================================================
1097 void PC_ClearTokenWhiteSpace(token_t *token)
1098 {
1099 token->whitespace_p = NULL;
1100 token->endwhitespace_p = NULL;
1101 token->linescrossed = 0;
1102 } //end of the function PC_ClearTokenWhiteSpace
1103 //============================================================================
1104 //
1105 // Parameter: -
1106 // Returns: -
1107 // Changes Globals: -
1108 //============================================================================
1109 int PC_Directive_undef(source_t *source)
1110 {
1111 token_t token;
1112 define_t *define, *lastdefine;
1113 int hash;
1114
1115 if (source->skip > 0) return qtrue;
1116 //
1117 if (!PC_ReadLine(source, &token))
1118 {
1119 SourceError(source, "undef without name");
1120 return qfalse;
1121 } //end if
1122 if (token.type != TT_NAME)
1123 {
1124 PC_UnreadSourceToken(source, &token);
1125 SourceError(source, "expected name, found %s", token.string);
1126 return qfalse;
1127 } //end if
1128 #if DEFINEHASHING
1129
1130 hash = PC_NameHash(token.string);
1131 for (lastdefine = NULL, define = source->definehash[hash]; define; define = define->hashnext)
1132 {
1133 if (!strcmp(define->name, token.string))
1134 {
1135 if (define->flags & DEFINE_FIXED)
1136 {
1137 SourceWarning(source, "can't undef %s", token.string);
1138 } //end if
1139 else
1140 {
1141 if (lastdefine) lastdefine->hashnext = define->hashnext;
1142 else source->definehash[hash] = define->hashnext;
1143 PC_FreeDefine(define);
1144 } //end else
1145 break;
1146 } //end if
1147 lastdefine = define;
1148 } //end for
1149 #else //DEFINEHASHING
1150 for (lastdefine = NULL, define = source->defines; define; define = define->next)
1151 {
1152 if (!strcmp(define->name, token.string))
1153 {
1154 if (define->flags & DEFINE_FIXED)
1155 {
1156 SourceWarning(source, "can't undef %s", token.string);
1157 } //end if
1158 else
1159 {
1160 if (lastdefine) lastdefine->next = define->next;
1161 else source->defines = define->next;
1162 PC_FreeDefine(define);
1163 } //end else
1164 break;
1165 } //end if
1166 lastdefine = define;
1167 } //end for
1168 #endif //DEFINEHASHING
1169 return qtrue;
1170 } //end of the function PC_Directive_undef
1171 //============================================================================
1172 //
1173 // Parameter: -
1174 // Returns: -
1175 // Changes Globals: -
1176 //============================================================================
1177 int PC_Directive_define(source_t *source)
1178 {
1179 token_t token, *t, *last;
1180 define_t *define;
1181
1182 if (source->skip > 0) return qtrue;
1183 //
1184 if (!PC_ReadLine(source, &token))
1185 {
1186 SourceError(source, "#define without name");
1187 return qfalse;
1188 } //end if
1189 if (token.type != TT_NAME)
1190 {
1191 PC_UnreadSourceToken(source, &token);
1192 SourceError(source, "expected name after #define, found %s", token.string);
1193 return qfalse;
1194 } //end if
1195 //check if the define already exists
1196 #if DEFINEHASHING
1197 define = PC_FindHashedDefine(source->definehash, token.string);
1198 #else
1199 define = PC_FindDefine(source->defines, token.string);
1200 #endif //DEFINEHASHING
1201 if (define)
1202 {
1203 if (define->flags & DEFINE_FIXED)
1204 {
1205 SourceError(source, "can't redefine %s", token.string);
1206 return qfalse;
1207 } //end if
1208 SourceWarning(source, "redefinition of %s", token.string);
1209 //unread the define name before executing the #undef directive
1210 PC_UnreadSourceToken(source, &token);
1211 if (!PC_Directive_undef(source)) return qfalse;
1212 } //end if
1213 //allocate define
1214 define = (define_t *) GetMemory(sizeof(define_t));
1215 Com_Memset(define, 0, sizeof(define_t));
1216 define->name = (char *) GetMemory(strlen(token.string) + 1);
1217 strcpy(define->name, token.string);
1218 //add the define to the source
1219 #if DEFINEHASHING
1220 PC_AddDefineToHash(define, source->definehash);
1221 #else //DEFINEHASHING
1222 define->next = source->defines;
1223 source->defines = define;
1224 #endif //DEFINEHASHING
1225 //if nothing is defined, just return
1226 if (!PC_ReadLine(source, &token)) return qtrue;
1227 //if it is a define with parameters
1228 if (!PC_WhiteSpaceBeforeToken(&token) && !strcmp(token.string, "("))
1229 {
1230 //read the define parameters
1231 last = NULL;
1232 if (!PC_CheckTokenString(source, ")"))
1233 {
1234 while(1)
1235 {
1236 if (!PC_ReadLine(source, &token))
1237 {
1238 SourceError(source, "expected define parameter");
1239 return qfalse;
1240 } //end if
1241 //if it isn't a name
1242 if (token.type != TT_NAME)
1243 {
1244 SourceError(source, "invalid define parameter");
1245 return qfalse;
1246 } //end if
1247 //
1248 if (PC_FindDefineParm(define, token.string) >= 0)
1249 {
1250 SourceError(source, "two the same define parameters");
1251 return qfalse;
1252 } //end if
1253 //add the define parm
1254 t = PC_CopyToken(&token);
1255 PC_ClearTokenWhiteSpace(t);
1256 t->next = NULL;
1257 if (last) last->next = t;
1258 else define->parms = t;
1259 last = t;
1260 define->numparms++;
1261 //read next token
1262 if (!PC_ReadLine(source, &token))
1263 {
1264 SourceError(source, "define parameters not terminated");
1265 return qfalse;
1266 } //end if
1267 //
1268 if (!strcmp(token.string, ")")) break;
1269 //then it must be a comma
1270 if (strcmp(token.string, ","))
1271 {
1272 SourceError(source, "define not terminated");
1273 return qfalse;
1274 } //end if
1275 } //end while
1276 } //end if
1277 if (!PC_ReadLine(source, &token)) return qtrue;
1278 } //end if
1279 //read the defined stuff
1280 last = NULL;
1281 do
1282 {
1283 t = PC_CopyToken(&token);
1284 if (t->type == TT_NAME && !strcmp(t->string, define->name))
1285 {
1286 SourceError(source, "recursive define (removed recursion)");
1287 continue;
1288 } //end if
1289 PC_ClearTokenWhiteSpace(t);
1290 t->next = NULL;
1291 if (last) last->next = t;
1292 else define->tokens = t;
1293 last = t;
1294 } while(PC_ReadLine(source, &token));
1295 //
1296 if (last)
1297 {
1298 //check for merge operators at the beginning or end
1299 if (!strcmp(define->tokens->string, "##") ||
1300 !strcmp(last->string, "##"))
1301 {
1302 SourceError(source, "define with misplaced ##");
1303 return qfalse;
1304 } //end if
1305 } //end if
1306 return qtrue;
1307 } //end of the function PC_Directive_define
1308 //============================================================================
1309 //
1310 // Parameter: -
1311 // Returns: -
1312 // Changes Globals: -
1313 //============================================================================
1314 define_t *PC_DefineFromString(char *string)
1315 {
1316 script_t *script;
1317 source_t src;
1318 token_t *t;
1319 int res, i;
1320 define_t *def;
1321
1322 PC_InitTokenHeap();
1323
1324 script = LoadScriptMemory(string, strlen(string), "*extern");
1325 //create a new source
1326 Com_Memset(&src, 0, sizeof(source_t));
1327 Q_strncpyz(src.filename, "*extern", sizeof(src.filename));
1328 src.scriptstack = script;
1329 #if DEFINEHASHING
1330 src.definehash = GetClearedMemory(DEFINEHASHSIZE * sizeof(define_t *));
1331 #endif //DEFINEHASHING
1332 //create a define from the source
1333 res = PC_Directive_define(&src);
1334 //free any tokens if left
1335 for (t = src.tokens; t; t = src.tokens)
1336 {
1337 src.tokens = src.tokens->next;
1338 PC_FreeToken(t);
1339 } //end for
1340 #ifdef DEFINEHASHING
1341 def = NULL;
1342 for (i = 0; i < DEFINEHASHSIZE; i++)
1343 {
1344 if (src.definehash[i])
1345 {
1346 def = src.definehash[i];
1347 break;
1348 } //end if
1349 } //end for
1350 #else
1351 def = src.defines;
1352 #endif //DEFINEHASHING
1353 //
1354 #if DEFINEHASHING
1355 FreeMemory(src.definehash);
1356 #endif //DEFINEHASHING
1357 //
1358 FreeScript(script);
1359 //if the define was created successfully
1360 if (res > 0) return def;
1361 //free the define is created
1362 if (src.defines) PC_FreeDefine(def);
1363 //
1364 return NULL;
1365 } //end of the function PC_DefineFromString
1366 //============================================================================
1367 //
1368 // Parameter: -
1369 // Returns: -
1370 // Changes Globals: -
1371 //============================================================================
1372 int PC_AddDefine(source_t *source, char *string)
1373 {
1374 define_t *define;
1375
1376 define = PC_DefineFromString(string);
1377 if (!define) return qfalse;
1378 #if DEFINEHASHING
1379 PC_AddDefineToHash(define, source->definehash);
1380 #else //DEFINEHASHING
1381 define->next = source->defines;
1382 source->defines = define;
1383 #endif //DEFINEHASHING
1384 return qtrue;
1385 } //end of the function PC_AddDefine
1386 //============================================================================
1387 // add a globals define that will be added to all opened sources
1388 //
1389 // Parameter: -
1390 // Returns: -
1391 // Changes Globals: -
1392 //============================================================================
1393 int PC_AddGlobalDefine(char *string)
1394 {
1395 define_t *define;
1396
1397 define = PC_DefineFromString(string);
1398 if (!define) return qfalse;
1399 define->next = globaldefines;
1400 globaldefines = define;
1401 return qtrue;
1402 } //end of the function PC_AddGlobalDefine
1403 //============================================================================
1404 // remove the given global define
1405 //
1406 // Parameter: -
1407 // Returns: -
1408 // Changes Globals: -
1409 //============================================================================
1410 int PC_RemoveGlobalDefine(char *name)
1411 {
1412 define_t *define;
1413
1414 define = PC_FindDefine(globaldefines, name);
1415 if (define)
1416 {
1417 PC_FreeDefine(define);
1418 return qtrue;
1419 } //end if
1420 return qfalse;
1421 } //end of the function PC_RemoveGlobalDefine
1422 //============================================================================
1423 // remove all globals defines
1424 //
1425 // Parameter: -
1426 // Returns: -
1427 // Changes Globals: -
1428 //============================================================================
1429 void PC_RemoveAllGlobalDefines(void)
1430 {
1431 define_t *define;
1432
1433 for (define = globaldefines; define; define = globaldefines)
1434 {
1435 globaldefines = globaldefines->next;
1436 PC_FreeDefine(define);
1437 } //end for
1438 } //end of the function PC_RemoveAllGlobalDefines
1439 //============================================================================
1440 //
1441 // Parameter: -
1442 // Returns: -
1443 // Changes Globals: -
1444 //============================================================================
1445 define_t *PC_CopyDefine(source_t *source, define_t *define)
1446 {
1447 define_t *newdefine;
1448 token_t *token, *newtoken, *lasttoken;
1449
1450 newdefine = (define_t *) GetMemory(sizeof(define_t));
1451 //copy the define name
1452 newdefine->name = (char *) GetMemory(strlen(define->name) + 1);
1453 strcpy(newdefine->name, define->name);
1454 newdefine->flags = define->flags;
1455 newdefine->builtin = define->builtin;
1456 newdefine->numparms = define->numparms;
1457 //the define is not linked
1458 newdefine->next = NULL;
1459 newdefine->hashnext = NULL;
1460 //copy the define tokens
1461 newdefine->tokens = NULL;
1462 for (lasttoken = NULL, token = define->tokens; token; token = token->next)
1463 {
1464 newtoken = PC_CopyToken(token);
1465 newtoken->next = NULL;
1466 if (lasttoken) lasttoken->next = newtoken;
1467 else newdefine->tokens = newtoken;
1468 lasttoken = newtoken;
1469 } //end for
1470 //copy the define parameters
1471 newdefine->parms = NULL;
1472 for (lasttoken = NULL, token = define->parms; token; token = token->next)
1473 {
1474 newtoken = PC_CopyToken(token);
1475 newtoken->next = NULL;
1476 if (lasttoken) lasttoken->next = newtoken;
1477 else newdefine->parms = newtoken;
1478 lasttoken = newtoken;
1479 } //end for
1480 return newdefine;
1481 } //end of the function PC_CopyDefine
1482 //============================================================================
1483 //
1484 // Parameter: -
1485 // Returns: -
1486 // Changes Globals: -
1487 //============================================================================
1488 void PC_AddGlobalDefinesToSource(source_t *source)
1489 {
1490 define_t *define, *newdefine;
1491
1492 for (define = globaldefines; define; define = define->next)
1493 {
1494 newdefine = PC_CopyDefine(source, define);
1495 #if DEFINEHASHING
1496 PC_AddDefineToHash(newdefine, source->definehash);
1497 #else //DEFINEHASHING
1498 newdefine->next = source->defines;
1499 source->defines = newdefine;
1500 #endif //DEFINEHASHING
1501 } //end for
1502 } //end of the function PC_AddGlobalDefinesToSource
1503 //============================================================================
1504 //
1505 // Parameter: -
1506 // Returns: -
1507 // Changes Globals: -
1508 //============================================================================
1509 int PC_Directive_if_def(source_t *source, int type)
1510 {
1511 token_t token;
1512 define_t *d;
1513 int skip;
1514
1515 if (!PC_ReadLine(source, &token))
1516 {
1517 SourceError(source, "#ifdef without name");
1518 return qfalse;
1519 } //end if
1520 if (token.type != TT_NAME)
1521 {
1522 PC_UnreadSourceToken(source, &token);
1523 SourceError(source, "expected name after #ifdef, found %s", token.string);
1524 return qfalse;
1525 } //end if
1526 #if DEFINEHASHING
1527 d = PC_FindHashedDefine(source->definehash, token.string);
1528 #else
1529 d = PC_FindDefine(source->defines, token.string);
1530 #endif //DEFINEHASHING
1531 skip = (type == INDENT_IFDEF) == (d == NULL);
1532 PC_PushIndent(source, type, skip);
1533 return qtrue;
1534 } //end of the function PC_Directiveif_def
1535 //============================================================================
1536 //
1537 // Parameter: -
1538 // Returns: -
1539 // Changes Globals: -
1540 //============================================================================
1541 int PC_Directive_ifdef(source_t *source)
1542 {
1543 return PC_Directive_if_def(source, INDENT_IFDEF);
1544 } //end of the function PC_Directive_ifdef
1545 //============================================================================
1546 //
1547 // Parameter: -
1548 // Returns: -
1549 // Changes Globals: -
1550 //============================================================================
1551 int PC_Directive_ifndef(source_t *source)
1552 {
1553 return PC_Directive_if_def(source, INDENT_IFNDEF);
1554 } //end of the function PC_Directive_ifndef
1555 //============================================================================
1556 //
1557 // Parameter: -
1558 // Returns: -
1559 // Changes Globals: -
1560 //============================================================================
1561 int PC_Directive_else(source_t *source)
1562 {
1563 int type, skip;
1564
1565 PC_PopIndent(source, &type, &skip);
1566 if (!type)
1567 {
1568 SourceError(source, "misplaced #else");
1569 return qfalse;
1570 } //end if
1571 if (type == INDENT_ELSE)
1572 {
1573 SourceError(source, "#else after #else");
1574 return qfalse;
1575 } //end if
1576 PC_PushIndent(source, INDENT_ELSE, !skip);
1577 return qtrue;
1578 } //end of the function PC_Directive_else
1579 //============================================================================
1580 //
1581 // Parameter: -
1582 // Returns: -
1583 // Changes Globals: -
1584 //============================================================================
1585 int PC_Directive_endif(source_t *source)
1586 {
1587 int type, skip;
1588
1589 PC_PopIndent(source, &type, &skip);
1590 if (!type)
1591 {
1592 SourceError(source, "misplaced #endif");
1593 return qfalse;
1594 } //end if
1595 return qtrue;
1596 } //end of the function PC_Directive_endif
1597 //============================================================================
1598 //
1599 // Parameter: -
1600 // Returns: -
1601 // Changes Globals: -
1602 //============================================================================
1603 typedef struct operator_s
1604 {
1605 int operator;
1606 int priority;
1607 int parentheses;
1608 struct operator_s *prev, *next;
1609 } operator_t;
1610
1611 typedef struct value_s
1612 {
1613 signed long int intvalue;
1614 float floatvalue;
1615 int parentheses;
1616 struct value_s *prev, *next;
1617 } value_t;
1618
1619 int PC_OperatorPriority(int op)
1620 {
1621 switch(op)
1622 {
1623 case P_MUL: return 15;
1624 case P_DIV: return 15;
1625 case P_MOD: return 15;
1626 case P_ADD: return 14;
1627 case P_SUB: return 14;
1628
1629 case P_LOGIC_AND: return 7;
1630 case P_LOGIC_OR: return 6;
1631 case P_LOGIC_GEQ: return 12;
1632 case P_LOGIC_LEQ: return 12;
1633 case P_LOGIC_EQ: return 11;
1634 case P_LOGIC_UNEQ: return 11;
1635
1636 case P_LOGIC_NOT: return 16;
1637 case P_LOGIC_GREATER: return 12;
1638 case P_LOGIC_LESS: return 12;
1639
1640 case P_RSHIFT: return 13;
1641 case P_LSHIFT: return 13;
1642
1643 case P_BIN_AND: return 10;
1644 case P_BIN_OR: return 8;
1645 case P_BIN_XOR: return 9;
1646 case P_BIN_NOT: return 16;
1647
1648 case P_COLON: return 5;
1649 case P_QUESTIONMARK: return 5;
1650 } //end switch
1651 return qfalse;
1652 } //end of the function PC_OperatorPriority
1653
1654 //#define AllocValue() GetClearedMemory(sizeof(value_t));
1655 //#define FreeValue(val) FreeMemory(val)
1656 //#define AllocOperator(op) op = (operator_t *) GetClearedMemory(sizeof(operator_t));
1657 //#define FreeOperator(op) FreeMemory(op);
1658
1659 #define MAX_VALUES 64
1660 #define MAX_OPERATORS 64
1661 #define AllocValue(val) \
1662 if (numvalues >= MAX_VALUES) { \
1663 SourceError(source, "out of value space"); \
1664 error = 1; \
1665 break; \
1666 } \
1667 else \
1668 val = &value_heap[numvalues++];
1669 #define FreeValue(val)
1670 //
1671 #define AllocOperator(op) \
1672 if (numoperators >= MAX_OPERATORS) { \
1673 SourceError(source, "out of operator space"); \
1674 error = 1; \
1675 break; \
1676 } \
1677 else \
1678 op = &operator_heap[numoperators++];
1679 #define FreeOperator(op)
1680
1681 int PC_EvaluateTokens(source_t *source, token_t *tokens, signed long int *intvalue,
1682 float *floatvalue, int integer)
1683 {
1684 operator_t *o, *firstoperator, *lastoperator;
1685 value_t *v, *firstvalue, *lastvalue, *v1, *v2;
1686 token_t *t;
1687 int brace = 0;
1688 int parentheses = 0;
1689 int error = 0;
1690 int lastwasvalue = 0;
1691 int negativevalue = 0;
1692 int questmarkintvalue = 0;
1693 float questmarkfloatvalue = 0;
1694 int gotquestmarkvalue = qfalse;
1695 //
1696 operator_t operator_heap[MAX_OPERATORS];
1697 int numoperators = 0;
1698 value_t value_heap[MAX_VALUES];
1699 int numvalues = 0;
1700
1701 firstoperator = lastoperator = NULL;
1702 firstvalue = lastvalue = NULL;
1703 if (intvalue) *intvalue = 0;
1704 if (floatvalue) *floatvalue = 0;
1705 for (t = tokens; t; t = t->next)
1706 {
1707 switch(t->type)
1708 {
1709 case TT_NAME:
1710 {
1711 if (lastwasvalue || negativevalue)
1712 {
1713 SourceError(source, "syntax error in #if/#elif");
1714 error = 1;
1715 break;
1716 } //end if
1717 if (strcmp(t->string, "defined"))
1718 {
1719 SourceError(source, "undefined name %s in #if/#elif", t->string);
1720 error = 1;
1721 break;
1722 } //end if
1723 t = t->next;
1724 if (!strcmp(t->string, "("))
1725 {
1726 brace = qtrue;
1727 t = t->next;
1728 } //end if
1729 if (!t || t->type != TT_NAME)
1730 {
1731 SourceError(source, "defined without name in #if/#elif");
1732 error = 1;
1733 break;
1734 } //end if
1735 //v = (value_t *) GetClearedMemory(sizeof(value_t));
1736 AllocValue(v);
1737 #if DEFINEHASHING
1738 if (PC_FindHashedDefine(source->definehash, t->string))
1739 #else
1740 if (PC_FindDefine(source->defines, t->string))
1741 #endif //DEFINEHASHING
1742 {
1743 v->intvalue = 1;
1744 v->floatvalue = 1;
1745 } //end if
1746 else
1747 {
1748 v->intvalue = 0;
1749 v->floatvalue = 0;
1750 } //end else
1751 v->parentheses = parentheses;
1752 v->next = NULL;
1753 v->prev = lastvalue;
1754 if (lastvalue) lastvalue->next = v;
1755 else firstvalue = v;
1756 lastvalue = v;
1757 if (brace)
1758 {
1759 t = t->next;
1760 if (!t || strcmp(t->string, ")"))
1761 {
1762 SourceError(source, "defined without ) in #if/#elif");
1763 error = 1;
1764 break;
1765 } //end if
1766 } //end if
1767 brace = qfalse;
1768 // defined() creates a value
1769 lastwasvalue = 1;
1770 break;
1771 } //end case
1772 case TT_NUMBER:
1773 {
1774 if (lastwasvalue)
1775 {
1776 SourceError(source, "syntax error in #if/#elif");
1777 error = 1;
1778 break;
1779 } //end if
1780 //v = (value_t *) GetClearedMemory(sizeof(value_t));
1781 AllocValue(v);
1782 if (negativevalue)
1783 {
1784 v->intvalue = - (signed int) t->intvalue;
1785 v->floatvalue = - t->floatvalue;
1786 } //end if
1787 else
1788 {
1789 v->intvalue = t->intvalue;
1790 v->floatvalue = t->floatvalue;
1791 } //end else
1792 v->parentheses = parentheses;
1793 v->next = NULL;
1794 v->prev = lastvalue;
1795 if (lastvalue) lastvalue->next = v;
1796 else firstvalue = v;
1797 lastvalue = v;
1798 //last token was a value
1799 lastwasvalue = 1;
1800 //
1801 negativevalue = 0;
1802 break;
1803 } //end case
1804 case TT_PUNCTUATION:
1805 {
1806 if (negativevalue)
1807 {
1808 SourceError(source, "misplaced minus sign in #if/#elif");
1809 error = 1;
1810 break;
1811 } //end if
1812 if (t->subtype == P_PARENTHESESOPEN)
1813 {
1814 parentheses++;
1815 break;
1816 } //end if
1817 else if (t->subtype == P_PARENTHESESCLOSE)
1818 {
1819 parentheses--;
1820 if (parentheses < 0)
1821 {
1822 SourceError(source, "too many ) in #if/#elsif");
1823 error = 1;
1824 } //end if
1825 break;
1826 } //end else if
1827 //check for invalid operators on floating point values
1828 if (!integer)
1829 {
1830 if (t->subtype == P_BIN_NOT || t->subtype == P_MOD ||
1831 t->subtype == P_RSHIFT || t->subtype == P_LSHIFT ||
1832 t->subtype == P_BIN_AND || t->subtype == P_BIN_OR ||
1833 t->subtype == P_BIN_XOR)
1834 {
1835 SourceError(source, "illigal operator %s on floating point operands", t->string);
1836 error = 1;
1837 break;
1838 } //end if
1839 } //end if
1840 switch(t->subtype)
1841 {
1842 case P_LOGIC_NOT:
1843 case P_BIN_NOT:
1844 {
1845 if (lastwasvalue)
1846 {
1847 SourceError(source, "! or ~ after value in #if/#elif");
1848 error = 1;
1849 break;
1850 } //end if
1851 break;
1852 } //end case
1853 case P_INC:
1854 case P_DEC:
1855 {
1856 SourceError(source, "++ or -- used in #if/#elif");
1857 break;
1858 } //end case
1859 case P_SUB:
1860 {
1861 if (!lastwasvalue)
1862 {
1863 negativevalue = 1;
1864 break;
1865 } //end if
1866 } //end case
1867
1868 case P_MUL:
1869 case P_DIV:
1870 case P_MOD:
1871 case P_ADD:
1872
1873 case P_LOGIC_AND:
1874 case P_LOGIC_OR:
1875 case P_LOGIC_GEQ:
1876 case P_LOGIC_LEQ:
1877 case P_LOGIC_EQ:
1878 case P_LOGIC_UNEQ:
1879
1880 case P_LOGIC_GREATER:
1881 case P_LOGIC_LESS:
1882
1883 case P_RSHIFT:
1884 case P_LSHIFT:
1885
1886 case P_BIN_AND:
1887 case P_BIN_OR:
1888 case P_BIN_XOR:
1889
1890 case P_COLON:
1891 case P_QUESTIONMARK:
1892 {
1893 if (!lastwasvalue)
1894 {
1895 SourceError(source, "operator %s after operator in #if/#elif", t->string);
1896 error = 1;
1897 break;
1898 } //end if
1899 break;
1900 } //end case
1901 default:
1902 {
1903 SourceError(source, "invalid operator %s in #if/#elif", t->string);
1904 error = 1;
1905 break;
1906 } //end default
1907 } //end switch
1908 if (!error && !negativevalue)
1909 {
1910 //o = (operator_t *) GetClearedMemory(sizeof(operator_t));
1911 AllocOperator(o);
1912 o->operator = t->subtype;
1913 o->priority = PC_OperatorPriority(t->subtype);
1914 o->parentheses = parentheses;
1915 o->next = NULL;
1916 o->prev = lastoperator;
1917 if (lastoperator) lastoperator->next = o;
1918 else firstoperator = o;
1919 lastoperator = o;
1920 lastwasvalue = 0;
1921 } //end if
1922 break;
1923 } //end case
1924 default:
1925 {
1926 SourceError(source, "unknown %s in #if/#elif", t->string);
1927 error = 1;
1928 break;
1929 } //end default
1930 } //end switch
1931 if (error) break;
1932 } //end for
1933 if (!error)
1934 {
1935 if (!lastwasvalue)
1936 {
1937 SourceError(source, "trailing operator in #if/#elif");
1938 error = 1;
1939 } //end if
1940 else if (parentheses)
1941 {
1942 SourceError(source, "too many ( in #if/#elif");
1943 error = 1;
1944 } //end else if
1945 } //end if
1946 //
1947 gotquestmarkvalue = qfalse;
1948 questmarkintvalue = 0;
1949 questmarkfloatvalue = 0;
1950 //while there are operators
1951 while(!error && firstoperator)
1952 {
1953 v = firstvalue;
1954 for (o = firstoperator; o->next; o = o->next)
1955 {
1956 //if the current operator is nested deeper in parentheses
1957 //than the next operator
1958 if (o->parentheses > o->next->parentheses) break;
1959 //if the current and next operator are nested equally deep in parentheses
1960 if (o->parentheses == o->next->parentheses)
1961 {
1962 //if the priority of the current operator is equal or higher
1963 //than the priority of the next operator
1964 if (o->priority >= o->next->priority) break;
1965 } //end if
1966 //if the arity of the operator isn't equal to 1
1967 if (o->operator != P_LOGIC_NOT
1968 && o->operator != P_BIN_NOT) v = v->next;
1969 //if there's no value or no next value
1970 if (!v)
1971 {
1972 SourceError(source, "mising values in #if/#elif");
1973 error = 1;
1974 break;
1975 } //end if
1976 } //end for
1977 if (error) break;
1978 v1 = v;
1979 v2 = v->next;
1980 #ifdef DEBUG_EVAL
1981 if (integer)
1982 {
1983 Log_Write("operator %s, value1 = %d", PunctuationFromNum(source->scriptstack, o->operator), v1->intvalue);
1984 if (v2) Log_Write("value2 = %d", v2->intvalue);
1985 } //end if
1986 else
1987 {
1988 Log_Write("operator %s, value1 = %f", PunctuationFromNum(source->scriptstack, o->operator), v1->floatvalue);
1989 if (v2) Log_Write("value2 = %f", v2->floatvalue);
1990 } //end else
1991 #endif //DEBUG_EVAL
1992 switch(o->operator)
1993 {
1994 case P_LOGIC_NOT: v1->intvalue = !v1->intvalue;
1995 v1->floatvalue = !v1->floatvalue; break;
1996 case P_BIN_NOT: v1->intvalue = ~v1->intvalue;
1997 break;
1998 case P_MUL: v1->intvalue *= v2->intvalue;
1999 v1->floatvalue *= v2->floatvalue; break;
2000 case P_DIV: if (!v2->intvalue || !v2->floatvalue)
2001 {
2002 SourceError(source, "divide by zero in #if/#elif");
2003 error = 1;
2004 break;
2005 }
2006 v1->intvalue /= v2->intvalue;
2007 v1->floatvalue /= v2->floatvalue; break;
2008 case P_MOD: if (!v2->intvalue)
2009 {
2010 SourceError(source, "divide by zero in #if/#elif");
2011 error = 1;
2012 break;
2013 }
2014 v1->intvalue %= v2->intvalue; break;
2015 case P_ADD: v1->intvalue += v2->intvalue;
2016 v1->floatvalue += v2->floatvalue; break;
2017 case P_SUB: v1->intvalue -= v2->intvalue;
2018 v1->floatvalue -= v2->floatvalue; break;
2019 case P_LOGIC_AND: v1->intvalue = v1->intvalue && v2->intvalue;
2020 v1->floatvalue = v1->floatvalue && v2->floatvalue; break;
2021 case P_LOGIC_OR: v1->intvalue = v1->intvalue || v2->intvalue;
2022 v1->floatvalue = v1->floatvalue || v2->floatvalue; break;
2023 case P_LOGIC_GEQ: v1->intvalue = v1->intvalue >= v2->intvalue;
2024 v1->floatvalue = v1->floatvalue >= v2->floatvalue; break;
2025 case P_LOGIC_LEQ: v1->intvalue = v1->intvalue <= v2->intvalue;
2026 v1->floatvalue = v1->floatvalue <= v2->floatvalue; break;
2027 case P_LOGIC_EQ: v1->intvalue = v1->intvalue == v2->intvalue;
2028 v1->floatvalue = v1->floatvalue == v2->floatvalue; break;
2029 case P_LOGIC_UNEQ: v1->intvalue = v1->intvalue != v2->intvalue;
2030 v1->floatvalue = v1->floatvalue != v2->floatvalue; break;
2031 case P_LOGIC_GREATER: v1->intvalue = v1->intvalue > v2->intvalue;
2032 v1->floatvalue = v1->floatvalue > v2->floatvalue; break;
2033 case P_LOGIC_LESS: v1->intvalue = v1->intvalue < v2->intvalue;
2034 v1->floatvalue = v1->floatvalue < v2->floatvalue; break;
2035 case P_RSHIFT: v1->intvalue >>= v2->intvalue;
2036 break;
2037 case P_LSHIFT: v1->intvalue <<= v2->intvalue;
2038 break;
2039 case P_BIN_AND: v1->intvalue &= v2->intvalue;
2040 break;
2041 case P_BIN_OR: v1->intvalue |= v2->intvalue;
2042 break;
2043 case P_BIN_XOR: v1->intvalue ^= v2->intvalue;
2044 break;
2045 case P_COLON:
2046 {
2047 if (!gotquestmarkvalue)
2048 {
2049 SourceError(source, ": without ? in #if/#elif");
2050 error = 1;
2051 break;
2052 } //end if
2053 if (integer)
2054 {
2055 if (!questmarkintvalue) v1->intvalue = v2->intvalue;
2056 } //end if
2057 else
2058 {
2059 if (!questmarkfloatvalue) v1->floatvalue = v2->floatvalue;
2060 } //end else
2061 gotquestmarkvalue = qfalse;
2062 break;
2063 } //end case
2064 case P_QUESTIONMARK:
2065 {
2066 if (gotquestmarkvalue)
2067 {
2068 SourceError(source, "? after ? in #if/#elif");
2069 error = 1;
2070 break;
2071 } //end if
2072 questmarkintvalue = v1->intvalue;
2073 questmarkfloatvalue = v1->floatvalue;
2074 gotquestmarkvalue = qtrue;
2075 break;
2076 } //end if
2077 } //end switch
2078 #ifdef DEBUG_EVAL
2079 if (integer) Log_Write("result value = %d", v1->intvalue);
2080 else Log_Write("result value = %f", v1->floatvalue);
2081 #endif //DEBUG_EVAL
2082 if (error) break;
2083 //if not an operator with arity 1
2084 if (o->operator != P_LOGIC_NOT
2085 && o->operator != P_BIN_NOT)
2086 {
2087 //remove the second value if not question mark operator
2088 if (o->operator != P_QUESTIONMARK) v = v->next;
2089 //
2090 if (v)
2091 {
2092 if (v->prev) v->prev->next = v->next;
2093 else firstvalue = v->next;
2094 if (v->next) v->next->prev = v->prev;
2095 }
2096 //FreeMemory(v);
2097 FreeValue(v);
2098 } //end if
2099 //remove the operator
2100 if (o->prev) o->prev->next = o->next;
2101 else firstoperator = o->next;
2102 if (o->next) o->next->prev = o->prev;
2103 //FreeMemory(o);
2104 FreeOperator(o);
2105 } //end while
2106 if (firstvalue)
2107 {
2108 if (intvalue) *intvalue = firstvalue->intvalue;
2109 if (floatvalue) *floatvalue = firstvalue->floatvalue;
2110 } //end if
2111 for (o = firstoperator; o; o = lastoperator)
2112 {
2113 lastoperator = o->next;
2114 //FreeMemory(o);
2115 FreeOperator(o);
2116 } //end for
2117 for (v = firstvalue; v; v = lastvalue)
2118 {
2119 lastvalue = v->next;
2120 //FreeMemory(v);
2121 FreeValue(v);
2122 } //end for
2123 if (!error) return qtrue;
2124 if (intvalue) *intvalue = 0;
2125 if (floatvalue) *floatvalue = 0;
2126 return qfalse;
2127 } //end of the function PC_EvaluateTokens
2128 //============================================================================
2129 //
2130 // Parameter: -
2131 // Returns: -
2132 // Changes Globals: -
2133 //============================================================================
2134 int PC_Evaluate(source_t *source, signed long int *intvalue,
2135 float *floatvalue, int integer)
2136 {
2137 token_t token, *firsttoken, *lasttoken;
2138 token_t *t, *nexttoken;
2139 define_t *define;
2140 int defined = qfalse;
2141
2142 if (intvalue) *intvalue = 0;
2143 if (floatvalue) *floatvalue = 0;
2144 //
2145 if (!PC_ReadLine(source, &token))
2146 {
2147 SourceError(source, "no value after #if/#elif");
2148 return qfalse;
2149 } //end if
2150 firsttoken = NULL;
2151 lasttoken = NULL;
2152 do
2153 {
2154 //if the token is a name
2155 if (token.type == TT_NAME)
2156 {
2157 if (defined)
2158 {
2159 defined = qfalse;
2160 t = PC_CopyToken(&token);
2161 t->next = NULL;
2162 if (lasttoken) lasttoken->next = t;
2163 else firsttoken = t;
2164 lasttoken = t;
2165 } //end if
2166 else if (!strcmp(token.string, "defined"))
2167 {
2168 defined = qtrue;
2169 t = PC_CopyToken(&token);
2170 t->next = NULL;
2171 if (lasttoken) lasttoken->next = t;
2172 else firsttoken = t;
2173 lasttoken = t;
2174 } //end if
2175 else
2176 {
2177 //then it must be a define
2178 #if DEFINEHASHING
2179 define = PC_FindHashedDefine(source->definehash, token.string);
2180 #else
2181 define = PC_FindDefine(source->defines, token.string);
2182 #endif //DEFINEHASHING
2183 if (!define)
2184 {
2185 SourceError(source, "can't evaluate %s, not defined", token.string);
2186 return qfalse;
2187 } //end if
2188 if (!PC_ExpandDefineIntoSource(source, &token, define)) return qfalse;
2189 } //end else
2190 } //end if
2191 //if the token is a number or a punctuation
2192 else if (token.type == TT_NUMBER || token.type == TT_PUNCTUATION)
2193 {
2194 t = PC_CopyToken(&token);
2195 t->next = NULL;
2196 if (lasttoken) lasttoken->next = t;
2197 else firsttoken = t;
2198 lasttoken = t;
2199 } //end else
2200 else //can't evaluate the token
2201 {
2202 SourceError(source, "can't evaluate %s", token.string);
2203 return qfalse;
2204 } //end else
2205 } while(PC_ReadLine(source, &token));
2206 //
2207 if (!PC_EvaluateTokens(source, firsttoken, intvalue, floatvalue, integer)) return qfalse;
2208 //
2209 #ifdef DEBUG_EVAL
2210 Log_Write("eval:");
2211 #endif //DEBUG_EVAL
2212 for (t = firsttoken; t; t = nexttoken)
2213 {
2214 #ifdef DEBUG_EVAL
2215 Log_Write(" %s", t->string);
2216 #endif //DEBUG_EVAL
2217 nexttoken = t->next;
2218 PC_FreeToken(t);
2219 } //end for
2220 #ifdef DEBUG_EVAL
2221 if (integer) Log_Write("eval result: %d", *intvalue);
2222 else Log_Write("eval result: %f", *floatvalue);
2223 #endif //DEBUG_EVAL
2224 //
2225 return qtrue;
2226 } //end of the function PC_Evaluate
2227 //============================================================================
2228 //
2229 // Parameter: -
2230 // Returns: -
2231 // Changes Globals: -
2232 //============================================================================
2233 int PC_DollarEvaluate(source_t *source, signed long int *intvalue,
2234 float *floatvalue, int integer)
2235 {
2236 int indent, defined = qfalse;
2237 token_t token, *firsttoken, *lasttoken;
2238 token_t *t, *nexttoken;
2239 define_t *define;
2240
2241 if (intvalue) *intvalue = 0;
2242 if (floatvalue) *floatvalue = 0;
2243 //
2244 if (!PC_ReadSourceToken(source, &token))
2245 {
2246 SourceError(source, "no leading ( after $evalint/$evalfloat");
2247 return qfalse;
2248 } //end if
2249 if (!PC_ReadSourceToken(source, &token))
2250 {
2251 SourceError(source, "nothing to evaluate");
2252 return qfalse;
2253 } //end if
2254 indent = 1;
2255 firsttoken = NULL;
2256 lasttoken = NULL;
2257 do
2258 {
2259 //if the token is a name
2260 if (token.type == TT_NAME)
2261 {
2262 if (defined)
2263 {
2264 defined = qfalse;
2265 t = PC_CopyToken(&token);
2266 t->next = NULL;
2267 if (lasttoken) lasttoken->next = t;
2268 else firsttoken = t;
2269 lasttoken = t;
2270 } //end if
2271 else if (!strcmp(token.string, "defined"))
2272 {
2273 defined = qtrue;
2274 t = PC_CopyToken(&token);
2275 t->next = NULL;
2276 if (lasttoken) lasttoken->next = t;
2277 else firsttoken = t;
2278 lasttoken = t;
2279 } //end if
2280 else
2281 {
2282 //then it must be a define
2283 #if DEFINEHASHING
2284 define = PC_FindHashedDefine(source->definehash, token.string);
2285 #else
2286 define = PC_FindDefine(source->defines, token.string);
2287 #endif //DEFINEHASHING
2288 if (!define)
2289 {
2290 SourceError(source, "can't evaluate %s, not defined", token.string);
2291 return qfalse;
2292 } //end if
2293 if (!PC_ExpandDefineIntoSource(source, &token, define)) return qfalse;
2294 } //end else
2295 } //end if
2296 //if the token is a number or a punctuation
2297 else if (token.type == TT_NUMBER || token.type == TT_PUNCTUATION)
2298 {
2299 if (*token.string == '(') indent++;
2300 else if (*token.string == ')') indent--;
2301 if (indent <= 0) break;
2302 t = PC_CopyToken(&token);
2303 t->next = NULL;
2304 if (lasttoken) lasttoken->next = t;
2305 else firsttoken = t;
2306 lasttoken = t;
2307 } //end else
2308 else //can't evaluate the token
2309 {
2310 SourceError(source, "can't evaluate %s", token.string);
2311 return qfalse;
2312 } //end else
2313 } while(PC_ReadSourceToken(source, &token));
2314 //
2315 if (!PC_EvaluateTokens(source, firsttoken, intvalue, floatvalue, integer)) return qfalse;
2316 //
2317 #ifdef DEBUG_EVAL
2318 Log_Write("$eval:");
2319 #endif //DEBUG_EVAL
2320 for (t = firsttoken; t; t = nexttoken)
2321 {
2322 #ifdef DEBUG_EVAL
2323 Log_Write(" %s", t->string);
2324 #endif //DEBUG_EVAL
2325 nexttoken = t->next;
2326 PC_FreeToken(t);
2327 } //end for
2328 #ifdef DEBUG_EVAL
2329 if (integer) Log_Write("$eval result: %d", *intvalue);
2330 else Log_Write("$eval result: %f", *floatvalue);
2331 #endif //DEBUG_EVAL
2332 //
2333 return qtrue;
2334 } //end of the function PC_DollarEvaluate
2335 //============================================================================
2336 //
2337 // Parameter: -
2338 // Returns: -
2339 // Changes Globals: -
2340 //============================================================================
2341 int PC_Directive_elif(source_t *source)
2342 {
2343 signed long int value;
2344 int type, skip;
2345
2346 PC_PopIndent(source, &type, &skip);
2347 if (!type || type == INDENT_ELSE)
2348 {
2349 SourceError(source, "misplaced #elif");
2350 return qfalse;
2351 } //end if
2352 if (!PC_Evaluate(source, &value, NULL, qtrue)) return qfalse;
2353 skip = (value == 0);
2354 PC_PushIndent(source, INDENT_ELIF, skip);
2355 return qtrue;
2356 } //end of the function PC_Directive_elif
2357 //============================================================================
2358 //
2359 // Parameter: -
2360 // Returns: -
2361 // Changes Globals: -
2362 //============================================================================
2363 int PC_Directive_if(source_t *source)
2364 {
2365 signed long int value;
2366 int skip;
2367
2368 if (!PC_Evaluate(source, &value, NULL, qtrue)) return qfalse;
2369 skip = (value == 0);
2370 PC_PushIndent(source, INDENT_IF, skip);
2371 return qtrue;
2372 } //end of the function PC_Directive
2373 //============================================================================
2374 //
2375 // Parameter: -
2376 // Returns: -
2377 // Changes Globals: -
2378 //============================================================================
2379 int PC_Directive_line(source_t *source)
2380 {
2381 SourceError(source, "#line directive not supported");
2382 return qfalse;
2383 } //end of the function PC_Directive_line
2384 //============================================================================
2385 //
2386 // Parameter: -
2387 // Returns: -
2388 // Changes Globals: -
2389 //============================================================================
2390 int PC_Directive_error(source_t *source)
2391 {
2392 token_t token;
2393
2394 strcpy(token.string, "");
2395 PC_ReadSourceToken(source, &token);
2396 SourceError(source, "#error directive: %s", token.string);
2397 return qfalse;
2398 } //end of the function PC_Directive_error
2399 //============================================================================
2400 //
2401 // Parameter: -
2402 // Returns: -
2403 // Changes Globals: -
2404 //============================================================================
2405 int PC_Directive_pragma(source_t *source)
2406 {
2407 token_t token;
2408
2409 SourceWarning(source, "#pragma directive not supported");
2410 while(PC_ReadLine(source, &token)) ;
2411 return qtrue;
2412 } //end of the function PC_Directive_pragma
2413 //============================================================================
2414 //
2415 // Parameter: -
2416 // Returns: -
2417 // Changes Globals: -
2418 //============================================================================
2419 void UnreadSignToken(source_t *source)
2420 {
2421 token_t token;
2422
2423 token.line = source->scriptstack->line;
2424 token.whitespace_p = source->scriptstack->script_p;
2425 token.endwhitespace_p = source->scriptstack->script_p;
2426 token.linescrossed = 0;
2427 strcpy(token.string, "-");
2428 token.type = TT_PUNCTUATION;
2429 token.subtype = P_SUB;
2430 PC_UnreadSourceToken(source, &token);
2431 } //end of the function UnreadSignToken
2432 //============================================================================
2433 //
2434 // Parameter: -
2435 // Returns: -
2436 // Changes Globals: -
2437 //============================================================================
2438 int PC_Directive_eval(source_t *source)
2439 {
2440 signed long int value;
2441 token_t token;
2442
2443 if (!PC_Evaluate(source, &value, NULL, qtrue)) return qfalse;
2444 //
2445 token.line = source->scriptstack->line;
2446 token.whitespace_p = source->scriptstack->script_p;
2447 token.endwhitespace_p = source->scriptstack->script_p;
2448 token.linescrossed = 0;
2449 sprintf(token.string, "%ld", labs(value));
2450 token.type = TT_NUMBER;
2451 token.subtype = TT_INTEGER|TT_LONG|TT_DECIMAL;
2452 PC_UnreadSourceToken(source, &token);
2453 if (value < 0) UnreadSignToken(source);
2454 return qtrue;
2455 } //end of the function PC_Directive_eval
2456 //============================================================================
2457 //
2458 // Parameter: -
2459 // Returns: -
2460 // Changes Globals: -
2461 //============================================================================
2462 int PC_Directive_evalfloat(source_t *source)
2463 {
2464 float value;
2465 token_t token;
2466
2467 if (!PC_Evaluate(source, NULL, &value, qfalse)) return qfalse;
2468 token.line = source->scriptstack->line;
2469 token.whitespace_p = source->scriptstack->script_p;
2470 token.endwhitespace_p = source->scriptstack->script_p;
2471 token.linescrossed = 0;
2472 sprintf(token.string, "%1.2f", fabs(value));
2473 token.type = TT_NUMBER;
2474 token.subtype = TT_FLOAT|TT_LONG|TT_DECIMAL;
2475 PC_UnreadSourceToken(source, &token);
2476 if (value < 0) UnreadSignToken(source);
2477 return qtrue;
2478 } //end of the function PC_Directive_evalfloat
2479 //============================================================================
2480 //
2481 // Parameter: -
2482 // Returns: -
2483 // Changes Globals: -
2484 //============================================================================
2485 directive_t directives[20] =
2486 {
2487 {"if", PC_Directive_if},
2488 {"ifdef", PC_Directive_ifdef},
2489 {"ifndef", PC_Directive_ifndef},
2490 {"elif", PC_Directive_elif},
2491 {"else", PC_Directive_else},
2492 {"endif", PC_Directive_endif},
2493 {"include", PC_Directive_include},
2494 {"define", PC_Directive_define},
2495 {"undef", PC_Directive_undef},
2496 {"line", PC_Directive_line},
2497 {"error", PC_Directive_error},
2498 {"pragma", PC_Directive_pragma},
2499 {"eval", PC_Directive_eval},
2500 {"evalfloat", PC_Directive_evalfloat},
2501 {NULL, NULL}
2502 };
2503
2504 int PC_ReadDirective(source_t *source)
2505 {
2506 token_t token;
2507 int i;
2508
2509 //read the directive name
2510 if (!PC_ReadSourceToken(source, &token))
2511 {
2512 SourceError(source, "found # without name");
2513 return qfalse;
2514 } //end if
2515 //directive name must be on the same line
2516 if (token.linescrossed > 0)
2517 {
2518 PC_UnreadSourceToken(source, &token);
2519 SourceError(source, "found # at end of line");
2520 return qfalse;
2521 } //end if
2522 //if if is a name
2523 if (token.type == TT_NAME)
2524 {
2525 //find the precompiler directive
2526 for (i = 0; directives[i].name; i++)
2527 {
2528 if (!strcmp(directives[i].name, token.string))
2529 {
2530 return directives[i].func(source);
2531 } //end if
2532 } //end for
2533 } //end if
2534 SourceError(source, "unknown precompiler directive %s", token.string);
2535 return qfalse;
2536 } //end of the function PC_ReadDirective
2537 //============================================================================
2538 //
2539 // Parameter: -
2540 // Returns: -
2541 // Changes Globals: -
2542 //============================================================================
2543 int PC_DollarDirective_evalint(source_t *source)
2544 {
2545 signed long int value;
2546 token_t token;
2547
2548 if (!PC_DollarEvaluate(source, &value, NULL, qtrue)) return qfalse;
2549 //
2550 token.line = source->scriptstack->line;
2551 token.whitespace_p = source->scriptstack->script_p;
2552 token.endwhitespace_p = source->scriptstack->script_p;
2553 token.linescrossed = 0;
2554 sprintf(token.string, "%ld", labs(value));
2555 token.type = TT_NUMBER;
2556 token.subtype = TT_INTEGER|TT_LONG|TT_DECIMAL;
2557
2558 #ifdef NUMBERVALUE
2559 token.intvalue = labs(value);
2560 token.floatvalue = token.intvalue;
2561 #endif //NUMBERVALUE
2562
2563 PC_UnreadSourceToken(source, &token);
2564 if (value < 0)
2565 UnreadSignToken(source);
2566
2567 return qtrue;
2568 } //end of the function PC_DollarDirective_evalint
2569 //============================================================================
2570 //
2571 // Parameter: -
2572 // Returns: -
2573 // Changes Globals: -
2574 //============================================================================
2575 int PC_DollarDirective_evalfloat(source_t *source)
2576 {
2577 float value;
2578 token_t token;
2579
2580 if (!PC_DollarEvaluate(source, NULL, &value, qfalse)) return qfalse;
2581 token.line = source->scriptstack->line;
2582 token.whitespace_p = source->scriptstack->script_p;
2583 token.endwhitespace_p = source->scriptstack->script_p;
2584 token.linescrossed = 0;
2585 sprintf(token.string, "%1.2f", fabs(value));
2586 token.type = TT_NUMBER;
2587 token.subtype = TT_FLOAT|TT_LONG|TT_DECIMAL;
2588
2589 #ifdef NUMBERVALUE
2590 token.floatvalue = fabs(value);
2591 token.intvalue = (unsigned long) token.floatvalue;
2592 #endif //NUMBERVALUE
2593
2594 PC_UnreadSourceToken(source, &token);
2595 if (value < 0)
2596 UnreadSignToken(source);
2597
2598 return qtrue;
2599 } //end of the function PC_DollarDirective_evalfloat
2600 //============================================================================
2601 //
2602 // Parameter: -
2603 // Returns: -
2604 // Changes Globals: -
2605 //============================================================================
2606 directive_t dollardirectives[20] =
2607 {
2608 {"evalint", PC_DollarDirective_evalint},
2609 {"evalfloat", PC_DollarDirective_evalfloat},
2610 {NULL, NULL}
2611 };
2612
2613 int PC_ReadDollarDirective(source_t *source)
2614 {
2615 token_t token;
2616 int i;
2617
2618 //read the directive name
2619 if (!PC_ReadSourceToken(source, &token))
2620 {
2621 SourceError(source, "found $ without name");
2622 return qfalse;
2623 } //end if
2624 //directive name must be on the same line
2625 if (token.linescrossed > 0)
2626 {
2627 PC_UnreadSourceToken(source, &token);
2628 SourceError(source, "found $ at end of line");
2629 return qfalse;
2630 } //end if
2631 //if if is a name
2632 if (token.type == TT_NAME)
2633 {
2634 //find the precompiler directive
2635 for (i = 0; dollardirectives[i].name; i++)
2636 {
2637 if (!strcmp(dollardirectives[i].name, token.string))
2638 {
2639 return dollardirectives[i].func(source);
2640 } //end if
2641 } //end for
2642 } //end if
2643 PC_UnreadSourceToken(source, &token);
2644 SourceError(source, "unknown precompiler directive %s", token.string);
2645 return qfalse;
2646 } //end of the function PC_ReadDirective
2647
2648 #ifdef QUAKEC
2649 //============================================================================
2650 //
2651 // Parameter: -
2652 // Returns: -
2653 // Changes Globals: -
2654 //============================================================================
2655 int BuiltinFunction(source_t *source)
2656 {
2657 token_t token;
2658
2659 if (!PC_ReadSourceToken(source, &token)) return qfalse;
2660 if (token.type == TT_NUMBER)
2661 {
2662 PC_UnreadSourceToken(source, &token);
2663 return qtrue;
2664 } //end if
2665 else
2666 {
2667 PC_UnreadSourceToken(source, &token);
2668 return qfalse;
2669 } //end else
2670 } //end of the function BuiltinFunction
2671 //============================================================================
2672 //
2673 // Parameter: -
2674 // Returns: -
2675 // Changes Globals: -
2676 //============================================================================
2677 int QuakeCMacro(source_t *source)
2678 {
2679 int i;
2680 token_t token;
2681
2682 if (!PC_ReadSourceToken(source, &token)) return qtrue;
2683 if (token.type != TT_NAME)
2684 {
2685 PC_UnreadSourceToken(source, &token);
2686 return qtrue;
2687 } //end if
2688 //find the precompiler directive
2689 for (i = 0; dollardirectives[i].name; i++)
2690 {
2691 if (!strcmp(dollardirectives[i].name, token.string))
2692 {
2693 PC_UnreadSourceToken(source, &token);
2694 return qfalse;
2695 } //end if
2696 } //end for
2697 PC_UnreadSourceToken(source, &token);
2698 return qtrue;
2699 } //end of the function QuakeCMacro
2700 #endif //QUAKEC
2701 //============================================================================
2702 //
2703 // Parameter: -
2704 // Returns: -
2705 // Changes Globals: -
2706 //============================================================================
2707 int PC_ReadToken(source_t *source, token_t *token)
2708 {
2709 define_t *define;
2710
2711 while(1)
2712 {
2713 if (!PC_ReadSourceToken(source, token)) return qfalse;
2714 //check for precompiler directives
2715 if (token->type == TT_PUNCTUATION && *token->string == '#')
2716 {
2717 #ifdef QUAKEC
2718 if (!BuiltinFunction(source))
2719 #endif //QUAKC
2720 {
2721 //read the precompiler directive
2722 if (!PC_ReadDirective(source)) return qfalse;
2723 continue;
2724 } //end if
2725 } //end if
2726 if (token->type == TT_PUNCTUATION && *token->string == '$')
2727 {
2728 #ifdef QUAKEC
2729 if (!QuakeCMacro(source))
2730 #endif //QUAKEC
2731 {
2732 //read the precompiler directive
2733 if (!PC_ReadDollarDirective(source)) return qfalse;
2734 continue;
2735 } //end if
2736 } //end if
2737 // recursively concatenate strings that are behind each other still resolving defines
2738 if (token->type == TT_STRING)
2739 {
2740 token_t newtoken;
2741 if (PC_ReadToken(source, &newtoken))
2742 {
2743 if (newtoken.type == TT_STRING)
2744 {
2745 token->string[strlen(token->string)-1] = '\0';
2746 if (strlen(token->string) + strlen(newtoken.string+1) + 1 >= MAX_TOKEN)
2747 {
2748 SourceError(source, "string longer than MAX_TOKEN %d", MAX_TOKEN);
2749 return qfalse;
2750 }
2751 strcat(token->string, newtoken.string+1);
2752 }
2753 else
2754 {
2755 PC_UnreadToken(source, &newtoken);
2756 }
2757 }
2758 } //end if
2759 //if skipping source because of conditional compilation
2760 if (source->skip) continue;
2761 //if the token is a name
2762 if (token->type == TT_NAME)
2763 {
2764 //check if the name is a define macro
2765 #if DEFINEHASHING
2766 define = PC_FindHashedDefine(source->definehash, token->string);
2767 #else
2768 define = PC_FindDefine(source->defines, token->string);
2769 #endif //DEFINEHASHING
2770 //if it is a define macro
2771 if (define)
2772 {
2773 //expand the defined macro
2774 if (!PC_ExpandDefineIntoSource(source, token, define)) return qfalse;
2775 continue;
2776 } //end if
2777 } //end if
2778 //copy token for unreading
2779 Com_Memcpy(&source->token, token, sizeof(token_t));
2780 //found a token
2781 return qtrue;
2782 } //end while
2783 } //end of the function PC_ReadToken
2784 //============================================================================
2785 //
2786 // Parameter: -
2787 // Returns: -
2788 // Changes Globals: -
2789 //============================================================================
2790 int PC_ExpectTokenString(source_t *source, char *string)
2791 {
2792 token_t token;
2793
2794 if (!PC_ReadToken(source, &token))
2795 {
2796 SourceError(source, "couldn't find expected %s", string);
2797 return qfalse;
2798 } //end if
2799
2800 if (strcmp(token.string, string))
2801 {
2802 SourceError(source, "expected %s, found %s", string, token.string);
2803 return qfalse;
2804 } //end if
2805 return qtrue;
2806 } //end of the function PC_ExpectTokenString
2807 //============================================================================
2808 //
2809 // Parameter: -
2810 // Returns: -
2811 // Changes Globals: -
2812 //============================================================================
2813 int PC_ExpectTokenType(source_t *source, int type, int subtype, token_t *token)
2814 {
2815 char str[MAX_TOKEN];
2816
2817 if (!PC_ReadToken(source, token))
2818 {
2819 SourceError(source, "couldn't read expected token");
2820 return qfalse;
2821 } //end if
2822
2823 if (token->type != type)
2824 {
2825 strcpy(str, "");
2826 if (type == TT_STRING) strcpy(str, "string");
2827 if (type == TT_LITERAL) strcpy(str, "literal");
2828 if (type == TT_NUMBER) strcpy(str, "number");
2829 if (type == TT_NAME) strcpy(str, "name");
2830 if (type == TT_PUNCTUATION) strcpy(str, "punctuation");
2831 SourceError(source, "expected a %s, found %s", str, token->string);
2832 return qfalse;
2833 } //end if
2834 if (token->type == TT_NUMBER)
2835 {
2836 if ((token->subtype & subtype) != subtype)
2837 {
2838 strcpy(str, "");
2839 if (subtype & TT_DECIMAL) strcpy(str, "decimal");
2840 if (subtype & TT_HEX) strcpy(str, "hex");
2841 if (subtype & TT_OCTAL) strcpy(str, "octal");
2842 if (subtype & TT_BINARY) strcpy(str, "binary");
2843 if (subtype & TT_LONG) strcat(str, " long");
2844 if (subtype & TT_UNSIGNED) strcat(str, " unsigned");
2845 if (subtype & TT_FLOAT) strcat(str, " float");
2846 if (subtype & TT_INTEGER) strcat(str, " integer");
2847 SourceError(source, "expected %s, found %s", str, token->string);
2848 return qfalse;
2849 } //end if
2850 } //end if
2851 else if (token->type == TT_PUNCTUATION)
2852 {
2853 if (token->subtype != subtype)
2854 {
2855 SourceError(source, "found %s", token->string);
2856 return qfalse;
2857 } //end if
2858 } //end else if
2859 return qtrue;
2860 } //end of the function PC_ExpectTokenType
2861 //============================================================================
2862 //
2863 // Parameter: -
2864 // Returns: -
2865 // Changes Globals: -
2866 //============================================================================
2867 int PC_ExpectAnyToken(source_t *source, token_t *token)
2868 {
2869 if (!PC_ReadToken(source, token))
2870 {
2871 SourceError(source, "couldn't read expected token");
2872 return qfalse;
2873 } //end if
2874 else
2875 {
2876 return qtrue;
2877 } //end else
2878 } //end of the function PC_ExpectAnyToken
2879 //============================================================================
2880 //
2881 // Parameter: -
2882 // Returns: -
2883 // Changes Globals: -
2884 //============================================================================
2885 int PC_CheckTokenString(source_t *source, char *string)
2886 {
2887 token_t tok;
2888
2889 if (!PC_ReadToken(source, &tok)) return qfalse;
2890 //if the token is available
2891 if (!strcmp(tok.string, string)) return qtrue;
2892 //
2893 PC_UnreadSourceToken(source, &tok);
2894 return qfalse;
2895 } //end of the function PC_CheckTokenString
2896 //============================================================================
2897 //
2898 // Parameter: -
2899 // Returns: -
2900 // Changes Globals: -
2901 //============================================================================
2902 int PC_CheckTokenType(source_t *source, int type, int subtype, token_t *token)
2903 {
2904 token_t tok;
2905
2906 if (!PC_ReadToken(source, &tok)) return qfalse;
2907 //if the type matches
2908 if (tok.type == type &&
2909 (tok.subtype & subtype) == subtype)
2910 {
2911 Com_Memcpy(token, &tok, sizeof(token_t));
2912 return qtrue;
2913 } //end if
2914 //
2915 PC_UnreadSourceToken(source, &tok);
2916 return qfalse;
2917 } //end of the function PC_CheckTokenType
2918 //============================================================================
2919 //
2920 // Parameter: -
2921 // Returns: -
2922 // Changes Globals: -
2923 //============================================================================
2924 int PC_SkipUntilString(source_t *source, char *string)
2925 {
2926 token_t token;
2927
2928 while(PC_ReadToken(source, &token))
2929 {
2930 if (!strcmp(token.string, string)) return qtrue;
2931 } //end while
2932 return qfalse;
2933 } //end of the function PC_SkipUntilString
2934 //============================================================================
2935 //
2936 // Parameter: -
2937 // Returns: -
2938 // Changes Globals: -
2939 //============================================================================
2940 void PC_UnreadLastToken(source_t *source)
2941 {
2942 PC_UnreadSourceToken(source, &source->token);
2943 } //end of the function PC_UnreadLastToken
2944 //============================================================================
2945 //
2946 // Parameter: -
2947 // Returns: -
2948 // Changes Globals: -
2949 //============================================================================
2950 void PC_UnreadToken(source_t *source, token_t *token)
2951 {
2952 PC_UnreadSourceToken(source, token);
2953 } //end of the function PC_UnreadToken
2954 //============================================================================
2955 //
2956 // Parameter: -
2957 // Returns: -
2958 // Changes Globals: -
2959 //============================================================================
2960 void PC_SetIncludePath(source_t *source, char *path)
2961 {
2962 size_t len;
2963
2964 Q_strncpyz(source->includepath, path, sizeof(source->includepath)-1);
2965
2966 len = strlen(source->includepath);
2967 //add trailing path seperator
2968 if (len > 0 && source->includepath[len-1] != '\\' &&
2969 source->includepath[len-1] != '/')
2970 {
2971 strcat(source->includepath, PATHSEPERATOR_STR);
2972 } //end if
2973 } //end of the function PC_SetIncludePath
2974 //============================================================================
2975 //
2976 // Parameter: -
2977 // Returns: -
2978 // Changes Globals: -
2979 //============================================================================
2980 void PC_SetPunctuations(source_t *source, punctuation_t *p)
2981 {
2982 source->punctuations = p;
2983 } //end of the function PC_SetPunctuations
2984 //============================================================================
2985 //
2986 // Parameter: -
2987 // Returns: -
2988 // Changes Globals: -
2989 //============================================================================
2990 source_t *LoadSourceFile(const char *filename)
2991 {
2992 source_t *source;
2993 script_t *script;
2994
2995 PC_InitTokenHeap();
2996
2997 script = LoadScriptFile(filename);
2998 if (!script) return NULL;
2999
3000 script->next = NULL;
3001
3002 source = (source_t *) GetMemory(sizeof(source_t));
3003 Com_Memset(source, 0, sizeof(source_t));
3004
3005 Q_strncpyz(source->filename, filename, sizeof(source->filename));
3006 source->scriptstack = script;
3007 source->tokens = NULL;
3008 source->defines = NULL;
3009 source->indentstack = NULL;
3010 source->skip = 0;
3011
3012 #if DEFINEHASHING
3013 source->definehash = GetClearedMemory(DEFINEHASHSIZE * sizeof(define_t *));
3014 #endif //DEFINEHASHING
3015 PC_AddGlobalDefinesToSource(source);
3016 return source;
3017 } //end of the function LoadSourceFile
3018 //============================================================================
3019 //
3020 // Parameter: -
3021 // Returns: -
3022 // Changes Globals: -
3023 //============================================================================
3024 source_t *LoadSourceMemory(char *ptr, int length, char *name)
3025 {
3026 source_t *source;
3027 script_t *script;
3028
3029 PC_InitTokenHeap();
3030
3031 script = LoadScriptMemory(ptr, length, name);
3032 if (!script) return NULL;
3033 script->next = NULL;
3034
3035 source = (source_t *) GetMemory(sizeof(source_t));
3036 Com_Memset(source, 0, sizeof(source_t));
3037
3038 Q_strncpyz(source->filename, name, sizeof(source->filename));
3039 source->scriptstack = script;
3040 source->tokens = NULL;
3041 source->defines = NULL;
3042 source->indentstack = NULL;
3043 source->skip = 0;
3044
3045 #if DEFINEHASHING
3046 source->definehash = GetClearedMemory(DEFINEHASHSIZE * sizeof(define_t *));
3047 #endif //DEFINEHASHING
3048 PC_AddGlobalDefinesToSource(source);
3049 return source;
3050 } //end of the function LoadSourceMemory
3051 //============================================================================
3052 //
3053 // Parameter: -
3054 // Returns: -
3055 // Changes Globals: -
3056 //============================================================================
3057 void FreeSource(source_t *source)
3058 {
3059 script_t *script;
3060 token_t *token;
3061 define_t *define;
3062 indent_t *indent;
3063 int i;
3064
3065 //PC_PrintDefineHashTable(source->definehash);
3066 //free all the scripts
3067 while(source->scriptstack)
3068 {
3069 script = source->scriptstack;
3070 source->scriptstack = source->scriptstack->next;
3071 FreeScript(script);
3072 } //end for
3073 //free all the tokens
3074 while(source->tokens)
3075 {
3076 token = source->tokens;
3077 source->tokens = source->tokens->next;
3078 PC_FreeToken(token);
3079 } //end for
3080 #if DEFINEHASHING
3081 for (i = 0; i < DEFINEHASHSIZE; i++)
3082 {
3083 while(source->definehash[i])
3084 {
3085 define = source->definehash[i];
3086 source->definehash[i] = source->definehash[i]->hashnext;
3087 PC_FreeDefine(define);
3088 } //end while
3089 } //end for
3090 #else //DEFINEHASHING
3091 //free all defines
3092 while(source->defines)
3093 {
3094 define = source->defines;
3095 source->defines = source->defines->next;
3096 PC_FreeDefine(define);
3097 } //end for
3098 #endif //DEFINEHASHING
3099 //free all indents
3100 while(source->indentstack)
3101 {
3102 indent = source->indentstack;
3103 source->indentstack = source->indentstack->next;
3104 FreeMemory(indent);
3105 } //end for
3106 #if DEFINEHASHING
3107 //
3108 if (source->definehash) FreeMemory(source->definehash);
3109 #endif //DEFINEHASHING
3110 //free the source itself
3111 FreeMemory(source);
3112 } //end of the function FreeSource
3113 //============================================================================
3114 //
3115 // Parameter: -
3116 // Returns: -
3117 // Changes Globals: -
3118 //============================================================================
3119
3120 #define MAX_SOURCEFILES 64
3121
3122 source_t *sourceFiles[MAX_SOURCEFILES];
3123
3124 int PC_LoadSourceHandle(const char *filename)
3125 {
3126 source_t *source;
3127 int i;
3128
3129 for (i = 1; i < MAX_SOURCEFILES; i++)
3130 {
3131 if (!sourceFiles[i])
3132 break;
3133 } //end for
3134 if (i >= MAX_SOURCEFILES)
3135 return 0;
3136 PS_SetBaseFolder("");
3137 source = LoadSourceFile(filename);
3138 if (!source)
3139 return 0;
3140 sourceFiles[i] = source;
3141 return i;
3142 } //end of the function PC_LoadSourceHandle
3143 //============================================================================
3144 //
3145 // Parameter: -
3146 // Returns: -
3147 // Changes Globals: -
3148 //============================================================================
3149 int PC_FreeSourceHandle(int handle)
3150 {
3151 if (handle < 1 || handle >= MAX_SOURCEFILES)
3152 return qfalse;
3153 if (!sourceFiles[handle])
3154 return qfalse;
3155
3156 FreeSource(sourceFiles[handle]);
3157 sourceFiles[handle] = NULL;
3158 return qtrue;
3159 } //end of the function PC_FreeSourceHandle
3160 //============================================================================
3161 //
3162 // Parameter: -
3163 // Returns: -
3164 // Changes Globals: -
3165 //============================================================================
3166 int PC_ReadTokenHandle(int handle, pc_token_t *pc_token)
3167 {
3168 token_t token;
3169 int ret;
3170
3171 if (handle < 1 || handle >= MAX_SOURCEFILES)
3172 return 0;
3173 if (!sourceFiles[handle])
3174 return 0;
3175
3176 ret = PC_ReadToken(sourceFiles[handle], &token);
3177 strcpy(pc_token->string, token.string);
3178 pc_token->type = token.type;
3179 pc_token->subtype = token.subtype;
3180 pc_token->intvalue = token.intvalue;
3181 pc_token->floatvalue = token.floatvalue;
3182 if (pc_token->type == TT_STRING)
3183 StripDoubleQuotes(pc_token->string);
3184 return ret;
3185 } //end of the function PC_ReadTokenHandle
3186 //============================================================================
3187 //
3188 // Parameter: -
3189 // Returns: -
3190 // Changes Globals: -
3191 //============================================================================
3192 int PC_SourceFileAndLine(int handle, char *filename, int *line)
3193 {
3194 if (handle < 1 || handle >= MAX_SOURCEFILES)
3195 return qfalse;
3196 if (!sourceFiles[handle])
3197 return qfalse;
3198
3199 strcpy(filename, sourceFiles[handle]->filename);
3200 if (sourceFiles[handle]->scriptstack)
3201 *line = sourceFiles[handle]->scriptstack->line;
3202 else
3203 *line = 0;
3204 return qtrue;
3205 } //end of the function PC_SourceFileAndLine
3206 //============================================================================
3207 //
3208 // Parameter: -
3209 // Returns: -
3210 // Changes Globals: -
3211 //============================================================================
3212 void PC_SetBaseFolder(char *path)
3213 {
3214 PS_SetBaseFolder(path);
3215 } //end of the function PC_SetBaseFolder
3216 //============================================================================
3217 //
3218 // Parameter: -
3219 // Returns: -
3220 // Changes Globals: -
3221 //============================================================================
3222 void PC_CheckOpenSourceHandles(void)
3223 {
3224 int i;
3225
3226 for (i = 1; i < MAX_SOURCEFILES; i++)
3227 {
3228 if (sourceFiles[i])
3229 {
3230 #ifdef BOTLIB
3231 botimport.Print(PRT_ERROR, "file %s still open in precompiler\n", sourceFiles[i]->scriptstack->filename);
3232 #endif //BOTLIB
3233 } //end if
3234 } //end for
3235 } //end of the function PC_CheckOpenSourceHandles
3236