X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=parser.c;h=534f007db4d2ba65d0071a8408088ec8e5346f2a;hp=de0bc52023a18a1e034e8c050ef521f5fe1f75b5;hb=84088cb5f0e41a88e88b1af310a9ed12e56a038b;hpb=89d86420ad57dc004b7692d229e25a9adc96300a diff --git a/parser.c b/parser.c index de0bc52..534f007 100644 --- a/parser.c +++ b/parser.c @@ -380,6 +380,44 @@ static sy_elem syparen(lex_ctx ctx, int p, size_t off) { # define DEBUGSHUNTDO(x) #endif +/* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n], + * so we need to rotate it to become ent.(foo[n]). + */ +static bool rotate_entfield_array_index_nodes(ast_expression **out) +{ + ast_array_index *index; + ast_entfield *entfield; + + ast_value *field; + ast_expression *sub; + ast_expression *entity; + + lex_ctx ctx = ast_ctx(*out); + + if (!ast_istype(*out, ast_array_index)) + return false; + index = (ast_array_index*)*out; + + if (!ast_istype(index->array, ast_entfield)) + return false; + entfield = (ast_entfield*)index->array; + + if (!ast_istype(entfield->field, ast_value)) + return false; + field = (ast_value*)entfield->field; + + sub = index->index; + entity = entfield->entity; + + ast_delete(index); + + index = ast_array_index_new(ctx, (ast_expression*)field, sub); + entfield = ast_entfield_new(ctx, entity, (ast_expression*)index); + *out = (ast_expression*)entfield; + + return true; +} + static bool parser_sy_pop(parser_t *parser, shunt *sy) { const oper_info *op; @@ -407,7 +445,7 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy) op = &operators[sy->ops[sy->ops_count-1].etype - 1]; ctx = sy->ops[sy->ops_count-1].ctx; - DEBUGSHUNTDO(printf("apply %s\n", op->op)); + DEBUGSHUNTDO(con_out("apply %s\n", op->op)); if (sy->out_count < op->operands) { parseerror(parser, "internal error: not enough operands: %i (operator %s (%i))", sy->out_count, @@ -464,7 +502,10 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy) break; case opid1('['): - if (exprs[0]->expression.vtype != TYPE_ARRAY) { + if (exprs[0]->expression.vtype != TYPE_ARRAY && + !(exprs[0]->expression.vtype == TYPE_FIELD && + exprs[0]->expression.next->expression.vtype == TYPE_ARRAY)) + { ast_type_to_string(exprs[0], ty1, sizeof(ty1)); parseerror(parser, "cannot index value of type %s", ty1); return false; @@ -475,6 +516,15 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy) return false; } out = (ast_expression*)ast_array_index_new(ctx, exprs[0], exprs[1]); + if (rotate_entfield_array_index_nodes(&out)) + { + if (opts_standard != COMPILER_GMQCC) { + /* this error doesn't need to make us bail out */ + (void)!parsewarning(parser, WARN_EXTENSIONS, + "accessing array-field members of an entity without parenthesis\n" + " -> this is an extension from -std=gmqcc"); + } + } break; case opid1(','): @@ -725,7 +775,7 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy) return false; } if (opts_standard == COMPILER_GMQCC) - printf("TODO: early out logic\n"); + con_out("TODO: early out logic\n"); if (CanConstFold(exprs[0], exprs[1])) out = (ast_expression*)parser_const_float(parser, (generated_op == INSTR_OR ? (ConstF(0) || ConstF(1)) : (ConstF(0) && ConstF(1)))); @@ -872,7 +922,7 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy) return false; } - DEBUGSHUNTDO(printf("applied %s\n", op->op)); + DEBUGSHUNTDO(con_out("applied %s\n", op->op)); sy->out[sy->out_count++] = syexp(ctx, out); return true; } @@ -1109,7 +1159,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma parseerror(parser, "out of memory"); goto onerr; } - DEBUGSHUNTDO(printf("push %s\n", parser_tokval(parser))); + DEBUGSHUNTDO(con_out("push %s\n", parser_tokval(parser))); } else if (parser->tok == TOKEN_FLOATCONST) { ast_value *val; @@ -1125,7 +1175,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma parseerror(parser, "out of memory"); goto onerr; } - DEBUGSHUNTDO(printf("push %g\n", parser_token(parser)->constval.f)); + DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f)); } else if (parser->tok == TOKEN_INTCONST) { ast_value *val; @@ -1141,7 +1191,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma parseerror(parser, "out of memory"); goto onerr; } - DEBUGSHUNTDO(printf("push %i\n", parser_token(parser)->constval.i)); + DEBUGSHUNTDO(con_out("push %i\n", parser_token(parser)->constval.i)); } else if (parser->tok == TOKEN_STRINGCONST) { ast_value *val; @@ -1157,7 +1207,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma parseerror(parser, "out of memory"); goto onerr; } - DEBUGSHUNTDO(printf("push string\n")); + DEBUGSHUNTDO(con_out("push string\n")); } else if (parser->tok == TOKEN_VECTORCONST) { ast_value *val; @@ -1173,7 +1223,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma parseerror(parser, "out of memory"); goto onerr; } - DEBUGSHUNTDO(printf("push '%g %g %g'\n", + DEBUGSHUNTDO(con_out("push '%g %g %g'\n", parser_token(parser)->constval.v.x, parser_token(parser)->constval.v.y, parser_token(parser)->constval.v.z)); @@ -1188,7 +1238,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma } else if (parser->tok == ')') { if (wantop) { - DEBUGSHUNTDO(printf("do[op] )\n")); + DEBUGSHUNTDO(con_out("do[op] )\n")); --parens; if (parens < 0) break; @@ -1197,7 +1247,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma if (!parser_close_paren(parser, &sy, false)) goto onerr; } else { - DEBUGSHUNTDO(printf("do[nop] )\n")); + DEBUGSHUNTDO(con_out("do[nop] )\n")); --parens; if (parens < 0) break; @@ -1288,7 +1338,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma if (op->id == opid1('(')) { if (wantop) { - DEBUGSHUNTDO(printf("push [op] (\n")); + DEBUGSHUNTDO(con_out("push [op] (\n")); ++parens; /* we expected an operator, this is the function-call operator */ if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), SY_PAREN_FUNC, sy.out_count-1))) { @@ -1301,7 +1351,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma parseerror(parser, "out of memory"); goto onerr; } - DEBUGSHUNTDO(printf("push [nop] (\n")); + DEBUGSHUNTDO(con_out("push [nop] (\n")); } wantop = false; } else if (op->id == opid1('[')) { @@ -1317,7 +1367,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma goto onerr; wantop = false; } else { - DEBUGSHUNTDO(printf("push operator %s\n", op->op)); + DEBUGSHUNTDO(con_out("push operator %s\n", op->op)); if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op))) goto onerr; wantop = false; @@ -1344,7 +1394,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma expr = sy.out[0].out; MEM_VECTOR_CLEAR(&sy, out); MEM_VECTOR_CLEAR(&sy, ops); - DEBUGSHUNTDO(printf("shunt done\n")); + DEBUGSHUNTDO(con_out("shunt done\n")); return expr; onerr: @@ -1370,11 +1420,23 @@ static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out) { ast_ifthen *ifthen; ast_expression *cond, *ontrue, *onfalse = NULL; + bool ifnot = false; lex_ctx ctx = parser_ctx(parser); - /* skip the 'if' and check for opening paren */ - if (!parser_next(parser) || parser->tok != '(') { + /* skip the 'if', parse an optional 'not' and check for an opening paren */ + if (!parser_next(parser)) { + parseerror(parser, "expected condition or 'not'"); + return false; + } + if (parser->tok == TOKEN_KEYWORD && !strcmp(parser_tokval(parser), "not")) { + ifnot = true; + if (!parser_next(parser)) { + parseerror(parser, "expected condition in parenthesis"); + return false; + } + } + if (parser->tok != '(') { parseerror(parser, "expected 'if' condition in parenthesis"); return false; } @@ -1421,7 +1483,10 @@ static bool parse_if(parser_t *parser, ast_block *block, ast_expression **out) } } - ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse); + if (ifnot) + ifthen = ast_ifthen_new(ctx, cond, onfalse, ontrue); + else + ifthen = ast_ifthen_new(ctx, cond, ontrue, onfalse); *out = (ast_expression*)ifthen; return true; } @@ -2344,16 +2409,19 @@ static ast_expression *array_field_setter_node( ast_return *ret; ast_entfield *entfield; ast_array_index *subscript; - int assignop = type_store_instr[value->expression.vtype]; + int assignop = type_storep_instr[value->expression.vtype]; if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR) - assignop = INSTR_STORE_V; + assignop = INSTR_STOREP_V; subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from)); if (!subscript) return NULL; - entfield = ast_entfield_new(ctx, (ast_expression*)entity, (ast_expression*)subscript); + entfield = ast_entfield_new_force(ctx, + (ast_expression*)entity, + (ast_expression*)subscript, + (ast_expression*)subscript); if (!entfield) { ast_delete(subscript); return NULL; @@ -3477,14 +3545,8 @@ bool parser_init() return true; } -bool parser_compile(const char *filename) +bool parser_compile() { - parser->lex = lex_open(filename); - if (!parser->lex) { - printf("failed to open file \"%s\"\n", filename); - return false; - } - /* initial lexer/parser state */ parser->lex->flags.noops = true; @@ -3515,6 +3577,26 @@ bool parser_compile(const char *filename) return !parser->errors; } +bool parser_compile_file(const char *filename) +{ + parser->lex = lex_open(filename); + if (!parser->lex) { + con_out("failed to open file \"%s\"\n", filename); + return false; + } + return parser_compile(); +} + +bool parser_compile_string(const char *name, const char *str) +{ + parser->lex = lex_open_string(str, strlen(str), name); + if (!parser->lex) { + con_out("failed to create lexer for string \"%s\"\n", name); + return false; + } + return parser_compile(); +} + void parser_cleanup() { size_t i; @@ -3631,7 +3713,7 @@ bool parser_finish(const char *output) { ir = ir_builder_new("gmqcc_out"); if (!ir) { - printf("failed to allocate builder\n"); + con_out("failed to allocate builder\n"); return false; } @@ -3644,7 +3726,7 @@ bool parser_finish(const char *output) isconst = field->isconst; field->isconst = false; if (!ast_global_codegen((ast_value*)field, ir, true)) { - printf("failed to generate field %s\n", field->name); + con_out("failed to generate field %s\n", field->name); ir_builder_delete(ir); return false; } @@ -3675,28 +3757,28 @@ bool parser_finish(const char *output) } } if (!ast_global_codegen(asvalue, ir, false)) { - printf("failed to generate global %s\n", parser->globals[i].name); + con_out("failed to generate global %s\n", parser->globals[i].name); ir_builder_delete(ir); return false; } } for (i = 0; i < parser->imm_float_count; ++i) { if (!ast_global_codegen(parser->imm_float[i], ir, false)) { - printf("failed to generate global %s\n", parser->imm_float[i]->name); + con_out("failed to generate global %s\n", parser->imm_float[i]->name); ir_builder_delete(ir); return false; } } for (i = 0; i < parser->imm_string_count; ++i) { if (!ast_global_codegen(parser->imm_string[i], ir, false)) { - printf("failed to generate global %s\n", parser->imm_string[i]->name); + con_out("failed to generate global %s\n", parser->imm_string[i]->name); ir_builder_delete(ir); return false; } } for (i = 0; i < parser->imm_vector_count; ++i) { if (!ast_global_codegen(parser->imm_vector[i], ir, false)) { - printf("failed to generate global %s\n", parser->imm_vector[i]->name); + con_out("failed to generate global %s\n", parser->imm_vector[i]->name); ir_builder_delete(ir); return false; } @@ -3740,7 +3822,7 @@ bool parser_finish(const char *output) !ast_function_codegen(asvalue->setter->constval.vfunc, ir) || !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func)) { - printf("failed to generate setter for %s\n", parser->globals[i].name); + printf("failed to generate setter for %s\n", parser->fields[i].name); ir_builder_delete(ir); return false; } @@ -3750,7 +3832,7 @@ bool parser_finish(const char *output) !ast_function_codegen(asvalue->getter->constval.vfunc, ir) || !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func)) { - printf("failed to generate getter for %s\n", parser->globals[i].name); + printf("failed to generate getter for %s\n", parser->fields[i].name); ir_builder_delete(ir); return false; } @@ -3758,12 +3840,12 @@ bool parser_finish(const char *output) } 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); + con_out("failed to generate function %s\n", parser->functions[i]->name); ir_builder_delete(ir); return false; } if (!ir_function_finalize(parser->functions[i]->ir_func)) { - printf("failed to finalize function %s\n", parser->functions[i]->name); + con_out("failed to finalize function %s\n", parser->functions[i]->name); ir_builder_delete(ir); return false; } @@ -3771,12 +3853,12 @@ bool parser_finish(const char *output) if (retval) { if (opts_dump) - ir_builder_dump(ir, printf); + ir_builder_dump(ir, con_out); generate_checksum(parser); if (!ir_builder_generate(ir, output)) { - printf("*** failed to generate output file\n"); + con_out("*** failed to generate output file\n"); ir_builder_delete(ir); return false; } @@ -3786,6 +3868,6 @@ bool parser_finish(const char *output) return retval; } - printf("*** there were compile errors\n"); + con_out("*** there were compile errors\n"); return false; }