From fe05d5afd9893d176d3283306404a9767323d1b5 Mon Sep 17 00:00:00 2001 From: "Wolfgang (Blub) Bumiller" Date: Sat, 10 Nov 2012 12:10:30 +0100 Subject: [PATCH] added 'var' to the keywords when not in -std=qcc, supporting 'var .float x;' in global-scope to declare a field-pointer variable without creating a field of that name --- lexer.c | 3 ++- parser.c | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lexer.c b/lexer.c index 0526b52..9f1fc4e 100644 --- a/lexer.c +++ b/lexer.c @@ -1054,7 +1054,8 @@ int lex_do(lex_file *lex) !strcmp(v, "struct") || !strcmp(v, "union") || !strcmp(v, "break") || - !strcmp(v, "continue")) + !strcmp(v, "continue") || + !strcmp(v, "var")) { lex->tok.ttype = TOKEN_KEYWORD; } diff --git a/parser.c b/parser.c index c02bd41..96e4adb 100644 --- a/parser.c +++ b/parser.c @@ -48,7 +48,7 @@ MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals) MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions) static bool GMQCC_WARN parser_pop_local(parser_t *parser); -static bool parse_variable(parser_t *parser, ast_block *localblock); +static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields); static ast_block* parse_block(parser_t *parser, bool warnreturn); static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn); static ast_expression* parse_statement_or_block(parser_t *parser); @@ -1498,7 +1498,7 @@ static bool parse_for(parser_t *parser, ast_block *block, ast_expression **out) parseerror(parser, "TODO: assignment of new variables to be non-const"); goto onerr; - if (!parse_variable(parser, block)) + if (!parse_variable(parser, block, true)) goto onerr; } else if (parser->tok != ';') @@ -1594,7 +1594,7 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression * if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable")) return false; } - if (!parse_variable(parser, block)) + if (!parse_variable(parser, block, false)) return false; *out = NULL; return true; @@ -1611,7 +1611,7 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression * parseerror(parser, "expected variable declaration"); return false; } - if (!parse_variable(parser, block)) + if (!parse_variable(parser, block, true)) return false; *out = NULL; return true; @@ -2358,7 +2358,7 @@ static ast_value *parse_typename(parser_t *parser, ast_value **storebase) return var; } -static bool parse_variable(parser_t *parser, ast_block *localblock) +static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields) { ast_value *var; ast_value *proto; @@ -2425,7 +2425,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock) } } - if (var->expression.vtype == TYPE_FIELD) + if (!nofields && var->expression.vtype == TYPE_FIELD) { /* deal with field declarations */ old = parser_find_field(parser, var->name); @@ -2552,7 +2552,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock) if (!localblock) { /* deal with global variables, fields, functions */ - if (var->expression.vtype == TYPE_FIELD) { + if (!nofields && var->expression.vtype == TYPE_FIELD) { if (!(retval = parser_t_fields_add(parser, varent))) goto cleanup; if (isvector) { @@ -2622,7 +2622,7 @@ skipvar: if (parser->tok == ',') goto another; - if (!var || (!localblock && basetype->expression.vtype == TYPE_FIELD)) { + if (!var || (!localblock && !nofields && basetype->expression.vtype == TYPE_FIELD)) { parseerror(parser, "missing comma or semicolon while parsing variables"); break; } @@ -2787,11 +2787,18 @@ static bool parser_global_statement(parser_t *parser) { if (parser->tok == TOKEN_TYPENAME || parser->tok == '.') { - return parse_variable(parser, NULL); + return parse_variable(parser, NULL, false); } else if (parser->tok == TOKEN_KEYWORD) { /* handle 'var' and 'const' */ + if (!strcmp(parser_tokval(parser), "var")) { + if (!parser_next(parser)) { + parseerror(parser, "expected variable declaration after 'var'"); + return false; + } + return parse_variable(parser, NULL, true); + } return false; } else if (parser->tok == '$') -- 2.39.2