pr_comp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /* Copyright (C) 1996-1997 Id Software, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. See file, 'COPYING', for details.
  14. */
  15. #include "qcc.h"
  16. pr_info_t pr;
  17. def_t *pr_global_defs[MAX_REGS]; // to find def for a global variable
  18. int pr_edict_size;
  19. //========================================
  20. def_t *pr_scope; // the function being parsed, or NULL
  21. boolean pr_dumpasm;
  22. string_t s_file; // filename for function definition
  23. int locals_end; // for tracking local variables vs temps
  24. jmp_buf pr_parse_abort; // longjump with this on parse error
  25. void PR_ParseDefs (void);
  26. //========================================
  27. opcode_t pr_opcodes[] =
  28. {
  29. {"<DONE>", "DONE", -1, false, &def_entity, &def_field, &def_void},
  30. {"*", "MUL_F", 2, false, &def_float, &def_float, &def_float},
  31. {"*", "MUL_V", 2, false, &def_vector, &def_vector, &def_float},
  32. {"*", "MUL_FV", 2, false, &def_float, &def_vector, &def_vector},
  33. {"*", "MUL_VF", 2, false, &def_vector, &def_float, &def_vector},
  34. {"/", "DIV", 2, false, &def_float, &def_float, &def_float},
  35. {"+", "ADD_F", 3, false, &def_float, &def_float, &def_float},
  36. {"+", "ADD_V", 3, false, &def_vector, &def_vector, &def_vector},
  37. {"-", "SUB_F", 3, false, &def_float, &def_float, &def_float},
  38. {"-", "SUB_V", 3, false, &def_vector, &def_vector, &def_vector},
  39. {"==", "EQ_F", 4, false, &def_float, &def_float, &def_float},
  40. {"==", "EQ_V", 4, false, &def_vector, &def_vector, &def_float},
  41. {"==", "EQ_S", 4, false, &def_string, &def_string, &def_float},
  42. {"==", "EQ_E", 4, false, &def_entity, &def_entity, &def_float},
  43. {"==", "EQ_FNC", 4, false, &def_function, &def_function, &def_float},
  44. {"!=", "NE_F", 4, false, &def_float, &def_float, &def_float},
  45. {"!=", "NE_V", 4, false, &def_vector, &def_vector, &def_float},
  46. {"!=", "NE_S", 4, false, &def_string, &def_string, &def_float},
  47. {"!=", "NE_E", 4, false, &def_entity, &def_entity, &def_float},
  48. {"!=", "NE_FNC", 4, false, &def_function, &def_function, &def_float},
  49. {"<=", "LE", 4, false, &def_float, &def_float, &def_float},
  50. {">=", "GE", 4, false, &def_float, &def_float, &def_float},
  51. {"<", "LT", 4, false, &def_float, &def_float, &def_float},
  52. {">", "GT", 4, false, &def_float, &def_float, &def_float},
  53. {".", "INDIRECT", 1, false, &def_entity, &def_field, &def_float},
  54. {".", "INDIRECT", 1, false, &def_entity, &def_field, &def_vector},
  55. {".", "INDIRECT", 1, false, &def_entity, &def_field, &def_string},
  56. {".", "INDIRECT", 1, false, &def_entity, &def_field, &def_entity},
  57. {".", "INDIRECT", 1, false, &def_entity, &def_field, &def_field},
  58. {".", "INDIRECT", 1, false, &def_entity, &def_field, &def_function},
  59. {".", "ADDRESS", 1, false, &def_entity, &def_field, &def_pointer},
  60. {"=", "STORE_F", 5, true, &def_float, &def_float, &def_float},
  61. {"=", "STORE_V", 5, true, &def_vector, &def_vector, &def_vector},
  62. {"=", "STORE_S", 5, true, &def_string, &def_string, &def_string},
  63. {"=", "STORE_ENT", 5, true, &def_entity, &def_entity, &def_entity},
  64. {"=", "STORE_FLD", 5, true, &def_field, &def_field, &def_field},
  65. {"=", "STORE_FNC", 5, true, &def_function, &def_function, &def_function},
  66. {"=", "STOREP_F", 5, true, &def_pointer, &def_float, &def_float},
  67. {"=", "STOREP_V", 5, true, &def_pointer, &def_vector, &def_vector},
  68. {"=", "STOREP_S", 5, true, &def_pointer, &def_string, &def_string},
  69. {"=", "STOREP_ENT", 5, true, &def_pointer, &def_entity, &def_entity},
  70. {"=", "STOREP_FLD", 5, true, &def_pointer, &def_field, &def_field},
  71. {"=", "STOREP_FNC", 5, true, &def_pointer, &def_function, &def_function},
  72. {"<RETURN>", "RETURN", -1, false, &def_void, &def_void, &def_void},
  73. {"!", "NOT_F", -1, false, &def_float, &def_void, &def_float},
  74. {"!", "NOT_V", -1, false, &def_vector, &def_void, &def_float},
  75. {"!", "NOT_S", -1, false, &def_vector, &def_void, &def_float},
  76. {"!", "NOT_ENT", -1, false, &def_entity, &def_void, &def_float},
  77. {"!", "NOT_FNC", -1, false, &def_function, &def_void, &def_float},
  78. {"<IF>", "IF", -1, false, &def_float, &def_float, &def_void},
  79. {"<IFNOT>", "IFNOT", -1, false, &def_float, &def_float, &def_void},
  80. // calls returns REG_RETURN
  81. {"<CALL0>", "CALL0", -1, false, &def_function, &def_void, &def_void},
  82. {"<CALL1>", "CALL1", -1, false, &def_function, &def_void, &def_void},
  83. {"<CALL2>", "CALL2", -1, false, &def_function, &def_void, &def_void},
  84. {"<CALL3>", "CALL3", -1, false, &def_function, &def_void, &def_void},
  85. {"<CALL4>", "CALL4", -1, false, &def_function, &def_void, &def_void},
  86. {"<CALL5>", "CALL5", -1, false, &def_function, &def_void, &def_void},
  87. {"<CALL6>", "CALL6", -1, false, &def_function, &def_void, &def_void},
  88. {"<CALL7>", "CALL7", -1, false, &def_function, &def_void, &def_void},
  89. {"<CALL8>", "CALL8", -1, false, &def_function, &def_void, &def_void},
  90. {"<STATE>", "STATE", -1, false, &def_float, &def_float, &def_void},
  91. {"<GOTO>", "GOTO", -1, false, &def_float, &def_void, &def_void},
  92. {"&&", "AND", 6, false, &def_float, &def_float, &def_float},
  93. {"||", "OR", 6, false, &def_float, &def_float, &def_float},
  94. {"&", "BITAND", 2, false, &def_float, &def_float, &def_float},
  95. {"|", "BITOR", 2, false, &def_float, &def_float, &def_float},
  96. {NULL}
  97. };
  98. #define TOP_PRIORITY 6
  99. #define NOT_PRIORITY 4
  100. def_t *PR_Expression (int priority);
  101. def_t junkdef;
  102. //===========================================================================
  103. /*
  104. ============
  105. PR_Statement
  106. Emits a primitive statement, returning the var it places it's value in
  107. ============
  108. */
  109. def_t *PR_Statement ( opcode_t *op, def_t *var_a, def_t *var_b)
  110. {
  111. dstatement_t *statement;
  112. def_t *var_c;
  113. statement = &statements[numstatements];
  114. numstatements++;
  115. statement_linenums[statement-statements] = pr_source_line;
  116. statement->op = op - pr_opcodes;
  117. statement->a = var_a ? var_a->ofs : 0;
  118. statement->b = var_b ? var_b->ofs : 0;
  119. if (op->type_c == &def_void || op->right_associative)
  120. {
  121. var_c = NULL;
  122. statement->c = 0; // ifs, gotos, and assignments
  123. // don't need vars allocated
  124. }
  125. else
  126. { // allocate result space
  127. var_c = malloc (sizeof(def_t));
  128. memset (var_c, 0, sizeof(def_t));
  129. var_c->ofs = numpr_globals;
  130. var_c->type = op->type_c->type;
  131. statement->c = numpr_globals;
  132. numpr_globals += type_size[op->type_c->type->type];
  133. }
  134. if (op->right_associative)
  135. return var_a;
  136. return var_c;
  137. }
  138. /*
  139. ============
  140. PR_ParseImmediate
  141. Looks for a preexisting constant
  142. ============
  143. */
  144. def_t *PR_ParseImmediate (void)
  145. {
  146. def_t *cn;
  147. // check for a constant with the same value
  148. for (cn=pr.def_head.next ; cn ; cn=cn->next)
  149. {
  150. if (!cn->initialized)
  151. continue;
  152. if (cn->type != pr_immediate_type)
  153. continue;
  154. if (pr_immediate_type == &type_string)
  155. {
  156. if (!strcmp(G_STRING(cn->ofs), pr_immediate_string) )
  157. {
  158. PR_Lex ();
  159. return cn;
  160. }
  161. }
  162. else if (pr_immediate_type == &type_float)
  163. {
  164. if ( G_FLOAT(cn->ofs) == pr_immediate._float )
  165. {
  166. PR_Lex ();
  167. return cn;
  168. }
  169. }
  170. else if (pr_immediate_type == &type_vector)
  171. {
  172. if ( ( G_FLOAT(cn->ofs) == pr_immediate.vector[0] )
  173. && ( G_FLOAT(cn->ofs+1) == pr_immediate.vector[1] )
  174. && ( G_FLOAT(cn->ofs+2) == pr_immediate.vector[2] ) )
  175. {
  176. PR_Lex ();
  177. return cn;
  178. }
  179. }
  180. else
  181. PR_ParseError ("weird immediate type");
  182. }
  183. // allocate a new one
  184. cn = malloc (sizeof(def_t));
  185. cn->next = NULL;
  186. pr.def_tail->next = cn;
  187. pr.def_tail = cn;
  188. cn->type = pr_immediate_type;
  189. cn->name = "IMMEDIATE";
  190. cn->initialized = 1;
  191. cn->scope = NULL; // always share immediates
  192. // copy the immediate to the global area
  193. cn->ofs = numpr_globals;
  194. pr_global_defs[cn->ofs] = cn;
  195. numpr_globals += type_size[pr_immediate_type->type];
  196. if (pr_immediate_type == &type_string)
  197. pr_immediate.string = CopyString (pr_immediate_string);
  198. memcpy (pr_globals + cn->ofs, &pr_immediate, 4*type_size[pr_immediate_type->type]);
  199. PR_Lex ();
  200. return cn;
  201. }
  202. void PrecacheSound (def_t *e, int ch)
  203. {
  204. char *n;
  205. int i;
  206. if (!e->ofs)
  207. return;
  208. n = G_STRING(e->ofs);
  209. for (i=0 ; i<numsounds ; i++)
  210. if (!strcmp(n, precache_sounds[i]))
  211. return;
  212. if (numsounds == MAX_SOUNDS)
  213. Error ("PrecacheSound: numsounds == MAX_SOUNDS");
  214. strcpy (precache_sounds[i], n);
  215. if (ch >= '1' && ch <= '9')
  216. precache_sounds_block[i] = ch - '0';
  217. else
  218. precache_sounds_block[i] = 1;
  219. numsounds++;
  220. }
  221. void PrecacheModel (def_t *e, int ch)
  222. {
  223. char *n;
  224. int i;
  225. if (!e->ofs)
  226. return;
  227. n = G_STRING(e->ofs);
  228. for (i=0 ; i<nummodels ; i++)
  229. if (!strcmp(n, precache_models[i]))
  230. return;
  231. if (numsounds == MAX_SOUNDS)
  232. Error ("PrecacheModels: numsounds == MAX_SOUNDS");
  233. strcpy (precache_models[i], n);
  234. if (ch >= '1' && ch <= '9')
  235. precache_models_block[i] = ch - '0';
  236. else
  237. precache_models_block[i] = 1;
  238. nummodels++;
  239. }
  240. void PrecacheFile (def_t *e, int ch)
  241. {
  242. char *n;
  243. int i;
  244. if (!e->ofs)
  245. return;
  246. n = G_STRING(e->ofs);
  247. for (i=0 ; i<numfiles ; i++)
  248. if (!strcmp(n, precache_files[i]))
  249. return;
  250. if (numfiles == MAX_FILES)
  251. Error ("PrecacheFile: numfiles == MAX_FILES");
  252. strcpy (precache_files[i], n);
  253. if (ch >= '1' && ch <= '9')
  254. precache_files_block[i] = ch - '0';
  255. else
  256. precache_files_block[i] = 1;
  257. numfiles++;
  258. }
  259. /*
  260. ============
  261. PR_ParseFunctionCall
  262. ============
  263. */
  264. def_t *PR_ParseFunctionCall (def_t *func)
  265. {
  266. def_t *e;
  267. int arg;
  268. type_t *t;
  269. t = func->type;
  270. if (t->type != ev_function)
  271. PR_ParseError ("not a function");
  272. // copy the arguments to the global parameter variables
  273. arg = 0;
  274. if (!PR_Check(")"))
  275. {
  276. do
  277. {
  278. if (t->num_parms != -1 && arg >= t->num_parms)
  279. PR_ParseError ("too many parameters");
  280. e = PR_Expression (TOP_PRIORITY);
  281. if (arg == 0 && func->name)
  282. {
  283. // save information for model and sound caching
  284. if (!strncmp(func->name,"precache_sound", 14))
  285. PrecacheSound (e, func->name[14]);
  286. else if (!strncmp(func->name,"precache_model", 14))
  287. PrecacheModel (e, func->name[14]);
  288. else if (!strncmp(func->name,"precache_file", 13))
  289. PrecacheFile (e, func->name[13]);
  290. }
  291. if (t->num_parms != -1 && ( e->type != t->parm_types[arg] ) )
  292. PR_ParseError ("type mismatch on parm %i", arg);
  293. // a vector copy will copy everything
  294. def_parms[arg].type = t->parm_types[arg];
  295. PR_Statement (&pr_opcodes[OP_STORE_V], e, &def_parms[arg]);
  296. arg++;
  297. } while (PR_Check (","));
  298. if (t->num_parms != -1 && arg != t->num_parms)
  299. PR_ParseError ("too few parameters");
  300. PR_Expect (")");
  301. }
  302. if (arg >8)
  303. PR_ParseError ("More than eight parameters");
  304. PR_Statement (&pr_opcodes[OP_CALL0+arg], func, 0);
  305. def_ret.type = t->aux_type;
  306. return &def_ret;
  307. }
  308. /*
  309. ============
  310. PR_ParseValue
  311. Returns the global ofs for the current token
  312. ============
  313. */
  314. def_t *PR_ParseValue (void)
  315. {
  316. def_t *d;
  317. char *name;
  318. // if the token is an immediate, allocate a constant for it
  319. if (pr_token_type == tt_immediate)
  320. return PR_ParseImmediate ();
  321. name = PR_ParseName ();
  322. // look through the defs
  323. d = PR_GetDef (NULL, name, pr_scope, false);
  324. if (!d)
  325. PR_ParseError ("Unknown value \"%s\"", name);
  326. return d;
  327. }
  328. /*
  329. ============
  330. PR_Term
  331. ============
  332. */
  333. def_t *PR_Term (void)
  334. {
  335. def_t *e, *e2;
  336. etype_t t;
  337. if (PR_Check ("!"))
  338. {
  339. e = PR_Expression (NOT_PRIORITY);
  340. t = e->type->type;
  341. if (t == ev_float)
  342. e2 = PR_Statement (&pr_opcodes[OP_NOT_F], e, 0);
  343. else if (t == ev_string)
  344. e2 = PR_Statement (&pr_opcodes[OP_NOT_S], e, 0);
  345. else if (t == ev_entity)
  346. e2 = PR_Statement (&pr_opcodes[OP_NOT_ENT], e, 0);
  347. else if (t == ev_vector)
  348. e2 = PR_Statement (&pr_opcodes[OP_NOT_V], e, 0);
  349. else if (t == ev_function)
  350. e2 = PR_Statement (&pr_opcodes[OP_NOT_FNC], e, 0);
  351. else
  352. {
  353. e2 = NULL; // shut up compiler warning;
  354. PR_ParseError ("type mismatch for !");
  355. }
  356. return e2;
  357. }
  358. if (PR_Check ("("))
  359. {
  360. e = PR_Expression (TOP_PRIORITY);
  361. PR_Expect (")");
  362. return e;
  363. }
  364. return PR_ParseValue ();
  365. }
  366. /*
  367. ==============
  368. PR_Expression
  369. ==============
  370. */
  371. def_t *PR_Expression (int priority)
  372. {
  373. opcode_t *op, *oldop;
  374. def_t *e, *e2;
  375. etype_t type_a, type_b, type_c;
  376. if (priority == 0)
  377. return PR_Term ();
  378. e = PR_Expression (priority-1);
  379. while (1)
  380. {
  381. if (priority == 1 && PR_Check ("(") )
  382. return PR_ParseFunctionCall (e);
  383. for (op=pr_opcodes ; op->name ; op++)
  384. {
  385. if (op->priority != priority)
  386. continue;
  387. if (!PR_Check (op->name))
  388. continue;
  389. if ( op->right_associative )
  390. {
  391. // if last statement is an indirect, change it to an address of
  392. if ( (unsigned)(statements[numstatements-1].op - OP_LOAD_F) < 6 )
  393. {
  394. statements[numstatements-1].op = OP_ADDRESS;
  395. def_pointer.type->aux_type = e->type;
  396. e->type = def_pointer.type;
  397. }
  398. e2 = PR_Expression (priority);
  399. }
  400. else
  401. e2 = PR_Expression (priority-1);
  402. // type check
  403. type_a = e->type->type;
  404. type_b = e2->type->type;
  405. if (op->name[0] == '.')// field access gets type from field
  406. {
  407. if (e2->type->aux_type)
  408. type_c = e2->type->aux_type->type;
  409. else
  410. type_c = -1; // not a field
  411. }
  412. else
  413. type_c = ev_void;
  414. oldop = op;
  415. while (type_a != op->type_a->type->type
  416. || type_b != op->type_b->type->type
  417. || (type_c != ev_void && type_c != op->type_c->type->type) )
  418. {
  419. op++;
  420. if (!op->name || strcmp (op->name , oldop->name))
  421. PR_ParseError ("type mismatch for %s", oldop->name);
  422. }
  423. if (type_a == ev_pointer && type_b != e->type->aux_type->type)
  424. PR_ParseError ("type mismatch for %s", op->name);
  425. if (op->right_associative)
  426. e = PR_Statement (op, e2, e);
  427. else
  428. e = PR_Statement (op, e, e2);
  429. if (type_c != ev_void) // field access gets type from field
  430. e->type = e2->type->aux_type;
  431. break;
  432. }
  433. if (!op->name)
  434. break; // next token isn't at this priority level
  435. }
  436. return e;
  437. }
  438. /*
  439. ============
  440. PR_ParseStatement
  441. ============
  442. */
  443. void PR_ParseStatement (void)
  444. {
  445. def_t *e;
  446. dstatement_t *patch1, *patch2;
  447. if (PR_Check ("{"))
  448. {
  449. do
  450. {
  451. PR_ParseStatement ();
  452. } while (!PR_Check ("}"));
  453. return;
  454. }
  455. if (PR_Check("return"))
  456. {
  457. if (PR_Check (";"))
  458. {
  459. PR_Statement (&pr_opcodes[OP_RETURN], 0, 0);
  460. return;
  461. }
  462. e = PR_Expression (TOP_PRIORITY);
  463. PR_Expect (";");
  464. PR_Statement (&pr_opcodes[OP_RETURN], e, 0);
  465. return;
  466. }
  467. if (PR_Check("while"))
  468. {
  469. PR_Expect ("(");
  470. patch2 = &statements[numstatements];
  471. e = PR_Expression (TOP_PRIORITY);
  472. PR_Expect (")");
  473. patch1 = &statements[numstatements];
  474. PR_Statement (&pr_opcodes[OP_IFNOT], e, 0);
  475. PR_ParseStatement ();
  476. junkdef.ofs = patch2 - &statements[numstatements];
  477. PR_Statement (&pr_opcodes[OP_GOTO], &junkdef, 0);
  478. patch1->b = &statements[numstatements] - patch1;
  479. return;
  480. }
  481. if (PR_Check("do"))
  482. {
  483. patch1 = &statements[numstatements];
  484. PR_ParseStatement ();
  485. PR_Expect ("while");
  486. PR_Expect ("(");
  487. e = PR_Expression (TOP_PRIORITY);
  488. PR_Expect (")");
  489. PR_Expect (";");
  490. junkdef.ofs = patch1 - &statements[numstatements];
  491. PR_Statement (&pr_opcodes[OP_IF], e, &junkdef);
  492. return;
  493. }
  494. if (PR_Check("local"))
  495. {
  496. PR_ParseDefs ();
  497. locals_end = numpr_globals;
  498. return;
  499. }
  500. if (PR_Check("if"))
  501. {
  502. PR_Expect ("(");
  503. e = PR_Expression (TOP_PRIORITY);
  504. PR_Expect (")");
  505. patch1 = &statements[numstatements];
  506. PR_Statement (&pr_opcodes[OP_IFNOT], e, 0);
  507. PR_ParseStatement ();
  508. if (PR_Check ("else"))
  509. {
  510. patch2 = &statements[numstatements];
  511. PR_Statement (&pr_opcodes[OP_GOTO], 0, 0);
  512. patch1->b = &statements[numstatements] - patch1;
  513. PR_ParseStatement ();
  514. patch2->a = &statements[numstatements] - patch2;
  515. }
  516. else
  517. patch1->b = &statements[numstatements] - patch1;
  518. return;
  519. }
  520. PR_Expression (TOP_PRIORITY);
  521. PR_Expect (";");
  522. }
  523. /*
  524. ==============
  525. PR_ParseState
  526. States are special functions made for convenience. They automatically
  527. set frame, nextthink (implicitly), and think (allowing forward definitions).
  528. // void() name = [framenum, nextthink] {code}
  529. // expands to:
  530. // function void name ()
  531. // {
  532. // self.frame=framenum;
  533. // self.nextthink = time + 0.1;
  534. // self.think = nextthink
  535. // <code>
  536. // };
  537. ==============
  538. */
  539. void PR_ParseState (void)
  540. {
  541. char *name;
  542. def_t *s1, *def;
  543. if (pr_token_type != tt_immediate || pr_immediate_type != &type_float)
  544. PR_ParseError ("state frame must be a number");
  545. s1 = PR_ParseImmediate ();
  546. PR_Expect (",");
  547. name = PR_ParseName ();
  548. def = PR_GetDef (&type_function, name,0, true);
  549. PR_Expect ("]");
  550. PR_Statement (&pr_opcodes[OP_STATE], s1, def);
  551. }
  552. /*
  553. ============
  554. PR_ParseImmediateStatements
  555. Parse a function body
  556. ============
  557. */
  558. function_t *PR_ParseImmediateStatements (type_t *type)
  559. {
  560. int i;
  561. function_t *f;
  562. def_t *defs[MAX_PARMS];
  563. f = malloc (sizeof(function_t));
  564. //
  565. // check for builtin function definition #1, #2, etc
  566. //
  567. if (PR_Check ("#"))
  568. {
  569. if (pr_token_type != tt_immediate
  570. || pr_immediate_type != &type_float
  571. || pr_immediate._float != (int)pr_immediate._float)
  572. PR_ParseError ("Bad builtin immediate");
  573. f->builtin = (int)pr_immediate._float;
  574. PR_Lex ();
  575. return f;
  576. }
  577. f->builtin = 0;
  578. //
  579. // define the parms
  580. //
  581. for (i=0 ; i<type->num_parms ; i++)
  582. {
  583. defs[i] = PR_GetDef (type->parm_types[i], pr_parm_names[i], pr_scope, true);
  584. f->parm_ofs[i] = defs[i]->ofs;
  585. if (i > 0 && f->parm_ofs[i] < f->parm_ofs[i-1])
  586. Error ("bad parm order");
  587. }
  588. f->code = numstatements;
  589. //
  590. // check for a state opcode
  591. //
  592. if (PR_Check ("["))
  593. PR_ParseState ();
  594. //
  595. // parse regular statements
  596. //
  597. PR_Expect ("{");
  598. while (!PR_Check("}"))
  599. PR_ParseStatement ();
  600. // emit an end of statements opcode
  601. PR_Statement (pr_opcodes, 0,0);
  602. return f;
  603. }
  604. /*
  605. ============
  606. PR_GetDef
  607. If type is NULL, it will match any type
  608. If allocate is true, a new def will be allocated if it can't be found
  609. ============
  610. */
  611. def_t *PR_GetDef (type_t *type, char *name, def_t *scope, boolean allocate)
  612. {
  613. def_t *def;
  614. char element[MAX_NAME];
  615. // see if the name is already in use
  616. for (def = pr.def_head.next ; def ; def = def->next)
  617. if (!strcmp(def->name,name) )
  618. {
  619. if ( def->scope && def->scope != scope)
  620. continue; // in a different function
  621. if (type && def->type != type)
  622. PR_ParseError ("Type mismatch on redeclaration of %s",name);
  623. return def;
  624. }
  625. if (!allocate)
  626. return NULL;
  627. // allocate a new def
  628. def = malloc (sizeof(def_t));
  629. memset (def, 0, sizeof(*def));
  630. def->next = NULL;
  631. pr.def_tail->next = def;
  632. pr.def_tail = def;
  633. def->name = malloc (strlen(name)+1);
  634. strcpy (def->name, name);
  635. def->type = type;
  636. def->scope = scope;
  637. def->ofs = numpr_globals;
  638. pr_global_defs[numpr_globals] = def;
  639. //
  640. // make automatic defs for the vectors elements
  641. // .origin can be accessed as .origin_x, .origin_y, and .origin_z
  642. //
  643. if (type->type == ev_vector)
  644. {
  645. sprintf (element, "%s_x",name);
  646. PR_GetDef (&type_float, element, scope, true);
  647. sprintf (element, "%s_y",name);
  648. PR_GetDef (&type_float, element, scope, true);
  649. sprintf (element, "%s_z",name);
  650. PR_GetDef (&type_float, element, scope, true);
  651. }
  652. else
  653. numpr_globals += type_size[type->type];
  654. if (type->type == ev_field)
  655. {
  656. *(int *)&pr_globals[def->ofs] = pr.size_fields;
  657. if (type->aux_type->type == ev_vector)
  658. {
  659. sprintf (element, "%s_x",name);
  660. PR_GetDef (&type_floatfield, element, scope, true);
  661. sprintf (element, "%s_y",name);
  662. PR_GetDef (&type_floatfield, element, scope, true);
  663. sprintf (element, "%s_z",name);
  664. PR_GetDef (&type_floatfield, element, scope, true);
  665. }
  666. else
  667. pr.size_fields += type_size[type->aux_type->type];
  668. }
  669. // if (pr_dumpasm)
  670. // PR_PrintOfs (def->ofs);
  671. return def;
  672. }
  673. /*
  674. ================
  675. PR_ParseDefs
  676. Called at the outer layer and when a local statement is hit
  677. ================
  678. */
  679. void PR_ParseDefs (void)
  680. {
  681. char *name;
  682. type_t *type;
  683. def_t *def;
  684. function_t *f;
  685. dfunction_t *df;
  686. int i;
  687. int locals_start;
  688. type = PR_ParseType ();
  689. if (pr_scope && (type->type == ev_field || type->type == ev_function) )
  690. PR_ParseError ("Fields and functions must be global");
  691. do
  692. {
  693. name = PR_ParseName ();
  694. def = PR_GetDef (type, name, pr_scope, true);
  695. // check for an initialization
  696. if ( PR_Check ("=") )
  697. {
  698. if (def->initialized)
  699. PR_ParseError ("%s redeclared", name);
  700. if (type->type == ev_function)
  701. {
  702. locals_start = locals_end = numpr_globals;
  703. pr_scope = def;
  704. f = PR_ParseImmediateStatements (type);
  705. pr_scope = NULL;
  706. def->initialized = 1;
  707. G_FUNCTION(def->ofs) = numfunctions;
  708. f->def = def;
  709. // if (pr_dumpasm)
  710. // PR_PrintFunction (def);
  711. // fill in the dfunction
  712. df = &functions[numfunctions];
  713. numfunctions++;
  714. if (f->builtin)
  715. df->first_statement = -f->builtin;
  716. else
  717. df->first_statement = f->code;
  718. df->s_name = CopyString (f->def->name);
  719. df->s_file = s_file;
  720. df->numparms = f->def->type->num_parms;
  721. df->locals = locals_end - locals_start;
  722. df->parm_start = locals_start;
  723. for (i=0 ; i<df->numparms ; i++)
  724. df->parm_size[i] = type_size[f->def->type->parm_types[i]->type];
  725. continue;
  726. }
  727. else if (pr_immediate_type != type)
  728. PR_ParseError ("wrong immediate type for %s", name);
  729. def->initialized = 1;
  730. memcpy (pr_globals + def->ofs, &pr_immediate, 4*type_size[pr_immediate_type->type]);
  731. PR_Lex ();
  732. }
  733. } while (PR_Check (","));
  734. PR_Expect (";");
  735. }
  736. /*
  737. ============
  738. PR_CompileFile
  739. compiles the 0 terminated text, adding defintions to the pr structure
  740. ============
  741. */
  742. boolean PR_CompileFile (char *string, char *filename)
  743. {
  744. if (!pr.memory)
  745. Error ("PR_CompileFile: Didn't clear");
  746. PR_ClearGrabMacros (); // clear the frame macros
  747. pr_file_p = string;
  748. s_file = CopyString (filename);
  749. pr_source_line = 0;
  750. PR_NewLine ();
  751. PR_Lex (); // read first token
  752. while (pr_token_type != tt_eof)
  753. {
  754. if (setjmp(pr_parse_abort))
  755. {
  756. if (++pr_error_count > MAX_ERRORS)
  757. return false;
  758. PR_SkipToSemicolon ();
  759. if (pr_token_type == tt_eof)
  760. return false;
  761. }
  762. pr_scope = NULL; // outside all functions
  763. PR_ParseDefs ();
  764. }
  765. return (pr_error_count == 0);
  766. }