ast_function *function;
+ /* All the labels the function defined...
+ * Should they be in ast_function instead?
+ */
+ ast_label **labels;
+ ast_goto **gotos;
+
/* A list of hashtables for each scope */
ht *variables;
ht htfields;
qcint memberof;
} parser_t;
+#define CV_NONE 0
+#define CV_CONST 1
+#define CV_VAR -1
+
static void parser_enterblock(parser_t *parser);
static bool parser_leaveblock(parser_t *parser);
static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
static bool parse_typedef(parser_t *parser);
-static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, bool is_const, ast_value *cached_typedef);
+static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef);
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);
vec_push(sy.out, syexp(parser_ctx(parser), (ast_expression*)val));
DEBUGSHUNTDO(con_out("push %g\n", parser_token(parser)->constval.f));
}
- else if (parser->tok == TOKEN_INTCONST) {
+ else if (parser->tok == TOKEN_INTCONST || parser->tok == TOKEN_CHARCONST) {
ast_value *val;
if (wantop) {
parseerror(parser, "expected operator or end of statement, got constant");
"current standard does not allow variable declarations in for-loop initializers"))
goto onerr;
}
-
- parseerror(parser, "TODO: assignment of new variables to be non-const");
- goto onerr;
- if (!parse_variable(parser, block, true, false, typevar))
+ if (!parse_variable(parser, block, true, CV_VAR, typevar))
goto onerr;
}
else if (parser->tok != ';')
return true;
}
+static bool parse_goto(parser_t *parser, ast_expression **out)
+{
+ size_t i;
+ ast_goto *gt;
+
+ if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
+ parseerror(parser, "expected label name after `goto`");
+ return false;
+ }
+
+ gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
+
+ for (i = 0; i < vec_size(parser->labels); ++i) {
+ if (!strcmp(parser->labels[i]->name, parser_tokval(parser))) {
+ ast_goto_set_label(gt, parser->labels[i]);
+ break;
+ }
+ }
+ if (i == vec_size(parser->labels))
+ vec_push(parser->gotos, gt);
+
+ if (!parser_next(parser) || parser->tok != ';') {
+ parseerror(parser, "semicolon expected after goto label");
+ return false;
+ }
+ if (!parser_next(parser)) {
+ parseerror(parser, "parse error after goto");
+ return false;
+ }
+
+ *out = (ast_expression*)gt;
+ return true;
+}
+
static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
{
ast_value *typevar = NULL;
+ *out = NULL;
+
if (parser->tok == TOKEN_IDENT)
typevar = parser_find_typedef(parser, parser_tokval(parser), 0);
if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
return false;
}
- if (!parse_variable(parser, block, false, false, typevar))
+ if (!parse_variable(parser, block, false, CV_NONE, typevar))
return false;
*out = NULL;
return true;
}
else if (parser->tok == TOKEN_KEYWORD)
{
- if (!strcmp(parser_tokval(parser), "local"))
+ if (!strcmp(parser_tokval(parser), "local") ||
+ !strcmp(parser_tokval(parser), "const"))
{
+ int cvq = parser_tokval(parser)[0] == 'c' ? CV_CONST : CV_VAR;
+
if (!block) {
parseerror(parser, "cannot declare a local variable here");
return false;
parseerror(parser, "expected variable declaration");
return false;
}
- if (!parse_variable(parser, block, true, false, NULL))
+ if (!parse_variable(parser, block, true, cvq, NULL))
return false;
*out = NULL;
return true;
}
return true;
}
+ else if (!strcmp(parser_tokval(parser), "goto"))
+ {
+ return parse_goto(parser, out);
+ }
else if (!strcmp(parser_tokval(parser), "typedef"))
{
if (!parser_next(parser)) {
*out = (ast_expression*)inner;
return true;
}
+ else if (parser->tok == ':')
+ {
+ size_t i;
+ ast_label *label;
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected label name");
+ return false;
+ }
+ if (parser->tok != TOKEN_IDENT) {
+ parseerror(parser, "label must be an identifier");
+ return false;
+ }
+ label = ast_label_new(parser_ctx(parser), parser_tokval(parser));
+ if (!label)
+ return false;
+ vec_push(parser->labels, label);
+ *out = (ast_expression*)label;
+ if (!parser_next(parser)) {
+ parseerror(parser, "parse error after label");
+ return false;
+ }
+ for (i = 0; i < vec_size(parser->gotos); ++i) {
+ if (!strcmp(parser->gotos[i]->name, label->name)) {
+ ast_goto_set_label(parser->gotos[i], label);
+ vec_remove(parser->gotos, i, 1);
+ --i;
+ }
+ }
+ return true;
+ }
else if (parser->tok == ';')
{
if (!parser_next(parser)) {
has_frame_think = false;
old = parser->function;
+ if (vec_size(parser->gotos) || vec_size(parser->labels)) {
+ parseerror(parser, "gotos/labels leaking");
+ return false;
+ }
+
if (var->expression.variadic) {
if (parsewarning(parser, WARN_VARIADIC_FUNCTION,
"variadic function with implementation will not be able to access additional parameters"))
return true;
}
-static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, bool is_const, ast_value *cached_typedef)
+static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int is_const_var, ast_value *cached_typedef)
{
ast_value *var;
ast_value *proto;
}
}
- if (is_const)
+ if (is_const_var == CV_CONST)
var->constant = true;
/* Part 1:
}
else if (parser->tok == '{' || parser->tok == '[')
{
+ size_t i;
if (localblock) {
parseerror(parser, "cannot declare functions within functions");
break;
if (!parse_function_body(parser, var))
break;
ast_delete(basetype);
+ for (i = 0; i < vec_size(parser->gotos); ++i)
+ parseerror(parser, "undefined label: `%s`", parser->gotos[i]->name);
+ vec_free(parser->gotos);
+ vec_free(parser->labels);
return true;
} else {
ast_expression *cexp;
if (!localblock) {
cval = (ast_value*)cexp;
- if (!ast_istype(cval, ast_value) || !cval->hasvalue)
+ if (!ast_istype(cval, ast_value) || !cval->hasvalue || !cval->constant)
parseerror(parser, "cannot initialize a global constant variable with a non-constant expression");
else
{
- if (opts_standard != COMPILER_GMQCC && !OPTS_FLAG(INITIALIZED_NONCONSTANTS))
+ if (opts_standard != COMPILER_GMQCC &&
+ !OPTS_FLAG(INITIALIZED_NONCONSTANTS) &&
+ is_const_var != CV_VAR)
+ {
var->constant = true;
+ }
var->hasvalue = true;
if (cval->expression.vtype == TYPE_STRING)
var->constval.vstring = parser_strdup(cval->constval.vstring);
ast_unref(cval);
}
} else {
+ bool cvq;
shunt sy = { NULL, NULL };
+ cvq = var->constant;
+ var->constant = false;
vec_push(sy.out, syexp(ast_ctx(var), (ast_expression*)var));
vec_push(sy.out, syexp(ast_ctx(cexp), (ast_expression*)cexp));
vec_push(sy.ops, syop(ast_ctx(var), parser->assign_op));
}
vec_free(sy.out);
vec_free(sy.ops);
+ var->constant = cvq;
}
}
if (istype || parser->tok == TOKEN_TYPENAME || parser->tok == '.')
{
- return parse_variable(parser, NULL, false, false, istype);
+ return parse_variable(parser, NULL, false, CV_NONE, istype);
}
else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "var"))
{
parseerror(parser, "expected variable declaration after 'var'");
return false;
}
- return parse_variable(parser, NULL, true, false, NULL);
+ return parse_variable(parser, NULL, true, CV_VAR, NULL);
}
}
else if (parser->tok == TOKEN_KEYWORD)
return false;
}
}
- return parse_variable(parser, NULL, true, true, NULL);
+ return parse_variable(parser, NULL, true, CV_CONST, NULL);
}
else if (!strcmp(parser_tokval(parser), "typedef")) {
if (!parser_next(parser)) {
vec_free(parser->typedefs);
vec_free(parser->_blocktypedefs);
+ vec_free(parser->labels);
+ vec_free(parser->gotos);
+
mem_d(parser);
}