2012-07-16 12:52:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-07-16 11:59:10 +00:00
|
|
|
#include "gmqcc.h"
|
|
|
|
#include "lexer.h"
|
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
ast_expression *var;
|
|
|
|
} varentry_t;
|
|
|
|
|
2012-07-16 12:14:37 +00:00
|
|
|
typedef struct {
|
|
|
|
lex_file *lex;
|
|
|
|
int tok;
|
2012-07-16 12:52:52 +00:00
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
MEM_VECTOR_MAKE(varentry_t, globals);
|
2012-08-11 13:40:03 +00:00
|
|
|
MEM_VECTOR_MAKE(varentry_t, fields);
|
2012-07-19 15:53:45 +00:00
|
|
|
MEM_VECTOR_MAKE(ast_function*, functions);
|
2012-07-20 12:34:45 +00:00
|
|
|
MEM_VECTOR_MAKE(ast_value*, imm_float);
|
2012-07-27 14:10:43 +00:00
|
|
|
MEM_VECTOR_MAKE(ast_value*, imm_string);
|
2012-07-27 14:20:53 +00:00
|
|
|
MEM_VECTOR_MAKE(ast_value*, imm_vector);
|
2012-07-19 17:55:25 +00:00
|
|
|
|
|
|
|
ast_function *function;
|
2012-08-10 20:59:47 +00:00
|
|
|
MEM_VECTOR_MAKE(varentry_t, locals);
|
2012-07-19 18:15:30 +00:00
|
|
|
size_t blocklocal;
|
2012-07-28 14:06:44 +00:00
|
|
|
|
|
|
|
size_t errors;
|
2012-08-12 17:24:08 +00:00
|
|
|
|
|
|
|
/* TYPE_FIELD -> parser_find_fields is used instead of find_var
|
|
|
|
* TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
|
|
|
|
* anything else: type error
|
|
|
|
*/
|
|
|
|
qcint memberof;
|
2012-07-16 12:14:37 +00:00
|
|
|
} parser_t;
|
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, varentry_t, globals)
|
2012-08-11 13:40:03 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, varentry_t, fields)
|
2012-07-20 12:34:45 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_float)
|
2012-07-27 14:10:43 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_string)
|
2012-07-27 14:20:53 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_vector)
|
2012-08-10 20:59:47 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals)
|
2012-07-19 15:53:45 +00:00
|
|
|
MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
|
2012-07-16 12:52:52 +00:00
|
|
|
|
|
|
|
void parseerror(parser_t *parser, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2012-07-28 14:06:44 +00:00
|
|
|
parser->errors++;
|
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
if (parser)
|
|
|
|
printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line);
|
|
|
|
else
|
|
|
|
printf("error: ");
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parser_next(parser_t *parser)
|
|
|
|
{
|
|
|
|
/* lex_do kills the previous token */
|
|
|
|
parser->tok = lex_do(parser->lex);
|
|
|
|
if (parser->tok == TOKEN_EOF || parser->tok >= TOKEN_ERROR)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lift a token out of the parser so it's not destroyed by parser_next */
|
|
|
|
token *parser_lift(parser_t *parser)
|
|
|
|
{
|
|
|
|
token *tok = parser->lex->tok;
|
|
|
|
parser->lex->tok = NULL;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define parser_tokval(p) (p->lex->tok->value)
|
|
|
|
#define parser_token(p) (p->lex->tok)
|
|
|
|
#define parser_ctx(p) (p->lex->tok->ctx)
|
|
|
|
|
2012-07-20 12:34:45 +00:00
|
|
|
ast_value* parser_const_float(parser_t *parser, double d)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
ast_value *out;
|
|
|
|
for (i = 0; i < parser->imm_float_count; ++i) {
|
|
|
|
if (parser->imm_float[i]->constval.vfloat == d)
|
|
|
|
return parser->imm_float[i];
|
|
|
|
}
|
|
|
|
out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
|
|
|
|
out->isconst = true;
|
|
|
|
out->constval.vfloat = d;
|
|
|
|
if (!parser_t_imm_float_add(parser, out)) {
|
|
|
|
ast_value_delete(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:10:43 +00:00
|
|
|
ast_value* parser_const_string(parser_t *parser, const char *str)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
ast_value *out;
|
|
|
|
for (i = 0; i < parser->imm_string_count; ++i) {
|
|
|
|
if (!strcmp(parser->imm_string[i]->constval.vstring, str))
|
|
|
|
return parser->imm_string[i];
|
|
|
|
}
|
|
|
|
out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
|
|
|
|
out->isconst = true;
|
|
|
|
out->constval.vstring = util_strdup(str);
|
|
|
|
if (!parser_t_imm_string_add(parser, out)) {
|
|
|
|
ast_value_delete(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:20:53 +00:00
|
|
|
ast_value* parser_const_vector(parser_t *parser, vector v)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
ast_value *out;
|
|
|
|
for (i = 0; i < parser->imm_vector_count; ++i) {
|
|
|
|
if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
|
|
|
|
return parser->imm_vector[i];
|
|
|
|
}
|
|
|
|
out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
|
|
|
|
out->isconst = true;
|
|
|
|
out->constval.vvec = v;
|
|
|
|
if (!parser_t_imm_vector_add(parser, out)) {
|
|
|
|
ast_value_delete(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-08-11 13:40:03 +00:00
|
|
|
ast_expression* parser_find_field(parser_t *parser, const char *name)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < parser->fields_count; ++i) {
|
|
|
|
if (!strcmp(parser->fields[i].name, name))
|
|
|
|
return parser->fields[i].var;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
ast_expression* parser_find_global(parser_t *parser, const char *name)
|
2012-07-16 12:52:52 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < parser->globals_count; ++i) {
|
2012-08-10 20:59:47 +00:00
|
|
|
if (!strcmp(parser->globals[i].name, name))
|
|
|
|
return parser->globals[i].var;
|
2012-07-16 12:52:52 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto)
|
2012-07-19 18:15:30 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2012-07-27 10:25:46 +00:00
|
|
|
ast_value *fun;
|
2012-07-19 18:15:30 +00:00
|
|
|
for (i = parser->locals_count; i > upto;) {
|
|
|
|
--i;
|
2012-08-10 20:59:47 +00:00
|
|
|
if (!strcmp(parser->locals[i].name, name))
|
|
|
|
return parser->locals[i].var;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
2012-07-27 10:25:46 +00:00
|
|
|
fun = parser->function->vtype;
|
2012-07-27 13:26:48 +00:00
|
|
|
for (i = 0; i < fun->expression.params_count; ++i) {
|
|
|
|
if (!strcmp(fun->expression.params[i]->name, name))
|
2012-08-10 20:59:47 +00:00
|
|
|
return (ast_expression*)(fun->expression.params[i]);
|
2012-07-27 10:25:46 +00:00
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
ast_expression* parser_find_var(parser_t *parser, const char *name)
|
2012-07-19 18:15:30 +00:00
|
|
|
{
|
2012-08-10 20:59:47 +00:00
|
|
|
ast_expression *v;
|
2012-07-19 18:15:30 +00:00
|
|
|
v = parser_find_local(parser, name, 0);
|
|
|
|
if (!v) v = parser_find_global(parser, name);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-07-19 09:41:16 +00:00
|
|
|
typedef struct {
|
|
|
|
MEM_VECTOR_MAKE(ast_value*, p);
|
|
|
|
} paramlist_t;
|
|
|
|
MEM_VEC_FUNCTIONS(paramlist_t, ast_value*, p)
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc)
|
2012-07-19 09:41:16 +00:00
|
|
|
{
|
|
|
|
paramlist_t params;
|
|
|
|
ast_value *var;
|
|
|
|
lex_ctx ctx = parser_ctx(parser);
|
2012-07-20 10:53:42 +00:00
|
|
|
int vtype = basetype;
|
|
|
|
int temptype;
|
2012-07-26 21:33:19 +00:00
|
|
|
size_t i;
|
2012-07-19 09:41:16 +00:00
|
|
|
|
|
|
|
MEM_VECTOR_INIT(¶ms, p);
|
|
|
|
|
2012-07-19 16:04:57 +00:00
|
|
|
*isfunc = false;
|
|
|
|
|
2012-07-19 09:41:16 +00:00
|
|
|
if (parser->tok == '(') {
|
2012-07-19 16:04:57 +00:00
|
|
|
*isfunc = true;
|
2012-07-19 09:41:16 +00:00
|
|
|
while (true) {
|
|
|
|
ast_value *param;
|
2012-07-19 16:04:57 +00:00
|
|
|
bool dummy;
|
2012-07-19 09:41:16 +00:00
|
|
|
|
2012-07-26 21:33:19 +00:00
|
|
|
if (!parser_next(parser))
|
|
|
|
goto on_error;
|
2012-07-19 09:41:16 +00:00
|
|
|
|
2012-07-19 17:53:58 +00:00
|
|
|
if (parser->tok == ')')
|
|
|
|
break;
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
temptype = parser_token(parser)->constval.t;
|
2012-07-26 21:33:19 +00:00
|
|
|
if (!parser_next(parser))
|
|
|
|
goto on_error;
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
param = parser_parse_type(parser, temptype, &dummy);
|
2012-07-19 16:04:57 +00:00
|
|
|
(void)dummy;
|
|
|
|
|
2012-07-26 21:33:19 +00:00
|
|
|
if (!param)
|
|
|
|
goto on_error;
|
|
|
|
|
|
|
|
if (parser->tok == TOKEN_IDENT) {
|
|
|
|
/* named parameter */
|
|
|
|
if (!ast_value_set_name(param, parser_tokval(parser)))
|
|
|
|
goto on_error;
|
|
|
|
if (!parser_next(parser))
|
|
|
|
goto on_error;
|
2012-07-19 09:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!paramlist_t_p_add(¶ms, param)) {
|
|
|
|
parseerror(parser, "Out of memory while parsing typename");
|
2012-07-26 21:33:19 +00:00
|
|
|
goto on_error;
|
2012-07-19 09:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parser->tok == ',')
|
|
|
|
continue;
|
|
|
|
if (parser->tok == ')')
|
|
|
|
break;
|
|
|
|
parseerror(parser, "Unexpected token");
|
2012-07-26 21:33:19 +00:00
|
|
|
goto on_error;
|
2012-07-19 09:41:16 +00:00
|
|
|
}
|
2012-07-26 21:33:19 +00:00
|
|
|
if (!parser_next(parser))
|
|
|
|
goto on_error;
|
2012-07-19 09:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var = ast_value_new(ctx, "<unnamed>", vtype);
|
2012-07-26 21:33:19 +00:00
|
|
|
if (!var)
|
|
|
|
goto on_error;
|
2012-07-27 13:26:48 +00:00
|
|
|
MEM_VECTOR_MOVE(¶ms, p, &var->expression, params);
|
2012-07-19 09:41:16 +00:00
|
|
|
return var;
|
2012-07-26 21:33:19 +00:00
|
|
|
on_error:
|
|
|
|
for (i = 0; i < params.p_count; ++i)
|
|
|
|
ast_value_delete(params.p[i]);
|
|
|
|
MEM_VECTOR_CLEAR(¶ms, p);
|
|
|
|
return NULL;
|
2012-07-19 09:41:16 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 12:04:52 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2012-07-20 13:02:18 +00:00
|
|
|
size_t etype; /* 0 = expression, others are operators */
|
2012-07-22 09:17:01 +00:00
|
|
|
int paren;
|
2012-07-27 13:02:39 +00:00
|
|
|
size_t off;
|
2012-07-20 19:19:30 +00:00
|
|
|
ast_expression *out;
|
2012-07-27 11:28:57 +00:00
|
|
|
ast_block *block; /* for commas and function calls */
|
2012-07-20 19:19:30 +00:00
|
|
|
lex_ctx ctx;
|
2012-07-20 12:04:52 +00:00
|
|
|
} sy_elem;
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
MEM_VECTOR_MAKE(sy_elem, out);
|
|
|
|
MEM_VECTOR_MAKE(sy_elem, ops);
|
2012-07-20 19:19:30 +00:00
|
|
|
} shunt;
|
|
|
|
MEM_VEC_FUNCTIONS(shunt, sy_elem, out)
|
|
|
|
MEM_VEC_FUNCTIONS(shunt, sy_elem, ops)
|
2012-07-20 12:34:45 +00:00
|
|
|
|
2012-07-20 19:19:30 +00:00
|
|
|
static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
|
2012-07-20 12:34:45 +00:00
|
|
|
sy_elem e;
|
|
|
|
e.etype = 0;
|
2012-07-20 19:19:30 +00:00
|
|
|
e.out = v;
|
2012-07-27 11:28:57 +00:00
|
|
|
e.block = NULL;
|
|
|
|
e.ctx = ctx;
|
|
|
|
e.paren = 0;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sy_elem syblock(lex_ctx ctx, ast_block *v) {
|
|
|
|
sy_elem e;
|
|
|
|
e.etype = 0;
|
|
|
|
e.out = (ast_expression*)v;
|
|
|
|
e.block = v;
|
2012-07-20 19:19:30 +00:00
|
|
|
e.ctx = ctx;
|
2012-07-22 09:17:01 +00:00
|
|
|
e.paren = 0;
|
2012-07-20 12:34:45 +00:00
|
|
|
return e;
|
|
|
|
}
|
2012-07-20 12:04:52 +00:00
|
|
|
|
2012-07-20 19:19:30 +00:00
|
|
|
static sy_elem syop(lex_ctx ctx, const oper_info *op) {
|
2012-07-20 13:02:18 +00:00
|
|
|
sy_elem e;
|
|
|
|
e.etype = 1 + (op - operators);
|
2012-07-20 19:19:30 +00:00
|
|
|
e.out = NULL;
|
2012-07-27 11:28:57 +00:00
|
|
|
e.block = NULL;
|
2012-07-20 19:19:30 +00:00
|
|
|
e.ctx = ctx;
|
2012-07-22 09:17:01 +00:00
|
|
|
e.paren = 0;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2012-07-27 13:02:39 +00:00
|
|
|
static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
|
2012-07-22 09:17:01 +00:00
|
|
|
sy_elem e;
|
|
|
|
e.etype = 0;
|
2012-07-27 13:02:39 +00:00
|
|
|
e.off = off;
|
2012-07-22 09:17:01 +00:00
|
|
|
e.out = NULL;
|
2012-07-27 11:28:57 +00:00
|
|
|
e.block = NULL;
|
2012-07-22 09:17:01 +00:00
|
|
|
e.ctx = ctx;
|
|
|
|
e.paren = p;
|
2012-07-20 13:02:18 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2012-08-12 18:13:14 +00:00
|
|
|
#ifdef DEBUGSHUNT
|
|
|
|
# define DEBUGSHUNTDO(x) x
|
|
|
|
#else
|
|
|
|
# define DEBUGSHUNTDO(x)
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 19:19:30 +00:00
|
|
|
static bool parser_sy_pop(parser_t *parser, shunt *sy)
|
2012-07-20 13:02:18 +00:00
|
|
|
{
|
2012-07-20 13:20:07 +00:00
|
|
|
const oper_info *op;
|
2012-07-20 19:19:30 +00:00
|
|
|
lex_ctx ctx;
|
2012-07-20 19:32:51 +00:00
|
|
|
ast_expression *out = NULL;
|
|
|
|
ast_expression *exprs[3];
|
2012-07-27 11:28:57 +00:00
|
|
|
ast_block *blocks[3];
|
2012-08-12 18:13:14 +00:00
|
|
|
size_t i, assignop;
|
2012-07-20 13:20:07 +00:00
|
|
|
|
2012-07-20 13:02:49 +00:00
|
|
|
if (!sy->ops_count) {
|
|
|
|
parseerror(parser, "internal error: missing operator");
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-20 13:20:07 +00:00
|
|
|
|
2012-07-22 09:17:01 +00:00
|
|
|
if (sy->ops[sy->ops_count-1].paren) {
|
|
|
|
parseerror(parser, "unmatched parenthesis");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 13:20:07 +00:00
|
|
|
op = &operators[sy->ops[sy->ops_count-1].etype - 1];
|
2012-07-20 19:19:30 +00:00
|
|
|
ctx = sy->ops[sy->ops_count-1].ctx;
|
2012-07-20 13:20:07 +00:00
|
|
|
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("apply %s\n", op->op));
|
|
|
|
|
2012-07-20 13:20:07 +00:00
|
|
|
if (sy->out_count < op->operands) {
|
2012-07-20 19:32:51 +00:00
|
|
|
parseerror(parser, "internal error: not enough operands: %i", sy->out_count);
|
2012-07-20 13:20:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sy->ops_count--;
|
|
|
|
|
|
|
|
sy->out_count -= op->operands;
|
2012-07-20 19:32:51 +00:00
|
|
|
for (i = 0; i < op->operands; ++i) {
|
2012-07-27 11:28:57 +00:00
|
|
|
exprs[i] = sy->out[sy->out_count+i].out;
|
|
|
|
blocks[i] = sy->out[sy->out_count+i].block;
|
2012-07-20 19:32:51 +00:00
|
|
|
}
|
2012-07-20 13:20:07 +00:00
|
|
|
|
2012-07-27 12:40:43 +00:00
|
|
|
if (blocks[0] && !blocks[0]->exprs_count && op->id != opid1(',')) {
|
|
|
|
parseerror(parser, "internal error: operator cannot be applied on empty blocks");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 13:20:07 +00:00
|
|
|
switch (op->id)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
parseerror(parser, "internal error: unhandled operand");
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
|
2012-08-12 18:13:14 +00:00
|
|
|
case opid1('.'):
|
|
|
|
if (exprs[0]->expression.vtype == TYPE_ENTITY) {
|
|
|
|
if (exprs[1]->expression.vtype != TYPE_FIELD) {
|
|
|
|
parseerror(parser, "type error: right hand of member-operand should be an entity-field");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
|
|
|
|
}
|
|
|
|
else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
|
|
|
|
parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-07-27 11:28:57 +00:00
|
|
|
case opid1(','):
|
|
|
|
if (blocks[0]) {
|
|
|
|
if (!ast_block_exprs_add(blocks[0], exprs[1]))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
blocks[0] = ast_block_new(ctx);
|
|
|
|
if (!ast_block_exprs_add(blocks[0], exprs[0]) ||
|
|
|
|
!ast_block_exprs_add(blocks[0], exprs[1]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ast_block_set_type(blocks[0], exprs[1]))
|
|
|
|
return false;
|
2012-07-27 11:42:02 +00:00
|
|
|
|
|
|
|
sy->out[sy->out_count++] = syblock(ctx, blocks[0]);
|
|
|
|
return true;
|
2012-07-27 11:28:57 +00:00
|
|
|
|
2012-07-20 19:19:30 +00:00
|
|
|
case opid1('+'):
|
2012-07-20 19:32:51 +00:00
|
|
|
if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
|
2012-07-20 19:19:30 +00:00
|
|
|
parseerror(parser, "Cannot add type %s and %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[0]->expression.vtype],
|
|
|
|
type_name[exprs[1]->expression.vtype]);
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
}
|
2012-08-13 13:09:27 +00:00
|
|
|
if (exprs[0]->expression.vteyp != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) {
|
|
|
|
parseerror(parser, "type error: %s - %s not defined",
|
|
|
|
type_name[exprs[0]->expression.vtype],
|
|
|
|
type_name[exprs[1]->expression.vtype]);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
switch (exprs[0]->expression.vtype) {
|
2012-07-20 19:19:30 +00:00
|
|
|
case TYPE_FLOAT:
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
case TYPE_VECTOR:
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parseerror(parser, "Cannot add type %s and %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[0]->expression.vtype],
|
|
|
|
type_name[exprs[1]->expression.vtype]);
|
2012-07-20 19:19:30 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case opid1('-'):
|
2012-07-20 19:32:51 +00:00
|
|
|
if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
|
2012-07-20 19:19:30 +00:00
|
|
|
parseerror(parser, "Cannot subtract type %s from %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[1]->expression.vtype],
|
|
|
|
type_name[exprs[0]->expression.vtype]);
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
}
|
2012-08-13 13:09:27 +00:00
|
|
|
if (exprs[0]->expression.vteyp != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) {
|
|
|
|
parseerror(parser, "type error: %s - %s not defined",
|
|
|
|
type_name[exprs[0]->expression.vtype],
|
|
|
|
type_name[exprs[1]->expression.vtype]);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
switch (exprs[0]->expression.vtype) {
|
2012-07-20 19:19:30 +00:00
|
|
|
case TYPE_FLOAT:
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
case TYPE_VECTOR:
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parseerror(parser, "Cannot add type %s from %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[1]->expression.vtype],
|
|
|
|
type_name[exprs[0]->expression.vtype]);
|
2012-07-20 19:19:30 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case opid1('*'):
|
2012-07-20 19:32:51 +00:00
|
|
|
if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
|
|
|
|
exprs[0]->expression.vtype != TYPE_VECTOR &&
|
|
|
|
exprs[0]->expression.vtype != TYPE_FLOAT &&
|
|
|
|
exprs[1]->expression.vtype != TYPE_VECTOR &&
|
|
|
|
exprs[1]->expression.vtype != TYPE_FLOAT)
|
2012-07-20 19:19:30 +00:00
|
|
|
{
|
|
|
|
parseerror(parser, "Cannot multiply type %s from %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[1]->expression.vtype],
|
|
|
|
type_name[exprs[0]->expression.vtype]);
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
switch (exprs[0]->expression.vtype) {
|
2012-07-20 19:19:30 +00:00
|
|
|
case TYPE_FLOAT:
|
2012-07-20 19:32:51 +00:00
|
|
|
if (exprs[1]->expression.vtype == TYPE_VECTOR)
|
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
else
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
case TYPE_VECTOR:
|
2012-07-20 19:32:51 +00:00
|
|
|
if (exprs[1]->expression.vtype == TYPE_FLOAT)
|
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
else
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parseerror(parser, "Cannot add type %s from %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[1]->expression.vtype],
|
|
|
|
type_name[exprs[0]->expression.vtype]);
|
2012-07-20 19:19:30 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case opid1('/'):
|
2012-07-20 19:32:51 +00:00
|
|
|
if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
|
|
|
|
exprs[0]->expression.vtype != TYPE_FLOAT)
|
2012-07-20 19:19:30 +00:00
|
|
|
{
|
|
|
|
parseerror(parser, "Cannot divide types %s and %s",
|
2012-07-20 19:32:51 +00:00
|
|
|
type_name[exprs[0]->expression.vtype],
|
|
|
|
type_name[exprs[1]->expression.vtype]);
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case opid1('='):
|
2012-08-12 18:13:14 +00:00
|
|
|
if (ast_istype(exprs[0], ast_entfield))
|
|
|
|
assignop = type_storep_instr[exprs[0]->expression.vtype];
|
|
|
|
else
|
|
|
|
assignop = type_store_instr[exprs[0]->expression.vtype];
|
|
|
|
out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
|
2012-07-20 19:19:30 +00:00
|
|
|
break;
|
2012-07-20 13:20:07 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 19:32:51 +00:00
|
|
|
if (!out) {
|
|
|
|
parseerror(parser, "failed to apply operand %s", op->op);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("applied %s\n", op->op));
|
2012-07-20 19:32:51 +00:00
|
|
|
sy->out[sy->out_count++] = syexp(ctx, out);
|
2012-07-20 13:02:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-27 13:02:39 +00:00
|
|
|
static bool parser_close_call(parser_t *parser, shunt *sy)
|
|
|
|
{
|
|
|
|
/* was a function call */
|
|
|
|
ast_expression *fun;
|
|
|
|
ast_call *call;
|
|
|
|
|
|
|
|
size_t fid;
|
2012-07-27 13:07:25 +00:00
|
|
|
size_t paramcount;
|
2012-07-27 13:02:39 +00:00
|
|
|
|
|
|
|
sy->ops_count--;
|
|
|
|
fid = sy->ops[sy->ops_count].off;
|
|
|
|
|
|
|
|
/* out[fid] is the function
|
|
|
|
* everything above is parameters...
|
|
|
|
* 0 params = nothing
|
|
|
|
* 1 params = ast_expression
|
|
|
|
* more = ast_block
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sy->out_count < 1 || sy->out_count <= fid) {
|
|
|
|
parseerror(parser, "internal error: function call needs function and parameter list...");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fun = sy->out[fid].out;
|
|
|
|
|
|
|
|
call = ast_call_new(sy->ops[sy->ops_count].ctx, fun);
|
|
|
|
if (!call) {
|
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fid+1 == sy->out_count) {
|
|
|
|
/* no arguments */
|
2012-07-27 13:07:25 +00:00
|
|
|
paramcount = 0;
|
2012-07-27 13:02:39 +00:00
|
|
|
} else if (fid+2 == sy->out_count) {
|
|
|
|
ast_block *params;
|
|
|
|
sy->out_count--;
|
|
|
|
params = sy->out[sy->out_count].block;
|
|
|
|
if (!params) {
|
|
|
|
/* 1 param */
|
2012-07-27 13:07:25 +00:00
|
|
|
paramcount = 1;
|
2012-07-27 13:02:39 +00:00
|
|
|
if (!ast_call_params_add(call, sy->out[sy->out_count].out)) {
|
|
|
|
ast_delete(sy->out[sy->out_count].out);
|
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2012-07-27 13:07:25 +00:00
|
|
|
paramcount = params->exprs_count;
|
2012-07-27 13:02:39 +00:00
|
|
|
MEM_VECTOR_MOVE(params, exprs, call, params);
|
|
|
|
ast_delete(params);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parseerror(parser, "invalid function call");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overwrite fid, the function, with a call */
|
|
|
|
sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
|
2012-07-27 13:07:25 +00:00
|
|
|
|
|
|
|
if (fun->expression.vtype != TYPE_FUNCTION) {
|
|
|
|
parseerror(parser, "not a function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fun->expression.next) {
|
2012-07-27 13:26:48 +00:00
|
|
|
parseerror(parser, "could not determine function return type");
|
2012-07-27 13:07:25 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2012-07-27 13:26:48 +00:00
|
|
|
if (fun->expression.params_count != paramcount) {
|
|
|
|
parseerror(parser, "expected %i parameters, got %i", (int)fun->expression.params_count, paramcount);
|
2012-07-27 13:07:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 13:02:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
|
2012-07-27 11:07:16 +00:00
|
|
|
{
|
|
|
|
if (!sy->ops_count) {
|
|
|
|
parseerror(parser, "unmatched closing paren");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sy->ops[sy->ops_count-1].paren == 1) {
|
|
|
|
parseerror(parser, "empty parenthesis expression");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (sy->ops_count) {
|
2012-07-27 12:40:43 +00:00
|
|
|
if (sy->ops[sy->ops_count-1].paren == 'f') {
|
2012-07-27 13:02:39 +00:00
|
|
|
if (!parser_close_call(parser, sy))
|
2012-07-27 12:40:43 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2012-07-27 11:07:16 +00:00
|
|
|
if (sy->ops[sy->ops_count-1].paren == 1) {
|
|
|
|
sy->ops_count--;
|
2012-07-27 13:02:39 +00:00
|
|
|
return !functions_only;
|
2012-07-27 11:07:16 +00:00
|
|
|
}
|
|
|
|
if (!parser_sy_pop(parser, sy))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-20 12:45:23 +00:00
|
|
|
static ast_expression* parser_expression(parser_t *parser)
|
2012-07-20 11:00:11 +00:00
|
|
|
{
|
2012-07-20 12:45:23 +00:00
|
|
|
ast_expression *expr = NULL;
|
2012-07-20 19:19:30 +00:00
|
|
|
shunt sy;
|
2012-07-20 12:45:23 +00:00
|
|
|
bool wantop = false;
|
2012-08-12 17:24:08 +00:00
|
|
|
bool gotmemberof = false;
|
2012-07-20 12:04:52 +00:00
|
|
|
|
2012-08-11 09:17:46 +00:00
|
|
|
/* count the parens because an if starts with one, so the
|
|
|
|
* end of a condition is an unmatched closing paren
|
|
|
|
*/
|
|
|
|
int parens = 0;
|
|
|
|
|
2012-07-20 12:04:52 +00:00
|
|
|
MEM_VECTOR_INIT(&sy, out);
|
|
|
|
MEM_VECTOR_INIT(&sy, ops);
|
|
|
|
|
2012-07-20 12:45:23 +00:00
|
|
|
while (true)
|
2012-07-20 12:34:45 +00:00
|
|
|
{
|
2012-08-12 17:24:08 +00:00
|
|
|
if (gotmemberof)
|
|
|
|
gotmemberof = false;
|
|
|
|
else
|
|
|
|
parser->memberof = 0;
|
2012-07-20 12:45:23 +00:00
|
|
|
if (!wantop)
|
|
|
|
{
|
2012-07-22 09:17:01 +00:00
|
|
|
bool nextwant = true;
|
2012-07-20 12:45:23 +00:00
|
|
|
if (parser->tok == TOKEN_IDENT)
|
|
|
|
{
|
|
|
|
/* variable */
|
2012-08-12 17:24:08 +00:00
|
|
|
ast_expression *var;
|
|
|
|
if (opts_standard == COMPILER_GMQCC)
|
|
|
|
{
|
2012-08-12 17:29:18 +00:00
|
|
|
if (parser->memberof == TYPE_ENTITY)
|
2012-08-12 17:24:08 +00:00
|
|
|
var = parser_find_field(parser, parser_tokval(parser));
|
|
|
|
else if (parser->memberof == TYPE_VECTOR)
|
|
|
|
{
|
|
|
|
parseerror(parser, "TODO: implement effective vector member access");
|
|
|
|
goto onerr;
|
|
|
|
}
|
|
|
|
else if (parser->memberof) {
|
|
|
|
parseerror(parser, "namespace for member not found");
|
|
|
|
goto onerr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
var = parser_find_var(parser, parser_tokval(parser));
|
|
|
|
} else {
|
|
|
|
var = parser_find_var(parser, parser_tokval(parser));
|
|
|
|
if (!var)
|
|
|
|
var = parser_find_field(parser, parser_tokval(parser));
|
|
|
|
}
|
2012-07-20 12:45:23 +00:00
|
|
|
if (!var) {
|
|
|
|
parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-10 20:59:47 +00:00
|
|
|
if (!shunt_out_add(&sy, syexp(parser_ctx(parser), var))) {
|
2012-07-20 12:45:23 +00:00
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push %s\n", parser_tokval(parser)));
|
2012-07-22 09:17:01 +00:00
|
|
|
}
|
|
|
|
else if (parser->tok == TOKEN_FLOATCONST) {
|
2012-07-20 12:45:23 +00:00
|
|
|
ast_value *val = parser_const_float(parser, (parser_token(parser)->constval.f));
|
|
|
|
if (!val)
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
|
2012-07-20 12:45:23 +00:00
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push %g\n", parser_token(parser)->constval.f));
|
2012-07-22 09:17:01 +00:00
|
|
|
}
|
|
|
|
else if (parser->tok == TOKEN_INTCONST) {
|
2012-07-20 12:45:23 +00:00
|
|
|
ast_value *val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
|
|
|
|
if (!val)
|
|
|
|
return false;
|
2012-07-20 19:19:30 +00:00
|
|
|
if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
|
2012-07-20 12:45:23 +00:00
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push %i\n", parser_token(parser)->constval.i));
|
2012-07-22 09:17:01 +00:00
|
|
|
}
|
2012-07-27 14:10:43 +00:00
|
|
|
else if (parser->tok == TOKEN_STRINGCONST) {
|
|
|
|
ast_value *val = parser_const_string(parser, parser_tokval(parser));
|
|
|
|
if (!val)
|
|
|
|
return false;
|
|
|
|
if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
|
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push string\n"));
|
2012-07-27 14:10:43 +00:00
|
|
|
}
|
2012-07-27 14:20:53 +00:00
|
|
|
else if (parser->tok == TOKEN_VECTORCONST) {
|
|
|
|
ast_value *val = parser_const_vector(parser, parser_token(parser)->constval.v);
|
|
|
|
if (!val)
|
|
|
|
return false;
|
|
|
|
if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
|
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push '%g %g %g'\n",
|
|
|
|
parser_token(parser)->constval.v.x,
|
|
|
|
parser_token(parser)->constval.v.y,
|
|
|
|
parser_token(parser)->constval.v.z));
|
2012-07-27 14:20:53 +00:00
|
|
|
}
|
2012-07-22 09:17:01 +00:00
|
|
|
else if (parser->tok == '(') {
|
2012-08-11 09:17:46 +00:00
|
|
|
++parens;
|
2012-07-22 09:17:01 +00:00
|
|
|
nextwant = false; /* not expecting an operator next */
|
2012-07-27 13:02:39 +00:00
|
|
|
if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 1, 0))) {
|
2012-07-22 09:17:01 +00:00
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push (\n"));
|
2012-07-22 09:17:01 +00:00
|
|
|
}
|
2012-07-27 13:02:39 +00:00
|
|
|
else if (parser->tok == ')') {
|
2012-08-12 18:21:21 +00:00
|
|
|
DEBUGSHUNTDO(printf("do[nop] )\n"));
|
2012-08-11 09:17:46 +00:00
|
|
|
--parens;
|
|
|
|
if (parens < 0)
|
|
|
|
break;
|
2012-07-27 13:02:39 +00:00
|
|
|
/* allowed for function calls */
|
|
|
|
if (!parser_close_paren(parser, &sy, true))
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-07-22 09:17:01 +00:00
|
|
|
else {
|
2012-07-20 12:45:23 +00:00
|
|
|
/* TODO: prefix operators */
|
|
|
|
parseerror(parser, "expected statement");
|
|
|
|
goto onerr;
|
|
|
|
}
|
2012-07-22 09:17:01 +00:00
|
|
|
wantop = nextwant;
|
|
|
|
parser->lex->flags.noops = !wantop;
|
2012-07-20 12:45:23 +00:00
|
|
|
} else {
|
2012-08-12 18:21:21 +00:00
|
|
|
bool nextwant = false;
|
2012-07-27 10:35:14 +00:00
|
|
|
if (parser->tok == '(') {
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push (\n"));
|
2012-08-11 09:17:46 +00:00
|
|
|
++parens;
|
2012-07-27 10:35:14 +00:00
|
|
|
/* we expected an operator, this is the function-call operator */
|
2012-07-27 13:02:39 +00:00
|
|
|
if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 'f', sy.out_count-1))) {
|
2012-07-27 12:40:43 +00:00
|
|
|
parseerror(parser, "out of memory");
|
|
|
|
goto onerr;
|
2012-07-27 10:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (parser->tok == ')') {
|
2012-08-12 18:21:21 +00:00
|
|
|
DEBUGSHUNTDO(printf("do[op] )\n"));
|
2012-08-11 09:17:46 +00:00
|
|
|
--parens;
|
|
|
|
if (parens < 0)
|
|
|
|
break;
|
2012-07-22 09:17:01 +00:00
|
|
|
/* we do expect an operator next */
|
|
|
|
/* closing an opening paren */
|
2012-07-27 13:02:39 +00:00
|
|
|
if (!parser_close_paren(parser, &sy, false))
|
2012-07-22 09:17:01 +00:00
|
|
|
goto onerr;
|
2012-08-12 18:21:21 +00:00
|
|
|
nextwant = true;
|
2012-07-22 09:17:01 +00:00
|
|
|
}
|
|
|
|
else if (parser->tok != TOKEN_OPERATOR) {
|
2012-07-20 12:45:23 +00:00
|
|
|
parseerror(parser, "expected operator or end of statement");
|
|
|
|
goto onerr;
|
2012-07-22 09:17:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-20 12:45:23 +00:00
|
|
|
/* classify the operator */
|
|
|
|
/* TODO: suffix operators */
|
2012-07-20 13:02:18 +00:00
|
|
|
const oper_info *op;
|
|
|
|
const oper_info *olast = NULL;
|
2012-07-20 12:45:23 +00:00
|
|
|
size_t o;
|
|
|
|
for (o = 0; o < operator_count; ++o) {
|
|
|
|
if (!(operators[o].flags & OP_PREFIX) &&
|
|
|
|
!(operators[o].flags & OP_SUFFIX) && /* remove this */
|
|
|
|
!strcmp(parser_tokval(parser), operators[o].op))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (o == operator_count) {
|
|
|
|
/* no operator found... must be the end of the statement */
|
|
|
|
break;
|
|
|
|
}
|
2012-07-20 13:02:18 +00:00
|
|
|
/* found an operator */
|
|
|
|
op = &operators[o];
|
2012-08-12 17:24:08 +00:00
|
|
|
if (op->id == opid1('.')) {
|
|
|
|
/* for gmqcc standard: open up the namespace of the previous type */
|
|
|
|
ast_expression *prevex = sy.out[sy.out_count-1].out;
|
|
|
|
if (!prevex) {
|
|
|
|
parseerror(parser, "unexpected member operator");
|
|
|
|
goto onerr;
|
|
|
|
}
|
|
|
|
if (prevex->expression.vtype == TYPE_ENTITY)
|
2012-08-12 17:29:18 +00:00
|
|
|
parser->memberof = TYPE_ENTITY;
|
2012-08-12 17:24:08 +00:00
|
|
|
else if (prevex->expression.vtype == TYPE_VECTOR)
|
|
|
|
parser->memberof = TYPE_VECTOR;
|
|
|
|
else {
|
|
|
|
parseerror(parser, "type error: type has no members");
|
|
|
|
goto onerr;
|
|
|
|
}
|
|
|
|
gotmemberof = true;
|
|
|
|
}
|
2012-07-20 13:02:18 +00:00
|
|
|
|
2012-07-22 09:17:01 +00:00
|
|
|
if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
|
2012-07-20 13:02:18 +00:00
|
|
|
olast = &operators[sy.ops[sy.ops_count-1].etype-1];
|
|
|
|
|
|
|
|
while (olast && (
|
|
|
|
(op->prec < olast->prec) ||
|
|
|
|
(op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
|
|
|
|
{
|
|
|
|
if (!parser_sy_pop(parser, &sy))
|
|
|
|
goto onerr;
|
2012-07-22 09:17:01 +00:00
|
|
|
if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
|
|
|
|
olast = &operators[sy.ops[sy.ops_count-1].etype-1];
|
2012-08-12 18:13:14 +00:00
|
|
|
else
|
|
|
|
olast = NULL;
|
2012-07-20 13:02:18 +00:00
|
|
|
}
|
|
|
|
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("push operator %s\n", op->op));
|
2012-07-20 19:19:30 +00:00
|
|
|
if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
|
2012-07-20 13:02:18 +00:00
|
|
|
goto onerr;
|
2012-07-20 12:45:23 +00:00
|
|
|
}
|
2012-08-12 18:21:21 +00:00
|
|
|
wantop = nextwant;
|
|
|
|
parser->lex->flags.noops = !wantop;
|
2012-07-20 12:34:45 +00:00
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
if (!parser_next(parser)) {
|
|
|
|
goto onerr;
|
|
|
|
}
|
|
|
|
if (parser->tok == ';') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-11 09:17:46 +00:00
|
|
|
if (parens >= 0 && !parser_next(parser)) {
|
2012-07-20 19:32:51 +00:00
|
|
|
parseerror(parser, "Unexpected end of file");
|
|
|
|
goto onerr;
|
2012-07-20 12:34:45 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 13:02:18 +00:00
|
|
|
while (sy.ops_count) {
|
|
|
|
if (!parser_sy_pop(parser, &sy))
|
|
|
|
goto onerr;
|
|
|
|
}
|
|
|
|
|
2012-07-20 12:45:23 +00:00
|
|
|
parser->lex->flags.noops = true;
|
2012-07-20 13:02:18 +00:00
|
|
|
if (!sy.out_count) {
|
|
|
|
parseerror(parser, "empty expression");
|
|
|
|
expr = NULL;
|
|
|
|
} else
|
|
|
|
expr = sy.out[0].out;
|
2012-07-20 12:34:45 +00:00
|
|
|
MEM_VECTOR_CLEAR(&sy, out);
|
|
|
|
MEM_VECTOR_CLEAR(&sy, ops);
|
2012-08-12 18:13:14 +00:00
|
|
|
DEBUGSHUNTDO(printf("shut done\n"));
|
2012-07-20 12:45:23 +00:00
|
|
|
return expr;
|
2012-07-20 12:34:45 +00:00
|
|
|
|
|
|
|
onerr:
|
2012-07-20 12:45:23 +00:00
|
|
|
parser->lex->flags.noops = true;
|
2012-07-20 12:04:52 +00:00
|
|
|
MEM_VECTOR_CLEAR(&sy, out);
|
|
|
|
MEM_VECTOR_CLEAR(&sy, ops);
|
2012-07-20 12:45:23 +00:00
|
|
|
return NULL;
|
2012-07-20 11:00:11 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 19:32:51 +00:00
|
|
|
static bool parser_variable(parser_t *parser, ast_block *localblock);
|
2012-08-11 08:35:10 +00:00
|
|
|
static ast_block* parser_parse_block(parser_t *parser);
|
2012-08-11 09:17:46 +00:00
|
|
|
static ast_expression* parser_parse_statement_or_block(parser_t *parser);
|
2012-08-11 08:30:54 +00:00
|
|
|
static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
|
2012-07-19 17:57:24 +00:00
|
|
|
{
|
2012-07-19 18:15:30 +00:00
|
|
|
if (parser->tok == TOKEN_TYPENAME)
|
|
|
|
{
|
|
|
|
/* local variable */
|
2012-08-11 08:30:54 +00:00
|
|
|
if (!block) {
|
|
|
|
parseerror(parser, "cannot declare a variable from here");
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
if (!parser_variable(parser, block))
|
2012-07-20 10:46:59 +00:00
|
|
|
return false;
|
2012-08-11 08:30:54 +00:00
|
|
|
*out = NULL;
|
2012-07-20 10:46:59 +00:00
|
|
|
return true;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
2012-07-26 21:22:51 +00:00
|
|
|
else if (parser->tok == TOKEN_KEYWORD)
|
|
|
|
{
|
|
|
|
if (!strcmp(parser_tokval(parser), "return"))
|
|
|
|
{
|
2012-07-28 14:06:44 +00:00
|
|
|
ast_expression *exp = NULL;
|
|
|
|
ast_return *ret = NULL;
|
|
|
|
ast_value *expected = parser->function->vtype;
|
2012-07-26 21:26:34 +00:00
|
|
|
|
|
|
|
if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected return expression");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-28 14:06:44 +00:00
|
|
|
if (parser->tok != ';') {
|
|
|
|
exp = parser_expression(parser);
|
|
|
|
if (!exp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (exp->expression.vtype != expected->expression.next->expression.vtype) {
|
|
|
|
parseerror(parser, "return with invalid expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ast_return_new(exp->expression.node.context, exp);
|
|
|
|
if (!ret) {
|
|
|
|
ast_delete(exp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-11 08:30:54 +00:00
|
|
|
*out = (ast_expression*)ret;
|
2012-07-28 14:06:44 +00:00
|
|
|
} else if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected semicolon");
|
|
|
|
if (expected->expression.next->expression.vtype != TYPE_VOID) {
|
|
|
|
parseerror(parser, "return without value");
|
|
|
|
}
|
2012-07-26 21:22:51 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-11 09:17:46 +00:00
|
|
|
else if (!strcmp(parser_tokval(parser), "if"))
|
|
|
|
{
|
|
|
|
ast_ifthen *ifthen;
|
|
|
|
ast_expression *cond, *ontrue, *onfalse = NULL;
|
|
|
|
|
|
|
|
lex_ctx ctx = parser_ctx(parser);
|
|
|
|
|
|
|
|
/* skip the 'if' and check for opening paren */
|
|
|
|
if (!parser_next(parser) || parser->tok != '(') {
|
|
|
|
parseerror(parser, "expected 'if' condition in parenthesis");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* parse into the expression */
|
|
|
|
if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected 'if' condition after opening paren");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* parse the condition */
|
|
|
|
cond = parser_expression(parser);
|
|
|
|
if (!cond)
|
|
|
|
return false;
|
|
|
|
/* closing paren */
|
|
|
|
if (parser->tok != ')') {
|
|
|
|
parseerror(parser, "expected closing paren after 'if' condition");
|
|
|
|
ast_delete(cond);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* parse into the 'then' branch */
|
|
|
|
if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected statement for on-true branch of 'if'");
|
|
|
|
ast_delete(cond);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ontrue = parser_parse_statement_or_block(parser);
|
|
|
|
if (!ontrue) {
|
|
|
|
ast_delete(cond);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* check for an else */
|
|
|
|
if (!strcmp(parser_tokval(parser), "else")) {
|
|
|
|
/* parse into the 'else' branch */
|
|
|
|
if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected on-false branch after 'else'");
|
|
|
|
ast_delete(ontrue);
|
|
|
|
ast_delete(cond);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
onfalse = parser_parse_statement_or_block(parser);
|
|
|
|
if (!onfalse) {
|
|
|
|
ast_delete(ontrue);
|
|
|
|
ast_delete(cond);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
|
|
|
|
*out = (ast_expression*)ifthen;
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-26 21:25:33 +00:00
|
|
|
parseerror(parser, "Unexpected keyword");
|
|
|
|
return false;
|
2012-07-26 21:22:51 +00:00
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
else if (parser->tok == '{')
|
|
|
|
{
|
2012-08-11 08:35:10 +00:00
|
|
|
ast_block *inner;
|
|
|
|
inner = parser_parse_block(parser);
|
|
|
|
if (!inner)
|
|
|
|
return false;
|
|
|
|
*out = (ast_expression*)inner;
|
|
|
|
return true;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
2012-07-20 11:00:11 +00:00
|
|
|
else
|
2012-07-20 12:45:23 +00:00
|
|
|
{
|
|
|
|
ast_expression *exp = parser_expression(parser);
|
|
|
|
if (!exp)
|
|
|
|
return false;
|
2012-08-11 08:30:54 +00:00
|
|
|
*out = exp;
|
2012-07-20 12:45:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-19 17:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-11 08:35:10 +00:00
|
|
|
static void parser_pop_local(parser_t *parser)
|
|
|
|
{
|
|
|
|
parser->locals_count--;
|
|
|
|
mem_d(parser->locals[parser->locals_count].name);
|
|
|
|
}
|
|
|
|
|
2012-07-20 10:46:59 +00:00
|
|
|
static ast_block* parser_parse_block(parser_t *parser)
|
2012-07-16 12:14:37 +00:00
|
|
|
{
|
2012-07-19 18:30:36 +00:00
|
|
|
size_t oldblocklocal;
|
|
|
|
ast_block *block = NULL;
|
|
|
|
|
|
|
|
oldblocklocal = parser->blocklocal;
|
|
|
|
parser->blocklocal = parser->locals_count;
|
2012-07-19 18:15:30 +00:00
|
|
|
|
|
|
|
if (!parser_next(parser)) { /* skip the '{' */
|
|
|
|
parseerror(parser, "expected function body");
|
2012-07-19 18:30:36 +00:00
|
|
|
goto cleanup;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block = ast_block_new(parser_ctx(parser));
|
|
|
|
|
|
|
|
while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
|
2012-07-16 12:52:52 +00:00
|
|
|
{
|
2012-08-11 08:30:54 +00:00
|
|
|
ast_expression *expr;
|
2012-07-19 18:15:30 +00:00
|
|
|
if (parser->tok == '}')
|
|
|
|
break;
|
2012-07-16 12:52:52 +00:00
|
|
|
|
2012-08-11 08:30:54 +00:00
|
|
|
if (!parser_parse_statement(parser, block, &expr)) {
|
|
|
|
ast_block_delete(block);
|
|
|
|
block = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!expr)
|
|
|
|
continue;
|
|
|
|
if (!ast_block_exprs_add(block, expr)) {
|
|
|
|
ast_delete(expr);
|
2012-07-19 18:15:30 +00:00
|
|
|
ast_block_delete(block);
|
2012-07-19 18:30:36 +00:00
|
|
|
block = NULL;
|
|
|
|
goto cleanup;
|
2012-07-16 12:52:52 +00:00
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
2012-07-19 18:30:36 +00:00
|
|
|
|
2012-07-20 10:46:59 +00:00
|
|
|
if (parser->tok != '}') {
|
|
|
|
ast_block_delete(block);
|
|
|
|
block = NULL;
|
|
|
|
} else {
|
|
|
|
(void)parser_next(parser);
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:30:36 +00:00
|
|
|
cleanup:
|
2012-08-11 08:35:10 +00:00
|
|
|
while (parser->locals_count > parser->blocklocal)
|
|
|
|
parser_pop_local(parser);
|
2012-08-12 16:58:46 +00:00
|
|
|
parser->blocklocal = oldblocklocal;
|
|
|
|
/* unroll the local vector */
|
2012-07-19 18:15:30 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2012-08-11 08:30:54 +00:00
|
|
|
static ast_expression* parser_parse_statement_or_block(parser_t *parser)
|
|
|
|
{
|
|
|
|
ast_expression *expr;
|
|
|
|
if (parser->tok == '{')
|
|
|
|
return (ast_expression*)parser_parse_block(parser);
|
|
|
|
if (!parser_parse_statement(parser, NULL, &expr))
|
|
|
|
return NULL;
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2012-07-20 19:32:51 +00:00
|
|
|
static bool parser_variable(parser_t *parser, ast_block *localblock)
|
2012-07-19 18:15:30 +00:00
|
|
|
{
|
2012-07-20 10:53:42 +00:00
|
|
|
bool isfunc = false;
|
2012-07-19 18:15:30 +00:00
|
|
|
ast_function *func = NULL;
|
2012-07-20 10:53:42 +00:00
|
|
|
lex_ctx ctx;
|
|
|
|
ast_value *var;
|
2012-08-10 20:59:47 +00:00
|
|
|
varentry_t varent;
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
int basetype = parser_token(parser)->constval.t;
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!parser_next(parser)) { /* skip basetype or comma */
|
|
|
|
parseerror(parser, "expected variable declaration");
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
isfunc = false;
|
|
|
|
func = NULL;
|
|
|
|
ctx = parser_ctx(parser);
|
|
|
|
var = parser_parse_type(parser, basetype, &isfunc);
|
2012-07-16 12:52:52 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (!var)
|
|
|
|
return false;
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (parser->tok != TOKEN_IDENT) {
|
|
|
|
parseerror(parser, "expected variable name\n");
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 19:32:51 +00:00
|
|
|
if (!localblock && parser_find_global(parser, parser_tokval(parser))) {
|
2012-07-19 09:41:16 +00:00
|
|
|
ast_value_delete(var);
|
2012-07-20 10:53:42 +00:00
|
|
|
parseerror(parser, "global already exists: %s\n", parser_tokval(parser));
|
2012-07-16 12:52:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 19:32:51 +00:00
|
|
|
if (localblock && parser_find_local(parser, parser_tokval(parser), parser->blocklocal)) {
|
2012-07-19 09:41:16 +00:00
|
|
|
ast_value_delete(var);
|
2012-07-20 10:53:42 +00:00
|
|
|
parseerror(parser, "local variable already exists: %s\n", parser_tokval(parser));
|
2012-07-19 09:41:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (!ast_value_set_name(var, parser_tokval(parser))) {
|
|
|
|
parseerror(parser, "failed to set variable name\n");
|
|
|
|
ast_value_delete(var);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 15:53:45 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (isfunc) {
|
|
|
|
/* a function was defined */
|
|
|
|
ast_value *fval;
|
|
|
|
|
|
|
|
/* turn var into a value of TYPE_FUNCTION, with the old var
|
|
|
|
* as return type
|
|
|
|
*/
|
|
|
|
fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
|
|
|
|
func = ast_function_new(ctx, var->name, fval);
|
|
|
|
if (!fval || !func) {
|
|
|
|
ast_value_delete(var);
|
|
|
|
if (fval) ast_value_delete(fval);
|
|
|
|
if (func) ast_function_delete(func);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 15:53:45 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
fval->expression.next = (ast_expression*)var;
|
2012-07-27 13:26:48 +00:00
|
|
|
MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (!parser_t_functions_add(parser, func)) {
|
|
|
|
ast_value_delete(var);
|
|
|
|
if (fval) ast_value_delete(fval);
|
|
|
|
if (func) ast_function_delete(func);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
var = fval;
|
2012-07-19 15:39:19 +00:00
|
|
|
}
|
2012-07-20 10:53:42 +00:00
|
|
|
|
2012-08-10 20:59:47 +00:00
|
|
|
varent.name = util_strdup(var->name);
|
|
|
|
varent.var = (ast_expression*)var;
|
|
|
|
if (var->expression.vtype == TYPE_VECTOR)
|
2012-07-20 19:32:51 +00:00
|
|
|
{
|
2012-08-10 20:59:47 +00:00
|
|
|
size_t len = strlen(varent.name);
|
|
|
|
varentry_t vx, vy, vz;
|
|
|
|
vx.var = (ast_expression*)ast_member_new(var->expression.node.context, (ast_expression*)var, 0);
|
|
|
|
vy.var = (ast_expression*)ast_member_new(var->expression.node.context, (ast_expression*)var, 1);
|
|
|
|
vz.var = (ast_expression*)ast_member_new(var->expression.node.context, (ast_expression*)var, 2);
|
|
|
|
vx.name = mem_a(len+3);
|
|
|
|
vy.name = mem_a(len+3);
|
|
|
|
vz.name = mem_a(len+3);
|
|
|
|
strcpy(vx.name, varent.name);
|
|
|
|
strcpy(vy.name, varent.name);
|
|
|
|
strcpy(vz.name, varent.name);
|
|
|
|
vx.name[len] = vy.name[len] = vz.name[len] = '_';
|
|
|
|
vx.name[len+1] = 'x';
|
|
|
|
vy.name[len+1] = 'y';
|
|
|
|
vz.name[len+1] = 'z';
|
|
|
|
vx.name[len+2] = vy.name[len+2] = vz.name[len+2] = 0;
|
|
|
|
|
|
|
|
if (!localblock) {
|
|
|
|
(void)!parser_t_globals_add(parser, varent);
|
|
|
|
(void)!parser_t_globals_add(parser, vx);
|
|
|
|
(void)!parser_t_globals_add(parser, vy);
|
|
|
|
(void)!parser_t_globals_add(parser, vz);
|
|
|
|
} else {
|
|
|
|
(void)!parser_t_locals_add(parser, varent);
|
|
|
|
(void)!parser_t_locals_add(parser, vx);
|
|
|
|
(void)!parser_t_locals_add(parser, vy);
|
|
|
|
(void)!parser_t_locals_add(parser, vz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (!localblock && !parser_t_globals_add(parser, varent)) ||
|
|
|
|
( localblock && !parser_t_locals_add(parser, varent)) )
|
|
|
|
{
|
|
|
|
ast_value_delete(var);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-20 19:32:51 +00:00
|
|
|
}
|
|
|
|
if (localblock && !ast_block_locals_add(localblock, var))
|
2012-07-20 10:53:42 +00:00
|
|
|
{
|
2012-08-10 20:59:47 +00:00
|
|
|
parser_pop_local(parser);
|
2012-07-20 10:53:42 +00:00
|
|
|
ast_value_delete(var);
|
2012-07-19 18:15:30 +00:00
|
|
|
return false;
|
2012-07-19 10:17:00 +00:00
|
|
|
}
|
2012-07-20 10:53:42 +00:00
|
|
|
|
2012-07-19 18:15:30 +00:00
|
|
|
if (!parser_next(parser)) {
|
2012-07-20 10:53:42 +00:00
|
|
|
ast_value_delete(var);
|
2012-07-19 10:17:00 +00:00
|
|
|
return false;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
2012-07-20 10:53:42 +00:00
|
|
|
|
|
|
|
if (parser->tok == ';') {
|
|
|
|
if (!parser_next(parser))
|
|
|
|
return parser->tok == TOKEN_EOF;
|
|
|
|
return true;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
2012-07-19 10:17:00 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (parser->tok == ',') {
|
|
|
|
/* another var */
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-19 15:39:19 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (parser->tok != '=') {
|
|
|
|
parseerror(parser, "expected '=' or ';'");
|
2012-07-19 18:15:30 +00:00
|
|
|
return false;
|
2012-07-19 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (!parser_next(parser))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (parser->tok == '#') {
|
2012-07-20 19:32:51 +00:00
|
|
|
if (localblock) {
|
2012-07-20 10:53:42 +00:00
|
|
|
parseerror(parser, "cannot declare builtins within functions");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!isfunc || !func) {
|
|
|
|
parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected builtin number");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (parser->tok != TOKEN_INTCONST) {
|
|
|
|
parseerror(parser, "builtin number must be an integer constant");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (parser_token(parser)->constval.i <= 0) {
|
|
|
|
parseerror(parser, "builtin number must be positive integer greater than zero");
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 18:15:30 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
func->builtin = -parser_token(parser)->constval.i;
|
|
|
|
} else if (parser->tok == '{') {
|
|
|
|
/* function body */
|
|
|
|
ast_block *block;
|
|
|
|
ast_function *old = parser->function;
|
|
|
|
|
2012-07-20 19:32:51 +00:00
|
|
|
if (localblock) {
|
2012-07-20 10:53:42 +00:00
|
|
|
parseerror(parser, "cannot declare functions within functions");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser->function = func;
|
|
|
|
block = parser_parse_block(parser);
|
|
|
|
parser->function = old;
|
|
|
|
|
|
|
|
if (!block)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ast_function_blocks_add(func, block)) {
|
|
|
|
ast_block_delete(block);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
parseerror(parser, "TODO, const assignment");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parser_next(parser))
|
2012-07-19 15:39:19 +00:00
|
|
|
return false;
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
if (parser->tok == ',') {
|
|
|
|
/* another */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser->tok != ';') {
|
|
|
|
parseerror(parser, "expected semicolon");
|
2012-07-19 15:39:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
(void)parser_next(parser);
|
2012-07-19 17:52:44 +00:00
|
|
|
|
2012-07-20 10:53:42 +00:00
|
|
|
return true;
|
2012-07-19 18:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 10:46:59 +00:00
|
|
|
static bool parser_do(parser_t *parser)
|
2012-07-19 18:15:30 +00:00
|
|
|
{
|
|
|
|
if (parser->tok == TOKEN_TYPENAME)
|
|
|
|
{
|
2012-07-20 19:32:51 +00:00
|
|
|
return parser_variable(parser, NULL);
|
2012-07-16 12:52:52 +00:00
|
|
|
}
|
|
|
|
else if (parser->tok == TOKEN_KEYWORD)
|
|
|
|
{
|
|
|
|
/* handle 'var' and 'const' */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (parser->tok == '.')
|
|
|
|
{
|
2012-08-11 13:40:03 +00:00
|
|
|
ast_value *var;
|
|
|
|
ast_value *fld;
|
|
|
|
bool isfunc = false;
|
|
|
|
int basetype;
|
|
|
|
lex_ctx ctx = parser_ctx(parser);
|
|
|
|
varentry_t varent;
|
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
/* entity-member declaration */
|
2012-08-11 13:40:03 +00:00
|
|
|
if (!parser_next(parser) || parser->tok != TOKEN_TYPENAME) {
|
|
|
|
parseerror(parser, "expected member variable definition");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remember the base/return type */
|
|
|
|
basetype = parser_token(parser)->constval.t;
|
|
|
|
|
|
|
|
/* parse into the declaration */
|
|
|
|
if (!parser_next(parser)) {
|
|
|
|
parseerror(parser, "expected field def");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the field type fully */
|
|
|
|
var = parser_parse_type(parser, basetype, &isfunc);
|
|
|
|
if (!var)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* now the field name */
|
|
|
|
if (parser->tok != TOKEN_IDENT) {
|
|
|
|
parseerror(parser, "expected field name");
|
|
|
|
ast_delete(var);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for an existing field
|
|
|
|
* in original qc we also have to check for an existing
|
|
|
|
* global named like the field
|
|
|
|
*/
|
|
|
|
if (opts_standard == COMPILER_QCC) {
|
|
|
|
if (parser_find_global(parser, parser_tokval(parser))) {
|
|
|
|
parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
|
|
|
|
ast_delete(var);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parser_find_field(parser, parser_tokval(parser))) {
|
|
|
|
parseerror(parser, "field %s already exists", parser_tokval(parser));
|
|
|
|
ast_delete(var);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it was a function, turn it into a function */
|
|
|
|
if (isfunc) {
|
|
|
|
ast_value *fval;
|
|
|
|
/* turn var into a value of TYPE_FUNCTION, with the old var
|
|
|
|
* as return type
|
|
|
|
*/
|
|
|
|
fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
|
|
|
|
if (!fval) {
|
|
|
|
ast_value_delete(var);
|
|
|
|
ast_value_delete(fval);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fval->expression.next = (ast_expression*)var;
|
|
|
|
MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
|
|
|
|
|
|
|
|
var = fval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* turn it into a field */
|
|
|
|
fld = ast_value_new(ctx, parser_tokval(parser), TYPE_FIELD);
|
|
|
|
fld->expression.next = (ast_expression*)var;
|
|
|
|
|
|
|
|
varent.var = (ast_expression*)fld;
|
|
|
|
if (var->expression.vtype == TYPE_VECTOR)
|
|
|
|
{
|
|
|
|
/* create _x, _y and _z fields as well */
|
|
|
|
parseerror(parser, "TODO: vector field members (_x,_y,_z)");
|
|
|
|
ast_delete(fld);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
varent.name = util_strdup(fld->name);
|
|
|
|
(void)!parser_t_fields_add(parser, varent);
|
|
|
|
|
|
|
|
/* end with a semicolon */
|
|
|
|
if (!parser_next(parser) || parser->tok != ';') {
|
|
|
|
parseerror(parser, "semicolon expected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip the semicolon */
|
|
|
|
if (!parser_next(parser))
|
|
|
|
return parser->tok == TOKEN_EOF;
|
|
|
|
|
|
|
|
return true;
|
2012-07-16 12:52:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parseerror(parser, "unexpected token: %s", parser->lex->tok->value);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-16 12:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-28 19:21:41 +00:00
|
|
|
static parser_t *parser;
|
2012-07-16 12:14:37 +00:00
|
|
|
|
2012-07-28 19:21:41 +00:00
|
|
|
bool parser_init()
|
|
|
|
{
|
2012-07-16 12:14:37 +00:00
|
|
|
parser = (parser_t*)mem_a(sizeof(parser_t));
|
|
|
|
if (!parser)
|
|
|
|
return false;
|
|
|
|
|
2012-07-19 18:15:30 +00:00
|
|
|
memset(parser, 0, sizeof(parser));
|
2012-07-19 17:55:25 +00:00
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
MEM_VECTOR_INIT(parser, globals);
|
2012-07-19 18:15:30 +00:00
|
|
|
MEM_VECTOR_INIT(parser, locals);
|
2012-07-28 19:21:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-16 12:14:37 +00:00
|
|
|
|
2012-07-28 19:21:41 +00:00
|
|
|
bool parser_compile(const char *filename)
|
|
|
|
{
|
|
|
|
parser->lex = lex_open(filename);
|
2012-07-16 12:14:37 +00:00
|
|
|
if (!parser->lex) {
|
|
|
|
printf("failed to open file \"%s\"\n", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
/* initial lexer/parser state */
|
|
|
|
parser->lex->flags.noops = true;
|
|
|
|
|
|
|
|
if (parser_next(parser))
|
2012-07-16 12:14:37 +00:00
|
|
|
{
|
2012-07-16 12:52:52 +00:00
|
|
|
while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
|
|
|
|
{
|
|
|
|
if (!parser_do(parser)) {
|
2012-07-19 09:22:09 +00:00
|
|
|
if (parser->tok == TOKEN_EOF)
|
2012-07-19 10:17:00 +00:00
|
|
|
parseerror(parser, "unexpected eof");
|
|
|
|
else
|
|
|
|
parseerror(parser, "parse error\n");
|
2012-07-16 12:52:52 +00:00
|
|
|
lex_close(parser->lex);
|
|
|
|
mem_d(parser);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-16 12:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lex_close(parser->lex);
|
2012-07-19 09:25:53 +00:00
|
|
|
|
2012-07-28 19:21:41 +00:00
|
|
|
return !parser->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser_cleanup()
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < parser->functions_count; ++i) {
|
|
|
|
ast_delete(parser->functions[i]);
|
|
|
|
}
|
|
|
|
for (i = 0; i < parser->imm_vector_count; ++i) {
|
|
|
|
ast_delete(parser->imm_vector[i]);
|
|
|
|
}
|
|
|
|
for (i = 0; i < parser->imm_string_count; ++i) {
|
|
|
|
ast_delete(parser->imm_string[i]);
|
|
|
|
}
|
|
|
|
for (i = 0; i < parser->imm_float_count; ++i) {
|
|
|
|
ast_delete(parser->imm_float[i]);
|
|
|
|
}
|
|
|
|
for (i = 0; i < parser->globals_count; ++i) {
|
2012-08-10 20:59:47 +00:00
|
|
|
ast_delete(parser->globals[i].var);
|
|
|
|
mem_d(parser->globals[i].name);
|
2012-07-28 19:21:41 +00:00
|
|
|
}
|
|
|
|
MEM_VECTOR_CLEAR(parser, globals);
|
|
|
|
|
|
|
|
mem_d(parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parser_finish(const char *output)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
ir_builder *ir;
|
|
|
|
|
2012-07-28 14:06:44 +00:00
|
|
|
if (!parser->errors)
|
|
|
|
{
|
|
|
|
ir = ir_builder_new("gmqcc_out");
|
|
|
|
if (!ir) {
|
|
|
|
printf("failed to allocate builder\n");
|
2012-07-28 19:21:41 +00:00
|
|
|
return false;
|
2012-07-20 12:34:45 +00:00
|
|
|
}
|
2012-07-28 14:06:44 +00:00
|
|
|
|
|
|
|
for (i = 0; i < parser->imm_float_count; ++i) {
|
|
|
|
if (!ast_global_codegen(parser->imm_float[i], ir)) {
|
|
|
|
printf("failed to generate global %s\n", parser->imm_float[i]->name);
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
2012-07-28 14:06:44 +00:00
|
|
|
}
|
2012-07-27 14:10:43 +00:00
|
|
|
}
|
2012-07-28 14:06:44 +00:00
|
|
|
for (i = 0; i < parser->imm_string_count; ++i) {
|
|
|
|
if (!ast_global_codegen(parser->imm_string[i], ir)) {
|
|
|
|
printf("failed to generate global %s\n", parser->imm_string[i]->name);
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
2012-07-28 14:06:44 +00:00
|
|
|
}
|
2012-07-27 14:20:53 +00:00
|
|
|
}
|
2012-07-28 14:06:44 +00:00
|
|
|
for (i = 0; i < parser->imm_vector_count; ++i) {
|
|
|
|
if (!ast_global_codegen(parser->imm_vector[i], ir)) {
|
|
|
|
printf("failed to generate global %s\n", parser->imm_vector[i]->name);
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
2012-07-28 14:06:44 +00:00
|
|
|
}
|
2012-07-19 09:25:53 +00:00
|
|
|
}
|
2012-08-11 13:40:03 +00:00
|
|
|
for (i = 0; i < parser->fields_count; ++i) {
|
|
|
|
ast_value *field;
|
|
|
|
bool isconst;
|
|
|
|
if (!ast_istype(parser->fields[i].var, ast_value))
|
|
|
|
continue;
|
|
|
|
field = (ast_value*)parser->fields[i].var;
|
|
|
|
isconst = field->isconst;
|
|
|
|
field->isconst = false;
|
|
|
|
if (!ast_global_codegen((ast_value*)field, ir)) {
|
|
|
|
printf("failed to generate field %s\n", field->name);
|
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isconst) {
|
|
|
|
ir_value *ifld;
|
|
|
|
ast_expression *subtype;
|
|
|
|
field->isconst = true;
|
|
|
|
subtype = field->expression.next;
|
|
|
|
ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
|
|
|
|
if (subtype->expression.vtype == TYPE_FIELD)
|
|
|
|
ifld->fieldtype = subtype->expression.next->expression.vtype;
|
|
|
|
else if (subtype->expression.vtype == TYPE_FUNCTION)
|
|
|
|
ifld->outtype = subtype->expression.next->expression.vtype;
|
|
|
|
(void)!ir_value_set_field(field->ir_v, ifld);
|
|
|
|
}
|
|
|
|
}
|
2012-07-28 14:06:44 +00:00
|
|
|
for (i = 0; i < parser->globals_count; ++i) {
|
2012-08-10 20:59:47 +00:00
|
|
|
if (!ast_istype(parser->globals[i].var, ast_value))
|
|
|
|
continue;
|
|
|
|
if (!ast_global_codegen((ast_value*)(parser->globals[i].var), ir)) {
|
|
|
|
printf("failed to generate global %s\n", parser->globals[i].name);
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
2012-07-28 14:06:44 +00:00
|
|
|
}
|
2012-07-19 15:53:45 +00:00
|
|
|
}
|
2012-07-28 14:06:44 +00:00
|
|
|
for (i = 0; i < parser->functions_count; ++i) {
|
|
|
|
if (!ast_function_codegen(parser->functions[i], ir)) {
|
|
|
|
printf("failed to generate function %s\n", parser->functions[i]->name);
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
2012-07-28 14:06:44 +00:00
|
|
|
}
|
|
|
|
if (!ir_function_finalize(parser->functions[i]->ir_func)) {
|
|
|
|
printf("failed to finalize function %s\n", parser->functions[i]->name);
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
2012-07-28 14:06:44 +00:00
|
|
|
}
|
2012-07-19 15:53:45 +00:00
|
|
|
}
|
2012-07-19 09:25:53 +00:00
|
|
|
|
2012-07-28 14:06:44 +00:00
|
|
|
ir_builder_dump(ir, printf);
|
2012-07-19 09:25:53 +00:00
|
|
|
|
2012-07-28 19:21:41 +00:00
|
|
|
if (!ir_builder_generate(ir, output)) {
|
2012-07-28 14:06:44 +00:00
|
|
|
printf("*** failed to generate output file\n");
|
2012-07-28 19:21:41 +00:00
|
|
|
ir_builder_delete(ir);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-27 16:22:39 +00:00
|
|
|
|
2012-07-28 14:06:44 +00:00
|
|
|
ir_builder_delete(ir);
|
2012-07-28 19:21:41 +00:00
|
|
|
return true;
|
2012-07-16 12:52:52 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 19:21:41 +00:00
|
|
|
printf("*** there were compile errors\n");
|
|
|
|
return false;
|
2012-07-16 11:59:10 +00:00
|
|
|
}
|