summaryrefslogtreecommitdiffstats
path: root/microbe/expression.h
blob: 9607f1602a6ca6c5a9cd65bb8504a967807d2621 (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                              *
 *   [email protected]                                                   *
 *                                                                         *
 *   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 <qstring.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 QString & expression);
		void compileConditional( const QString & 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.
		 */
		QString processConstant( const QString & 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( QString 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 QString & expression, BTreeBase *tree, BTreeNode *node, int level );

		static int findSkipBrackets( const QString & expr, char ch, int startPos = 0);
		static int findSkipBrackets( const QString & expr, QString phrase, int startPos = 0);
	
		QString stripBrackets( QString expression );
	
		void mistake( Microbe::MistakeType type, const QString & 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 QString & expression );
		static bool isLiteral( const QString &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