home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / qcc / pr_comp.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  22KB  |  937 lines

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