You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ktechlab/microbe/expression.h

127 lines
4.2 KiB

/***************************************************************************
* Copyright (C) 2004-2005 by Daniel Clarke *
* daniel.jc@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include "microbe.h"
#include <tqstring.h>
class PIC14;
class BTreeNode;
class Microbe;
/**
@author Daniel Clarke
@author David Saxton
*/
class Expression
{
public:
enum Operation
{
noop,
addition,
subtraction,
multiplication,
division,
exponent,
equals,
notequals,
pin,//(returns the truth value obtatined by testing the pin)
notpin, //(the result of doing the pin op NOTted).]
read_keypad, //read value from keypad
function,
bwand,
bwor,
bwxor,
bwnot,
divbyzero, // used to make handling this situation easier
gt,
lt,
ge,
le
};
Expression(PIC14 *pic, Microbe *master, SourceLine sourceLine, bool supressNumberTooBig );
~Expression();
/**
* Generates the code needed to evaluate an expression. Firstly, a tree
* is generated from the expression string; then that tree is traversed
* to generate the assembly.
*/
void compileExpression( const TQString & expression);
void compileConditional( const TQString & expression, Code * ifCode, Code * elseCode );
/**
* Returns a *number* rather than evaluating code, and sets isConstant to true
* if it the expression evaluated to a constant.
*/
TQString processConstant( const TQString & expr, bool * isConsant );
private:
PIC14 *m_pic;
Microbe *mb;
/** Turns the operations encoded in the given tree into assembly code */
void traverseTree( BTreeNode *root, bool conditionalRoot = false );
bool isUnaryOp(Operation op);
void expressionValue( TQString expression, BTreeBase *tree, BTreeNode *node );
void doOp( Operation op, BTreeNode *left, BTreeNode *right );
void doUnaryOp( Operation op, BTreeNode *node );
/**
* Parses an expression, and generates a tree structure from it.
*/
void buildTree( const TQString & expression, BTreeBase *tree, BTreeNode *node, int level );
static int findSkipBrackets( const TQString & expr, char ch, int startPos = 0);
static int findSkipBrackets( const TQString & expr, TQString phrase, int startPos = 0);
TQString stripBrackets( TQString expression );
void mistake( Microbe::MistakeType type, const TQString & context = 0 );
SourceLine m_sourceLine;
Code * m_ifCode;
Code * m_elseCode;
/**
*Returns expression type
* 0 = directly usable number (literal)
* 1 = variable
* 2 = expression that needs evaluating
* (maybe not, see enum).
*/
ExprType expressionType( const TQString & expression );
static bool isLiteral( const TQString &text );
/**
* Normally, only allow numbers upto 255; but for some uses where the
* number is not going to be placed in a PIC register (such as when
* delaying), we can ignore numbers being too big.
*/
bool m_bSupressNumberTooBig;
};
#endif