| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#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 |
| 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 |
| 64 |
|
| 65 |
#ifdef MEQCC |
| 66 |
#include "qcc.h" |
| 67 |
#include "time.h" |
| 68 |
#include "math.h" |
| 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 |
| 77 |
|
| 78 |
#ifdef BSPC |
| 79 |
|
| 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 |
| 90 |
|
| 91 |
#if defined(QUAKE) && !defined(BSPC) |
| 92 |
#include "l_utils.h" |
| 93 |
#endif |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#define MAX_DEFINEPARMS 128 |
| 98 |
|
| 99 |
#define DEFINEHASHING 1 |
| 100 |
|
| 101 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
define_t *globaldefines; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
| 139 |
#ifdef MEQCC |
| 140 |
printf("error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text); |
| 141 |
#endif |
| 142 |
#ifdef BSPC |
| 143 |
Log_Print("error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text); |
| 144 |
#endif |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
| 163 |
#ifdef MEQCC |
| 164 |
printf("warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text); |
| 165 |
#endif |
| 166 |
#ifdef BSPC |
| 167 |
Log_Print("warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text); |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 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 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 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 |
|
| 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 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 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 |
} |
| 230 |
} |
| 231 |
|
| 232 |
script->next = source->scriptstack; |
| 233 |
source->scriptstack = script; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
void PC_InitTokenHeap(void) |
| 242 |
{ |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
token_t *PC_CopyToken(token_t *token) |
| 263 |
{ |
| 264 |
token_t *t; |
| 265 |
|
| 266 |
|
| 267 |
t = (token_t *) GetMemory(sizeof(token_t)); |
| 268 |
|
| 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 |
} |
| 278 |
|
| 279 |
Com_Memcpy(t, token, sizeof(token_t)); |
| 280 |
t->next = NULL; |
| 281 |
numtokens++; |
| 282 |
return t; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
void PC_FreeToken(token_t *token) |
| 291 |
{ |
| 292 |
|
| 293 |
FreeMemory(token); |
| 294 |
|
| 295 |
|
| 296 |
numtokens--; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 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 |
|
| 311 |
while(!source->tokens) |
| 312 |
{ |
| 313 |
|
| 314 |
if (PS_ReadToken(source->scriptstack, token)) return qtrue; |
| 315 |
|
| 316 |
if (EndOfScript(source->scriptstack)) |
| 317 |
{ |
| 318 |
|
| 319 |
while(source->indentstack && |
| 320 |
source->indentstack->script == source->scriptstack) |
| 321 |
{ |
| 322 |
SourceWarning(source, "missing #endif"); |
| 323 |
PC_PopIndent(source, &type, &skip); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (!source->scriptstack->next) return qfalse; |
| 328 |
|
| 329 |
script = source->scriptstack; |
| 330 |
source->scriptstack = source->scriptstack->next; |
| 331 |
FreeScript(script); |
| 332 |
} |
| 333 |
|
| 334 |
Com_Memcpy(token, source->tokens, sizeof(token_t)); |
| 335 |
|
| 336 |
t = source->tokens; |
| 337 |
source->tokens = source->tokens->next; |
| 338 |
PC_FreeToken(t); |
| 339 |
return qtrue; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 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 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 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 |
} |
| 372 |
|
| 373 |
if (define->numparms > maxparms) |
| 374 |
{ |
| 375 |
SourceError(source, "define with more than %d parameters", maxparms); |
| 376 |
return qfalse; |
| 377 |
} |
| 378 |
|
| 379 |
for (i = 0; i < define->numparms; i++) parms[i] = NULL; |
| 380 |
|
| 381 |
if (strcmp(token.string, "(")) |
| 382 |
{ |
| 383 |
PC_UnreadSourceToken(source, &token); |
| 384 |
SourceError(source, "define %s missing parms", define->name); |
| 385 |
return qfalse; |
| 386 |
} |
| 387 |
|
| 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 |
} |
| 395 |
if (numparms >= define->numparms) |
| 396 |
{ |
| 397 |
SourceWarning(source, "define %s has too many parms", define->name); |
| 398 |
return qfalse; |
| 399 |
} |
| 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 |
} |
| 411 |
|
| 412 |
if (!strcmp(token.string, ",")) |
| 413 |
{ |
| 414 |
if (indent <= 0) |
| 415 |
{ |
| 416 |
if (lastcomma) SourceWarning(source, "too many comma's"); |
| 417 |
break; |
| 418 |
} |
| 419 |
} |
| 420 |
lastcomma = 0; |
| 421 |
|
| 422 |
if (!strcmp(token.string, "(")) |
| 423 |
{ |
| 424 |
indent++; |
| 425 |
continue; |
| 426 |
} |
| 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 |
} |
| 435 |
done = 1; |
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 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 |
} |
| 449 |
} |
| 450 |
numparms++; |
| 451 |
} |
| 452 |
return qtrue; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 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 |
} |
| 474 |
strncat(token->string, "\"", MAX_TOKEN - strlen(token->string) - 1); |
| 475 |
return qtrue; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
int PC_MergeTokens(token_t *t1, token_t *t2) |
| 484 |
{ |
| 485 |
|
| 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 |
} |
| 491 |
|
| 492 |
if (t1->type == TT_STRING && t2->type == TT_STRING) |
| 493 |
{ |
| 494 |
|
| 495 |
t1->string[strlen(t1->string)-1] = '\0'; |
| 496 |
|
| 497 |
strcat(t1->string, &t2->string[1]); |
| 498 |
return qtrue; |
| 499 |
} |
| 500 |
|
| 501 |
return qfalse; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
#if DEFINEHASHING |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 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 |
} |
| 539 |
Log_Write("\n"); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 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 |
|
| 559 |
|
| 560 |
} |
| 561 |
hash = (hash ^ (hash >> 10) ^ (hash >> 20)) & (DEFINEHASHSIZE-1); |
| 562 |
return hash; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 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 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 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 |
} |
| 594 |
return NULL; |
| 595 |
} |
| 596 |
#endif |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 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 |
} |
| 611 |
return NULL; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 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 |
} |
| 631 |
return -1; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
void PC_FreeDefine(define_t *define) |
| 640 |
{ |
| 641 |
token_t *t, *next; |
| 642 |
|
| 643 |
|
| 644 |
for (t = define->parms; t; t = next) |
| 645 |
{ |
| 646 |
next = t->next; |
| 647 |
PC_FreeToken(t); |
| 648 |
} |
| 649 |
|
| 650 |
for (t = define->tokens; t; t = next) |
| 651 |
{ |
| 652 |
next = t->next; |
| 653 |
PC_FreeToken(t); |
| 654 |
} |
| 655 |
|
| 656 |
FreeMemory(define->name); |
| 657 |
FreeMemory(define); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 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 |
|
| 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 |
|
| 691 |
#if DEFINEHASHING |
| 692 |
PC_AddDefineToHash(define, source->definehash); |
| 693 |
#else |
| 694 |
define->next = source->defines; |
| 695 |
source->defines = define; |
| 696 |
#endif |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 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 |
| 723 |
token->type = TT_NUMBER; |
| 724 |
token->subtype = TT_DECIMAL | TT_INTEGER; |
| 725 |
*firsttoken = token; |
| 726 |
*lasttoken = token; |
| 727 |
break; |
| 728 |
} |
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 767 |
case BUILTIN_STDC: |
| 768 |
default: |
| 769 |
{ |
| 770 |
*firsttoken = NULL; |
| 771 |
*lasttoken = NULL; |
| 772 |
break; |
| 773 |
} |
| 774 |
} |
| 775 |
return qtrue; |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 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 |
|
| 791 |
if (define->builtin) |
| 792 |
{ |
| 793 |
return PC_ExpandBuiltinDefine(source, deftoken, define, firsttoken, lasttoken); |
| 794 |
} |
| 795 |
|
| 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 |
} |
| 807 |
} |
| 808 |
#endif |
| 809 |
} |
| 810 |
|
| 811 |
first = NULL; |
| 812 |
last = NULL; |
| 813 |
|
| 814 |
for (dt = define->tokens; dt; dt = dt->next) |
| 815 |
{ |
| 816 |
parmnum = -1; |
| 817 |
|
| 818 |
if (dt->type == TT_NAME) |
| 819 |
{ |
| 820 |
parmnum = PC_FindDefineParm(define, dt->string); |
| 821 |
} |
| 822 |
|
| 823 |
if (parmnum >= 0) |
| 824 |
{ |
| 825 |
for (pt = parms[parmnum]; pt; pt = pt->next) |
| 826 |
{ |
| 827 |
t = PC_CopyToken(pt); |
| 828 |
|
| 829 |
t->next = NULL; |
| 830 |
if (last) last->next = t; |
| 831 |
else first = t; |
| 832 |
last = t; |
| 833 |
} |
| 834 |
} |
| 835 |
else |
| 836 |
{ |
| 837 |
|
| 838 |
if (dt->string[0] == '#' && dt->string[1] == '\0') |
| 839 |
{ |
| 840 |
|
| 841 |
if (dt->next) parmnum = PC_FindDefineParm(define, dt->next->string); |
| 842 |
else parmnum = -1; |
| 843 |
|
| 844 |
if (parmnum >= 0) |
| 845 |
{ |
| 846 |
|
| 847 |
dt = dt->next; |
| 848 |
|
| 849 |
if (!PC_StringizeTokens(parms[parmnum], &token)) |
| 850 |
{ |
| 851 |
SourceError(source, "can't stringize tokens"); |
| 852 |
return qfalse; |
| 853 |
} |
| 854 |
t = PC_CopyToken(&token); |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
SourceWarning(source, "stringizing operator without define parameter"); |
| 859 |
continue; |
| 860 |
} |
| 861 |
} |
| 862 |
else |
| 863 |
{ |
| 864 |
t = PC_CopyToken(dt); |
| 865 |
} |
| 866 |
|
| 867 |
t->next = NULL; |
| 868 |
if (last) last->next = t; |
| 869 |
else first = t; |
| 870 |
last = t; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
for (t = first; t; ) |
| 875 |
{ |
| 876 |
if (t->next) |
| 877 |
{ |
| 878 |
|
| 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 |
} |
| 890 |
PC_FreeToken(t1->next); |
| 891 |
t1->next = t2->next; |
| 892 |
if (t2 == last) last = t1; |
| 893 |
PC_FreeToken(t2); |
| 894 |
continue; |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
t = t->next; |
| 899 |
} |
| 900 |
|
| 901 |
*firsttoken = first; |
| 902 |
*lasttoken = last; |
| 903 |
|
| 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 |
} |
| 911 |
} |
| 912 |
|
| 913 |
return qtrue; |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 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 |
} |
| 933 |
return qfalse; |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
void PC_ConvertPath(char *path) |
| 942 |
{ |
| 943 |
char *ptr; |
| 944 |
|
| 945 |
|
| 946 |
for (ptr = path; *ptr;) |
| 947 |
{ |
| 948 |
if ((*ptr == '\\' || *ptr == '/') && |
| 949 |
(*(ptr+1) == '\\' || *(ptr+1) == '/')) |
| 950 |
{ |
| 951 |
memmove(ptr, ptr+1, strlen(ptr)); |
| 952 |
} |
| 953 |
else |
| 954 |
{ |
| 955 |
ptr++; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
for (ptr = path; *ptr;) |
| 960 |
{ |
| 961 |
if (*ptr == '/' || *ptr == '\\') *ptr = PATHSEPERATOR_CHAR; |
| 962 |
ptr++; |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 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 |
| 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 |
} |
| 987 |
if (token.linescrossed > 0) |
| 988 |
{ |
| 989 |
SourceError(source, "#include without file name"); |
| 990 |
return qfalse; |
| 991 |
} |
| 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 |
} |
| 1003 |
} |
| 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 |
} |
| 1014 |
if (token.type == TT_PUNCTUATION && *token.string == '>') break; |
| 1015 |
Q_strcat(path, sizeof(path), token.string); |
| 1016 |
} |
| 1017 |
if (*token.string != '>') |
| 1018 |
{ |
| 1019 |
SourceWarning(source, "#include missing trailing >"); |
| 1020 |
} |
| 1021 |
if (!strlen(path)) |
| 1022 |
{ |
| 1023 |
SourceError(source, "#include without file name between < >"); |
| 1024 |
return qfalse; |
| 1025 |
} |
| 1026 |
PC_ConvertPath(path); |
| 1027 |
script = LoadScriptFile(path); |
| 1028 |
} |
| 1029 |
else |
| 1030 |
{ |
| 1031 |
SourceError(source, "#include without file name"); |
| 1032 |
return qfalse; |
| 1033 |
} |
| 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 |
} |
| 1041 |
#endif |
| 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 |
| 1051 |
} |
| 1052 |
PC_PushScript(source, script); |
| 1053 |
return qtrue; |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 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 |
} |
| 1077 |
crossline = 1; |
| 1078 |
} while(!strcmp(token->string, "\\")); |
| 1079 |
return qtrue; |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
int PC_WhiteSpaceBeforeToken(token_t *token) |
| 1088 |
{ |
| 1089 |
return token->endwhitespace_p - token->whitespace_p > 0; |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
void PC_ClearTokenWhiteSpace(token_t *token) |
| 1098 |
{ |
| 1099 |
token->whitespace_p = NULL; |
| 1100 |
token->endwhitespace_p = NULL; |
| 1101 |
token->linescrossed = 0; |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 1139 |
else |
| 1140 |
{ |
| 1141 |
if (lastdefine) lastdefine->hashnext = define->hashnext; |
| 1142 |
else source->definehash[hash] = define->hashnext; |
| 1143 |
PC_FreeDefine(define); |
| 1144 |
} |
| 1145 |
break; |
| 1146 |
} |
| 1147 |
lastdefine = define; |
| 1148 |
} |
| 1149 |
#else |
| 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 |
} |
| 1158 |
else |
| 1159 |
{ |
| 1160 |
if (lastdefine) lastdefine->next = define->next; |
| 1161 |
else source->defines = define->next; |
| 1162 |
PC_FreeDefine(define); |
| 1163 |
} |
| 1164 |
break; |
| 1165 |
} |
| 1166 |
lastdefine = define; |
| 1167 |
} |
| 1168 |
#endif |
| 1169 |
return qtrue; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 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 |
} |
| 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 |
} |
| 1195 |
|
| 1196 |
#if DEFINEHASHING |
| 1197 |
define = PC_FindHashedDefine(source->definehash, token.string); |
| 1198 |
#else |
| 1199 |
define = PC_FindDefine(source->defines, token.string); |
| 1200 |
#endif |
| 1201 |
if (define) |
| 1202 |
{ |
| 1203 |
if (define->flags & DEFINE_FIXED) |
| 1204 |
{ |
| 1205 |
SourceError(source, "can't redefine %s", token.string); |
| 1206 |
return qfalse; |
| 1207 |
} |
| 1208 |
SourceWarning(source, "redefinition of %s", token.string); |
| 1209 |
|
| 1210 |
PC_UnreadSourceToken(source, &token); |
| 1211 |
if (!PC_Directive_undef(source)) return qfalse; |
| 1212 |
} |
| 1213 |
|
| 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 |
|
| 1219 |
#if DEFINEHASHING |
| 1220 |
PC_AddDefineToHash(define, source->definehash); |
| 1221 |
#else |
| 1222 |
define->next = source->defines; |
| 1223 |
source->defines = define; |
| 1224 |
#endif |
| 1225 |
|
| 1226 |
if (!PC_ReadLine(source, &token)) return qtrue; |
| 1227 |
|
| 1228 |
if (!PC_WhiteSpaceBeforeToken(&token) && !strcmp(token.string, "(")) |
| 1229 |
{ |
| 1230 |
|
| 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 |
} |
| 1241 |
|
| 1242 |
if (token.type != TT_NAME) |
| 1243 |
{ |
| 1244 |
SourceError(source, "invalid define parameter"); |
| 1245 |
return qfalse; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (PC_FindDefineParm(define, token.string) >= 0) |
| 1249 |
{ |
| 1250 |
SourceError(source, "two the same define parameters"); |
| 1251 |
return qfalse; |
| 1252 |
} |
| 1253 |
|
| 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 |
|
| 1262 |
if (!PC_ReadLine(source, &token)) |
| 1263 |
{ |
| 1264 |
SourceError(source, "define parameters not terminated"); |
| 1265 |
return qfalse; |
| 1266 |
} |
| 1267 |
|
| 1268 |
if (!strcmp(token.string, ")")) break; |
| 1269 |
|
| 1270 |
if (strcmp(token.string, ",")) |
| 1271 |
{ |
| 1272 |
SourceError(source, "define not terminated"); |
| 1273 |
return qfalse; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
} |
| 1277 |
if (!PC_ReadLine(source, &token)) return qtrue; |
| 1278 |
} |
| 1279 |
|
| 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 |
} |
| 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 |
|
| 1299 |
if (!strcmp(define->tokens->string, "##") || |
| 1300 |
!strcmp(last->string, "##")) |
| 1301 |
{ |
| 1302 |
SourceError(source, "define with misplaced ##"); |
| 1303 |
return qfalse; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
return qtrue; |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 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 |
|
| 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 |
| 1332 |
|
| 1333 |
res = PC_Directive_define(&src); |
| 1334 |
|
| 1335 |
for (t = src.tokens; t; t = src.tokens) |
| 1336 |
{ |
| 1337 |
src.tokens = src.tokens->next; |
| 1338 |
PC_FreeToken(t); |
| 1339 |
} |
| 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 |
} |
| 1349 |
} |
| 1350 |
#else |
| 1351 |
def = src.defines; |
| 1352 |
#endif |
| 1353 |
|
| 1354 |
#if DEFINEHASHING |
| 1355 |
FreeMemory(src.definehash); |
| 1356 |
#endif |
| 1357 |
|
| 1358 |
FreeScript(script); |
| 1359 |
|
| 1360 |
if (res > 0) return def; |
| 1361 |
|
| 1362 |
if (src.defines) PC_FreeDefine(def); |
| 1363 |
|
| 1364 |
return NULL; |
| 1365 |
} |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 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 |
| 1381 |
define->next = source->defines; |
| 1382 |
source->defines = define; |
| 1383 |
#endif |
| 1384 |
return qtrue; |
| 1385 |
} |
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 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 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 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 |
} |
| 1420 |
return qfalse; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 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 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 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 |
|
| 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 |
|
| 1458 |
newdefine->next = NULL; |
| 1459 |
newdefine->hashnext = NULL; |
| 1460 |
|
| 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 |
} |
| 1470 |
|
| 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 |
} |
| 1480 |
return newdefine; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 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 |
| 1498 |
newdefine->next = source->defines; |
| 1499 |
source->defines = newdefine; |
| 1500 |
#endif |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 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 |
} |
| 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 |
} |
| 1526 |
#if DEFINEHASHING |
| 1527 |
d = PC_FindHashedDefine(source->definehash, token.string); |
| 1528 |
#else |
| 1529 |
d = PC_FindDefine(source->defines, token.string); |
| 1530 |
#endif |
| 1531 |
skip = (type == INDENT_IFDEF) == (d == NULL); |
| 1532 |
PC_PushIndent(source, type, skip); |
| 1533 |
return qtrue; |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
int PC_Directive_ifdef(source_t *source) |
| 1542 |
{ |
| 1543 |
return PC_Directive_if_def(source, INDENT_IFDEF); |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
int PC_Directive_ifndef(source_t *source) |
| 1552 |
{ |
| 1553 |
return PC_Directive_if_def(source, INDENT_IFNDEF); |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 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 |
} |
| 1571 |
if (type == INDENT_ELSE) |
| 1572 |
{ |
| 1573 |
SourceError(source, "#else after #else"); |
| 1574 |
return qfalse; |
| 1575 |
} |
| 1576 |
PC_PushIndent(source, INDENT_ELSE, !skip); |
| 1577 |
return qtrue; |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 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 |
} |
| 1595 |
return qtrue; |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 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 |
} |
| 1651 |
return qfalse; |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 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 |
} |
| 1717 |
if (strcmp(t->string, "defined")) |
| 1718 |
{ |
| 1719 |
SourceError(source, "undefined name %s in #if/#elif", t->string); |
| 1720 |
error = 1; |
| 1721 |
break; |
| 1722 |
} |
| 1723 |
t = t->next; |
| 1724 |
if (!strcmp(t->string, "(")) |
| 1725 |
{ |
| 1726 |
brace = qtrue; |
| 1727 |
t = t->next; |
| 1728 |
} |
| 1729 |
if (!t || t->type != TT_NAME) |
| 1730 |
{ |
| 1731 |
SourceError(source, "defined without name in #if/#elif"); |
| 1732 |
error = 1; |
| 1733 |
break; |
| 1734 |
} |
| 1735 |
|
| 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 |
| 1742 |
{ |
| 1743 |
v->intvalue = 1; |
| 1744 |
v->floatvalue = 1; |
| 1745 |
} |
| 1746 |
else |
| 1747 |
{ |
| 1748 |
v->intvalue = 0; |
| 1749 |
v->floatvalue = 0; |
| 1750 |
} |
| 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 |
} |
| 1766 |
} |
| 1767 |
brace = qfalse; |
| 1768 |
|
| 1769 |
lastwasvalue = 1; |
| 1770 |
break; |
| 1771 |
} |
| 1772 |
case TT_NUMBER: |
| 1773 |
{ |
| 1774 |
if (lastwasvalue) |
| 1775 |
{ |
| 1776 |
SourceError(source, "syntax error in #if/#elif"); |
| 1777 |
error = 1; |
| 1778 |
break; |
| 1779 |
} |
| 1780 |
|
| 1781 |
AllocValue(v); |
| 1782 |
if (negativevalue) |
| 1783 |
{ |
| 1784 |
v->intvalue = - (signed int) t->intvalue; |
| 1785 |
v->floatvalue = - t->floatvalue; |
| 1786 |
} |
| 1787 |
else |
| 1788 |
{ |
| 1789 |
v->intvalue = t->intvalue; |
| 1790 |
v->floatvalue = t->floatvalue; |
| 1791 |
} |
| 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 |
|
| 1799 |
lastwasvalue = 1; |
| 1800 |
|
| 1801 |
negativevalue = 0; |
| 1802 |
break; |
| 1803 |
} |
| 1804 |
case TT_PUNCTUATION: |
| 1805 |
{ |
| 1806 |
if (negativevalue) |
| 1807 |
{ |
| 1808 |
SourceError(source, "misplaced minus sign in #if/#elif"); |
| 1809 |
error = 1; |
| 1810 |
break; |
| 1811 |
} |
| 1812 |
if (t->subtype == P_PARENTHESESOPEN) |
| 1813 |
{ |
| 1814 |
parentheses++; |
| 1815 |
break; |
| 1816 |
} |
| 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 |
} |
| 1825 |
break; |
| 1826 |
} |
| 1827 |
|
| 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 |
} |
| 1839 |
} |
| 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 |
} |
| 1851 |
break; |
| 1852 |
} |
| 1853 |
case P_INC: |
| 1854 |
case P_DEC: |
| 1855 |
{ |
| 1856 |
SourceError(source, "++ or -- used in #if/#elif"); |
| 1857 |
break; |
| 1858 |
} |
| 1859 |
case P_SUB: |
| 1860 |
{ |
| 1861 |
if (!lastwasvalue) |
| 1862 |
{ |
| 1863 |
negativevalue = 1; |
| 1864 |
break; |
| 1865 |
} |
| 1866 |
} |
| 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 |
} |
| 1899 |
break; |
| 1900 |
} |
| 1901 |
default: |
| 1902 |
{ |
| 1903 |
SourceError(source, "invalid operator %s in #if/#elif", t->string); |
| 1904 |
error = 1; |
| 1905 |
break; |
| 1906 |
} |
| 1907 |
} |
| 1908 |
if (!error && !negativevalue) |
| 1909 |
{ |
| 1910 |
|
| 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 |
} |
| 1922 |
break; |
| 1923 |
} |
| 1924 |
default: |
| 1925 |
{ |
| 1926 |
SourceError(source, "unknown %s in #if/#elif", t->string); |
| 1927 |
error = 1; |
| 1928 |
break; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
if (error) break; |
| 1932 |
} |
| 1933 |
if (!error) |
| 1934 |
{ |
| 1935 |
if (!lastwasvalue) |
| 1936 |
{ |
| 1937 |
SourceError(source, "trailing operator in #if/#elif"); |
| 1938 |
error = 1; |
| 1939 |
} |
| 1940 |
else if (parentheses) |
| 1941 |
{ |
| 1942 |
SourceError(source, "too many ( in #if/#elif"); |
| 1943 |
error = 1; |
| 1944 |
} |
| 1945 |
} |
| 1946 |
|
| 1947 |
gotquestmarkvalue = qfalse; |
| 1948 |
questmarkintvalue = 0; |
| 1949 |
questmarkfloatvalue = 0; |
| 1950 |
|
| 1951 |
while(!error && firstoperator) |
| 1952 |
{ |
| 1953 |
v = firstvalue; |
| 1954 |
for (o = firstoperator; o->next; o = o->next) |
| 1955 |
{ |
| 1956 |
|
| 1957 |
|
| 1958 |
if (o->parentheses > o->next->parentheses) break; |
| 1959 |
|
| 1960 |
if (o->parentheses == o->next->parentheses) |
| 1961 |
{ |
| 1962 |
|
| 1963 |
|
| 1964 |
if (o->priority >= o->next->priority) break; |
| 1965 |
} |
| 1966 |
|
| 1967 |
if (o->operator != P_LOGIC_NOT |
| 1968 |
&& o->operator != P_BIN_NOT) v = v->next; |
| 1969 |
|
| 1970 |
if (!v) |
| 1971 |
{ |
| 1972 |
SourceError(source, "mising values in #if/#elif"); |
| 1973 |
error = 1; |
| 1974 |
break; |
| 1975 |
} |
| 1976 |
} |
| 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 |
} |
| 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 |
} |
| 1991 |
#endif |
| 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 |
} |
| 2053 |
if (integer) |
| 2054 |
{ |
| 2055 |
if (!questmarkintvalue) v1->intvalue = v2->intvalue; |
| 2056 |
} |
| 2057 |
else |
| 2058 |
{ |
| 2059 |
if (!questmarkfloatvalue) v1->floatvalue = v2->floatvalue; |
| 2060 |
} |
| 2061 |
gotquestmarkvalue = qfalse; |
| 2062 |
break; |
| 2063 |
} |
| 2064 |
case P_QUESTIONMARK: |
| 2065 |
{ |
| 2066 |
if (gotquestmarkvalue) |
| 2067 |
{ |
| 2068 |
SourceError(source, "? after ? in #if/#elif"); |
| 2069 |
error = 1; |
| 2070 |
break; |
| 2071 |
} |
| 2072 |
questmarkintvalue = v1->intvalue; |
| 2073 |
questmarkfloatvalue = v1->floatvalue; |
| 2074 |
gotquestmarkvalue = qtrue; |
| 2075 |
break; |
| 2076 |
} |
| 2077 |
} |
| 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 |
| 2082 |
if (error) break; |
| 2083 |
|
| 2084 |
if (o->operator != P_LOGIC_NOT |
| 2085 |
&& o->operator != P_BIN_NOT) |
| 2086 |
{ |
| 2087 |
|
| 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 |
|
| 2097 |
FreeValue(v); |
| 2098 |
} |
| 2099 |
|
| 2100 |
if (o->prev) o->prev->next = o->next; |
| 2101 |
else firstoperator = o->next; |
| 2102 |
if (o->next) o->next->prev = o->prev; |
| 2103 |
|
| 2104 |
FreeOperator(o); |
| 2105 |
} |
| 2106 |
if (firstvalue) |
| 2107 |
{ |
| 2108 |
if (intvalue) *intvalue = firstvalue->intvalue; |
| 2109 |
if (floatvalue) *floatvalue = firstvalue->floatvalue; |
| 2110 |
} |
| 2111 |
for (o = firstoperator; o; o = lastoperator) |
| 2112 |
{ |
| 2113 |
lastoperator = o->next; |
| 2114 |
|
| 2115 |
FreeOperator(o); |
| 2116 |
} |
| 2117 |
for (v = firstvalue; v; v = lastvalue) |
| 2118 |
{ |
| 2119 |
lastvalue = v->next; |
| 2120 |
|
| 2121 |
FreeValue(v); |
| 2122 |
} |
| 2123 |
if (!error) return qtrue; |
| 2124 |
if (intvalue) *intvalue = 0; |
| 2125 |
if (floatvalue) *floatvalue = 0; |
| 2126 |
return qfalse; |
| 2127 |
} |
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 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 |
} |
| 2150 |
firsttoken = NULL; |
| 2151 |
lasttoken = NULL; |
| 2152 |
do |
| 2153 |
{ |
| 2154 |
|
| 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 |
} |
| 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 |
} |
| 2175 |
else |
| 2176 |
{ |
| 2177 |
|
| 2178 |
#if DEFINEHASHING |
| 2179 |
define = PC_FindHashedDefine(source->definehash, token.string); |
| 2180 |
#else |
| 2181 |
define = PC_FindDefine(source->defines, token.string); |
| 2182 |
#endif |
| 2183 |
if (!define) |
| 2184 |
{ |
| 2185 |
SourceError(source, "can't evaluate %s, not defined", token.string); |
| 2186 |
return qfalse; |
| 2187 |
} |
| 2188 |
if (!PC_ExpandDefineIntoSource(source, &token, define)) return qfalse; |
| 2189 |
} |
| 2190 |
} |
| 2191 |
|
| 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 |
} |
| 2200 |
else |
| 2201 |
{ |
| 2202 |
SourceError(source, "can't evaluate %s", token.string); |
| 2203 |
return qfalse; |
| 2204 |
} |
| 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 |
| 2212 |
for (t = firsttoken; t; t = nexttoken) |
| 2213 |
{ |
| 2214 |
#ifdef DEBUG_EVAL |
| 2215 |
Log_Write(" %s", t->string); |
| 2216 |
#endif |
| 2217 |
nexttoken = t->next; |
| 2218 |
PC_FreeToken(t); |
| 2219 |
} |
| 2220 |
#ifdef DEBUG_EVAL |
| 2221 |
if (integer) Log_Write("eval result: %d", *intvalue); |
| 2222 |
else Log_Write("eval result: %f", *floatvalue); |
| 2223 |
#endif |
| 2224 |
|
| 2225 |
return qtrue; |
| 2226 |
} |
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 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 |
} |
| 2249 |
if (!PC_ReadSourceToken(source, &token)) |
| 2250 |
{ |
| 2251 |
SourceError(source, "nothing to evaluate"); |
| 2252 |
return qfalse; |
| 2253 |
} |
| 2254 |
indent = 1; |
| 2255 |
firsttoken = NULL; |
| 2256 |
lasttoken = NULL; |
| 2257 |
do |
| 2258 |
{ |
| 2259 |
|
| 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 |
} |
| 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 |
} |
| 2280 |
else |
| 2281 |
{ |
| 2282 |
|
| 2283 |
#if DEFINEHASHING |
| 2284 |
define = PC_FindHashedDefine(source->definehash, token.string); |
| 2285 |
#else |
| 2286 |
define = PC_FindDefine(source->defines, token.string); |
| 2287 |
#endif |
| 2288 |
if (!define) |
| 2289 |
{ |
| 2290 |
SourceError(source, "can't evaluate %s, not defined", token.string); |
| 2291 |
return qfalse; |
| 2292 |
} |
| 2293 |
if (!PC_ExpandDefineIntoSource(source, &token, define)) return qfalse; |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 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 |
} |
| 2308 |
else |
| 2309 |
{ |
| 2310 |
SourceError(source, "can't evaluate %s", token.string); |
| 2311 |
return qfalse; |
| 2312 |
} |
| 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 |
| 2320 |
for (t = firsttoken; t; t = nexttoken) |
| 2321 |
{ |
| 2322 |
#ifdef DEBUG_EVAL |
| 2323 |
Log_Write(" %s", t->string); |
| 2324 |
#endif |
| 2325 |
nexttoken = t->next; |
| 2326 |
PC_FreeToken(t); |
| 2327 |
} |
| 2328 |
#ifdef DEBUG_EVAL |
| 2329 |
if (integer) Log_Write("$eval result: %d", *intvalue); |
| 2330 |
else Log_Write("$eval result: %f", *floatvalue); |
| 2331 |
#endif |
| 2332 |
|
| 2333 |
return qtrue; |
| 2334 |
} |
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 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 |
} |
| 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 |
} |
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 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 |
} |
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
int PC_Directive_line(source_t *source) |
| 2380 |
{ |
| 2381 |
SourceError(source, "#line directive not supported"); |
| 2382 |
return qfalse; |
| 2383 |
} |
| 2384 |
|
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 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 |
} |
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 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 |
} |
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 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 |
} |
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 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 |
} |
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 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 |
} |
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 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 |
|
| 2510 |
if (!PC_ReadSourceToken(source, &token)) |
| 2511 |
{ |
| 2512 |
SourceError(source, "found # without name"); |
| 2513 |
return qfalse; |
| 2514 |
} |
| 2515 |
|
| 2516 |
if (token.linescrossed > 0) |
| 2517 |
{ |
| 2518 |
PC_UnreadSourceToken(source, &token); |
| 2519 |
SourceError(source, "found # at end of line"); |
| 2520 |
return qfalse; |
| 2521 |
} |
| 2522 |
|
| 2523 |
if (token.type == TT_NAME) |
| 2524 |
{ |
| 2525 |
|
| 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 |
} |
| 2532 |
} |
| 2533 |
} |
| 2534 |
SourceError(source, "unknown precompiler directive %s", token.string); |
| 2535 |
return qfalse; |
| 2536 |
} |
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
|
| 2541 |
|
| 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 |
| 2562 |
|
| 2563 |
PC_UnreadSourceToken(source, &token); |
| 2564 |
if (value < 0) |
| 2565 |
UnreadSignToken(source); |
| 2566 |
|
| 2567 |
return qtrue; |
| 2568 |
} |
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
|
| 2573 |
|
| 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 |
| 2593 |
|
| 2594 |
PC_UnreadSourceToken(source, &token); |
| 2595 |
if (value < 0) |
| 2596 |
UnreadSignToken(source); |
| 2597 |
|
| 2598 |
return qtrue; |
| 2599 |
} |
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 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 |
|
| 2619 |
if (!PC_ReadSourceToken(source, &token)) |
| 2620 |
{ |
| 2621 |
SourceError(source, "found $ without name"); |
| 2622 |
return qfalse; |
| 2623 |
} |
| 2624 |
|
| 2625 |
if (token.linescrossed > 0) |
| 2626 |
{ |
| 2627 |
PC_UnreadSourceToken(source, &token); |
| 2628 |
SourceError(source, "found $ at end of line"); |
| 2629 |
return qfalse; |
| 2630 |
} |
| 2631 |
|
| 2632 |
if (token.type == TT_NAME) |
| 2633 |
{ |
| 2634 |
|
| 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 |
} |
| 2641 |
} |
| 2642 |
} |
| 2643 |
PC_UnreadSourceToken(source, &token); |
| 2644 |
SourceError(source, "unknown precompiler directive %s", token.string); |
| 2645 |
return qfalse; |
| 2646 |
} |
| 2647 |
|
| 2648 |
#ifdef QUAKEC |
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 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 |
} |
| 2665 |
else |
| 2666 |
{ |
| 2667 |
PC_UnreadSourceToken(source, &token); |
| 2668 |
return qfalse; |
| 2669 |
} |
| 2670 |
} |
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 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 |
} |
| 2688 |
|
| 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 |
} |
| 2696 |
} |
| 2697 |
PC_UnreadSourceToken(source, &token); |
| 2698 |
return qtrue; |
| 2699 |
} |
| 2700 |
#endif |
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 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 |
|
| 2715 |
if (token->type == TT_PUNCTUATION && *token->string == '#') |
| 2716 |
{ |
| 2717 |
#ifdef QUAKEC |
| 2718 |
if (!BuiltinFunction(source)) |
| 2719 |
#endif |
| 2720 |
{ |
| 2721 |
|
| 2722 |
if (!PC_ReadDirective(source)) return qfalse; |
| 2723 |
continue; |
| 2724 |
} |
| 2725 |
} |
| 2726 |
if (token->type == TT_PUNCTUATION && *token->string == '$') |
| 2727 |
{ |
| 2728 |
#ifdef QUAKEC |
| 2729 |
if (!QuakeCMacro(source)) |
| 2730 |
#endif |
| 2731 |
{ |
| 2732 |
|
| 2733 |
if (!PC_ReadDollarDirective(source)) return qfalse; |
| 2734 |
continue; |
| 2735 |
} |
| 2736 |
} |
| 2737 |
|
| 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 |
} |
| 2759 |
|
| 2760 |
if (source->skip) continue; |
| 2761 |
|
| 2762 |
if (token->type == TT_NAME) |
| 2763 |
{ |
| 2764 |
|
| 2765 |
#if DEFINEHASHING |
| 2766 |
define = PC_FindHashedDefine(source->definehash, token->string); |
| 2767 |
#else |
| 2768 |
define = PC_FindDefine(source->defines, token->string); |
| 2769 |
#endif |
| 2770 |
|
| 2771 |
if (define) |
| 2772 |
{ |
| 2773 |
|
| 2774 |
if (!PC_ExpandDefineIntoSource(source, token, define)) return qfalse; |
| 2775 |
continue; |
| 2776 |
} |
| 2777 |
} |
| 2778 |
|
| 2779 |
Com_Memcpy(&source->token, token, sizeof(token_t)); |
| 2780 |
|
| 2781 |
return qtrue; |
| 2782 |
} |
| 2783 |
} |
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 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 |
} |
| 2799 |
|
| 2800 |
if (strcmp(token.string, string)) |
| 2801 |
{ |
| 2802 |
SourceError(source, "expected %s, found %s", string, token.string); |
| 2803 |
return qfalse; |
| 2804 |
} |
| 2805 |
return qtrue; |
| 2806 |
} |
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 2850 |
} |
| 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 |
} |
| 2858 |
} |
| 2859 |
return qtrue; |
| 2860 |
} |
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
|
| 2865 |
|
| 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 |
} |
| 2874 |
else |
| 2875 |
{ |
| 2876 |
return qtrue; |
| 2877 |
} |
| 2878 |
} |
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 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 |
|
| 2891 |
if (!strcmp(tok.string, string)) return qtrue; |
| 2892 |
|
| 2893 |
PC_UnreadSourceToken(source, &tok); |
| 2894 |
return qfalse; |
| 2895 |
} |
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 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 |
|
| 2908 |
if (tok.type == type && |
| 2909 |
(tok.subtype & subtype) == subtype) |
| 2910 |
{ |
| 2911 |
Com_Memcpy(token, &tok, sizeof(token_t)); |
| 2912 |
return qtrue; |
| 2913 |
} |
| 2914 |
|
| 2915 |
PC_UnreadSourceToken(source, &tok); |
| 2916 |
return qfalse; |
| 2917 |
} |
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 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 |
} |
| 2932 |
return qfalse; |
| 2933 |
} |
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
void PC_UnreadLastToken(source_t *source) |
| 2941 |
{ |
| 2942 |
PC_UnreadSourceToken(source, &source->token); |
| 2943 |
} |
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
void PC_UnreadToken(source_t *source, token_t *token) |
| 2951 |
{ |
| 2952 |
PC_UnreadSourceToken(source, token); |
| 2953 |
} |
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 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 |
|
| 2968 |
if (len > 0 && source->includepath[len-1] != '\\' && |
| 2969 |
source->includepath[len-1] != '/') |
| 2970 |
{ |
| 2971 |
strcat(source->includepath, PATHSEPERATOR_STR); |
| 2972 |
} |
| 2973 |
} |
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
void PC_SetPunctuations(source_t *source, punctuation_t *p) |
| 2981 |
{ |
| 2982 |
source->punctuations = p; |
| 2983 |
} |
| 2984 |
|
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 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 |
| 3015 |
PC_AddGlobalDefinesToSource(source); |
| 3016 |
return source; |
| 3017 |
} |
| 3018 |
|
| 3019 |
|
| 3020 |
|
| 3021 |
|
| 3022 |
|
| 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 |
| 3048 |
PC_AddGlobalDefinesToSource(source); |
| 3049 |
return source; |
| 3050 |
} |
| 3051 |
|
| 3052 |
|
| 3053 |
|
| 3054 |
|
| 3055 |
|
| 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 |
|
| 3066 |
|
| 3067 |
while(source->scriptstack) |
| 3068 |
{ |
| 3069 |
script = source->scriptstack; |
| 3070 |
source->scriptstack = source->scriptstack->next; |
| 3071 |
FreeScript(script); |
| 3072 |
} |
| 3073 |
|
| 3074 |
while(source->tokens) |
| 3075 |
{ |
| 3076 |
token = source->tokens; |
| 3077 |
source->tokens = source->tokens->next; |
| 3078 |
PC_FreeToken(token); |
| 3079 |
} |
| 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 |
} |
| 3089 |
} |
| 3090 |
#else |
| 3091 |
|
| 3092 |
while(source->defines) |
| 3093 |
{ |
| 3094 |
define = source->defines; |
| 3095 |
source->defines = source->defines->next; |
| 3096 |
PC_FreeDefine(define); |
| 3097 |
} |
| 3098 |
#endif |
| 3099 |
|
| 3100 |
while(source->indentstack) |
| 3101 |
{ |
| 3102 |
indent = source->indentstack; |
| 3103 |
source->indentstack = source->indentstack->next; |
| 3104 |
FreeMemory(indent); |
| 3105 |
} |
| 3106 |
#if DEFINEHASHING |
| 3107 |
|
| 3108 |
if (source->definehash) FreeMemory(source->definehash); |
| 3109 |
#endif |
| 3110 |
|
| 3111 |
FreeMemory(source); |
| 3112 |
} |
| 3113 |
|
| 3114 |
|
| 3115 |
|
| 3116 |
|
| 3117 |
|
| 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 |
} |
| 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 |
} |
| 3143 |
|
| 3144 |
|
| 3145 |
|
| 3146 |
|
| 3147 |
|
| 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 |
} |
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 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 |
} |
| 3186 |
|
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 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 |
} |
| 3206 |
|
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
void PC_SetBaseFolder(char *path) |
| 3213 |
{ |
| 3214 |
PS_SetBaseFolder(path); |
| 3215 |
} |
| 3216 |
|
| 3217 |
|
| 3218 |
|
| 3219 |
|
| 3220 |
|
| 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 |
| 3233 |
} |
| 3234 |
} |
| 3235 |
} |
| 3236 |
|