2001-06-12 21:06:28 +00:00
|
|
|
%{
|
2001-06-13 07:16:39 +00:00
|
|
|
#include <QF/hash.h>
|
2001-06-12 21:06:28 +00:00
|
|
|
#include "qfcc.h"
|
2001-06-15 19:38:43 +00:00
|
|
|
#include "expr.h"
|
2001-06-18 22:51:49 +00:00
|
|
|
#include "scope.h"
|
2001-06-12 21:06:28 +00:00
|
|
|
#include "qc-parse.h"
|
|
|
|
|
|
|
|
#define YY_NO_UNPUT
|
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
int lineno;
|
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
void error (char*s){fprintf(stderr,"%d: %s\n",lineno,s);}
|
2001-06-13 07:16:39 +00:00
|
|
|
|
|
|
|
int type_or_name (char *token);
|
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
int do_grab (char *token);
|
|
|
|
|
|
|
|
void add_frame_macro (char *token);
|
2001-06-13 07:16:39 +00:00
|
|
|
|
2001-06-25 22:11:20 +00:00
|
|
|
char *make_string (char *token);
|
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
extern YYSTYPE yylval;
|
|
|
|
|
2001-06-12 21:06:28 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
DIGIT [0-9]
|
|
|
|
ID [a-zA-Z_][a-zA-Z_0-9]*
|
2001-06-13 07:16:39 +00:00
|
|
|
FLOAT {DIGIT}+"."{DIGIT}*
|
|
|
|
NUM ({DIGIT}+("."{DIGIT}*)?)
|
|
|
|
s [ \t]
|
|
|
|
m ([\-+]?)
|
2001-06-12 21:06:28 +00:00
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
%x grab_frame grab_other
|
|
|
|
|
2001-06-12 21:06:28 +00:00
|
|
|
%%
|
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
"/*" {
|
|
|
|
int c;
|
|
|
|
do {
|
|
|
|
while ((c = input ()) != '*' && c != EOF
|
|
|
|
&& c != '\n')
|
|
|
|
;
|
|
|
|
while (c == '*')
|
|
|
|
c = input ();
|
|
|
|
if (c == EOF)
|
|
|
|
error ("EOF in comment");
|
|
|
|
if (c == '\n')
|
|
|
|
lineno++;
|
|
|
|
} while (c != '/' && c != EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
"//".*$ /* nothing to do */
|
|
|
|
|
|
|
|
{DIGIT}+ {
|
|
|
|
yylval.int_val = atoi (yytext);
|
|
|
|
return INT_VAL;
|
|
|
|
}
|
2001-06-12 21:06:28 +00:00
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
{FLOAT}* {
|
|
|
|
yylval.float_val = atof (yytext);
|
|
|
|
return FLOAT_VAL;
|
|
|
|
}
|
2001-06-12 22:26:10 +00:00
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
{ID} return type_or_name(yytext);
|
2001-06-12 21:06:28 +00:00
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
\"(\\.|[^"])*\" {
|
2001-06-25 22:11:20 +00:00
|
|
|
yylval.string_val = make_string (yytext);
|
2001-06-13 07:16:39 +00:00
|
|
|
return STRING_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
'{s}*{m}{NUM}{s}+{m}{NUM}{s}+{m}{NUM}{s}*' {
|
2001-06-25 17:15:56 +00:00
|
|
|
sscanf (yytext, "' %f %f %f '",
|
|
|
|
&yylval.vector_val[0], &yylval.vector_val[1],
|
|
|
|
&yylval.vector_val[2]);
|
2001-06-13 07:16:39 +00:00
|
|
|
return VECTOR_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
'{s}*{m}{NUM}{s}+{m}{NUM}{s}+{m}{NUM}{s}+{m}{NUM}{s}*' {
|
2001-06-25 17:15:56 +00:00
|
|
|
sscanf (yytext, "' %f %f %f %f '",
|
|
|
|
&yylval.vector_val[0], &yylval.vector_val[1],
|
|
|
|
&yylval.vector_val[2], &yylval.vector_val[3]);
|
2001-06-13 07:16:39 +00:00
|
|
|
return VECTOR_VAL;
|
|
|
|
}
|
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
^#{s}+{DIGIT}+{s}+\"(\.|[^"])*\".*$ {
|
|
|
|
}
|
|
|
|
|
|
|
|
"!"|"("|")"|"{"|"}"|"."|"*"|"/"|"&"|"|"|"+"|"-"|"="|"["|"]"|";"|","|"#" return yytext[0];
|
2001-06-13 07:16:39 +00:00
|
|
|
|
|
|
|
"..." return ELIPSIS;
|
2001-06-12 21:06:28 +00:00
|
|
|
|
|
|
|
"&&" return AND;
|
|
|
|
"||" return OR;
|
|
|
|
"==" return EQ;
|
|
|
|
"!=" return NE;
|
|
|
|
"<=" return LE;
|
|
|
|
">=" return GE;
|
|
|
|
"<" return LT;
|
|
|
|
">" return GT;
|
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
"$"{s}*{ID} {
|
|
|
|
int ret = do_grab(yytext);
|
|
|
|
if (ret > 0)
|
2001-06-13 07:16:39 +00:00
|
|
|
return ret;
|
2001-06-13 18:35:41 +00:00
|
|
|
else
|
|
|
|
BEGIN (-ret);
|
2001-06-13 07:16:39 +00:00
|
|
|
}
|
2001-06-12 21:06:28 +00:00
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
<grab_frame>{ID} add_frame_macro (yytext);
|
|
|
|
|
|
|
|
<grab_other>[^\n]* /* skip */
|
|
|
|
|
|
|
|
<*>\n {
|
|
|
|
lineno++;
|
|
|
|
BEGIN (INITIAL);
|
2001-06-13 07:16:39 +00:00
|
|
|
}
|
2001-06-12 21:06:28 +00:00
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
<*>{s}* /* skip */
|
2001-06-13 07:16:39 +00:00
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
<*>. error ("all your typo are belong to us");
|
2001-06-12 21:06:28 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
int
|
|
|
|
yywrap (void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2001-06-13 07:16:39 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
int value;
|
|
|
|
type_t *type;
|
|
|
|
} keyword_t;
|
|
|
|
|
|
|
|
static keyword_t keywords[] = {
|
|
|
|
{"float", TYPE, &type_float },
|
|
|
|
{"vector", TYPE, &type_vector},
|
|
|
|
{"entity", TYPE, &type_entity},
|
|
|
|
{"string", TYPE, &type_string},
|
|
|
|
{"void", TYPE, &type_void },
|
|
|
|
{"local", LOCAL, 0 },
|
|
|
|
{"return", RETURN, 0 },
|
|
|
|
{"while", WHILE, 0 },
|
|
|
|
{"do", DO, 0 },
|
|
|
|
{"if", IF, 0 },
|
|
|
|
{"else", ELSE, 0 },
|
|
|
|
{"for", FOR, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
keyword_get_key (void *kw, void *unused)
|
|
|
|
{
|
|
|
|
return ((keyword_t*)kw)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
type_or_name (char *token)
|
|
|
|
{
|
|
|
|
static int initialized = 0;
|
|
|
|
static hashtab_t *keyword_tab;
|
|
|
|
keyword_t *keyword;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
int i;
|
|
|
|
keyword_tab = Hash_NewTable (1021, keyword_get_key, 0, 0);
|
|
|
|
for (i = 0; i < sizeof (keywords) / sizeof (keywords[0]); i++)
|
|
|
|
Hash_Add (keyword_tab, &keywords[i]);
|
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
keyword = Hash_Find (keyword_tab, token);
|
|
|
|
if (keyword) {
|
|
|
|
yylval.type = keyword->type;
|
|
|
|
return keyword->value;
|
|
|
|
}
|
2001-06-15 19:38:43 +00:00
|
|
|
yylval.string_val = strdup (token);
|
2001-06-13 07:16:39 +00:00
|
|
|
return NAME;
|
|
|
|
}
|
|
|
|
|
2001-06-13 18:35:41 +00:00
|
|
|
static hashtab_t *frame_tab;
|
|
|
|
static hashtab_t *grab_tab;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
int num;
|
|
|
|
} frame_t;
|
|
|
|
|
|
|
|
static frame_t grab_list[] = {
|
|
|
|
{"cd", 0},
|
|
|
|
{"origin", 0},
|
|
|
|
{"base", 0},
|
|
|
|
{"flags", 0},
|
|
|
|
{"scale", 0},
|
|
|
|
{"skin", 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
frame_get_key (void *f, void *unused)
|
|
|
|
{
|
|
|
|
return ((frame_t*)f)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
frame_free (void *f, void *unused)
|
|
|
|
{
|
|
|
|
free ((char*)((frame_t*)f)->name);
|
|
|
|
free (f);
|
|
|
|
}
|
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
int
|
2001-06-13 18:35:41 +00:00
|
|
|
do_grab (char *token)
|
2001-06-13 07:16:39 +00:00
|
|
|
{
|
|
|
|
static int initialized;
|
2001-06-13 18:35:41 +00:00
|
|
|
frame_t *frame;
|
2001-06-13 07:16:39 +00:00
|
|
|
|
|
|
|
if (!initialized) {
|
2001-06-13 18:35:41 +00:00
|
|
|
int i;
|
|
|
|
|
2001-06-13 07:16:39 +00:00
|
|
|
initialized = 1;
|
2001-06-13 18:35:41 +00:00
|
|
|
frame_tab = Hash_NewTable (1021, frame_get_key, frame_free, 0);
|
|
|
|
grab_tab = Hash_NewTable (1021, frame_get_key, 0, 0);
|
|
|
|
for (i = 0; i < sizeof (grab_list) / sizeof (grab_list[0]); i++)
|
|
|
|
Hash_Add (grab_tab, &grab_list[i]);
|
|
|
|
}
|
|
|
|
while (isspace (*++token)) // advance over $ and leading space
|
|
|
|
;
|
|
|
|
if (!strcmp (token, "frame"))
|
|
|
|
return -grab_frame;
|
|
|
|
if (Hash_Find (grab_tab, token))
|
|
|
|
return -grab_other;
|
|
|
|
frame = Hash_Find (frame_tab, token);
|
|
|
|
if (frame) {
|
|
|
|
yylval.int_val = frame->num;
|
|
|
|
return INT_VAL;
|
2001-06-13 07:16:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-13 18:35:41 +00:00
|
|
|
|
|
|
|
static int frame_number;
|
|
|
|
|
|
|
|
void
|
|
|
|
add_frame_macro (char *token)
|
|
|
|
{
|
|
|
|
frame_t *frame = malloc (sizeof (frame_t));
|
|
|
|
|
|
|
|
frame->name = strdup (token);
|
|
|
|
frame->num = frame_number++;
|
|
|
|
Hash_Add (frame_tab, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clear_frame_macros (void)
|
|
|
|
{
|
|
|
|
frame_number = 0;
|
|
|
|
if (frame_tab)
|
|
|
|
Hash_FlushTable (frame_tab);
|
|
|
|
}
|
2001-06-25 22:11:20 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
make_string (char *token)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2001-06-25 22:53:15 +00:00
|
|
|
pr_file_p = token;
|
2001-06-25 22:11:20 +00:00
|
|
|
PR_LexString ();
|
|
|
|
str = malloc (pr_token_len + 1);
|
|
|
|
memcpy (str, pr_token, pr_token_len + 1);
|
|
|
|
return str;
|
|
|
|
}
|