File indexing completed on 2024-07-21 08:05:24

0001 /***************************************************************************
0002  *   Copyright (C) 2004-2005 by Daniel Clarke                              *
0003  *   daniel.jc@gmail.com                                                   *
0004  *                                     *
0005  *   24-04-2007                                                            *
0006  *   Modified to add pic 16f877,16f627 and 16f628              *
0007  *   by george john george@space-kerala.org,az.j.george@gmail.com      *
0008  *   supported by SPACE www.space-kerala.org                   *
0009  *                                                                         *
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the                         *
0022  *   Free Software Foundation, Inc.,                                       *
0023  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0024  ***************************************************************************/
0025 
0026 #ifndef EXPRESSION_H
0027 #define EXPRESSION_H
0028 
0029 #include "microbe.h"
0030 
0031 #include <QString>
0032 
0033 class PIC14;
0034 class BTreeNode;
0035 class MicrobeApp;
0036 
0037 /**
0038 @author Daniel Clarke
0039 @author David Saxton
0040 */
0041 class Expression
0042 {
0043     public:
0044         enum Operation
0045         {
0046             noop,
0047             addition,
0048             subtraction,
0049             multiplication,
0050             division,
0051             exponent,
0052             equals,
0053             notequals,
0054             pin,//(returns the truth value obtatined by testing the pin)
0055             notpin, //(the result of doing the pin op NOTted).]
0056             read_keypad, //read value from keypad
0057             function,
0058             bwand,
0059             bwor,
0060             bwxor,
0061             bwnot,
0062             divbyzero, // used to make handling this situation easier
0063             gt,
0064             lt,
0065             ge,
0066             le
0067         };
0068         
0069         Expression(PIC14 *pic, MicrobeApp *master, SourceLineMicrobe sourceLine, bool supressNumberTooBig );
0070         ~Expression();
0071         
0072         /**
0073          * Generates the code needed to evaluate an expression. Firstly, a tree
0074          * is generated from the expression string; then that tree is traversed
0075          * to generate the assembly.
0076          */
0077         void compileExpression( const QString & expression);
0078         void compileConditional( const QString & expression, Code * ifCode, Code * elseCode );
0079         /** 
0080          * Returns a *number* rather than evaluating code, and sets isConstant to true
0081          * if it the expression evaluated to a constant.
0082          */
0083         QString processConstant( const QString & expr, bool * isConsant );
0084         
0085     private:
0086         PIC14 *m_pic;
0087         MicrobeApp *mb;
0088     
0089         /** Turns the operations encoded in the given tree into assembly code */
0090         void traverseTree( BTreeNode *root, bool conditionalRoot = false );
0091     
0092         bool isUnaryOp(Operation op);
0093     
0094         void expressionValue( QString expression, BTreeBase *tree, BTreeNode *node );
0095         void doOp( Operation op, BTreeNode *left, BTreeNode *right );
0096         void doUnaryOp( Operation op, BTreeNode *node );
0097         /**
0098          * Parses an expression, and generates a tree structure from it.
0099          */
0100         void buildTree( const QString & expression, BTreeBase *tree, BTreeNode *node, int level );
0101 
0102         static int findSkipBrackets( const QString & expr, char ch, int startPos = 0);
0103         static int findSkipBrackets( const QString & expr, QString phrase, int startPos = 0);
0104     
0105         QString stripBrackets( QString expression );
0106     
0107         void mistake( MicrobeApp::MistakeType type, const QString & context = nullptr );
0108     
0109         SourceLineMicrobe m_sourceLine;
0110     
0111         Code * m_ifCode;
0112         Code * m_elseCode;
0113     
0114         /** 
0115          *Returns expression type
0116          * 0 = directly usable number (literal)
0117          * 1 = variable
0118          * 2 = expression that needs evaluating
0119          * (maybe not, see enum).
0120          */
0121         ExprType expressionType( const QString & expression );
0122         static bool isLiteral( const QString &text );
0123         /**
0124          * Normally, only allow numbers upto 255; but for some uses where the
0125          * number is not going to be placed in a PIC register (such as when
0126          * delaying), we can ignore numbers being too big.
0127          */
0128         bool m_bSupressNumberTooBig;
0129 };
0130 
0131 #endif