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 "../utils/FormatableString.h" 00023 #include <ostream> 00024 00025 namespace Aseba 00026 { 00029 00030 std::wstring binaryOperatorToString(AsebaBinaryOperator op) 00031 { 00032 switch (op) 00033 { 00034 case ASEBA_OP_SHIFT_LEFT: return L"<<"; 00035 case ASEBA_OP_SHIFT_RIGHT: return L">>"; 00036 case ASEBA_OP_ADD: return L"+"; 00037 case ASEBA_OP_SUB: return L"-"; 00038 case ASEBA_OP_MULT: return L"*"; 00039 case ASEBA_OP_DIV: return L"/"; 00040 case ASEBA_OP_MOD: return L"modulo"; 00041 00042 case ASEBA_OP_BIT_OR: return L"binary or"; 00043 case ASEBA_OP_BIT_XOR: return L"binary xor"; 00044 case ASEBA_OP_BIT_AND: return L"binary and"; 00045 00046 case ASEBA_OP_EQUAL: return L"=="; 00047 case ASEBA_OP_NOT_EQUAL: return L"!="; 00048 case ASEBA_OP_BIGGER_THAN: return L">"; 00049 case ASEBA_OP_BIGGER_EQUAL_THAN: return L">="; 00050 case ASEBA_OP_SMALLER_THAN: return L"<"; 00051 case ASEBA_OP_SMALLER_EQUAL_THAN: return L"<="; 00052 00053 case ASEBA_OP_OR: return L"or"; 00054 case ASEBA_OP_AND: return L"and"; 00055 00056 default: return L"? (binary operator)"; 00057 } 00058 } 00059 00060 std::wstring unaryOperatorToString(AsebaUnaryOperator op) 00061 { 00062 switch (op) 00063 { 00064 case ASEBA_UNARY_OP_SUB: return L"unary -"; 00065 case ASEBA_UNARY_OP_ABS: return L"abs"; 00066 case ASEBA_UNARY_OP_BIT_NOT: return L"binary not"; 00067 case ASEBA_UNARY_OP_NOT: return L"not"; 00068 00069 default: return L"? (unary operator)"; 00070 } 00071 } 00072 00073 void Node::dump(std::wostream& dest, unsigned& indent) const 00074 { 00075 for (unsigned i = 0; i < indent; i++) 00076 dest << L" "; 00077 dest << toWString() << L"\n"; 00078 indent++; 00079 for (size_t i = 0; i < children.size(); i++) 00080 children[i]->dump(dest, indent); 00081 indent--; 00082 } 00083 00084 void TupleVectorNode::dump(std::wostream& dest, unsigned& indent) const 00085 { 00086 for (unsigned i = 0; i < indent; i++) 00087 dest << L" "; 00088 dest << toWString() << L"\n"; 00089 indent++; 00090 for (size_t i = 0; i < children.size(); i++) 00091 { 00092 for (unsigned j = 0; j < indent; j++) 00093 dest << L" "; 00094 dest << WFormatableString(L"[%0]:\n").arg(i); 00095 indent++; 00096 children[i]->dump(dest, indent); 00097 indent--; 00098 } 00099 indent--; 00100 } 00101 00102 std::wstring IfWhenNode::toWString() const 00103 { 00104 std::wstring s; 00105 if (edgeSensitive) 00106 s += L"When: "; 00107 else 00108 s += L"If: "; 00109 return s; 00110 } 00111 00112 std::wstring FoldedIfWhenNode::toWString() const 00113 { 00114 std::wstring s; 00115 if (edgeSensitive) 00116 s += L"Folded When: "; 00117 else 00118 s += L"Folded If: "; 00119 s += binaryOperatorToString(op); 00120 return s; 00121 } 00122 00123 std::wstring WhileNode::toWString() const 00124 { 00125 std::wstring s = L"While: "; 00126 return s; 00127 }; 00128 00129 std::wstring FoldedWhileNode::toWString() const 00130 { 00131 std::wstring s = L"Folded While: "; 00132 s += binaryOperatorToString(op); 00133 return s; 00134 }; 00135 00136 std::wstring EventDeclNode::toWString() const 00137 { 00138 if (eventId == ASEBA_EVENT_INIT) 00139 return L"ContextSwitcher: to init"; 00140 else 00141 return WFormatableString(L"ContextSwitcher: to event %0").arg(eventId); 00142 } 00143 00144 std::wstring EmitNode::toWString() const 00145 { 00146 std::wstring s = WFormatableString(L"Emit: %0 ").arg(eventId); 00147 if (arraySize) 00148 s += WFormatableString(L"addr %0 size %1 ").arg(arrayAddr).arg(arraySize); 00149 return s; 00150 } 00151 00152 std::wstring SubDeclNode::toWString() const 00153 { 00154 return WFormatableString(L"Sub: %0").arg(subroutineId); 00155 } 00156 00157 std::wstring CallSubNode::toWString() const 00158 { 00159 return WFormatableString(L"CallSub: %0").arg(subroutineId); 00160 } 00161 00162 std::wstring BinaryArithmeticNode::toWString() const 00163 { 00164 std::wstring s = L"BinaryArithmetic: "; 00165 s += binaryOperatorToString(op); 00166 return s; 00167 } 00168 00169 std::wstring UnaryArithmeticNode::toWString() const 00170 { 00171 std::wstring s = L"UnaryArithmetic: "; 00172 s += unaryOperatorToString(op); 00173 return s; 00174 } 00175 00176 std::wstring ImmediateNode::toWString() const 00177 { 00178 return WFormatableString(L"Immediate: %0").arg(value); 00179 } 00180 00181 std::wstring LoadNode::toWString() const 00182 { 00183 return WFormatableString(L"Load: addr %0").arg(varAddr); 00184 } 00185 00186 std::wstring StoreNode::toWString() const 00187 { 00188 return WFormatableString(L"Store: addr %0").arg(varAddr); 00189 } 00190 00191 std::wstring ArrayReadNode::toWString() const 00192 { 00193 return WFormatableString(L"ArrayRead: addr %0 size %1").arg(arrayAddr).arg(arraySize); 00194 } 00195 00196 std::wstring ArrayWriteNode::toWString() const 00197 { 00198 return WFormatableString(L"ArrayWrite: addr %0 size %1").arg(arrayAddr).arg(arraySize); 00199 } 00200 00201 std::wstring CallNode::toWString() const 00202 { 00203 std::wstring s = WFormatableString(L"Call: id %0").arg(funcId); 00204 for (unsigned i = 0; i < argumentsAddr.size(); i++) 00205 s += WFormatableString(L", arg %0 is addr %1").arg(i).arg(argumentsAddr[i]); 00206 return s; 00207 } 00208 00209 std::wstring TupleVectorNode::toWString() const 00210 { 00211 return WFormatableString(L"Tuple Vector: size %0").arg(getVectorSize()); 00212 } 00213 00214 std::wstring MemoryVectorNode::toWString() const 00215 { 00216 return WFormatableString(L"Memory Vector Access: addr %0 size %1 (var %2)").arg(arrayAddr).arg(arraySize).arg(arrayName); 00217 } 00218 00219 std::wstring ArithmeticAssignmentNode::toWString() const 00220 { 00221 std::wstring s = L"BinaryArithmeticAssign: "; 00222 s += binaryOperatorToString(op); 00223 return s; 00224 } 00225 00226 std::wstring UnaryArithmeticAssignmentNode::toWString() const 00227 { 00228 std::wstring s = L"UnaryArithmeticAssign: "; 00229 if (arithmeticOp == ASEBA_OP_ADD) 00230 s += L"++"; 00231 else if (arithmeticOp == ASEBA_OP_SUB) 00232 s += L"--"; 00233 else 00234 s += L"unknown"; 00235 return s; 00236 } 00237 00240 }; // Aseba