Resurrect constant_expr().

This commit is contained in:
Bill Currie 2011-02-15 09:28:27 +09:00
parent bc882ddc3c
commit d162838299
2 changed files with 28 additions and 0 deletions

View file

@ -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.

View file

@ -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)
{