ThymioIntermediateRepresentation.h
Go to the documentation of this file.
00001 #ifndef THYMIO_INTERMEDIATE_REPRESENTATION_H
00002 #define THYMIO_INTERMEDIATE_REPRESENTATION_H
00003 
00004 #include <vector>
00005 #include <string>
00006 #include <map>
00007 #include <set>
00008 #include <iostream>
00009 
00010 namespace Aseba
00011 {
00012         using namespace std;
00013         
00014         enum ThymioIRButtonName 
00015         {
00016                 THYMIO_BUTTONS_IR = 0,
00017                 THYMIO_PROX_IR,
00018                 THYMIO_PROX_GROUND_IR,
00019                 THYMIO_TAP_IR,
00020                 THYMIO_CLAP_IR,
00021                 THYMIO_MOVE_IR,
00022                 THYMIO_COLOR_IR,
00023                 THYMIO_CIRCLE_IR,
00024                 THYMIO_SOUND_IR,
00025                 THYMIO_MEMORY_IR
00026         };
00027         
00028         enum ThymioIRErrorCode
00029         {
00030                 THYMIO_NO_ERROR = 0,
00031                 THYMIO_MISSING_EVENT,
00032                 THYMIO_MISSING_ACTION,
00033                 THYMIO_EVENT_MULTISET,
00034                 THYMIO_INVALID_CODE
00035         };
00036         
00037         enum ThymioIRErrorType
00038         {
00039                 THYMIO_NO_TYPE_ERROR = 0,
00040                 THYMIO_SYNTAX_ERROR,
00041                 THYMIO_TYPE_ERROR,
00042                 THYMIO_CODE_ERROR
00043         };
00044         
00045         class ThymioIRVisitor;
00046         
00047         class ThymioIRButton 
00048         {
00049         public:
00050                 ThymioIRButton(int size=0, ThymioIRButtonName n=THYMIO_BUTTONS_IR, int states=2);
00051                 ~ThymioIRButton();
00052 
00053                 void setClicked(int i, int status);
00054                 int isClicked(int i) const;
00055                 int getNumStates() const;
00056                 int size() const;
00057                 void setMemoryState(int s);
00058                 int getMemoryState() const;
00059 
00060                 ThymioIRButtonName getName() const;
00061                 void setBasename(wstring n);
00062                 wstring getBasename() const;
00063 
00064                 bool isEventButton() const;
00065                 bool isSet() const;
00066 
00067                 void accept(ThymioIRVisitor *visitor);
00068 
00069         private:
00070                 vector<int> buttons;
00071                 int memory;
00072                 int numStates;
00073                 ThymioIRButtonName name;
00074                 wstring basename;
00075         };
00076         
00077         class ThymioIRButtonSet
00078         {
00079         public:
00080                 ThymioIRButtonSet(ThymioIRButton *event=0, ThymioIRButton *action=0);
00081                 
00082                 void addEventButton(ThymioIRButton *event);
00083                 void addActionButton(ThymioIRButton *action);
00084                 
00085                 ThymioIRButton *getEventButton();
00086                 ThymioIRButton *getActionButton();
00087                 
00088                 bool hasEventButton() const;
00089                 bool hasActionButton() const;
00090         
00091                 void accept(ThymioIRVisitor *visitor);
00092         
00093         private:
00094                 ThymioIRButton *eventButton;
00095                 ThymioIRButton *actionButton;
00096         };
00097         
00098         class ThymioIRVisitor
00099         {
00100         public:
00101                 ThymioIRVisitor() : errorCode(THYMIO_NO_ERROR) {}
00102                 
00103                 virtual void visit(ThymioIRButton *button);
00104                 virtual void visit(ThymioIRButtonSet *buttonSet);
00105 
00106                 ThymioIRErrorCode getErrorCode() const;
00107                 bool isSuccessful() const;
00108 
00109         protected:
00110                 ThymioIRErrorCode errorCode;
00111                 wstring toWstring(int val);
00112         };
00113 
00114         class ThymioIRTypeChecker : public ThymioIRVisitor
00115         {
00116         public:
00117                 ThymioIRTypeChecker() : ThymioIRVisitor(), activeActionName(THYMIO_BUTTONS_IR) {}
00118                 ~ThymioIRTypeChecker();
00119                 
00120                 virtual void visit(ThymioIRButton *button);
00121                 virtual void visit(ThymioIRButtonSet *buttonSet);               
00122                 
00123                 void reset();
00124                 void clear();
00125                 
00126         private:
00127                 ThymioIRButtonName activeActionName;
00128                 
00129                 multimap<wstring, ThymioIRButton*> moveHash;
00130                 multimap<wstring, ThymioIRButton*> colorHash;
00131                 multimap<wstring, ThymioIRButton*> circleHash;
00132                 multimap<wstring, ThymioIRButton*> soundHash;
00133                 multimap<wstring, ThymioIRButton*> memoryHash;
00134 
00135                 set<ThymioIRButtonName> tapSeenActions;
00136                 set<ThymioIRButtonName> clapSeenActions;
00137         };
00138 
00139         class ThymioIRSyntaxChecker : public ThymioIRVisitor
00140         {
00141         public:
00142                 ThymioIRSyntaxChecker() : ThymioIRVisitor() {}
00143                 ~ThymioIRSyntaxChecker() {}
00144                 
00145                 virtual void visit(ThymioIRButton *button);
00146                 virtual void visit(ThymioIRButtonSet *buttonSet); 
00147         
00148                 void clear() {}
00149         };
00150 
00151         class ThymioIRCodeGenerator : public ThymioIRVisitor
00152         {
00153         public:
00154                 ThymioIRCodeGenerator();
00155                 ~ThymioIRCodeGenerator();
00156                 
00157                 virtual void visit(ThymioIRButton *button);
00158                 virtual void visit(ThymioIRButtonSet *buttonSet);
00159 
00160                 vector<wstring>::const_iterator beginCode() const { return generatedCode.begin(); }
00161                 vector<wstring>::const_iterator endCode() const { return generatedCode.end(); }
00162                 void reset();
00163                 void clear();
00164                 
00165                 int buttonToCode(int id) const;
00166                 
00167         private:
00168                 map<ThymioIRButtonName, pair<int, int> > editor;
00169 
00170                 vector<wstring> generatedCode;
00171                 vector<wstring> directions;
00172                 int currentBlock;
00173                 bool inIfBlock;
00174                 vector<int> buttonToCodeMap;
00175         };
00176 
00177         class ThymioCompiler 
00178         {
00179         public:
00180                 ThymioCompiler();
00181                 ~ThymioCompiler();
00182                 
00183                 void compile();
00184                 void generateCode();
00185 
00186                 void addButtonSet(ThymioIRButtonSet *set);
00187                 void insertButtonSet(int row, ThymioIRButtonSet *set);
00188                 void removeButtonSet(int row);
00189                 void replaceButtonSet(int row, ThymioIRButtonSet *set);
00190                 void swap(int row1, int row2);
00191                 int buttonToCode(int id) const;
00192 
00193                 ThymioIRErrorCode getErrorCode() const;
00194                 bool isSuccessful() const;
00195                 int getErrorLine() const;
00196 
00197                 vector<wstring>::const_iterator beginCode() const;
00198                 vector<wstring>::const_iterator endCode() const; 
00199 
00200                 void clear();
00201 
00202         private:
00203                 vector<ThymioIRButtonSet*> buttonSet;
00204                 ThymioIRTypeChecker   typeChecker;
00205                 ThymioIRSyntaxChecker syntaxChecker;
00206                 ThymioIRCodeGenerator codeGenerator;
00207                 
00208                 ThymioIRErrorType errorType;
00209 
00210                 int errorLine;
00211         };
00212 
00213 }; // Aseba
00214 
00215 #endif


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:39