$search
00001 /* 00002 Aseba - an event-based framework for distributed robot control 00003 Copyright (C) 2007--2012: 00004 Stephane Magnenat <stephane at magnenat dot net> 00005 (http://stephane.magnenat.net) 00006 and other contributors, see authors.txt for details 00007 00008 This program is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU Lesser General Public License as published 00010 by the Free Software Foundation, version 3 of the License. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #include "tree.h" 00022 #include <cstdlib> 00023 #include <iostream> 00024 00025 00026 namespace Aseba 00027 { 00030 00031 const AsebaBinaryOperator ArithmeticAssignmentNode::operatorMap[] = { 00032 ASEBA_OP_ADD, // TOKEN_OP_ADD_EQUAL 00033 ASEBA_OP_SUB, // TOKEN_OP_NEG_EQUAL 00034 ASEBA_OP_MULT, // TOKEN_OP_MULT_EQUAL 00035 ASEBA_OP_DIV, // TOKEN_OP_DIV_EQUAL 00036 ASEBA_OP_MOD, // TOKEN_OP_MOD_EQUAL 00037 ASEBA_OP_SHIFT_LEFT, // TOKEN_OP_SHIFT_LEFT_EQUAL 00038 ASEBA_OP_SHIFT_RIGHT, // TOKEN_OP_SHIFT_RIGHT_EQUAL 00039 ASEBA_OP_BIT_OR, // TOKEN_OP_BIT_OR_EQUAL 00040 ASEBA_OP_BIT_XOR, // TOKEN_OP_BIT_XOR_EQUAL 00041 ASEBA_OP_BIT_AND // TOKEN_OP_BIT_AND_EQUAL 00042 }; 00043 00045 Node::~Node() 00046 { 00047 // we assume that if children is 0, another node has taken ownership of it 00048 for (size_t i = 0; i < children.size(); i++) 00049 if (children[i]) 00050 delete children[i]; 00051 } 00052 00053 Node* Node::deepCopy() 00054 { 00055 Node* newCopy = shallowCopy(); 00056 for (size_t i = 0; i < children.size(); i++) 00057 if (children[i]) 00058 newCopy->children[i] = children[i]->deepCopy(); 00059 return newCopy; 00060 } 00061 00063 EventDeclNode::EventDeclNode(const SourcePos& sourcePos, unsigned eventId) : 00064 Node(sourcePos), 00065 eventId(eventId) 00066 { 00067 00068 } 00069 00071 SubDeclNode::SubDeclNode(const SourcePos& sourcePos, unsigned subroutineId) : 00072 Node(sourcePos), 00073 subroutineId(subroutineId) 00074 { 00075 00076 } 00077 00079 CallSubNode::CallSubNode(const SourcePos& sourcePos, unsigned subroutineId) : 00080 Node(sourcePos), 00081 subroutineId(subroutineId) 00082 { 00083 00084 } 00085 00087 BinaryArithmeticNode::BinaryArithmeticNode(const SourcePos& sourcePos, AsebaBinaryOperator op, Node *left, Node *right) : 00088 Node(sourcePos), 00089 op(op) 00090 { 00091 children.push_back(left); 00092 children.push_back(right); 00093 } 00094 00096 BinaryArithmeticNode *BinaryArithmeticNode::fromComparison(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right) 00097 { 00098 return new BinaryArithmeticNode( 00099 sourcePos, 00100 static_cast<AsebaBinaryOperator>((op - Compiler::Token::TOKEN_OP_EQUAL) + ASEBA_OP_EQUAL), 00101 left, 00102 right 00103 ); 00104 } 00105 00107 BinaryArithmeticNode *BinaryArithmeticNode::fromShiftExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right) 00108 { 00109 return new BinaryArithmeticNode( 00110 sourcePos, 00111 static_cast<AsebaBinaryOperator>((op - Compiler::Token::TOKEN_OP_SHIFT_LEFT) + ASEBA_OP_SHIFT_LEFT), 00112 left, 00113 right 00114 ); 00115 } 00116 00118 BinaryArithmeticNode *BinaryArithmeticNode::fromAddExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right) 00119 { 00120 return new BinaryArithmeticNode( 00121 sourcePos, 00122 static_cast<AsebaBinaryOperator>((op - Compiler::Token::TOKEN_OP_ADD) + ASEBA_OP_ADD), 00123 left, 00124 right 00125 ); 00126 } 00127 00129 BinaryArithmeticNode *BinaryArithmeticNode::fromMultExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right) 00130 { 00131 return new BinaryArithmeticNode( 00132 sourcePos, 00133 static_cast<AsebaBinaryOperator>((op - Compiler::Token::TOKEN_OP_MULT) + ASEBA_OP_MULT), 00134 left, 00135 right 00136 ); 00137 } 00138 00140 BinaryArithmeticNode *BinaryArithmeticNode::fromBinaryExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right) 00141 { 00142 return new BinaryArithmeticNode( 00143 sourcePos, 00144 static_cast<AsebaBinaryOperator>((op - Compiler::Token::TOKEN_OP_BIT_OR) + ASEBA_OP_BIT_OR), 00145 left, 00146 right 00147 ); 00148 } 00149 00151 UnaryArithmeticNode::UnaryArithmeticNode(const SourcePos& sourcePos, AsebaUnaryOperator op, Node *child) : 00152 Node(sourcePos), 00153 op(op) 00154 { 00155 children.push_back(child); 00156 } 00157 00159 ArrayReadNode::ArrayReadNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::wstring &arrayName) : 00160 Node(sourcePos), 00161 arrayAddr(arrayAddr), 00162 arraySize(arraySize), 00163 arrayName(arrayName) 00164 { 00165 00166 } 00167 00169 ArrayWriteNode::ArrayWriteNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::wstring &arrayName) : 00170 Node(sourcePos), 00171 arrayAddr(arrayAddr), 00172 arraySize(arraySize), 00173 arrayName(arrayName) 00174 { 00175 00176 } 00177 00179 MemoryVectorNode::MemoryVectorNode(const SourcePos &sourcePos, unsigned arrayAddr, unsigned arraySize, const std::wstring &arrayName) : 00180 AbstractTreeNode(sourcePos), 00181 arrayAddr(arrayAddr), 00182 arraySize(arraySize), 00183 arrayName(arrayName), 00184 write(false) 00185 { 00186 00187 } 00188 00190 CallNode::CallNode(const SourcePos& sourcePos, unsigned funcId) : 00191 Node(sourcePos), 00192 funcId(funcId) 00193 { 00194 00195 } 00196 00198 ArithmeticAssignmentNode::ArithmeticAssignmentNode(const SourcePos& sourcePos, AsebaBinaryOperator op, Node *left, Node *right) : 00199 AbstractTreeNode(sourcePos), 00200 op(op) 00201 { 00202 children.push_back(left); 00203 children.push_back(right); 00204 } 00205 00206 ArithmeticAssignmentNode* ArithmeticAssignmentNode::fromArithmeticAssignmentToken(const SourcePos& sourcePos, Compiler::Token::Type token, Node *left, Node *right) 00207 { 00208 return new ArithmeticAssignmentNode(sourcePos, getBinaryOperator(token), left, right); 00209 } 00210 00211 AsebaBinaryOperator ArithmeticAssignmentNode::getBinaryOperator(Compiler::Token::Type token) 00212 { 00213 return operatorMap[token - Compiler::Token::TOKEN_OP_ADD_EQUAL]; 00214 } 00215 00217 UnaryArithmeticAssignmentNode::UnaryArithmeticAssignmentNode(const SourcePos& sourcePos, Compiler::Token::Type token, Node *memory) : 00218 AbstractTreeNode(sourcePos) 00219 { 00220 switch (token) 00221 { 00222 case Compiler::Token::TOKEN_OP_PLUS_PLUS: 00223 arithmeticOp = ASEBA_OP_ADD; 00224 break; 00225 00226 case Compiler::Token::TOKEN_OP_MINUS_MINUS: 00227 arithmeticOp = ASEBA_OP_SUB; 00228 break; 00229 00230 default: 00231 throw TranslatableError(sourcePos, ERROR_UNARY_ARITH_BUILD_UNEXPECTED); 00232 break; 00233 } 00234 00235 children.push_back(memory); 00236 } 00237 00240 }; // Aseba