00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "tree.h"
00025 #include <cstdlib>
00026
00027
00028 template<typename Derived, typename Base>
00029 static inline Derived polymorphic_downcast(Base base)
00030 {
00031 Derived derived = dynamic_cast<Derived>(base);
00032 if (!derived)
00033 abort();
00034 return derived;
00035 }
00036
00037 namespace Aseba
00038 {
00041
00043 Node::~Node()
00044 {
00045
00046 for (size_t i = 0; i < children.size(); i++)
00047 if (children[i])
00048 delete children[i];
00049 }
00050
00052 EventDeclNode::EventDeclNode(const SourcePos& sourcePos, unsigned eventId) :
00053 Node(sourcePos),
00054 eventId(eventId)
00055 {
00056
00057 }
00058
00060 SubDeclNode::SubDeclNode(const SourcePos& sourcePos, unsigned subroutineId) :
00061 Node(sourcePos),
00062 subroutineId(subroutineId)
00063 {
00064
00065 }
00066
00068 CallSubNode::CallSubNode(const SourcePos& sourcePos, unsigned subroutineId) :
00069 Node(sourcePos),
00070 subroutineId(subroutineId)
00071 {
00072
00073 }
00074
00076 BinaryArithmeticNode::BinaryArithmeticNode(const SourcePos& sourcePos, AsebaBinaryOperator op, Node *left, Node *right) :
00077 Node(sourcePos),
00078 op(op)
00079 {
00080 children.push_back(left);
00081 children.push_back(right);
00082 }
00083
00085 BinaryArithmeticNode *BinaryArithmeticNode::fromComparison(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right)
00086 {
00087 return new BinaryArithmeticNode(
00088 sourcePos,
00089 static_cast<AsebaBinaryOperator>((op - Compiler::Token::TOKEN_OP_EQUAL) + ASEBA_OP_EQUAL),
00090 left,
00091 right
00092 );
00093 }
00094
00096 BinaryArithmeticNode *BinaryArithmeticNode::fromShiftExpression(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_SHIFT_LEFT) + ASEBA_OP_SHIFT_LEFT),
00101 left,
00102 right
00103 );
00104 }
00105
00107 BinaryArithmeticNode *BinaryArithmeticNode::fromAddExpression(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_ADD) + ASEBA_OP_ADD),
00112 left,
00113 right
00114 );
00115 }
00116
00118 BinaryArithmeticNode *BinaryArithmeticNode::fromMultExpression(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_MULT) + ASEBA_OP_MULT),
00123 left,
00124 right
00125 );
00126 }
00127
00129 UnaryArithmeticNode::UnaryArithmeticNode(const SourcePos& sourcePos, AsebaUnaryOperator op, Node *child) :
00130 Node(sourcePos),
00131 op(op)
00132 {
00133 children.push_back(child);
00134 }
00135
00137 ArrayReadNode::ArrayReadNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::string &arrayName) :
00138 Node(sourcePos),
00139 arrayAddr(arrayAddr),
00140 arraySize(arraySize),
00141 arrayName(arrayName)
00142 {
00143
00144 }
00145
00147 ArrayWriteNode::ArrayWriteNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::string &arrayName) :
00148 Node(sourcePos),
00149 arrayAddr(arrayAddr),
00150 arraySize(arraySize),
00151 arrayName(arrayName)
00152 {
00153
00154 }
00155
00157 CallNode::CallNode(const SourcePos& sourcePos, unsigned funcId) :
00158 Node(sourcePos),
00159 funcId(funcId)
00160 {
00161
00162 }
00163
00166 };