16 MEM_VECTOR_MAKE(varentry_t, globals);
17 MEM_VECTOR_MAKE(varentry_t, fields);
18 MEM_VECTOR_MAKE(ast_function*, functions);
19 MEM_VECTOR_MAKE(ast_value*, imm_float);
20 MEM_VECTOR_MAKE(ast_value*, imm_string);
21 MEM_VECTOR_MAKE(ast_value*, imm_vector);
23 ast_function *function;
24 MEM_VECTOR_MAKE(varentry_t, locals);
29 /* TYPE_FIELD -> parser_find_fields is used instead of find_var
30 * TODO: TYPE_VECTOR -> x, y and z are accepted in the gmqcc standard
31 * anything else: type error
36 MEM_VEC_FUNCTIONS(parser_t, varentry_t, globals)
37 MEM_VEC_FUNCTIONS(parser_t, varentry_t, fields)
38 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_float)
39 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_string)
40 MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_vector)
41 MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals)
42 MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
44 static void parser_pop_local(parser_t *parser);
45 static bool parser_variable(parser_t *parser, ast_block *localblock);
46 static ast_block* parser_parse_block(parser_t *parser);
47 static ast_expression* parser_parse_statement_or_block(parser_t *parser);
48 static ast_expression* parser_expression_leave(parser_t *parser);
49 static ast_expression* parser_expression(parser_t *parser);
51 void parseerror(parser_t *parser, const char *fmt, ...)
58 vprintmsg(LVL_ERROR, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "parse error", fmt, ap);
64 /* returns true if it counts as an error */
65 bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
68 int lvl = LVL_WARNING;
70 if (!OPTS_WARN(warntype))
73 if (OPTS_WARN(WARN_ERROR)) {
79 vprintmsg(lvl, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "warning", fmt, ap);
82 return OPTS_WARN(WARN_ERROR);
85 /**********************************************************************
86 * some maths used for constant folding
89 vector vec3_add(vector a, vector b)
98 vector vec3_sub(vector a, vector b)
107 qcfloat vec3_mulvv(vector a, vector b)
109 return (a.x * b.x + a.y * b.y + a.z * b.z);
112 vector vec3_mulvf(vector a, float b)
121 /**********************************************************************
125 bool parser_next(parser_t *parser)
127 /* lex_do kills the previous token */
128 parser->tok = lex_do(parser->lex);
129 if (parser->tok == TOKEN_EOF || parser->tok >= TOKEN_ERROR)
134 /* lift a token out of the parser so it's not destroyed by parser_next */
135 token *parser_lift(parser_t *parser)
137 token *tok = parser->lex->tok;
138 parser->lex->tok = NULL;
142 #define parser_tokval(p) (p->lex->tok->value)
143 #define parser_token(p) (p->lex->tok)
144 #define parser_ctx(p) (p->lex->tok->ctx)
146 ast_value* parser_const_float(parser_t *parser, double d)
150 for (i = 0; i < parser->imm_float_count; ++i) {
151 if (parser->imm_float[i]->constval.vfloat == d)
152 return parser->imm_float[i];
154 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_FLOAT);
156 out->constval.vfloat = d;
157 if (!parser_t_imm_float_add(parser, out)) {
158 ast_value_delete(out);
164 ast_value* parser_const_string(parser_t *parser, const char *str)
168 for (i = 0; i < parser->imm_string_count; ++i) {
169 if (!strcmp(parser->imm_string[i]->constval.vstring, str))
170 return parser->imm_string[i];
172 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
174 out->constval.vstring = util_strdup(str);
175 if (!parser_t_imm_string_add(parser, out)) {
176 ast_value_delete(out);
182 ast_value* parser_const_vector(parser_t *parser, vector v)
186 for (i = 0; i < parser->imm_vector_count; ++i) {
187 if (!memcmp(&parser->imm_vector[i]->constval.vvec, &v, sizeof(v)))
188 return parser->imm_vector[i];
190 out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
192 out->constval.vvec = v;
193 if (!parser_t_imm_vector_add(parser, out)) {
194 ast_value_delete(out);
200 ast_expression* parser_find_field(parser_t *parser, const char *name)
203 for (i = 0; i < parser->fields_count; ++i) {
204 if (!strcmp(parser->fields[i].name, name))
205 return parser->fields[i].var;
210 ast_expression* parser_find_global(parser_t *parser, const char *name)
213 for (i = 0; i < parser->globals_count; ++i) {
214 if (!strcmp(parser->globals[i].name, name))
215 return parser->globals[i].var;
220 ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto)
224 for (i = parser->locals_count; i > upto;) {
226 if (!strcmp(parser->locals[i].name, name))
227 return parser->locals[i].var;
229 fun = parser->function->vtype;
230 for (i = 0; i < fun->expression.params_count; ++i) {
231 if (!strcmp(fun->expression.params[i]->name, name))
232 return (ast_expression*)(fun->expression.params[i]);
237 ast_expression* parser_find_var(parser_t *parser, const char *name)
240 v = parser_find_local(parser, name, 0);
241 if (!v) v = parser_find_global(parser, name);
246 MEM_VECTOR_MAKE(ast_value*, p);
248 MEM_VEC_FUNCTIONS(paramlist_t, ast_value*, p)
250 static ast_value *parser_parse_type(parser_t *parser, int basetype, bool *isfunc)
254 lex_ctx ctx = parser_ctx(parser);
255 int vtype = basetype;
259 MEM_VECTOR_INIT(¶ms, p);
263 if (parser->tok == '(') {
269 if (!parser_next(parser))
272 if (parser->tok == ')')
275 temptype = parser_token(parser)->constval.t;
276 if (!parser_next(parser))
279 param = parser_parse_type(parser, temptype, &dummy);
285 if (parser->tok == TOKEN_IDENT) {
286 /* named parameter */
287 if (!ast_value_set_name(param, parser_tokval(parser)))
289 if (!parser_next(parser))
293 if (!paramlist_t_p_add(¶ms, param)) {
294 parseerror(parser, "Out of memory while parsing typename");
298 if (parser->tok == ',')
300 if (parser->tok == ')')
302 parseerror(parser, "Unexpected token");
305 if (!parser_next(parser))
309 var = ast_value_new(ctx, "<unnamed>", vtype);
312 MEM_VECTOR_MOVE(¶ms, p, &var->expression, params);
315 for (i = 0; i < params.p_count; ++i)
316 ast_value_delete(params.p[i]);
317 MEM_VECTOR_CLEAR(¶ms, p);
323 size_t etype; /* 0 = expression, others are operators */
327 ast_block *block; /* for commas and function calls */
332 MEM_VECTOR_MAKE(sy_elem, out);
333 MEM_VECTOR_MAKE(sy_elem, ops);
335 MEM_VEC_FUNCTIONS(shunt, sy_elem, out)
336 MEM_VEC_FUNCTIONS(shunt, sy_elem, ops)
338 static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
348 static sy_elem syblock(lex_ctx ctx, ast_block *v) {
351 e.out = (ast_expression*)v;
358 static sy_elem syop(lex_ctx ctx, const oper_info *op) {
360 e.etype = 1 + (op - operators);
368 static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
380 # define DEBUGSHUNTDO(x) x
382 # define DEBUGSHUNTDO(x)
385 static bool parser_sy_pop(parser_t *parser, shunt *sy)
389 ast_expression *out = NULL;
390 ast_expression *exprs[3];
391 ast_block *blocks[3];
392 ast_value *asvalue[3];
394 qcint generated_op = 0;
396 if (!sy->ops_count) {
397 parseerror(parser, "internal error: missing operator");
401 if (sy->ops[sy->ops_count-1].paren) {
402 parseerror(parser, "unmatched parenthesis");
406 op = &operators[sy->ops[sy->ops_count-1].etype - 1];
407 ctx = sy->ops[sy->ops_count-1].ctx;
409 DEBUGSHUNTDO(printf("apply %s\n", op->op));
411 if (sy->out_count < op->operands) {
412 parseerror(parser, "internal error: not enough operands: %i", sy->out_count);
418 sy->out_count -= op->operands;
419 for (i = 0; i < op->operands; ++i) {
420 exprs[i] = sy->out[sy->out_count+i].out;
421 blocks[i] = sy->out[sy->out_count+i].block;
422 asvalue[i] = (ast_value*)exprs[i];
425 if (blocks[0] && !blocks[0]->exprs_count && op->id != opid1(',')) {
426 parseerror(parser, "internal error: operator cannot be applied on empty blocks");
430 #define NotSameType(T) \
431 (exprs[0]->expression.vtype != exprs[1]->expression.vtype || \
432 exprs[0]->expression.vtype != T)
433 #define CanConstFold(A, B) \
434 (ast_istype((A), ast_value) && ast_istype((B), ast_value) && \
435 ((ast_value*)(A))->isconst && ((ast_value*)(B))->isconst)
436 #define ConstV(i) (asvalue[(i)]->constval.vvec)
437 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
441 parseerror(parser, "internal error: unhandled operand");
445 if (exprs[0]->expression.vtype == TYPE_ENTITY) {
446 if (exprs[1]->expression.vtype != TYPE_FIELD) {
447 parseerror(parser, "type error: right hand of member-operand should be an entity-field");
450 out = (ast_expression*)ast_entfield_new(ctx, exprs[0], exprs[1]);
452 else if (exprs[0]->expression.vtype == TYPE_VECTOR) {
453 parseerror(parser, "internal error: vector access is not supposed to be handled at this point");
457 parseerror(parser, "type error: member-of operator on something that is not an entity or vector");
464 if (!ast_block_exprs_add(blocks[0], exprs[1]))
467 blocks[0] = ast_block_new(ctx);
468 if (!ast_block_exprs_add(blocks[0], exprs[0]) ||
469 !ast_block_exprs_add(blocks[0], exprs[1]))
474 if (!ast_block_set_type(blocks[0], exprs[1]))
477 sy->out[sy->out_count++] = syblock(ctx, blocks[0]);
481 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
482 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
484 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
485 type_name[exprs[0]->expression.vtype],
486 type_name[exprs[1]->expression.vtype]);
489 switch (exprs[0]->expression.vtype) {
491 if (CanConstFold(exprs[0], exprs[1]))
493 out = (ast_expression*)parser_const_float(parser, ConstF(0) + ConstF(1));
496 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
499 if (CanConstFold(exprs[0], exprs[1]))
500 out = (ast_expression*)parser_const_vector(parser, vec3_add(ConstV(0), ConstV(1)));
502 out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
505 parseerror(parser, "invalid types used in expression: cannot add type %s and %s",
506 type_name[exprs[0]->expression.vtype],
507 type_name[exprs[1]->expression.vtype]);
512 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
513 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
515 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
516 type_name[exprs[1]->expression.vtype],
517 type_name[exprs[0]->expression.vtype]);
520 switch (exprs[0]->expression.vtype) {
522 if (CanConstFold(exprs[0], exprs[1]))
523 out = (ast_expression*)parser_const_float(parser, ConstF(0) - ConstF(1));
525 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
528 if (CanConstFold(exprs[0], exprs[1]))
529 out = (ast_expression*)parser_const_vector(parser, vec3_sub(ConstV(0), ConstV(1)));
531 out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
534 parseerror(parser, "invalid types used in expression: cannot subtract type %s from %s",
535 type_name[exprs[1]->expression.vtype],
536 type_name[exprs[0]->expression.vtype]);
541 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype &&
542 exprs[0]->expression.vtype != TYPE_VECTOR &&
543 exprs[0]->expression.vtype != TYPE_FLOAT &&
544 exprs[1]->expression.vtype != TYPE_VECTOR &&
545 exprs[1]->expression.vtype != TYPE_FLOAT)
547 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
548 type_name[exprs[1]->expression.vtype],
549 type_name[exprs[0]->expression.vtype]);
552 switch (exprs[0]->expression.vtype) {
554 if (exprs[1]->expression.vtype == TYPE_VECTOR)
556 if (CanConstFold(exprs[0], exprs[1]))
557 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(1), ConstF(0)));
559 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
563 if (CanConstFold(exprs[0], exprs[1]))
564 out = (ast_expression*)parser_const_float(parser, ConstF(0) * ConstF(1));
566 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
570 if (exprs[1]->expression.vtype == TYPE_FLOAT)
572 if (CanConstFold(exprs[0], exprs[1]))
573 out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), ConstF(1)));
575 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
579 if (CanConstFold(exprs[0], exprs[1]))
580 out = (ast_expression*)parser_const_float(parser, vec3_mulvv(ConstV(0), ConstV(1)));
582 out = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
586 parseerror(parser, "invalid types used in expression: cannot multiply types %s and %s",
587 type_name[exprs[1]->expression.vtype],
588 type_name[exprs[0]->expression.vtype]);
593 if (NotSameType(TYPE_FLOAT)) {
594 parseerror(parser, "invalid types used in expression: cannot divide types %s and %s",
595 type_name[exprs[0]->expression.vtype],
596 type_name[exprs[1]->expression.vtype]);
599 if (CanConstFold(exprs[0], exprs[1]))
600 out = (ast_expression*)parser_const_float(parser, ConstF(0) / ConstF(1));
602 out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
606 parseerror(parser, "qc does not have a modulo operator");
610 if (NotSameType(TYPE_FLOAT)) {
611 parseerror(parser, "invalid types used in expression: cannot perform bit operations between types %s and %s",
612 type_name[exprs[0]->expression.vtype],
613 type_name[exprs[1]->expression.vtype]);
616 if (CanConstFold(exprs[0], exprs[1]))
617 out = (ast_expression*)parser_const_float(parser,
618 (op->id == opid1('|') ? (float)( ((qcint)ConstF(0)) | ((qcint)ConstF(1)) ) :
619 (float)( ((qcint)ConstF(0)) & ((qcint)ConstF(1)) ) ));
621 out = (ast_expression*)ast_binary_new(ctx,
622 (op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
626 parseerror(parser, "TODO: bitxor");
631 case opid3('<','<','='):
632 case opid3('>','>','='):
633 parseerror(parser, "TODO: shifts");
637 generated_op += 1; /* INSTR_OR */
639 generated_op += INSTR_AND;
640 if (NotSameType(TYPE_FLOAT)) {
641 parseerror(parser, "invalid types used in expression: cannot perform logical operations between types %s and %s",
642 type_name[exprs[0]->expression.vtype],
643 type_name[exprs[1]->expression.vtype]);
644 parseerror(parser, "TODO: logical ops for arbitrary types using INSTR_NOT");
645 parseerror(parser, "TODO: optional early out");
648 if (opts_standard == COMPILER_GMQCC)
649 printf("TODO: early out logic\n");
650 if (CanConstFold(exprs[0], exprs[1]))
651 out = (ast_expression*)parser_const_float(parser,
652 (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1))));
654 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
658 generated_op += 1; /* INSTR_GT */
660 generated_op += 1; /* INSTR_LT */
661 case opid2('>', '='):
662 generated_op += 1; /* INSTR_GE */
663 case opid2('<', '='):
664 generated_op += INSTR_LE;
665 if (NotSameType(TYPE_FLOAT)) {
666 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
667 type_name[exprs[0]->expression.vtype],
668 type_name[exprs[1]->expression.vtype]);
671 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
673 case opid2('!', '='):
674 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
675 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
676 type_name[exprs[0]->expression.vtype],
677 type_name[exprs[1]->expression.vtype]);
680 out = (ast_expression*)ast_binary_new(ctx, type_ne_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
682 case opid2('=', '='):
683 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
684 parseerror(parser, "invalid types used in expression: cannot perform comparison between types %s and %s",
685 type_name[exprs[0]->expression.vtype],
686 type_name[exprs[1]->expression.vtype]);
689 out = (ast_expression*)ast_binary_new(ctx, type_eq_instr[exprs[0]->expression.vtype], exprs[0], exprs[1]);
693 if (ast_istype(exprs[0], ast_entfield))
694 assignop = type_storep_instr[exprs[0]->expression.vtype];
696 assignop = type_store_instr[exprs[0]->expression.vtype];
697 out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
701 if (exprs[0]->expression.vtype != exprs[1]->expression.vtype ||
702 (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) )
704 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
705 type_name[exprs[0]->expression.vtype],
706 type_name[exprs[1]->expression.vtype]);
709 if (ast_istype(exprs[0], ast_entfield))
710 assignop = type_storep_instr[exprs[0]->expression.vtype];
712 assignop = type_store_instr[exprs[0]->expression.vtype];
713 switch (exprs[0]->expression.vtype) {
715 out = (ast_expression*)ast_binstore_new(ctx, assignop,
716 (op->id == opid2('+','=') ? INSTR_ADD_F : INSTR_SUB_F),
720 out = (ast_expression*)ast_binstore_new(ctx, assignop,
721 (op->id == opid2('+','=') ? INSTR_ADD_V : INSTR_SUB_V),
725 parseerror(parser, "invalid types used in expression: cannot add or subtract type %s and %s",
726 type_name[exprs[0]->expression.vtype],
727 type_name[exprs[1]->expression.vtype]);
735 parseerror(parser, "failed to apply operand %s", op->op);
739 DEBUGSHUNTDO(printf("applied %s\n", op->op));
740 sy->out[sy->out_count++] = syexp(ctx, out);
744 static bool parser_close_call(parser_t *parser, shunt *sy)
746 /* was a function call */
754 fid = sy->ops[sy->ops_count].off;
756 /* out[fid] is the function
757 * everything above is parameters...
759 * 1 params = ast_expression
763 if (sy->out_count < 1 || sy->out_count <= fid) {
764 parseerror(parser, "internal error: function call needs function and parameter list...");
768 fun = sy->out[fid].out;
770 call = ast_call_new(sy->ops[sy->ops_count].ctx, fun);
772 parseerror(parser, "out of memory");
776 if (fid+1 == sy->out_count) {
779 } else if (fid+2 == sy->out_count) {
782 params = sy->out[sy->out_count].block;
786 if (!ast_call_params_add(call, sy->out[sy->out_count].out)) {
787 ast_delete(sy->out[sy->out_count].out);
788 parseerror(parser, "out of memory");
792 paramcount = params->exprs_count;
793 MEM_VECTOR_MOVE(params, exprs, call, params);
797 parseerror(parser, "invalid function call");
801 /* overwrite fid, the function, with a call */
802 sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
804 if (fun->expression.vtype != TYPE_FUNCTION) {
805 parseerror(parser, "not a function");
809 if (!fun->expression.next) {
810 parseerror(parser, "could not determine function return type");
813 if (fun->expression.params_count != paramcount) {
814 parseerror(parser, "expected %i parameters, got %i", (int)fun->expression.params_count, paramcount);
822 static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
824 if (!sy->ops_count) {
825 parseerror(parser, "unmatched closing paren");
828 if (sy->ops[sy->ops_count-1].paren == 1) {
829 parseerror(parser, "empty parenthesis expression");
832 while (sy->ops_count) {
833 if (sy->ops[sy->ops_count-1].paren == 'f') {
834 if (!parser_close_call(parser, sy))
838 if (sy->ops[sy->ops_count-1].paren == 1) {
840 return !functions_only;
842 if (!parser_sy_pop(parser, sy))
848 static ast_expression* parser_expression_leave(parser_t *parser)
850 ast_expression *expr = NULL;
853 bool gotmemberof = false;
855 /* count the parens because an if starts with one, so the
856 * end of a condition is an unmatched closing paren
860 MEM_VECTOR_INIT(&sy, out);
861 MEM_VECTOR_INIT(&sy, ops);
868 parser->memberof = 0;
871 bool nextwant = true;
872 if (parser->tok == TOKEN_IDENT)
876 if (opts_standard == COMPILER_GMQCC)
878 if (parser->memberof == TYPE_ENTITY)
879 var = parser_find_field(parser, parser_tokval(parser));
880 else if (parser->memberof == TYPE_VECTOR)
882 parseerror(parser, "TODO: implement effective vector member access");
885 else if (parser->memberof) {
886 parseerror(parser, "namespace for member not found");
890 var = parser_find_var(parser, parser_tokval(parser));
892 var = parser_find_var(parser, parser_tokval(parser));
894 var = parser_find_field(parser, parser_tokval(parser));
897 parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
900 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), var))) {
901 parseerror(parser, "out of memory");
904 DEBUGSHUNTDO(printf("push %s\n", parser_tokval(parser)));
906 else if (parser->tok == TOKEN_FLOATCONST) {
907 ast_value *val = parser_const_float(parser, (parser_token(parser)->constval.f));
910 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
911 parseerror(parser, "out of memory");
914 DEBUGSHUNTDO(printf("push %g\n", parser_token(parser)->constval.f));
916 else if (parser->tok == TOKEN_INTCONST) {
917 ast_value *val = parser_const_float(parser, (double)(parser_token(parser)->constval.i));
920 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
921 parseerror(parser, "out of memory");
924 DEBUGSHUNTDO(printf("push %i\n", parser_token(parser)->constval.i));
926 else if (parser->tok == TOKEN_STRINGCONST) {
927 ast_value *val = parser_const_string(parser, parser_tokval(parser));
930 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
931 parseerror(parser, "out of memory");
934 DEBUGSHUNTDO(printf("push string\n"));
936 else if (parser->tok == TOKEN_VECTORCONST) {
937 ast_value *val = parser_const_vector(parser, parser_token(parser)->constval.v);
940 if (!shunt_out_add(&sy, syexp(parser_ctx(parser), (ast_expression*)val))) {
941 parseerror(parser, "out of memory");
944 DEBUGSHUNTDO(printf("push '%g %g %g'\n",
945 parser_token(parser)->constval.v.x,
946 parser_token(parser)->constval.v.y,
947 parser_token(parser)->constval.v.z));
949 else if (parser->tok == '(') {
951 nextwant = false; /* not expecting an operator next */
952 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 1, 0))) {
953 parseerror(parser, "out of memory");
956 DEBUGSHUNTDO(printf("push (\n"));
958 else if (parser->tok == ')') {
959 DEBUGSHUNTDO(printf("do[nop] )\n"));
963 /* allowed for function calls */
964 if (!parser_close_paren(parser, &sy, true))
968 /* TODO: prefix operators */
969 parseerror(parser, "expected statement");
973 parser->lex->flags.noops = !wantop;
975 bool nextwant = false;
976 if (parser->tok == '(') {
977 DEBUGSHUNTDO(printf("push (\n"));
979 /* we expected an operator, this is the function-call operator */
980 if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 'f', sy.out_count-1))) {
981 parseerror(parser, "out of memory");
985 else if (parser->tok == ')') {
986 DEBUGSHUNTDO(printf("do[op] )\n"));
990 /* we do expect an operator next */
991 /* closing an opening paren */
992 if (!parser_close_paren(parser, &sy, false))
996 else if (parser->tok != TOKEN_OPERATOR) {
997 parseerror(parser, "expected operator or end of statement");
1001 /* classify the operator */
1002 /* TODO: suffix operators */
1003 const oper_info *op;
1004 const oper_info *olast = NULL;
1006 for (o = 0; o < operator_count; ++o) {
1007 if (!(operators[o].flags & OP_PREFIX) &&
1008 !(operators[o].flags & OP_SUFFIX) && /* remove this */
1009 !strcmp(parser_tokval(parser), operators[o].op))
1014 if (o == operator_count) {
1015 /* no operator found... must be the end of the statement */
1018 /* found an operator */
1020 if (op->id == opid1('.')) {
1021 /* for gmqcc standard: open up the namespace of the previous type */
1022 ast_expression *prevex = sy.out[sy.out_count-1].out;
1024 parseerror(parser, "unexpected member operator");
1027 if (prevex->expression.vtype == TYPE_ENTITY)
1028 parser->memberof = TYPE_ENTITY;
1029 else if (prevex->expression.vtype == TYPE_VECTOR)
1030 parser->memberof = TYPE_VECTOR;
1032 parseerror(parser, "type error: type has no members");
1038 if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
1039 olast = &operators[sy.ops[sy.ops_count-1].etype-1];
1042 (op->prec < olast->prec) ||
1043 (op->assoc == ASSOC_LEFT && op->prec <= olast->prec) ) )
1045 if (!parser_sy_pop(parser, &sy))
1047 if (sy.ops_count && !sy.ops[sy.ops_count-1].paren)
1048 olast = &operators[sy.ops[sy.ops_count-1].etype-1];
1053 DEBUGSHUNTDO(printf("push operator %s\n", op->op));
1054 if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
1058 parser->lex->flags.noops = !wantop;
1060 if (!parser_next(parser)) {
1063 if (parser->tok == ';') {
1068 while (sy.ops_count) {
1069 if (!parser_sy_pop(parser, &sy))
1073 parser->lex->flags.noops = true;
1074 if (!sy.out_count) {
1075 parseerror(parser, "empty expression");
1078 expr = sy.out[0].out;
1079 MEM_VECTOR_CLEAR(&sy, out);
1080 MEM_VECTOR_CLEAR(&sy, ops);
1081 DEBUGSHUNTDO(printf("shut done\n"));
1085 parser->lex->flags.noops = true;
1086 MEM_VECTOR_CLEAR(&sy, out);
1087 MEM_VECTOR_CLEAR(&sy, ops);
1091 static ast_expression* parser_expression(parser_t *parser)
1093 ast_expression *e = parser_expression_leave(parser);
1096 if (!parser_next(parser)) {
1103 static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression **out)
1106 ast_expression *cond, *ontrue, *onfalse = NULL;
1108 lex_ctx ctx = parser_ctx(parser);
1110 /* skip the 'if' and check for opening paren */
1111 if (!parser_next(parser) || parser->tok != '(') {
1112 parseerror(parser, "expected 'if' condition in parenthesis");
1115 /* parse into the expression */
1116 if (!parser_next(parser)) {
1117 parseerror(parser, "expected 'if' condition after opening paren");
1120 /* parse the condition */
1121 cond = parser_expression_leave(parser);
1125 if (parser->tok != ')') {
1126 parseerror(parser, "expected closing paren after 'if' condition");
1130 /* parse into the 'then' branch */
1131 if (!parser_next(parser)) {
1132 parseerror(parser, "expected statement for on-true branch of 'if'");
1136 ontrue = parser_parse_statement_or_block(parser);
1141 /* check for an else */
1142 if (!strcmp(parser_tokval(parser), "else")) {
1143 /* parse into the 'else' branch */
1144 if (!parser_next(parser)) {
1145 parseerror(parser, "expected on-false branch after 'else'");
1150 onfalse = parser_parse_statement_or_block(parser);
1158 ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse);
1159 *out = (ast_expression*)ifthen;
1163 static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expression **out)
1166 ast_expression *cond, *ontrue;
1168 lex_ctx ctx = parser_ctx(parser);
1170 /* skip the 'while' and check for opening paren */
1171 if (!parser_next(parser) || parser->tok != '(') {
1172 parseerror(parser, "expected 'while' condition in parenthesis");
1175 /* parse into the expression */
1176 if (!parser_next(parser)) {
1177 parseerror(parser, "expected 'while' condition after opening paren");
1180 /* parse the condition */
1181 cond = parser_expression_leave(parser);
1185 if (parser->tok != ')') {
1186 parseerror(parser, "expected closing paren after 'while' condition");
1190 /* parse into the 'then' branch */
1191 if (!parser_next(parser)) {
1192 parseerror(parser, "expected while-loop body");
1196 ontrue = parser_parse_statement_or_block(parser);
1202 aloop = ast_loop_new(ctx, NULL, cond, NULL, NULL, ontrue);
1203 *out = (ast_expression*)aloop;
1207 static bool parser_parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
1210 ast_expression *cond, *ontrue;
1212 lex_ctx ctx = parser_ctx(parser);
1214 /* skip the 'do' and get the body */
1215 if (!parser_next(parser)) {
1216 parseerror(parser, "expected loop body");
1219 ontrue = parser_parse_statement_or_block(parser);
1223 /* expect the "while" */
1224 if (parser->tok != TOKEN_KEYWORD ||
1225 strcmp(parser_tokval(parser), "while"))
1227 parseerror(parser, "expected 'while' and condition");
1232 /* skip the 'while' and check for opening paren */
1233 if (!parser_next(parser) || parser->tok != '(') {
1234 parseerror(parser, "expected 'while' condition in parenthesis");
1238 /* parse into the expression */
1239 if (!parser_next(parser)) {
1240 parseerror(parser, "expected 'while' condition after opening paren");
1244 /* parse the condition */
1245 cond = parser_expression_leave(parser);
1249 if (parser->tok != ')') {
1250 parseerror(parser, "expected closing paren after 'while' condition");
1256 if (!parser_next(parser) || parser->tok != ';') {
1257 parseerror(parser, "expected semicolon after condition");
1263 if (!parser_next(parser)) {
1264 parseerror(parser, "parse error");
1270 aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
1271 *out = (ast_expression*)aloop;
1275 static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression **out)
1278 ast_expression *initexpr, *cond, *increment, *ontrue;
1279 size_t oldblocklocal;
1281 lex_ctx ctx = parser_ctx(parser);
1283 oldblocklocal = parser->blocklocal;
1284 parser->blocklocal = parser->locals_count;
1291 /* skip the 'while' and check for opening paren */
1292 if (!parser_next(parser) || parser->tok != '(') {
1293 parseerror(parser, "expected 'for' expressions in parenthesis");
1296 /* parse into the expression */
1297 if (!parser_next(parser)) {
1298 parseerror(parser, "expected 'for' initializer after opening paren");
1302 if (parser->tok == TOKEN_TYPENAME) {
1303 if (opts_standard != COMPILER_GMQCC) {
1304 if (parsewarning(parser, WARN_EXTENSIONS,
1305 "current standard does not allow variable declarations in for-loop initializers"))
1309 parseerror(parser, "TODO: assignment of new variables to be non-const");
1311 if (!parser_variable(parser, block))
1314 else if (parser->tok != ';')
1316 initexpr = parser_expression_leave(parser);
1321 /* move on to condition */
1322 if (parser->tok != ';') {
1323 parseerror(parser, "expected semicolon after for-loop initializer");
1326 if (!parser_next(parser)) {
1327 parseerror(parser, "expected for-loop condition");
1331 /* parse the condition */
1332 if (parser->tok != ';') {
1333 cond = parser_expression_leave(parser);
1338 /* move on to incrementor */
1339 if (parser->tok != ';') {
1340 parseerror(parser, "expected semicolon after for-loop initializer");
1343 if (!parser_next(parser)) {
1344 parseerror(parser, "expected for-loop condition");
1348 /* parse the incrementor */
1349 if (parser->tok != ')') {
1350 increment = parser_expression_leave(parser);
1356 if (parser->tok != ')') {
1357 parseerror(parser, "expected closing paren after 'for-loop' incrementor");
1360 /* parse into the 'then' branch */
1361 if (!parser_next(parser)) {
1362 parseerror(parser, "expected for-loop body");
1365 ontrue = parser_parse_statement_or_block(parser);
1370 aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
1371 *out = (ast_expression*)aloop;
1373 while (parser->locals_count > parser->blocklocal)
1374 parser_pop_local(parser);
1375 parser->blocklocal = oldblocklocal;
1378 if (initexpr) ast_delete(initexpr);
1379 if (cond) ast_delete(cond);
1380 if (increment) ast_delete(increment);
1381 while (parser->locals_count > parser->blocklocal)
1382 parser_pop_local(parser);
1383 parser->blocklocal = oldblocklocal;
1387 static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
1389 if (parser->tok == TOKEN_TYPENAME)
1391 /* local variable */
1393 parseerror(parser, "cannot declare a variable from here");
1396 if (opts_standard == COMPILER_QCC) {
1397 if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
1400 if (!parser_variable(parser, block))
1405 else if (parser->tok == TOKEN_KEYWORD)
1407 if (!strcmp(parser_tokval(parser), "local"))
1410 parseerror(parser, "cannot declare a local variable here");
1413 if (!parser_next(parser)) {
1414 parseerror(parser, "expected variable declaration");
1417 if (!parser_variable(parser, block))
1422 else if (!strcmp(parser_tokval(parser), "return"))
1424 ast_expression *exp = NULL;
1425 ast_return *ret = NULL;
1426 ast_value *expected = parser->function->vtype;
1428 if (!parser_next(parser)) {
1429 parseerror(parser, "expected return expression");
1433 if (parser->tok != ';') {
1434 exp = parser_expression(parser);
1438 if (exp->expression.vtype != expected->expression.next->expression.vtype) {
1439 parseerror(parser, "return with invalid expression");
1442 ret = ast_return_new(exp->expression.node.context, exp);
1448 *out = (ast_expression*)ret;
1449 } else if (!parser_next(parser)) {
1450 parseerror(parser, "expected semicolon");
1451 if (expected->expression.next->expression.vtype != TYPE_VOID) {
1452 parseerror(parser, "return without value");
1457 else if (!strcmp(parser_tokval(parser), "if"))
1459 return parser_parse_if(parser, block, out);
1461 else if (!strcmp(parser_tokval(parser), "while"))
1463 return parser_parse_while(parser, block, out);
1465 else if (!strcmp(parser_tokval(parser), "do"))
1467 return parser_parse_dowhile(parser, block, out);
1469 else if (!strcmp(parser_tokval(parser), "for"))
1471 if (opts_standard == COMPILER_QCC) {
1472 if (parsewarning(parser, WARN_EXTENSIONS, "for loops are not recognized in the original Quake C standard, to enable try an alternate standard --std=?"))
1475 return parser_parse_for(parser, block, out);
1477 parseerror(parser, "Unexpected keyword");
1480 else if (parser->tok == '{')
1483 inner = parser_parse_block(parser);
1486 *out = (ast_expression*)inner;
1491 ast_expression *exp = parser_expression(parser);
1499 static void parser_pop_local(parser_t *parser)
1501 parser->locals_count--;
1502 mem_d(parser->locals[parser->locals_count].name);
1505 static ast_block* parser_parse_block(parser_t *parser)
1507 size_t oldblocklocal;
1508 ast_block *block = NULL;
1510 oldblocklocal = parser->blocklocal;
1511 parser->blocklocal = parser->locals_count;
1513 if (!parser_next(parser)) { /* skip the '{' */
1514 parseerror(parser, "expected function body");
1518 block = ast_block_new(parser_ctx(parser));
1520 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
1522 ast_expression *expr;
1523 if (parser->tok == '}')
1526 if (!parser_parse_statement(parser, block, &expr)) {
1527 ast_block_delete(block);
1533 if (!ast_block_exprs_add(block, expr)) {
1535 ast_block_delete(block);
1541 if (parser->tok != '}') {
1542 ast_block_delete(block);
1545 (void)parser_next(parser);
1549 while (parser->locals_count > parser->blocklocal)
1550 parser_pop_local(parser);
1551 parser->blocklocal = oldblocklocal;
1552 /* unroll the local vector */
1556 static ast_expression* parser_parse_statement_or_block(parser_t *parser)
1558 ast_expression *expr;
1559 if (parser->tok == '{')
1560 return (ast_expression*)parser_parse_block(parser);
1561 if (!parser_parse_statement(parser, NULL, &expr))
1566 static bool parser_variable(parser_t *parser, ast_block *localblock)
1568 bool isfunc = false;
1569 ast_function *func = NULL;
1573 ast_expression *olddecl;
1575 int basetype = parser_token(parser)->constval.t;
1579 if (!parser_next(parser)) { /* skip basetype or comma */
1580 parseerror(parser, "expected variable declaration");
1587 ctx = parser_ctx(parser);
1588 var = parser_parse_type(parser, basetype, &isfunc);
1593 if (parser->tok != TOKEN_IDENT) {
1594 parseerror(parser, "expected variable name\n");
1599 if (!localblock && (olddecl = parser_find_global(parser, parser_tokval(parser)))) {
1600 ast_value_delete(var);
1601 parseerror(parser, "global %s already declared here: %s:%i\n",
1602 parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
1606 if (localblock && parser_find_local(parser, parser_tokval(parser), parser->blocklocal)) {
1607 ast_value_delete(var);
1608 parseerror(parser, "local %s already declared here: %s:%i\n",
1609 parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
1614 if (!ast_value_set_name(var, parser_tokval(parser))) {
1615 parseerror(parser, "failed to set variable name\n");
1616 ast_value_delete(var);
1621 /* a function was defined */
1623 ast_value *proto = NULL;
1626 olddecl = parser_find_global(parser, parser_tokval(parser));
1628 olddecl = parser_find_local(parser, parser_tokval(parser), parser->blocklocal);
1631 /* we had a prototype */
1632 if (!ast_istype(olddecl, ast_value)) {
1633 /* theoretically not possible you think?
1639 parseerror(parser, "cannot declare a function with the same name as a vector's member: %s",
1640 parser_tokval(parser));
1641 ast_value_delete(var);
1645 proto = (ast_value*)olddecl;
1648 /* turn var into a value of TYPE_FUNCTION, with the old var
1651 fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
1652 func = ast_function_new(ctx, var->name, fval);
1653 if (!fval || !func) {
1654 ast_value_delete(var);
1655 if (fval) ast_value_delete(fval);
1656 if (func) ast_function_delete(func);
1660 fval->expression.next = (ast_expression*)var;
1661 MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
1663 /* we compare the type late here, but it's easier than
1664 * messing with the parameter-vector etc. earlier
1667 if (!ast_compare_type((ast_expression*)proto, (ast_expression*)fval)) {
1668 parseerror(parser, "conflicting types for `%s`, previous declaration was here: %s:%i",
1670 ast_ctx(proto).file, ast_ctx(proto).line);
1671 ast_function_delete(func);
1672 ast_value_delete(fval);
1675 ast_function_delete(func);
1676 ast_value_delete(fval);
1678 func = var->constval.vfunc;
1682 if (!parser_t_functions_add(parser, func)) {
1683 ast_function_delete(func);
1684 ast_value_delete(fval);
1692 varent.name = util_strdup(var->name);
1693 varent.var = (ast_expression*)var;
1694 if (var->expression.vtype == TYPE_VECTOR)
1696 size_t len = strlen(varent.name);
1697 varentry_t vx, vy, vz;
1698 vx.var = (ast_expression*)ast_member_new(var->expression.node.context, (ast_expression*)var, 0);
1699 vy.var = (ast_expression*)ast_member_new(var->expression.node.context, (ast_expression*)var, 1);
1700 vz.var = (ast_expression*)ast_member_new(var->expression.node.context, (ast_expression*)var, 2);
1701 vx.name = mem_a(len+3);
1702 vy.name = mem_a(len+3);
1703 vz.name = mem_a(len+3);
1704 memcpy(vx.name, varent.name, len);
1705 memcpy(vy.name, varent.name, len);
1706 memcpy(vz.name, varent.name, len);
1707 vx.name[len] = vy.name[len] = vz.name[len] = '_';
1708 vx.name[len+1] = 'x';
1709 vy.name[len+1] = 'y';
1710 vz.name[len+1] = 'z';
1711 vx.name[len+2] = vy.name[len+2] = vz.name[len+2] = 0;
1714 (void)!parser_t_globals_add(parser, varent);
1715 (void)!parser_t_globals_add(parser, vx);
1716 (void)!parser_t_globals_add(parser, vy);
1717 (void)!parser_t_globals_add(parser, vz);
1719 (void)!parser_t_locals_add(parser, varent);
1720 (void)!parser_t_locals_add(parser, vx);
1721 (void)!parser_t_locals_add(parser, vy);
1722 (void)!parser_t_locals_add(parser, vz);
1727 if ( (!localblock && !parser_t_globals_add(parser, varent)) ||
1728 ( localblock && !parser_t_locals_add(parser, varent)) )
1730 ast_value_delete(var);
1734 if (localblock && !ast_block_locals_add(localblock, var))
1736 parser_pop_local(parser);
1737 ast_value_delete(var);
1741 if (!parser_next(parser)) {
1742 ast_value_delete(var);
1746 if (parser->tok == ';') {
1747 if (!parser_next(parser))
1748 return parser->tok == TOKEN_EOF;
1752 if (parser->tok == ',') {
1757 if (parser->tok != '=') {
1758 parseerror(parser, "expected '=' or ';'");
1762 if (!parser_next(parser))
1765 if (parser->tok == '#') {
1767 parseerror(parser, "cannot declare builtins within functions");
1770 if (!isfunc || !func) {
1771 parseerror(parser, "unexpected builtin number, '%s' is not a function", var->name);
1774 if (!parser_next(parser)) {
1775 parseerror(parser, "expected builtin number");
1778 if (parser->tok != TOKEN_INTCONST) {
1779 parseerror(parser, "builtin number must be an integer constant");
1782 if (parser_token(parser)->constval.i <= 0) {
1783 parseerror(parser, "builtin number must be positive integer greater than zero");
1787 func->builtin = -parser_token(parser)->constval.i;
1788 } else if (parser->tok == '{') {
1791 ast_function *old = parser->function;
1794 parseerror(parser, "cannot declare functions within functions");
1798 parser->function = func;
1799 block = parser_parse_block(parser);
1800 parser->function = old;
1805 if (!ast_function_blocks_add(func, block)) {
1806 ast_block_delete(block);
1810 if (parser->tok == ';')
1811 return parser_next(parser) || parser->tok == TOKEN_EOF;
1812 else if (opts_standard == COMPILER_QCC)
1813 parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
1816 parseerror(parser, "TODO, const assignment");
1819 if (!parser_next(parser))
1822 if (parser->tok == ',') {
1827 if (parser->tok != ';') {
1828 parseerror(parser, "expected semicolon");
1832 (void)parser_next(parser);
1838 static bool parser_do(parser_t *parser)
1840 if (parser->tok == TOKEN_TYPENAME)
1842 return parser_variable(parser, NULL);
1844 else if (parser->tok == TOKEN_KEYWORD)
1846 /* handle 'var' and 'const' */
1849 else if (parser->tok == '.')
1853 bool isfunc = false;
1855 lex_ctx ctx = parser_ctx(parser);
1858 /* entity-member declaration */
1859 if (!parser_next(parser) || parser->tok != TOKEN_TYPENAME) {
1860 parseerror(parser, "expected member variable definition");
1864 /* remember the base/return type */
1865 basetype = parser_token(parser)->constval.t;
1867 /* parse into the declaration */
1868 if (!parser_next(parser)) {
1869 parseerror(parser, "expected field def");
1873 /* parse the field type fully */
1874 var = parser_parse_type(parser, basetype, &isfunc);
1879 /* now the field name */
1880 if (parser->tok != TOKEN_IDENT) {
1881 parseerror(parser, "expected field name");
1886 /* check for an existing field
1887 * in original qc we also have to check for an existing
1888 * global named like the field
1890 if (opts_standard == COMPILER_QCC) {
1891 if (parser_find_global(parser, parser_tokval(parser))) {
1892 parseerror(parser, "cannot declare a field and a global of the same name with -std=qcc");
1897 if (parser_find_field(parser, parser_tokval(parser))) {
1898 parseerror(parser, "field %s already exists", parser_tokval(parser));
1903 /* if it was a function, turn it into a function */
1906 /* turn var into a value of TYPE_FUNCTION, with the old var
1909 fval = ast_value_new(ctx, var->name, TYPE_FUNCTION);
1911 ast_value_delete(var);
1912 ast_value_delete(fval);
1916 fval->expression.next = (ast_expression*)var;
1917 MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
1922 /* turn it into a field */
1923 fld = ast_value_new(ctx, parser_tokval(parser), TYPE_FIELD);
1924 fld->expression.next = (ast_expression*)var;
1926 varent.var = (ast_expression*)fld;
1927 varent.name = util_strdup(fld->name);
1928 (void)!parser_t_fields_add(parser, varent);
1930 if (var->expression.vtype == TYPE_VECTOR)
1932 /* create _x, _y and _z fields as well */
1934 varentry_t vx, vy, vz;
1936 len = strlen(varent.name);
1937 vx.var = (ast_expression*)ast_member_new(ast_ctx(fld), (ast_expression*)fld, 0);
1938 vy.var = (ast_expression*)ast_member_new(ast_ctx(fld), (ast_expression*)fld, 1);
1939 vz.var = (ast_expression*)ast_member_new(ast_ctx(fld), (ast_expression*)fld, 2);
1940 vx.name = mem_a(len+3);
1941 vy.name = mem_a(len+3);
1942 vz.name = mem_a(len+3);
1943 memcpy(vx.name, varent.name, len);
1944 memcpy(vy.name, varent.name, len);
1945 memcpy(vz.name, varent.name, len);
1946 vx.name[len] = vy.name[len] = vz.name[len] = '_';
1947 vx.name[len+1] = 'x';
1948 vy.name[len+1] = 'y';
1949 vz.name[len+1] = 'z';
1950 vx.name[len+2] = vy.name[len+2] = vz.name[len+2] = 0;
1951 (void)!parser_t_fields_add(parser, vx);
1952 (void)!parser_t_fields_add(parser, vy);
1953 (void)!parser_t_fields_add(parser, vz);
1956 if (!parser_next(parser)) {
1957 parseerror(parser, "expected semicolon or another field name");
1960 if (parser->tok == ';')
1962 if (parser->tok != ',' || !parser_next(parser)) {
1963 parseerror(parser, "expected semicolon or another field name");
1968 /* skip the semicolon */
1969 if (!parser_next(parser))
1970 return parser->tok == TOKEN_EOF;
1976 parseerror(parser, "unexpected token: %s", parser->lex->tok->value);
1982 static parser_t *parser;
1986 parser = (parser_t*)mem_a(sizeof(parser_t));
1990 memset(parser, 0, sizeof(*parser));
1994 bool parser_compile(const char *filename)
1996 parser->lex = lex_open(filename);
1998 printf("failed to open file \"%s\"\n", filename);
2002 /* initial lexer/parser state */
2003 parser->lex->flags.noops = true;
2005 if (parser_next(parser))
2007 while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
2009 if (!parser_do(parser)) {
2010 if (parser->tok == TOKEN_EOF)
2011 parseerror(parser, "unexpected eof");
2012 else if (!parser->errors)
2013 parseerror(parser, "parse error\n");
2014 lex_close(parser->lex);
2021 lex_close(parser->lex);
2023 return !parser->errors;
2026 void parser_cleanup()
2029 for (i = 0; i < parser->functions_count; ++i) {
2030 ast_delete(parser->functions[i]);
2032 for (i = 0; i < parser->imm_vector_count; ++i) {
2033 ast_delete(parser->imm_vector[i]);
2035 for (i = 0; i < parser->imm_string_count; ++i) {
2036 ast_delete(parser->imm_string[i]);
2038 for (i = 0; i < parser->imm_float_count; ++i) {
2039 ast_delete(parser->imm_float[i]);
2041 for (i = 0; i < parser->globals_count; ++i) {
2042 ast_delete(parser->globals[i].var);
2043 mem_d(parser->globals[i].name);
2045 MEM_VECTOR_CLEAR(parser, globals);
2050 bool parser_finish(const char *output)
2055 if (!parser->errors)
2057 ir = ir_builder_new("gmqcc_out");
2059 printf("failed to allocate builder\n");
2063 for (i = 0; i < parser->imm_float_count; ++i) {
2064 if (!ast_global_codegen(parser->imm_float[i], ir)) {
2065 printf("failed to generate global %s\n", parser->imm_float[i]->name);
2066 ir_builder_delete(ir);
2070 for (i = 0; i < parser->imm_string_count; ++i) {
2071 if (!ast_global_codegen(parser->imm_string[i], ir)) {
2072 printf("failed to generate global %s\n", parser->imm_string[i]->name);
2073 ir_builder_delete(ir);
2077 for (i = 0; i < parser->imm_vector_count; ++i) {
2078 if (!ast_global_codegen(parser->imm_vector[i], ir)) {
2079 printf("failed to generate global %s\n", parser->imm_vector[i]->name);
2080 ir_builder_delete(ir);
2084 for (i = 0; i < parser->fields_count; ++i) {
2087 if (!ast_istype(parser->fields[i].var, ast_value))
2089 field = (ast_value*)parser->fields[i].var;
2090 isconst = field->isconst;
2091 field->isconst = false;
2092 if (!ast_global_codegen((ast_value*)field, ir)) {
2093 printf("failed to generate field %s\n", field->name);
2094 ir_builder_delete(ir);
2099 ast_expression *subtype;
2100 field->isconst = true;
2101 subtype = field->expression.next;
2102 ifld = ir_builder_create_field(ir, field->name, subtype->expression.vtype);
2103 if (subtype->expression.vtype == TYPE_FIELD)
2104 ifld->fieldtype = subtype->expression.next->expression.vtype;
2105 else if (subtype->expression.vtype == TYPE_FUNCTION)
2106 ifld->outtype = subtype->expression.next->expression.vtype;
2107 (void)!ir_value_set_field(field->ir_v, ifld);
2110 for (i = 0; i < parser->globals_count; ++i) {
2111 if (!ast_istype(parser->globals[i].var, ast_value))
2113 if (!ast_global_codegen((ast_value*)(parser->globals[i].var), ir)) {
2114 printf("failed to generate global %s\n", parser->globals[i].name);
2115 ir_builder_delete(ir);
2119 for (i = 0; i < parser->functions_count; ++i) {
2120 if (!ast_function_codegen(parser->functions[i], ir)) {
2121 printf("failed to generate function %s\n", parser->functions[i]->name);
2122 ir_builder_delete(ir);
2125 if (!ir_function_finalize(parser->functions[i]->ir_func)) {
2126 printf("failed to finalize function %s\n", parser->functions[i]->name);
2127 ir_builder_delete(ir);
2133 ir_builder_dump(ir, printf);
2135 if (!ir_builder_generate(ir, output)) {
2136 printf("*** failed to generate output file\n");
2137 ir_builder_delete(ir);
2141 ir_builder_delete(ir);
2145 printf("*** there were compile errors\n");