summaryrefslogtreecommitdiffstats
path: root/microbe/expression.h
blob: 1e34bb8f49406351f60abd41e3b364168e716139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/***************************************************************************
 *   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