00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef INSTRUCTION_HPP
00021 #define INSTRUCTION_HPP
00022
00023 #include <list>
00024 #include <string>
00025 #include <Expression.hpp>
00026 #include <BooleanExpression.hpp>
00027
00028 #include <SceneExpression.hpp>
00029 #include <Variable.hpp>
00030 #include <OStreamExpression.hpp>
00031 #include <IStreamExpression.hpp>
00032
00033 #include <FileDescriptor.hpp>
00034 #include <FieldExpressionList.hpp>
00035
00036 #include <ErrorHandler.hpp>
00037
00046 class Instruction
00047 {
00048 public:
00049 enum Type {
00050 list,
00051 ifStatement,
00052 doWhileStatement,
00053 whileStatement,
00054 forStatement,
00055 exitStatement,
00056 coarseMesh,
00057
00058 declaration,
00059 affectation,
00060 increment,
00061 input,
00062 decrement,
00063 evaluation,
00064
00065 blockBegin,
00066 blockEnd,
00067
00068 output,
00069 Using,
00070 exec,
00071 cat,
00072 plot,
00073 save,
00074 none
00075 };
00076
00077 protected:
00078 const Instruction::Type __type;
00079
00080 public:
00081 const Instruction::Type& type() const
00082 {
00083 return __type;
00084 }
00085
00086 virtual void execute() = 0;
00087
00088 Instruction(const Instruction::Type& t)
00089 : __type(t)
00090 {
00091 ;
00092 }
00093
00094 Instruction(const Instruction& I)
00095 : __type(I.__type)
00096 {
00097 ;
00098 }
00099
00100 virtual ~Instruction()
00101 {
00102 ;
00103 }
00104 };
00105
00106 class InstructionList
00107 : public Instruction
00108 {
00109 public:
00110 typedef std::list<ReferenceCounting<Instruction> > InstructionSet;
00111 InstructionSet __instructions;
00112
00113 public:
00114 void execute()
00115 {
00116 for(InstructionSet::iterator i = __instructions.begin();
00117 i != __instructions.end(); ++i) {
00118 (*i)->execute();
00119 }
00120 }
00121
00122 void add(ReferenceCounting<Instruction> i)
00123 {
00124 __instructions.push_back(i);
00125 }
00126
00127 InstructionList()
00128 : Instruction(Instruction::list)
00129 {
00130 ;
00131 }
00132
00133 InstructionList(const InstructionList& I)
00134 : Instruction(I),
00135 __instructions(I.__instructions)
00136 {
00137 ;
00138 }
00139
00140 ~InstructionList()
00141 {
00142 ;
00143 }
00144 };
00145
00146 class InstructionIfStatement
00147 : public Instruction
00148 {
00149 public:
00150 ReferenceCounting<BooleanExpression> __booleanExpression;
00151 ReferenceCounting<Instruction> __firstStatement;
00152 ReferenceCounting<Instruction> __secondStatement;
00153
00154 public:
00155 void execute()
00156 {
00157 (*__booleanExpression).execute();
00158 if ((*__booleanExpression).boolValue()) {
00159 (*__firstStatement).execute();
00160 } else {
00161 (*__secondStatement).execute();
00162 }
00163 }
00164
00165 InstructionIfStatement(ReferenceCounting<BooleanExpression> b,
00166 ReferenceCounting<Instruction> first,
00167 ReferenceCounting<Instruction> second)
00168 : Instruction(Instruction::ifStatement),
00169 __booleanExpression(b),
00170 __firstStatement(first),
00171 __secondStatement(second)
00172 {
00173 ;
00174 }
00175
00176 InstructionIfStatement(const InstructionIfStatement& I)
00177 : Instruction(I),
00178 __booleanExpression(I.__booleanExpression),
00179 __firstStatement(I.__firstStatement),
00180 __secondStatement(I.__secondStatement)
00181 {
00182 ;
00183 }
00184
00185 ~InstructionIfStatement()
00186 {
00187 ;
00188 }
00189 };
00190
00191 class InstructionDoWhileStatement
00192 : public Instruction
00193 {
00194 public:
00195 ReferenceCounting<Instruction> __statement;
00196 ReferenceCounting<BooleanExpression> __booleanExpression;
00197
00198 public:
00199 void execute()
00200 {
00201 do {
00202 __statement->execute();
00203 __booleanExpression->execute();
00204 } while (__booleanExpression->boolValue());
00205 }
00206
00207 InstructionDoWhileStatement(ReferenceCounting<Instruction> statement,
00208 ReferenceCounting<BooleanExpression> b)
00209 : Instruction(Instruction::doWhileStatement),
00210 __statement(statement),
00211 __booleanExpression(b)
00212 {
00213 ;
00214 }
00215
00216 InstructionDoWhileStatement(const InstructionDoWhileStatement& I)
00217 : Instruction(I),
00218 __statement(I.__statement),
00219 __booleanExpression(I.__booleanExpression)
00220 {
00221 ;
00222 }
00223
00224 ~InstructionDoWhileStatement()
00225 {
00226 ;
00227 }
00228 };
00229
00230 class InstructionForStatement
00231 : public Instruction
00232 {
00233 public:
00234 ReferenceCounting<Instruction> __instruction1;
00235 ReferenceCounting<BooleanExpression> __booleanExpression;
00236 ReferenceCounting<Instruction> __instruction2;
00237 ReferenceCounting<Instruction> __statement;
00238
00239 public:
00240 void execute()
00241 {
00242 for (__instruction1->execute();
00243 __booleanExpression->execute(),
00244 __booleanExpression->boolValue();
00245 __instruction2->execute())
00246 {
00247 __statement->execute();
00248 }
00249 }
00250
00251 InstructionForStatement(ReferenceCounting<Instruction> a,
00252 ReferenceCounting<BooleanExpression> b,
00253 ReferenceCounting<Instruction> c,
00254 ReferenceCounting<Instruction> statement)
00255 : Instruction(Instruction::forStatement),
00256 __instruction1(a),
00257 __booleanExpression(b),
00258 __instruction2(c),
00259 __statement(statement)
00260 {
00261 ;
00262 }
00263
00264 InstructionForStatement(const InstructionForStatement& I)
00265 : Instruction(I),
00266 __instruction1(I.__instruction1),
00267 __booleanExpression(I.__booleanExpression),
00268 __instruction2(I.__instruction2),
00269 __statement(I.__statement)
00270 {
00271 ;
00272 }
00273
00274 ~InstructionForStatement()
00275 {
00276 ;
00277 }
00278 };
00279
00280 class InstructionWhileStatement
00281 : public Instruction
00282 {
00283 public:
00284 ReferenceCounting<Instruction> __statement;
00285 ReferenceCounting<BooleanExpression> __booleanExpression;
00286
00287 public:
00288 void execute()
00289 {
00290 __booleanExpression->execute();
00291 while (__booleanExpression->boolValue()) {
00292 __statement->execute();
00293 __booleanExpression->execute();
00294 }
00295 }
00296
00297 InstructionWhileStatement(ReferenceCounting<BooleanExpression> b,
00298 ReferenceCounting<Instruction> statement)
00299 : Instruction(Instruction::whileStatement),
00300 __statement(statement),
00301 __booleanExpression(b)
00302 {
00303 ;
00304 }
00305
00306 InstructionWhileStatement(const InstructionWhileStatement& I)
00307 : Instruction(I),
00308 __statement(I.__statement),
00309 __booleanExpression(I.__booleanExpression)
00310 {
00311 ;
00312 }
00313
00314 ~InstructionWhileStatement()
00315 {
00316 ;
00317 }
00318 };
00319
00320 class InstructionOutput
00321 : public Instruction
00322 {
00323 private:
00324 ReferenceCounting<OStreamExpression> __ostream;
00325 ReferenceCounting<Expression> __expression;
00326
00327 public:
00328 void execute()
00329 {
00330 __ostream->execute();
00331 __expression->execute();
00332 *__ostream << *__expression;
00333 }
00334
00335 InstructionOutput(ReferenceCounting<OStreamExpression> o,
00336 ReferenceCounting<Expression> e)
00337 : Instruction(Instruction::output),
00338 __ostream(o),
00339 __expression(e)
00340 {
00341 ;
00342 }
00343
00344 InstructionOutput(const InstructionOutput& I)
00345 : Instruction(I),
00346 __ostream(I.__ostream),
00347 __expression(I.__expression)
00348 {
00349 ;
00350 }
00351
00352 ~InstructionOutput()
00353 {
00354 ;
00355 }
00356 };
00357
00358 class InstructionInput
00359 : public Instruction
00360 {
00361 private:
00362 ReferenceCounting<IStreamExpression> __istream;
00363 ReferenceCounting<Expression> __expression;
00364
00365 public:
00366 void execute()
00367 {
00368 __istream->execute();
00369 __expression->execute();
00370 *__istream >> *__expression;
00371 }
00372
00373 InstructionInput(ReferenceCounting<IStreamExpression> i,
00374 ReferenceCounting<Expression> e)
00375 : Instruction(Instruction::input),
00376 __istream(i),
00377 __expression(e)
00378 {
00379 ;
00380 }
00381
00382 InstructionInput(const InstructionInput& I)
00383 : Instruction(I),
00384 __istream(I.__istream),
00385 __expression(I.__expression)
00386 {
00387 ;
00388 }
00389
00390 ~InstructionInput()
00391 {
00392 ;
00393 }
00394 };
00395
00405 class InstructionSave
00406 : public Instruction
00407 {
00408 protected:
00409 ReferenceCounting<FileDescriptor> __fileDescriptor;
00410 ReferenceCounting<StringExpression> __fileName;
00411 ReferenceCounting<MeshExpression> __mesh;
00412
00413 public:
00414
00415 InstructionSave(ReferenceCounting<FileDescriptor> descriptor,
00416 ReferenceCounting<StringExpression> fileName,
00417 ReferenceCounting<MeshExpression> mesh);
00418
00419 InstructionSave(const InstructionSave& I);
00420
00421 ~InstructionSave();
00422 };
00423
00431 class InstructionExec
00432 : public Instruction
00433 {
00434 protected:
00435 ReferenceCounting<StringExpression> __command;
00436
00437 public:
00438 void execute();
00439
00440 InstructionExec(ReferenceCounting<StringExpression> command);
00441
00442 InstructionExec(const InstructionExec& I);
00443
00444 ~InstructionExec();
00445 };
00446
00454 class InstructionCat
00455 : public Instruction
00456 {
00457 protected:
00458 ReferenceCounting<StringExpression> __filename;
00459
00460 public:
00461 void execute();
00462
00463 InstructionCat(ReferenceCounting<StringExpression> command);
00464
00465 InstructionCat(const InstructionCat& I);
00466
00467 ~InstructionCat();
00468 };
00469
00479 class InstructionSaveMesh
00480 : public InstructionSave
00481 {
00482 public:
00483 void execute();
00484
00485 InstructionSaveMesh(ReferenceCounting<FileDescriptor> descriptor,
00486 ReferenceCounting<StringExpression> fileName,
00487 ReferenceCounting<MeshExpression> mesh);
00488
00489 InstructionSaveMesh(const InstructionSaveMesh& I);
00490
00491 ~InstructionSaveMesh();
00492 };
00493
00501 class InstructionSaveFieldList
00502 : public InstructionSave
00503 {
00504 private:
00505 ReferenceCounting<FieldExpressionList> __fieldList;
00506
00507 public:
00508 void execute();
00509
00510 InstructionSaveFieldList(ReferenceCounting<FileDescriptor> descriptor,
00511 ReferenceCounting<StringExpression> fileName,
00512 ReferenceCounting<FieldExpressionList> fieldList,
00513 ReferenceCounting<MeshExpression> mesh);
00514
00515 InstructionSaveFieldList(const InstructionSaveFieldList& I);
00516
00517 ~InstructionSaveFieldList();
00518 };
00519
00520
00529 class InstructionPlot
00530 : public Instruction
00531 {
00532 private:
00533 ReferenceCounting<MeshExpression> __mesh;
00534 ReferenceCounting<FunctionExpression> __f;
00535
00536 public:
00541 void execute();
00542
00548 InstructionPlot(ReferenceCounting<MeshExpression> mesh,
00549 ReferenceCounting<FunctionExpression> f = 0);
00550
00555 InstructionPlot(const InstructionPlot& __instruction);
00556
00561 ~InstructionPlot();
00562 };
00563
00564
00572 class InstructionNone
00573 : public Instruction
00574 {
00575 public:
00576 void execute()
00577 {
00578 ;
00579 }
00580
00581 InstructionNone()
00582 : Instruction(Instruction::none)
00583 {
00584 ;
00585 }
00586
00587 InstructionNone(const InstructionNone& I)
00588 : Instruction(I)
00589 {
00590 ;
00591 }
00592
00593 ~InstructionNone()
00594 {
00595 ;
00596 }
00597 };
00598
00599 #include <VariableRepository.hpp>
00600
00601 template <typename DataType, typename VarType>
00602 class InstructionDeclaration
00603 : public Instruction
00604 {
00605 private:
00606 const std::string __name;
00607 ReferenceCounting<DataType> __expression;
00608
00609 public:
00610 void execute()
00611 {
00612 ReferenceCounting<Variable> a
00613 = new VarType(__name, __expression);
00614 VariableRepository::instance().add(a);
00615
00616 ReferenceCounting<VarType> V
00617 = VariableRepository::instance().findVariable<VarType>(__name);
00618
00619 __expression->execute();
00620 (*V) = __expression;
00621 }
00622
00623 InstructionDeclaration(const std::string name,
00624 ReferenceCounting<DataType> e)
00625 : Instruction(Instruction::declaration),
00626 __name(name),
00627 __expression(e)
00628 {
00629 ;
00630 }
00631
00632 ~InstructionDeclaration()
00633 {
00634 ;
00635 }
00636 };
00637
00638 template<typename DataType,
00639 typename VarType>
00640 class InstructionAffectation
00641 : public Instruction
00642 {
00643 private:
00644 std::string __variableName;
00645 ReferenceCounting<DataType> __expression;
00646
00647 public:
00648 void execute()
00649 {
00650 __expression->execute();
00651
00652 VarType* V = VariableRepository::instance().findVariable<VarType>(__variableName);
00653 (*V) = __expression;
00654 }
00655
00656 InstructionAffectation(const std::string& variableName,
00657 ReferenceCounting<DataType> e)
00658 : Instruction(Instruction::affectation),
00659 __variableName(variableName),
00660 __expression(e)
00661 {
00662 ;
00663 }
00664
00665 InstructionAffectation(const InstructionAffectation<DataType, VarType>& I)
00666 : Instruction(I),
00667 __variableName(I.__variableName),
00668 __expression(I.__expression)
00669 {
00670 ;
00671 }
00672
00673 ~InstructionAffectation()
00674 {
00675 ;
00676 }
00677 };
00678
00679 class InstructionRealExpressionIncrement
00680 : public Instruction
00681 {
00682 private:
00683 std::string __variableName;
00684
00685 public:
00686 void execute()
00687 {
00688 RealVariable* realVariable
00689 = VariableRepository::instance().findVariable<RealVariable>(__variableName);;
00690
00691 const real_t r = realVariable->expression()->realValue();
00692 (*realVariable) = new RealExpressionValue(r+1);
00693 }
00694
00695 InstructionRealExpressionIncrement(const std::string& variableName)
00696 : Instruction(Instruction::increment),
00697 __variableName(variableName)
00698 {
00699 ;
00700 }
00701
00702 InstructionRealExpressionIncrement(const InstructionRealExpressionIncrement& I)
00703 : Instruction(I),
00704 __variableName(I.__variableName)
00705 {
00706 ;
00707 }
00708
00709 ~InstructionRealExpressionIncrement()
00710 {
00711 ;
00712 }
00713 };
00714
00715 class InstructionRealExpressionDecrement
00716 : public Instruction
00717 {
00718 private:
00719 std::string __variableName;
00720
00721 public:
00722 void execute()
00723 {
00724 RealVariable* realVariable = VariableRepository::instance().findVariable<RealVariable>(__variableName);
00725
00726 const real_t r = realVariable->expression()->realValue();
00727 (*realVariable) = new RealExpressionValue(r-1);
00728 }
00729
00730 InstructionRealExpressionDecrement(const std::string& variableName)
00731 : Instruction(Instruction::decrement),
00732 __variableName(variableName)
00733 {
00734 ;
00735 }
00736
00737 InstructionRealExpressionDecrement(const InstructionRealExpressionDecrement& I)
00738 : Instruction(I),
00739 __variableName(I.__variableName)
00740 {
00741 ;
00742 }
00743
00744 ~InstructionRealExpressionDecrement()
00745 {
00746 ;
00747 }
00748 };
00749
00750 class FunctionExpression;
00751 class FunctionExpressionFEM;
00752 template<>
00753 class InstructionAffectation<FunctionExpression, FunctionVariable>
00754 : public Instruction
00755 {
00756 private:
00757 std::string __variableName;
00758 ReferenceCounting<FunctionExpression> __expression;
00759
00760 public:
00761 void execute();
00762
00763 InstructionAffectation(const std::string& variableName,
00764 ReferenceCounting<FunctionExpression> e)
00765 : Instruction(Instruction::affectation),
00766 __variableName(variableName),
00767 __expression(e)
00768 {
00769 ;
00770 }
00771
00772 InstructionAffectation(const InstructionAffectation<FunctionExpression,
00773 FunctionVariable>& I)
00774 : Instruction(I),
00775 __variableName(I.__variableName),
00776 __expression(I.__expression)
00777 {
00778 ;
00779 }
00780
00781 ~InstructionAffectation()
00782 {
00783 ;
00784 }
00785 };
00786
00787
00788 template<typename ExpType>
00789 class InstructionEvaluation
00790 : public Instruction
00791 {
00792 private:
00793 ReferenceCounting<ExpType> __expression;
00794
00795 public:
00796 void execute()
00797 {
00798 __expression->execute();
00799 }
00800
00801 InstructionEvaluation(ReferenceCounting<ExpType> e)
00802 : Instruction(Instruction::evaluation),
00803 __expression(e)
00804 {
00805 ;
00806 }
00807
00808 InstructionEvaluation(const InstructionEvaluation<ExpType>& I)
00809 : Instruction(I),
00810 __expression(I.__expression)
00811 {
00812 ;
00813 }
00814
00815 ~InstructionEvaluation()
00816 {
00817 ;
00818 }
00819 };
00820
00821
00822 class InstructionUsingScene
00823 : public Instruction
00824 {
00825 private:
00826 ReferenceCounting<SceneExpression> __sceneExpression;
00827
00828 public:
00829 void execute();
00830
00831 InstructionUsingScene(ReferenceCounting<SceneExpression> e);
00832
00833 InstructionUsingScene(const InstructionUsingScene& I);
00834
00835 ~InstructionUsingScene();
00836 };
00837
00838 class InstructionExit
00839 : public Instruction
00840 {
00841 private:
00842 ReferenceCounting<RealExpression> __realExpression;
00843
00844 public:
00845 void execute()
00846 {
00847 if (__realExpression != 0) {
00848 __realExpression->execute();
00849 throw ErrorHandler(__FILE__,__LINE__,
00850 "exit called (argument ignored)",
00851 ErrorHandler::asked);
00852 }
00853 throw ErrorHandler(__FILE__,__LINE__,
00854 "exit called",
00855 ErrorHandler::asked);
00856 }
00857
00858 InstructionExit()
00859 : Instruction(Instruction::exitStatement),
00860 __realExpression(0)
00861 {
00862 ;
00863 }
00864
00865 InstructionExit(ReferenceCounting<RealExpression> e)
00866 : Instruction(Instruction::exitStatement),
00867 __realExpression(e)
00868 {
00869 ;
00870 }
00871
00872 InstructionExit(const InstructionExit& I)
00873 : Instruction(I),
00874 __realExpression(I.__realExpression)
00875 {
00876 ;
00877 }
00878
00879 ~InstructionExit()
00880 {
00881 ;
00882 }
00883 };
00884
00885
00886 class InstructionCoarseMesh
00887 : public Instruction
00888 {
00889 private:
00890 bool __coarseMesh;
00891
00892 public:
00893 void execute();
00894
00895 InstructionCoarseMesh(const bool& fm)
00896 : Instruction(Instruction::coarseMesh),
00897 __coarseMesh(fm)
00898 {
00899 ;
00900 }
00901
00902 InstructionCoarseMesh(const InstructionCoarseMesh& I)
00903 : Instruction(I),
00904 __coarseMesh(I.__coarseMesh)
00905 {
00906 ;
00907 }
00908
00909 ~InstructionCoarseMesh()
00910 {
00911 ;
00912 }
00913 };
00914
00915 class InstructionBlockBegin
00916 : public Instruction
00917 {
00918 public:
00919 void execute();
00920
00921 InstructionBlockBegin()
00922 : Instruction(Instruction::blockBegin)
00923 {
00924 ;
00925 }
00926
00927 InstructionBlockBegin(const InstructionBlockBegin& I)
00928 : Instruction(I)
00929 {
00930 ;
00931 }
00932
00933 ~InstructionBlockBegin()
00934 {
00935 ;
00936 }
00937 };
00938
00939 class InstructionBlockEnd
00940 : public Instruction
00941 {
00942 public:
00943 void execute();
00944
00945 InstructionBlockEnd()
00946 : Instruction(Instruction::blockEnd)
00947 {
00948 ;
00949 }
00950
00951 InstructionBlockEnd(const InstructionBlockEnd& I)
00952 : Instruction(I)
00953 {
00954 ;
00955 }
00956
00957 ~InstructionBlockEnd()
00958 {
00959 ;
00960 }
00961 };
00962 #endif // INSTRUCTION_HPP