00001 /* 00002 Aseba - an event-based framework for distributed robot control 00003 Copyright (C) 2007--2009: 00004 Stephane Magnenat <stephane at magnenat dot net> 00005 (http://stephane.magnenat.net) 00006 and other contributors, see authors.txt for details 00007 Mobots group, Laboratory of Robotics Systems, EPFL, Lausanne 00008 00009 This program is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 any other version as decided by the two original authors 00013 Stephane Magnenat and Valentin Longchamp. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 */ 00023 00024 #include "tree.h" 00025 #include "../utils/FormatableString.h" 00026 #include <ostream> 00027 00028 namespace Aseba 00029 { 00032 00033 std::string binaryOperatorToString(AsebaBinaryOperator op) 00034 { 00035 switch (op) 00036 { 00037 case ASEBA_OP_SHIFT_LEFT: return "<<"; 00038 case ASEBA_OP_SHIFT_RIGHT: return ">>"; 00039 case ASEBA_OP_ADD: return "+"; 00040 case ASEBA_OP_SUB: return "-"; 00041 case ASEBA_OP_MULT: return "*"; 00042 case ASEBA_OP_DIV: return "/"; 00043 case ASEBA_OP_MOD: return "modulo"; 00044 00045 case ASEBA_OP_BIT_OR: return "binary or"; 00046 case ASEBA_OP_BIT_XOR: return "binary xor"; 00047 case ASEBA_OP_BIT_AND: return "binary and"; 00048 00049 case ASEBA_OP_EQUAL: return "=="; 00050 case ASEBA_OP_NOT_EQUAL: return "!="; 00051 case ASEBA_OP_BIGGER_THAN: return ">"; 00052 case ASEBA_OP_BIGGER_EQUAL_THAN: return ">="; 00053 case ASEBA_OP_SMALLER_THAN: return "<"; 00054 case ASEBA_OP_SMALLER_EQUAL_THAN: return "<="; 00055 00056 case ASEBA_OP_OR: return "or"; 00057 case ASEBA_OP_AND: return "and"; 00058 00059 default: return "? (binary operator)"; 00060 } 00061 } 00062 00063 std::string unaryOperatorToString(AsebaUnaryOperator op) 00064 { 00065 switch (op) 00066 { 00067 case ASEBA_UNARY_OP_SUB: return "unary -"; 00068 case ASEBA_UNARY_OP_ABS: return "abs"; 00069 case ASEBA_UNARY_OP_BIT_NOT: return "binary not"; 00070 case ASEBA_UNARY_OP_NOT: return "not"; 00071 00072 default: return "? (unary operator)"; 00073 } 00074 } 00075 00076 void Node::dump(std::ostream& dest, unsigned& indent) const 00077 { 00078 for (unsigned i = 0; i < indent; i++) 00079 dest << " "; 00080 dest << toString() << "\n"; 00081 indent++; 00082 for (size_t i = 0; i < children.size(); i++) 00083 children[i]->dump(dest, indent); 00084 indent--; 00085 } 00086 00087 std::string IfWhenNode::toString() const 00088 { 00089 std::string s; 00090 if (edgeSensitive) 00091 s += "When: "; 00092 else 00093 s += "If: "; 00094 return s; 00095 } 00096 00097 std::string FoldedIfWhenNode::toString() const 00098 { 00099 std::string s; 00100 if (edgeSensitive) 00101 s += "Folded When: "; 00102 else 00103 s += "Folded If: "; 00104 s += binaryOperatorToString(op); 00105 return s; 00106 } 00107 00108 std::string WhileNode::toString() const 00109 { 00110 std::string s = "While: "; 00111 return s; 00112 }; 00113 00114 std::string FoldedWhileNode::toString() const 00115 { 00116 std::string s = "Folded While: "; 00117 s += binaryOperatorToString(op); 00118 return s; 00119 }; 00120 00121 std::string EventDeclNode::toString() const 00122 { 00123 if (eventId == ASEBA_EVENT_INIT) 00124 return "ContextSwitcher: to init"; 00125 else 00126 return FormatableString("ContextSwitcher: to event %0").arg(eventId); 00127 } 00128 00129 std::string EmitNode::toString() const 00130 { 00131 std::string s = FormatableString("Emit: %0 ").arg(eventId); 00132 if (arraySize) 00133 s += FormatableString("addr %0 size %1 ").arg(arrayAddr).arg(arraySize); 00134 return s; 00135 } 00136 00137 std::string SubDeclNode::toString() const 00138 { 00139 return FormatableString("Sub: %0").arg(subroutineId); 00140 } 00141 00142 std::string CallSubNode::toString() const 00143 { 00144 return FormatableString("CallSub: %0").arg(subroutineId); 00145 } 00146 00147 std::string BinaryArithmeticNode::toString() const 00148 { 00149 std::string s = "BinaryArithmetic: "; 00150 s += binaryOperatorToString(op); 00151 return s; 00152 } 00153 00154 std::string UnaryArithmeticNode::toString() const 00155 { 00156 std::string s = "UnaryArithmetic: "; 00157 s += unaryOperatorToString(op); 00158 return s; 00159 } 00160 00161 std::string ImmediateNode::toString() const 00162 { 00163 return FormatableString("Immediate: %0").arg(value); 00164 } 00165 00166 std::string LoadNode::toString() const 00167 { 00168 return FormatableString("Load: addr %0").arg(varAddr); 00169 } 00170 00171 std::string StoreNode::toString() const 00172 { 00173 return FormatableString("Store: addr %0").arg(varAddr); 00174 } 00175 00176 std::string ArrayReadNode::toString() const 00177 { 00178 return FormatableString("ArrayRead: addr %0 size %1").arg(arrayAddr).arg(arraySize); 00179 } 00180 00181 std::string ArrayWriteNode::toString() const 00182 { 00183 return FormatableString("ArrayWrite: addr %0 size %1").arg(arrayAddr).arg(arraySize); 00184 } 00185 00186 std::string CallNode::toString() const 00187 { 00188 std::string s = FormatableString("Call: id %0").arg(funcId); 00189 for (unsigned i = 0; i < argumentsAddr.size(); i++) 00190 s += FormatableString(", arg %0 is addr %1").arg(i).arg(argumentsAddr[i]); 00191 return s; 00192 } 00193 00196 }; // Aseba