/***** * * Michael Riff * Object software version 1.0 Okto 2013. * * Header Datei zur Definition der Klassen zur Speicherung von Berechnungen. * * Version 1.6: 06 June 2016 * implemented handling of additional routines that can be called out of the text code. As example * we used the following: print() and print("constant string") that print out * the value of a variable or a string. *****/ #ifndef MAC_UI #include #include #include #ifndef Mac typedef enum { false = 0, true = 1, FALSE = 0, TRUE = 1 } bool; #else #include #define bool Boolean #endif #else #include #include #endif #define PI 3.1415926535897932384650 #define EXP 2.718281828459045 int InitCalc(void); /* Top level general class to describe an operand of a function or operator for arithmetic computations. */ //virtual class GeneralOperand { class GeneralOperand { public : GeneralOperand(); ~GeneralOperand(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* In case of parsing error, generate object that returns NAN value */ class ErrorOperand : public GeneralOperand { private: public : ErrorOperand(); ~ErrorOperand(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For constant arguments */ class ConstOperand : public GeneralOperand { private: // Constant value of argument // const float itsValue; double itsValue; public : ConstOperand(double value); ~ConstOperand(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* This class is used to read a value of the variable */ #define MAX_PARAM 10 class ParamOperand : public GeneralOperand { private: // Index of variable in value table int Position; public : ParamOperand(int itsPosition); ~ParamOperand(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For unary operators as +x or -x */ typedef enum {uPlus, uMoins} UnarOperType; class UnaryOperat : public GeneralOperand { private : // Pointer to argument GeneralOperand *Argument; // Pointer to operator (only library methematical functions) // float operator Operation(float); Pb : ou faire avec un typedef UnarOperType Operation; public : // UnaryOperat(float operator Signe(float), GeneralOperand *itsArgument); UnaryOperat(UnarOperType Signe, GeneralOperand *itsArgument); ~UnaryOperat(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For functions calls. Some similarity with binary operators */ extern "C" { typedef double (*refFuncType)(double); // For CW //typedef double (refFuncType)(double); } class FunctOperat : public GeneralOperand { private: // Pointer to argument GeneralOperand *Argument; // Pointer to function (only library methematical functions) // double (*refFunc)(double); Pb pointeur sur une fonction de la lib math écrite en C refFuncType refFunc; public : // FunctOperat((float (*refFunc)(float)) function, GeneralOperand *itsArgument) { // FunctOperat(double (*function)(double), GeneralOperand *itsArgument); FunctOperat(refFuncType function, GeneralOperand *itsArgument); ~FunctOperat(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For binary operators as + - * or / */ typedef enum { bPlus, bMoins, bMult, bDiv, bPow } BinOperType; class BinaryOperat : public GeneralOperand { private : // Pointer to arguments GeneralOperand *LeftArgument; GeneralOperand *RightArgument; // Pointer to operator (only library methematical + - * / =) // (float) operator Operation(float,float); Pb ou faire avec typedef BinOperType Operation; public : // BinaryOperat((float) operator Action(float,float), GeneralOperand *itsLeftArgument); BinaryOperat(BinOperType Action, GeneralOperand *itsLeftArgument); ~BinaryOperat(); virtual double Calculate(); void SetRight(GeneralOperand *itsRightArgument); GeneralOperand *getRightArgument(void); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* To handle variables */ // Table to store the values of variables typedef struct { char VarName[10]; double ActValue; } VarTableType; /* VarTableType VarTable[20]; int NumVars = 0; */ /* This class is used to assign a value to the variable */ class VarAssign : public GeneralOperand { private: // Index of variable in value table int Position; // Reference to arithmetic expression determining its value GeneralOperand *itsExpression; public : VarAssign(int itsPosition); ~VarAssign(); void SetArithmetic(GeneralOperand *itsArithmetic); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* This class is used to read a value of the variable */ class VarOperand : public GeneralOperand { private: // Index of variable in value table int Position; public : VarOperand(int itsPosition); ~VarOperand(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* Top level general class to describe an operand of a function or operator for boolean computations. */ //virtual class GeneralBoolOperand { class GeneralBoolOperand { public : GeneralBoolOperand(); ~GeneralBoolOperand(); #ifndef MAC_UI virtual bool Calculate(); // bool causes :virtual storage class is illegal in this context #else virtual Boolean Calculate(); #endif #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* In case of parsing error, generate object that returns NAN value */ class ErrorBoolOperand : public GeneralBoolOperand { private: public : ErrorBoolOperand(); ~ErrorBoolOperand(); #ifndef MAC_UI virtual bool Calculate(); #else virtual Boolean Calculate(); #endif #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For unary bool operator not */ class UnaryBoolOperat : public GeneralBoolOperand { private: // Reference to its associated expression GeneralBoolOperand *Argument; public : UnaryBoolOperat(GeneralBoolOperand *itsArgument); ~UnaryBoolOperat(); #ifndef MAC_UI virtual bool Calculate(); #else virtual Boolean Calculate(); #endif #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For binary bool operators and or xor */ typedef enum { _or, _xor, _and } BinBoolOperType; class BinaryBoolOperat : public GeneralBoolOperand { private: // References to the arguments GeneralBoolOperand *LeftArgument; GeneralBoolOperand *RightArgument; // Pointer to operator (only library methematical and or xor) // (bool) operator Operation(bool,bool); Pb ou faire avec un typedef BinBoolOperType Operation; public : // BinaryBoolOperat((bool) operator itsComb(bool,bool), GeneralBoolOperand *itsLeftArgument); BinaryBoolOperat(BinBoolOperType itsComb, GeneralBoolOperand *itsLeftArgument); ~BinaryBoolOperat(); #ifndef MAC_UI virtual bool Calculate(); #else virtual Boolean Calculate(); #endif virtual void SetRight(GeneralBoolOperand *itsRightArgument); virtual GeneralBoolOperand* GetRightArgument(void); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For comparison operators as == != < > <= >= */ typedef enum { eq, neq, gt, lt, get, let } BinCompOperType; class BinaryCompOperator : public GeneralBoolOperand { private : // References to arguments GeneralOperand *LeftArgument; GeneralOperand *RightArgument; // Pointer to operator (only library methematical < > >= >= != ==) // (bool) operator Operation(float,float); BinCompOperType Operation; public : // BinaryCompOperator((bool) operator itsCmp(float,float), GeneralOperand *LeftArgument); BinaryCompOperator(BinCompOperType itsCmp, GeneralOperand *LeftArgument); ~BinaryCompOperator(); #ifndef MAC_UI virtual bool Calculate(); #else virtual Boolean Calculate(); #endif void SetRight(GeneralOperand *itsRightArgument); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For control structures if else or for while */ class OperatorIf : public GeneralOperand { private: // Reference to test expression GeneralBoolOperand *Argument; /* References to arithmetic to be executed if test is true or false. In the if or else branches, several arithmetic expressions separated by a ; may follow each other. Therefore we need an array of references for which we will call the execution method in loop, and of course the number of elements. */ GeneralOperand **IfArithmetic; int numIf; int numBlocksIf; GeneralOperand **ElseArithmetic; // null if no else case int numElse; int numBlocksElse; public : OperatorIf(GeneralBoolOperand *itsTest); ~OperatorIf(); virtual double Calculate() ; // Set routine for elements parsed after generation of object void SetIf(GeneralOperand *itsIf); void SetElse(GeneralOperand *itsElse); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; class OperatorFor : public GeneralOperand { private: // Reference to loop start arithmetic GeneralOperand *Start; // Reference to test expression GeneralBoolOperand *Condition; // Reference to loop instruction GeneralOperand *Increment; /* Refences to arithmetics to execute as long as test is true. In the loop , several arithmetic expressions separated by a ; may follow each other. Therefore we need an array of references for which we will call the execution method in loop, and of course the number of elements. */ GeneralOperand **LoopArithmetic; int numBlockLoop; int numLoop; public : OperatorFor(GeneralOperand *itsStart); ~OperatorFor(); virtual double Calculate(); void SetCondition(GeneralBoolOperand *itsCondition); void SetIncrement(GeneralOperand *itsIncrement); void SetLoop(GeneralOperand *itsLoop); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* Classes to optimise the incrementation/decrementation of a variable often used in the for loops */ class OperatorIncDec : public GeneralOperand { private: // Indication to pre or post int PrePost; // Indication to increment or decrement int Step; // -1 o +1 // Position of variable in the variable table int Position; public: OperatorIncDec(int itsPrePost, int itsStep); ~OperatorIncDec(); virtual double Calculate(); void SetPosition(int itsPosition); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; class OperatorWhile : public GeneralOperand { private: // Reference to test expression GeneralBoolOperand *Condition; /* Refence to arithmetics to execute as long as test is true. In the loop , several arithmetic expressions separated by a ; may follow each other. Therefore we need an array of references for which we will call the execution method in loop, and of course the number of elements. */ GeneralOperand **LoopArithmetic; int numBlockLoop; int numLoop; public : OperatorWhile(GeneralBoolOperand *itsCondition); ~OperatorWhile(); virtual double Calculate(); void SetLoop(GeneralOperand *itsLoop); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; class OperatorDo : public GeneralOperand { private: // Reference to test expression GeneralBoolOperand *Condition; /* Refence to arithmetics to execute as long as test is true. In the loop , several arithmetic expressions separated by a ; may follow each other. Therefore we need an array of references for which we will call the execution method in loop, and of course the number of elements. */ GeneralOperand **LoopArithmetic; int numBlockLoop; int numLoop; public : OperatorDo(); ~OperatorDo(); virtual double Calculate(); void SetLoop(GeneralOperand *itsLoop); void SetCondition(GeneralBoolOperand *itsCondition); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* Special routines to perform other actions whithin the software As an example we define printing objects ================================*/ #ifdef ADD_FUNCT /* For debugging purpose, generate object that prints the value of a variable */ class PrintVarOperat : public GeneralOperand { private : int varIndex; public : PrintVarOperat(int varidx); ~PrintVarOperat(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; /* For debugging purpose, generate object that prints a string */ class PrintMsgOperat : public GeneralOperand { private: char *message; public : PrintMsgOperat(char* msg); ~PrintMsgOperat(); virtual double Calculate(); #ifndef MAC_UI virtual void Print(); #else virtual void Print(TEHandle txtBuf); #endif }; #endif