summaryrefslogtreecommitdiff
path: root/noncore/tools/calc2/engine.cpp
Unidiff
Diffstat (limited to 'noncore/tools/calc2/engine.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/tools/calc2/engine.cpp10
1 files changed, 5 insertions, 5 deletions
diff --git a/noncore/tools/calc2/engine.cpp b/noncore/tools/calc2/engine.cpp
index a9a47c4..e843e29 100644
--- a/noncore/tools/calc2/engine.cpp
+++ b/noncore/tools/calc2/engine.cpp
@@ -8,207 +8,207 @@
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include "engine.h" 21#include "engine.h"
22#include <qstring.h> 22#include <qstring.h>
23#include <math.h> 23#include <math.h>
24#include <qlcdnumber.h> 24#include <qlcdnumber.h>
25 25
26Data Engine::evalStack (Data num, bool inbrace = FALSE) 26Data Engine::evalStack (Data num, bool inbrace = FALSE)
27{ 27{
28 if (state != sError) { 28 if (state != sError) {
29 Instruction *i; 29 Instruction *i;
30 30
31// Pop the next op from the stack 31// Pop the next op from the stack
32 while (!stack.isEmpty () && (braces || !inbrace)) { 32 while (!stack.isEmpty () && (braces || !inbrace)) {
33 i = stack.pop (); 33 i = stack.pop ();
34 34
35// Check this ops prec vs next ops prec 35// Check this ops prec vs next ops prec
36 if (!stack.isEmpty ()) 36 if (!stack.isEmpty ())
37 if (i->precedence <= stack.top()->precedence) 37 if (i->precedence <= stack.top()->precedence)
38 i->acc = evalStack (i->acc, inbrace); 38 i->acc = evalStack (i->acc, inbrace);
39 39
40// Evaluate this instruction 40// Evaluate this instruction
41 num = i->eval (num); 41 num = i->eval (num);
42 42
43// Error-check ( change this to work for all types ) 43// Error-check ( change this to work for all types )
44 if (isnan (num.dbl) || isinf (num.dbl)) { 44 if (isnan (num.dbl) || isinf (num.dbl)) {
45 qDebug ("bad result from operation"); 45 qDebug ("bad result from operation");
46 state = sError; 46 state = sError;
47 clearData(&num); 47 clearData(&num);
48 return num; 48 return num;
49 } 49 }
50 } 50 }
51 } 51 }
52 return num; 52 return num;
53} 53}
54 54
55// Plugins call this to request the stack be evaluated 55// Plugins call this to request the stack be evaluated
56void Engine::eval () 56void Engine::eval ()
57{ 57{
58 num = evalStack (num); 58 num = evalStack (num);
59 if (state != sError) { 59 if (state != sError) {
60 displayData(num); 60 displayData(num);
61 state = sStart; 61 state = sStart;
62 } 62 }
63// if the user didnt close all their braces, its no big deal 63// if the user didnt close all their braces, its no big deal
64 braces = 0; 64 braces = 0;
65} 65}
66 66
67void Engine::immediateInstruction (Instruction * i) 67void Engine::immediateInstruction (Instruction * i)
68{ 68{
69 if (state != sError) { 69 if (state != sError) {
70 i->setRep(currentRep); 70 i->setRep(currentRep);
71 num = i->eval (num); 71 num = i->eval (num);
72 displayData(num); 72 displayData(num);
73 state = sStart; 73 state = sStart;
74 } 74 }
75} 75}
76 76
77void Engine::pushInstruction (Instruction * i) 77void Engine::pushInstruction (Instruction * i)
78{ 78{
79 if (state != sError) { 79 if (state != sError) {
80 i->setRep(currentRep); 80 i->setRep(currentRep);
81 i->acc = num; 81 i->acc = num;
82 stack.push (i); 82 stack.push (i);
83 state = sStart; 83 state = sStart;
84 } 84 }
85} 85}
86 86
87void Engine::pushValue (char v) 87void Engine::pushValue (char v)
88{ 88{
89 if (state == sAppend) { 89 if (state == sAppend) {
90 bool ok = FALSE; 90 bool ok = FALSE;
91 switch (currentRep) { 91 switch (currentRep) {
92 case rDouble: 92 case rDouble:
93 displayString.append(v); 93 displayString.append(v);
94 num.dbl=displayString.toDouble(&ok); 94 num.dbl=displayString.toDouble(&ok);
95 break; 95 break;
96 case rFraction: 96 case rFraction:
97 break; 97 break;
98 default: 98 default:
99 displayString.append(v); 99 displayString.append(v);
100 num.i=displayString.toInt(&ok, calcBase()); 100 num.i=displayString.toInt(&ok, calcBase());
101 }; 101 };
102 if (!ok) { 102 if (!ok) {
103 state = sError; 103 state = sError;
104 qDebug("pushValue() - num->string conversion"); 104 odebug << "pushValue() - num->string conversion" << oendl;
105 } else { 105 } else {
106 const QString constString = displayString; 106 const QString constString = displayString;
107 emit(display(constString)); 107 emit(display(constString));
108 }; 108 };
109 109
110 } else if (state == sStart) { 110 } else if (state == sStart) {
111 softReset(); 111 softReset();
112 displayString.truncate(0); 112 displayString.truncate(0);
113 state = sAppend; 113 state = sAppend;
114 pushValue (v); 114 pushValue (v);
115 } else if (state == sError) { 115 } else if (state == sError) {
116 qDebug ("in error state"); 116 qDebug ("in error state");
117 return; 117 return;
118 } 118 }
119} 119}
120 120
121void Engine::del () 121void Engine::del ()
122{ 122{
123 bool ok; 123 bool ok;
124 switch (currentRep) { 124 switch (currentRep) {
125 case rDouble: 125 case rDouble:
126 displayString.truncate(displayString.length()); 126 displayString.truncate(displayString.length());
127 num.dbl=displayString.toDouble(&ok); 127 num.dbl=displayString.toDouble(&ok);
128 break; 128 break;
129 case rFraction: 129 case rFraction:
130 qDebug("not available"); 130 odebug << "not available" << oendl;
131 break; 131 break;
132 default: 132 default:
133 displayString.truncate(displayString.length()); 133 displayString.truncate(displayString.length());
134 num.i = displayString.toInt(&ok, calcBase()); 134 num.i = displayString.toInt(&ok, calcBase());
135 }; 135 };
136 136
137 if (!ok) { 137 if (!ok) {
138 state = sError; 138 state = sError;
139 qDebug("del() - num->string conversion"); 139 odebug << "del() - num->string conversion" << oendl;
140 } else { 140 } else {
141 const QString constString = displayString; 141 const QString constString = displayString;
142 emit(display(constString)); 142 emit(display(constString));
143 }; 143 };
144} 144}
145 145
146void Engine::displayData(Data d) { 146void Engine::displayData(Data d) {
147 switch (currentRep) { 147 switch (currentRep) {
148 case rDouble: 148 case rDouble:
149 displayString.setNum(d.dbl); 149 displayString.setNum(d.dbl);
150 break; 150 break;
151 case rFraction: 151 case rFraction:
152 qDebug("fractional display not yet impl"); 152 odebug << "fractional display not yet impl" << oendl;
153 break; 153 break;
154 default: 154 default:
155 displayString.setNum(d.i, calcBase()); 155 displayString.setNum(d.i, calcBase());
156 break; 156 break;
157 }; 157 };
158 const QString constString= displayString; 158 const QString constString= displayString;
159 emit(display(constString)); 159 emit(display(constString));
160} 160}
161 161
162// Returns the base when Rep is an integer type 162// Returns the base when Rep is an integer type
163int Engine::calcBase () { 163int Engine::calcBase () {
164 switch (currentRep) { 164 switch (currentRep) {
165 case rBin: 165 case rBin:
166 return 2; 166 return 2;
167 case rOct: 167 case rOct:
168 return 8; 168 return 8;
169 case rDec: 169 case rDec:
170 return 10; 170 return 10;
171 case rHex: 171 case rHex:
172 return 16; 172 return 16;
173 default: 173 default:
174 state = sError; 174 state = sError;
175 qDebug("Error - attempt to calc base for non-integer"); 175 odebug << "Error - attempt to calc base for non-integer" << oendl;
176 return 10; 176 return 10;
177 }; 177 };
178} 178}
179 179
180// Special instruction for internal use only 180// Special instruction for internal use only
181class iOpenBrace:public Instruction { 181class iOpenBrace:public Instruction {
182 public: 182 public:
183 iOpenBrace (Engine *e):Instruction (100) {engine = e;}; 183 iOpenBrace (Engine *e):Instruction (100) {engine = e;};
184 ~iOpenBrace () {}; 184 ~iOpenBrace () {};
185 185
186 Data eval (Data num) { 186 Data eval (Data num) {
187 engine->decBraces(); 187 engine->decBraces();
188 return num; 188 return num;
189 }; 189 };
190 private: 190 private:
191 Engine *engine; 191 Engine *engine;
192}; 192};
193 193
194void Engine::openBrace() { 194void Engine::openBrace() {
195 pushInstruction(new iOpenBrace(this)); 195 pushInstruction(new iOpenBrace(this));
196} 196}
197 197
198void Engine::closeBrace() { 198void Engine::closeBrace() {
199 braces++;evalStack(num,TRUE); 199 braces++;evalStack(num,TRUE);
200} 200}
201 201
202// will need to show and hide display widgets 202// will need to show and hide display widgets
203void Engine::setRepresentation(Representation r) { 203void Engine::setRepresentation(Representation r) {
204 currentRep = r; 204 currentRep = r;
205 clearData(&num); 205 clearData(&num);
206 clearData(&mem); 206 clearData(&mem);
207 state = sStart; 207 state = sStart;
208} 208}
209 209
210void Engine::clearData(Data *d) { 210void Engine::clearData(Data *d) {
211 d->i = d->fraction.numerator = d->fraction.denominator = 0; 211 d->i = d->fraction.numerator = d->fraction.denominator = 0;
212 d->dbl = 0; 212 d->dbl = 0;
213} 213}
214 214