src/compiler/ConstantEvaluator.cpp
changeset 263 ba85a47ee414
parent 213 a9b8d24967d6
     1.1 --- a/src/compiler/ConstantEvaluator.cpp	Mon Jun 10 00:46:29 2013 +0100
     1.2 +++ b/src/compiler/ConstantEvaluator.cpp	Mon Jun 10 01:04:43 2013 +0100
     1.3 @@ -1,94 +1,94 @@
     1.4 -/* vim: set ts=4 sw=4 tw=99 et:
     1.5 - *
     1.6 - * Copyright (C) 2012 David Anderson
     1.7 - *
     1.8 - * This file is part of SourcePawn.
     1.9 - *
    1.10 - * SourcePawn is free software: you can redistribute it and/or modify it under
    1.11 - * the terms of the GNU General Public License as published by the Free
    1.12 - * Software Foundation, either version 3 of the License, or (at your option)
    1.13 - * any later version.
    1.14 - * 
    1.15 - * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 - *
    1.19 - * You should have received a copy of the GNU General Public License along with
    1.20 - * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.21 - */
    1.22 -#include "AST.h"
    1.23 -#include "ConstantEvaluator.h"
    1.24 -#include "../Heap-inl.h"
    1.25 -#include "../Handles-inl.h"
    1.26 -
    1.27 -using namespace ke;
    1.28 -
    1.29 -static inline bool
    1.30 -EvaluateBinaryOperation(TokenKind kind, const BoxedPrimitive &left, const BoxedPrimitive &right,
    1.31 -                        BoxedPrimitive *out)
    1.32 -{
    1.33 -    switch (kind) {
    1.34 -      case TOK_PLUS:
    1.35 -        *out = BoxedPrimitive::Int(left.toInt() + right.toInt());
    1.36 -        return true;
    1.37 -
    1.38 -      case TOK_SHL:
    1.39 -        *out = BoxedPrimitive::Int(left.toInt() << right.toInt());
    1.40 -        return true;
    1.41 -
    1.42 -      default:
    1.43 -        return false;
    1.44 -    }
    1.45 -}
    1.46 -
    1.47 -static inline bool
    1.48 -EvaluateUnaryOperation(TokenKind kind, const BoxedPrimitive &inner, BoxedPrimitive *out)
    1.49 -{
    1.50 -  switch (kind) {
    1.51 -    case TOK_MINUS:
    1.52 -      *out = BoxedPrimitive::Int(-inner.toInt());
    1.53 -      return true;
    1.54 -
    1.55 -    default:
    1.56 -      return false;
    1.57 -  }
    1.58 -}
    1.59 -
    1.60 -bool
    1.61 -ke::EvaluateForConstant(Expression *expr, BoxedPrimitive *out)
    1.62 -{
    1.63 -    switch (expr->kind()) {
    1.64 -      case AstNode::kBinaryExpression:
    1.65 -      {
    1.66 -        BoxedPrimitive left;
    1.67 -        BoxedPrimitive right;
    1.68 -        BinaryExpression *b = expr->toBinaryExpression();
    1.69 -        if (!EvaluateForConstant(b->left(), &left) ||
    1.70 -            !EvaluateForConstant(b->right(), &right))
    1.71 -        {
    1.72 -            return false;
    1.73 -        }
    1.74 -        return EvaluateBinaryOperation(b->token(), left, right, out);
    1.75 -      }
    1.76 -
    1.77 -      case AstNode::kUnaryExpression:
    1.78 -      {
    1.79 -        BoxedPrimitive inner;
    1.80 -        UnaryExpression *u = expr->toUnaryExpression();
    1.81 -        if (!EvaluateForConstant(u->expression(), &inner))
    1.82 -            return false;
    1.83 -        return EvaluateUnaryOperation(u->token(), inner, out);
    1.84 -      }
    1.85 -
    1.86 -      case AstNode::kBooleanLiteral:
    1.87 -        *out = BoxedPrimitive::Int(expr->toBooleanLiteral()->token() == TOK_TRUE ? 1 : 0);
    1.88 -        return true;
    1.89 -
    1.90 -      case AstNode::kIntegerLiteral:
    1.91 -        *out = BoxedPrimitive::Int(expr->toIntegerLiteral()->value());
    1.92 -        return true;
    1.93 -
    1.94 -      default:
    1.95 -        return false;
    1.96 -    }
    1.97 -}
    1.98 +/* vim: set ts=4 sw=4 tw=99 et:
    1.99 + *
   1.100 + * Copyright (C) 2012 David Anderson
   1.101 + *
   1.102 + * This file is part of SourcePawn.
   1.103 + *
   1.104 + * SourcePawn is free software: you can redistribute it and/or modify it under
   1.105 + * the terms of the GNU General Public License as published by the Free
   1.106 + * Software Foundation, either version 3 of the License, or (at your option)
   1.107 + * any later version.
   1.108 + * 
   1.109 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
   1.110 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   1.111 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   1.112 + *
   1.113 + * You should have received a copy of the GNU General Public License along with
   1.114 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
   1.115 + */
   1.116 +#include "AST.h"
   1.117 +#include "ConstantEvaluator.h"
   1.118 +#include "../Heap-inl.h"
   1.119 +#include "../Handles-inl.h"
   1.120 +
   1.121 +using namespace ke;
   1.122 +
   1.123 +static inline bool
   1.124 +EvaluateBinaryOperation(TokenKind kind, const BoxedPrimitive &left, const BoxedPrimitive &right,
   1.125 +                        BoxedPrimitive *out)
   1.126 +{
   1.127 +    switch (kind) {
   1.128 +      case TOK_PLUS:
   1.129 +        *out = BoxedPrimitive::Int(left.toInt() + right.toInt());
   1.130 +        return true;
   1.131 +
   1.132 +      case TOK_SHL:
   1.133 +        *out = BoxedPrimitive::Int(left.toInt() << right.toInt());
   1.134 +        return true;
   1.135 +
   1.136 +      default:
   1.137 +        return false;
   1.138 +    }
   1.139 +}
   1.140 +
   1.141 +static inline bool
   1.142 +EvaluateUnaryOperation(TokenKind kind, const BoxedPrimitive &inner, BoxedPrimitive *out)
   1.143 +{
   1.144 +  switch (kind) {
   1.145 +    case TOK_MINUS:
   1.146 +      *out = BoxedPrimitive::Int(-inner.toInt());
   1.147 +      return true;
   1.148 +
   1.149 +    default:
   1.150 +      return false;
   1.151 +  }
   1.152 +}
   1.153 +
   1.154 +bool
   1.155 +ke::EvaluateForConstant(Expression *expr, BoxedPrimitive *out)
   1.156 +{
   1.157 +    switch (expr->kind()) {
   1.158 +      case AstNode::kBinaryExpression:
   1.159 +      {
   1.160 +        BoxedPrimitive left;
   1.161 +        BoxedPrimitive right;
   1.162 +        BinaryExpression *b = expr->toBinaryExpression();
   1.163 +        if (!EvaluateForConstant(b->left(), &left) ||
   1.164 +            !EvaluateForConstant(b->right(), &right))
   1.165 +        {
   1.166 +            return false;
   1.167 +        }
   1.168 +        return EvaluateBinaryOperation(b->token(), left, right, out);
   1.169 +      }
   1.170 +
   1.171 +      case AstNode::kUnaryExpression:
   1.172 +      {
   1.173 +        BoxedPrimitive inner;
   1.174 +        UnaryExpression *u = expr->toUnaryExpression();
   1.175 +        if (!EvaluateForConstant(u->expression(), &inner))
   1.176 +            return false;
   1.177 +        return EvaluateUnaryOperation(u->token(), inner, out);
   1.178 +      }
   1.179 +
   1.180 +      case AstNode::kBooleanLiteral:
   1.181 +        *out = BoxedPrimitive::Int(expr->toBooleanLiteral()->token() == TOK_TRUE ? 1 : 0);
   1.182 +        return true;
   1.183 +
   1.184 +      case AstNode::kIntegerLiteral:
   1.185 +        *out = BoxedPrimitive::Int(expr->toIntegerLiteral()->value());
   1.186 +        return true;
   1.187 +
   1.188 +      default:
   1.189 +        return false;
   1.190 +    }
   1.191 +}