2001-02-24 06:58:54 +00:00
|
|
|
/*
|
|
|
|
pr_lex.c
|
|
|
|
|
|
|
|
Lexical parser for GameC
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2001 Jeff Teunissen <deek@dusknet.dhs.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public
|
|
|
|
License along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2001-12-08 20:40:50 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-02-24 06:58:54 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2001-10-24 22:50:06 +00:00
|
|
|
#include <QF/sys.h>
|
2001-02-24 06:58:54 +00:00
|
|
|
|
|
|
|
#include "qfcc.h"
|
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
int pr_source_line;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
char pr_parm_names[MAX_PARMS][MAX_NAME];
|
2001-08-10 18:42:13 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
char *pr_file_p;
|
|
|
|
char *pr_line_start; // start of current source line
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
int pr_bracelevel;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
char pr_token[2048];
|
|
|
|
int pr_token_len;
|
|
|
|
token_type_t pr_token_type;
|
|
|
|
type_t *pr_immediate_type;
|
|
|
|
eval_t pr_immediate;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
char pr_immediate_string[2048];
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
int pr_error_count;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
|
|
|
// simple types. function types are dynamically allocated
|
2001-12-06 19:49:40 +00:00
|
|
|
type_t type_void = { ev_void, &def_void };
|
|
|
|
type_t type_string = { ev_string, &def_string };
|
|
|
|
type_t type_float = { ev_float, &def_float };
|
|
|
|
type_t type_vector = { ev_vector, &def_vector };
|
|
|
|
type_t type_entity = { ev_entity, &def_entity };
|
|
|
|
type_t type_field = { ev_field, &def_field };
|
|
|
|
|
2001-02-24 06:58:54 +00:00
|
|
|
// type_function is a void() function used for state defs
|
2001-12-06 19:49:40 +00:00
|
|
|
type_t type_function = { ev_func, &def_function, NULL, &type_void };
|
|
|
|
type_t type_pointer = { ev_pointer, &def_pointer };
|
|
|
|
type_t type_quaternion = { ev_quaternion, &def_quaternion };
|
|
|
|
type_t type_integer = { ev_integer, &def_integer };
|
|
|
|
type_t type_uinteger = { ev_uinteger, &def_uinteger };
|
2001-12-07 20:10:30 +00:00
|
|
|
type_t type_short = { ev_short, &def_short };
|
2001-12-08 08:19:48 +00:00
|
|
|
type_t type_struct = { ev_struct, &def_struct };
|
2001-12-06 19:49:40 +00:00
|
|
|
|
|
|
|
type_t type_floatfield = { ev_field, &def_field, NULL, &type_float };
|
|
|
|
|
|
|
|
def_t def_void = { &type_void, "temp" };
|
|
|
|
def_t def_string = { &type_string, "temp" };
|
|
|
|
def_t def_float = { &type_float, "temp" };
|
|
|
|
def_t def_vector = { &type_vector, "temp" };
|
|
|
|
def_t def_entity = { &type_entity, "temp" };
|
|
|
|
def_t def_field = { &type_field, "temp" };
|
|
|
|
def_t def_function = { &type_function, "temp" };
|
|
|
|
def_t def_pointer = { &type_pointer, "temp" };
|
|
|
|
def_t def_quaternion = { &type_quaternion, "temp" };
|
|
|
|
def_t def_integer = { &type_integer, "temp" };
|
|
|
|
def_t def_uinteger = { &type_uinteger, "temp" };
|
2001-12-07 20:10:30 +00:00
|
|
|
def_t def_short = { &type_short, "temp" };
|
2001-12-08 08:19:48 +00:00
|
|
|
def_t def_struct = { &type_struct, "temp" };
|
2001-12-06 19:49:40 +00:00
|
|
|
|
|
|
|
def_t def_ret, def_parms[MAX_PARMS];
|
|
|
|
|
|
|
|
def_t *def_for_type[8] = {
|
2001-02-24 06:58:54 +00:00
|
|
|
&def_void, &def_string, &def_float, &def_vector,
|
|
|
|
&def_entity, &def_field, &def_function, &def_pointer
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
PR_LexString
|
|
|
|
|
|
|
|
Parse a quoted string
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PR_LexString (void)
|
|
|
|
{
|
|
|
|
int c;
|
2001-12-06 19:49:40 +00:00
|
|
|
int i;
|
|
|
|
int mask;
|
|
|
|
int boldnext;
|
2001-02-24 06:58:54 +00:00
|
|
|
|
2001-06-25 22:11:20 +00:00
|
|
|
pr_token_len = 0;
|
2001-06-19 23:27:07 +00:00
|
|
|
mask = 0x00;
|
|
|
|
boldnext = 0;
|
|
|
|
|
2001-02-24 06:58:54 +00:00
|
|
|
pr_file_p++;
|
|
|
|
do {
|
|
|
|
c = *pr_file_p++;
|
|
|
|
if (!c)
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "EOF inside quote");
|
2001-02-24 06:58:54 +00:00
|
|
|
if (c == '\n')
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "newline inside quote");
|
2001-02-24 06:58:54 +00:00
|
|
|
if (c == '\\') { // escape char
|
|
|
|
c = *pr_file_p++;
|
|
|
|
if (!c)
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "EOF inside quote");
|
2001-06-11 23:37:01 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
c = '\"';
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
for (i = c = 0; i < 3
|
2001-12-06 19:49:40 +00:00
|
|
|
&& *pr_file_p >= '0'
|
|
|
|
&& *pr_file_p <= '7'; i++, pr_file_p++) {
|
2001-06-11 23:37:01 +00:00
|
|
|
c *= 8;
|
|
|
|
c += *pr_file_p - '0';
|
|
|
|
}
|
|
|
|
if (!*pr_file_p)
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "EOF inside quote");
|
2001-06-11 23:37:01 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
c = 0;
|
|
|
|
while (*pr_file_p && isxdigit (*pr_file_p)) {
|
|
|
|
c *= 16;
|
2001-06-19 22:26:16 +00:00
|
|
|
if (*pr_file_p <= '9')
|
2001-06-11 23:37:01 +00:00
|
|
|
c += *pr_file_p - '0';
|
|
|
|
else if (*pr_file_p <= 'F')
|
2001-06-19 22:13:35 +00:00
|
|
|
c += *pr_file_p - 'A' + 10;
|
2001-06-11 23:37:01 +00:00
|
|
|
else
|
2001-06-19 22:13:35 +00:00
|
|
|
c += *pr_file_p - 'a' + 10;
|
2001-06-19 21:45:57 +00:00
|
|
|
pr_file_p++;
|
2001-06-11 23:37:01 +00:00
|
|
|
}
|
|
|
|
if (!*pr_file_p)
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "EOF inside quote");
|
2001-06-11 23:37:01 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
c = '\a';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
c = '\b';
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
c = '\033';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
c = '\f';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
c = '\v';
|
|
|
|
break;
|
2001-06-19 23:27:07 +00:00
|
|
|
case '^':
|
|
|
|
if (*pr_file_p == '\"')
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "Unexpected end of string after \\^");
|
2001-06-19 23:27:07 +00:00
|
|
|
boldnext = 1;
|
|
|
|
continue;
|
|
|
|
case '<':
|
|
|
|
mask = 0x80;
|
|
|
|
continue;
|
|
|
|
case '>':
|
|
|
|
mask = 0x00;
|
|
|
|
continue;
|
2001-06-11 23:37:01 +00:00
|
|
|
default:
|
2001-08-10 18:35:55 +00:00
|
|
|
error (0, "Unknown escape char");
|
2001-06-11 23:37:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-02-24 06:58:54 +00:00
|
|
|
} else if (c == '\"') {
|
2001-06-25 22:11:20 +00:00
|
|
|
pr_token[pr_token_len] = 0;
|
2001-02-24 06:58:54 +00:00
|
|
|
pr_token_type = tt_immediate;
|
|
|
|
pr_immediate_type = &type_string;
|
|
|
|
strcpy (pr_immediate_string, pr_token);
|
|
|
|
return;
|
|
|
|
}
|
2001-06-19 23:27:07 +00:00
|
|
|
if (boldnext)
|
|
|
|
c = c ^ 0x80;
|
|
|
|
boldnext = 0;
|
|
|
|
c = c ^ mask;
|
2001-06-25 22:11:20 +00:00
|
|
|
pr_token[pr_token_len] = c;
|
|
|
|
pr_token_len++;
|
2001-02-24 06:58:54 +00:00
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2001-06-19 07:28:50 +00:00
|
|
|
void
|
|
|
|
PR_PrintType (type_t *type)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
int i;
|
|
|
|
|
2001-06-19 07:28:50 +00:00
|
|
|
if (!type) {
|
2001-12-06 19:49:40 +00:00
|
|
|
printf ("(null)");
|
2001-06-19 07:28:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (type->type) {
|
|
|
|
case ev_void:
|
|
|
|
printf ("void");
|
|
|
|
break;
|
|
|
|
case ev_string:
|
|
|
|
printf ("string");
|
|
|
|
break;
|
|
|
|
case ev_float:
|
|
|
|
printf ("float");
|
|
|
|
break;
|
|
|
|
case ev_vector:
|
|
|
|
printf ("vector");
|
|
|
|
break;
|
|
|
|
case ev_entity:
|
|
|
|
printf ("entity");
|
|
|
|
break;
|
|
|
|
case ev_field:
|
|
|
|
printf (".");
|
|
|
|
PR_PrintType (type->aux_type);
|
|
|
|
break;
|
|
|
|
case ev_func:
|
|
|
|
PR_PrintType (type->aux_type);
|
|
|
|
printf ("(");
|
|
|
|
for (i = 0; i < type->num_parms; i++) {
|
|
|
|
PR_PrintType (type->parm_types[i]);
|
|
|
|
if (i < type->num_parms - 1)
|
|
|
|
printf (",");
|
|
|
|
}
|
|
|
|
if (type->num_parms == -1)
|
|
|
|
printf ("...");
|
|
|
|
printf (")");
|
|
|
|
break;
|
|
|
|
case ev_pointer:
|
|
|
|
printf ("pointer to ");
|
|
|
|
PR_PrintType (type->aux_type);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf ("unknown type %d", type->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-24 06:58:54 +00:00
|
|
|
/*
|
|
|
|
PR_FindType
|
|
|
|
|
|
|
|
Returns a preexisting complex type that matches the parm, or allocates
|
|
|
|
a new one and copies it out.
|
|
|
|
*/
|
|
|
|
type_t *
|
|
|
|
PR_FindType (type_t *type)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def;
|
|
|
|
type_t *check;
|
|
|
|
int i;
|
2001-06-19 16:01:38 +00:00
|
|
|
|
2001-02-24 06:58:54 +00:00
|
|
|
for (check = pr.types; check; check = check->next) {
|
|
|
|
if (check->type != type->type
|
|
|
|
|| check->aux_type != type->aux_type
|
|
|
|
|| check->num_parms != type->num_parms)
|
|
|
|
continue;
|
|
|
|
|
2001-06-19 07:28:50 +00:00
|
|
|
if (check->type != ev_func)
|
|
|
|
return check;
|
|
|
|
|
|
|
|
if (check->num_parms == -1)
|
|
|
|
return check;
|
|
|
|
|
2001-02-24 06:58:54 +00:00
|
|
|
for (i = 0; i < type->num_parms; i++)
|
|
|
|
if (check->parm_types[i] != type->parm_types[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == type->num_parms)
|
|
|
|
return check;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate a new one
|
|
|
|
check = malloc (sizeof (*check));
|
2001-10-24 22:50:06 +00:00
|
|
|
if (!check)
|
|
|
|
Sys_Error ("PR_FindType: Memory Allocation Failure\n");
|
2001-02-24 06:58:54 +00:00
|
|
|
*check = *type;
|
|
|
|
check->next = pr.types;
|
|
|
|
pr.types = check;
|
|
|
|
|
|
|
|
// allocate a generic def for the type, so fields can reference it
|
|
|
|
def = malloc (sizeof (def_t));
|
2001-12-08 08:19:48 +00:00
|
|
|
if (!def)
|
2001-10-24 22:50:06 +00:00
|
|
|
Sys_Error ("PR_FindType: Memory Allocation Failure\n");
|
2001-02-24 06:58:54 +00:00
|
|
|
def->name = "COMPLEX TYPE";
|
|
|
|
def->type = check;
|
|
|
|
check->def = def;
|
|
|
|
return check;
|
|
|
|
}
|