Support -x as a constant expression.
Support -x as a constant expression.
1.1 --- a/src/SemanticAnalysis.cpp Mon Jul 30 22:24:57 2012 -0700
1.2 +++ b/src/SemanticAnalysis.cpp Mon Jul 30 22:30:38 2012 -0700
1.3 @@ -58,6 +58,19 @@
1.4 }
1.5 }
1.6
1.7 +static inline bool
1.8 +EvaluateUnaryOperation(TokenKind kind, const BoxedPrimitive &inner, BoxedPrimitive *out)
1.9 +{
1.10 + switch (kind) {
1.11 + case TOK_MINUS:
1.12 + *out = BoxedPrimitive::Int(-inner.toInt());
1.13 + return true;
1.14 +
1.15 + default:
1.16 + return false;
1.17 + }
1.18 +}
1.19 +
1.20 static bool
1.21 EvaluateForConstant(Expression *expression, BoxedPrimitive *out)
1.22 {
1.23 @@ -75,6 +88,15 @@
1.24 return EvaluateBinaryOperation(b->token(), left, right, out);
1.25 }
1.26
1.27 + case AstNode::kUnaryExpression:
1.28 + {
1.29 + BoxedPrimitive inner;
1.30 + UnaryExpression *u = expression->toUnaryExpression();
1.31 + if (!EvaluateForConstant(u->expression(), &inner))
1.32 + return false;
1.33 + return EvaluateUnaryOperation(u->token(), inner, out);
1.34 + }
1.35 +
1.36 case AstNode::kNameProxy:
1.37 {
1.38 NameProxy *proxy = expression->toNameProxy();