diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index d61ad60cf..841688183 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -438,6 +438,16 @@ short expr_short (expr_t *e); */ int is_constant (expr_t *e); +/** Return a value expression representing the constant stored in \a e. + + If \a e does not represent a constant, or \a e is already a value or + nil expression, then \a e is returned rather than a new expression. + + \param e The expression from which to extract the value. + \return A new expression holding the value of \a e or \e itself. +*/ +expr_t *constant_expr (expr_t *e); + /** Check if the op-code is a comparison. \param op The op-code to check. diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index 8bd574bd4..0009198b2 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -602,6 +602,24 @@ is_constant (expr_t *e) return 0; } +expr_t * +constant_expr (expr_t *e) +{ + expr_t *new; + if (!is_constant (e)) + return e; + if (e->type == ex_nil || e->type == ex_value) + return e; + if (e->type != ex_symbol || e->e.symbol->sy_type != sy_const) + return e; + new = new_expr (); + new->type = ex_value; + new->line = e->line; + new->file = e->file; + new->e.value = e->e.symbol->s.value; + return new; +} + int is_string_val (expr_t *e) {