# HG changeset patch # User David Anderson # Date 1343712638 25200 # Node ID a8735148dcf03df153d93436e4be037587477a57 # Parent 393f1b8ac8c39e952adbb4f3dd807b6453e40a0b Support -x as a constant expression. diff -r 393f1b8ac8c3 -r a8735148dcf0 src/SemanticAnalysis.cpp --- a/src/SemanticAnalysis.cpp Mon Jul 30 22:24:57 2012 -0700 +++ b/src/SemanticAnalysis.cpp Mon Jul 30 22:30:38 2012 -0700 @@ -58,6 +58,19 @@ } } +static inline bool +EvaluateUnaryOperation(TokenKind kind, const BoxedPrimitive &inner, BoxedPrimitive *out) +{ + switch (kind) { + case TOK_MINUS: + *out = BoxedPrimitive::Int(-inner.toInt()); + return true; + + default: + return false; + } +} + static bool EvaluateForConstant(Expression *expression, BoxedPrimitive *out) { @@ -75,6 +88,15 @@ return EvaluateBinaryOperation(b->token(), left, right, out); } + case AstNode::kUnaryExpression: + { + BoxedPrimitive inner; + UnaryExpression *u = expression->toUnaryExpression(); + if (!EvaluateForConstant(u->expression(), &inner)) + return false; + return EvaluateUnaryOperation(u->token(), inner, out); + } + case AstNode::kNameProxy: { NameProxy *proxy = expression->toNameProxy();