Diffstat (limited to 'noncore/tools/calc2/engine.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/calc2/engine.cpp | 133 |
1 files changed, 70 insertions, 63 deletions
diff --git a/noncore/tools/calc2/engine.cpp b/noncore/tools/calc2/engine.cpp index e843e29..74cd701 100644 --- a/noncore/tools/calc2/engine.cpp +++ b/noncore/tools/calc2/engine.cpp | |||
@@ -10,51 +10,58 @@ | |||
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 | |||
23 | /* OPIE */ | ||
24 | #include <opie2/odebug.h> | ||
25 | |||
26 | /* QT */ | ||
22 | #include <qstring.h> | 27 | #include <qstring.h> |
23 | #include <math.h> | ||
24 | #include <qlcdnumber.h> | 28 | #include <qlcdnumber.h> |
25 | 29 | ||
30 | /* STD */ | ||
31 | #include <math.h> | ||
32 | |||
26 | Data Engine::evalStack (Data num, bool inbrace = FALSE) | 33 | Data Engine::evalStack (Data num, bool inbrace = FALSE) |
27 | { | 34 | { |
28 | if (state != sError) { | 35 | if (state != sError) { |
29 | Instruction *i; | 36 | Instruction *i; |
30 | 37 | ||
31 | // Pop the next op from the stack | 38 | // Pop the next op from the stack |
32 | while (!stack.isEmpty () && (braces || !inbrace)) { | 39 | while (!stack.isEmpty () && (braces || !inbrace)) { |
33 | i = stack.pop (); | 40 | i = stack.pop (); |
34 | 41 | ||
35 | // Check this ops prec vs next ops prec | 42 | // Check this ops prec vs next ops prec |
36 | if (!stack.isEmpty ()) | 43 | if (!stack.isEmpty ()) |
37 | if (i->precedence <= stack.top()->precedence) | 44 | if (i->precedence <= stack.top()->precedence) |
38 | i->acc = evalStack (i->acc, inbrace); | 45 | i->acc = evalStack (i->acc, inbrace); |
39 | 46 | ||
40 | // Evaluate this instruction | 47 | // Evaluate this instruction |
41 | num = i->eval (num); | 48 | num = i->eval (num); |
42 | 49 | ||
43 | // Error-check ( change this to work for all types ) | 50 | // Error-check ( change this to work for all types ) |
44 | if (isnan (num.dbl) || isinf (num.dbl)) { | 51 | if (isnan (num.dbl) || isinf (num.dbl)) { |
45 | qDebug ("bad result from operation"); | 52 | odebug << "bad result from operation" << oendl; |
46 | state = sError; | 53 | state = sError; |
47 | clearData(&num); | 54 | clearData(&num); |
48 | return num; | 55 | return num; |
49 | } | 56 | } |
50 | } | 57 | } |
51 | } | 58 | } |
52 | return num; | 59 | return num; |
53 | } | 60 | } |
54 | 61 | ||
55 | // Plugins call this to request the stack be evaluated | 62 | // Plugins call this to request the stack be evaluated |
56 | void Engine::eval () | 63 | void Engine::eval () |
57 | { | 64 | { |
58 | num = evalStack (num); | 65 | num = evalStack (num); |
59 | if (state != sError) { | 66 | if (state != sError) { |
60 | displayData(num); | 67 | displayData(num); |
@@ -80,124 +87,124 @@ void Engine::pushInstruction (Instruction * i) | |||
80 | i->setRep(currentRep); | 87 | i->setRep(currentRep); |
81 | i->acc = num; | 88 | i->acc = num; |
82 | stack.push (i); | 89 | stack.push (i); |
83 | state = sStart; | 90 | state = sStart; |
84 | } | 91 | } |
85 | } | 92 | } |
86 | 93 | ||
87 | void Engine::pushValue (char v) | 94 | void Engine::pushValue (char v) |
88 | { | 95 | { |
89 | if (state == sAppend) { | 96 | if (state == sAppend) { |
90 | bool ok = FALSE; | 97 | bool ok = FALSE; |
91 | switch (currentRep) { | 98 | switch (currentRep) { |
92 | case rDouble: | 99 | case rDouble: |
93 | displayString.append(v); | 100 | displayString.append(v); |
94 | num.dbl=displayString.toDouble(&ok); | 101 | num.dbl=displayString.toDouble(&ok); |
95 | break; | 102 | break; |
96 | case rFraction: | 103 | case rFraction: |
97 | break; | 104 | break; |
98 | default: | 105 | default: |
99 | displayString.append(v); | 106 | displayString.append(v); |
100 | num.i=displayString.toInt(&ok, calcBase()); | 107 | num.i=displayString.toInt(&ok, calcBase()); |
101 | }; | 108 | }; |
102 | if (!ok) { | 109 | if (!ok) { |
103 | state = sError; | 110 | state = sError; |
104 | odebug << "pushValue() - num->string conversion" << oendl; | 111 | odebug << "pushValue() - num->string conversion" << oendl; |
105 | } else { | 112 | } else { |
106 | const QString constString = displayString; | 113 | const QString constString = displayString; |
107 | emit(display(constString)); | 114 | emit(display(constString)); |
108 | }; | 115 | }; |
109 | 116 | ||
110 | } else if (state == sStart) { | 117 | } else if (state == sStart) { |
111 | softReset(); | 118 | softReset(); |
112 | displayString.truncate(0); | 119 | displayString.truncate(0); |
113 | state = sAppend; | 120 | state = sAppend; |
114 | pushValue (v); | 121 | pushValue (v); |
115 | } else if (state == sError) { | 122 | } else if (state == sError) { |
116 | qDebug ("in error state"); | 123 | odebug << "in error state" << oendl; |
117 | return; | 124 | return; |
118 | } | 125 | } |
119 | } | 126 | } |
120 | 127 | ||
121 | void Engine::del () | 128 | void Engine::del () |
122 | { | 129 | { |
123 | bool ok; | 130 | bool ok; |
124 | switch (currentRep) { | 131 | switch (currentRep) { |
125 | case rDouble: | 132 | case rDouble: |
126 | displayString.truncate(displayString.length()); | 133 | displayString.truncate(displayString.length()); |
127 | num.dbl=displayString.toDouble(&ok); | 134 | num.dbl=displayString.toDouble(&ok); |
128 | break; | 135 | break; |
129 | case rFraction: | 136 | case rFraction: |
130 | odebug << "not available" << oendl; | 137 | odebug << "not available" << oendl; |
131 | break; | 138 | break; |
132 | default: | 139 | default: |
133 | displayString.truncate(displayString.length()); | 140 | displayString.truncate(displayString.length()); |
134 | num.i = displayString.toInt(&ok, calcBase()); | 141 | num.i = displayString.toInt(&ok, calcBase()); |
135 | }; | 142 | }; |
136 | 143 | ||
137 | if (!ok) { | 144 | if (!ok) { |
138 | state = sError; | 145 | state = sError; |
139 | odebug << "del() - num->string conversion" << oendl; | 146 | odebug << "del() - num->string conversion" << oendl; |
140 | } else { | 147 | } else { |
141 | const QString constString = displayString; | 148 | const QString constString = displayString; |
142 | emit(display(constString)); | 149 | emit(display(constString)); |
143 | }; | 150 | }; |
144 | } | 151 | } |
145 | 152 | ||
146 | void Engine::displayData(Data d) { | 153 | void Engine::displayData(Data d) { |
147 | switch (currentRep) { | 154 | switch (currentRep) { |
148 | case rDouble: | 155 | case rDouble: |
149 | displayString.setNum(d.dbl); | 156 | displayString.setNum(d.dbl); |
150 | break; | 157 | break; |
151 | case rFraction: | 158 | case rFraction: |
152 | odebug << "fractional display not yet impl" << oendl; | 159 | odebug << "fractional display not yet impl" << oendl; |
153 | break; | 160 | break; |
154 | default: | 161 | default: |
155 | displayString.setNum(d.i, calcBase()); | 162 | displayString.setNum(d.i, calcBase()); |
156 | break; | 163 | break; |
157 | }; | 164 | }; |
158 | const QString constString= displayString; | 165 | const QString constString= displayString; |
159 | emit(display(constString)); | 166 | emit(display(constString)); |
160 | } | 167 | } |
161 | 168 | ||
162 | // Returns the base when Rep is an integer type | 169 | // Returns the base when Rep is an integer type |
163 | int Engine::calcBase () { | 170 | int Engine::calcBase () { |
164 | switch (currentRep) { | 171 | switch (currentRep) { |
165 | case rBin: | 172 | case rBin: |
166 | return 2; | 173 | return 2; |
167 | case rOct: | 174 | case rOct: |
168 | return 8; | 175 | return 8; |
169 | case rDec: | 176 | case rDec: |
170 | return 10; | 177 | return 10; |
171 | case rHex: | 178 | case rHex: |
172 | return 16; | 179 | return 16; |
173 | default: | 180 | default: |
174 | state = sError; | 181 | state = sError; |
175 | odebug << "Error - attempt to calc base for non-integer" << oendl; | 182 | odebug << "Error - attempt to calc base for non-integer" << oendl; |
176 | return 10; | 183 | return 10; |
177 | }; | 184 | }; |
178 | } | 185 | } |
179 | 186 | ||
180 | // Special instruction for internal use only | 187 | // Special instruction for internal use only |
181 | class iOpenBrace:public Instruction { | 188 | class iOpenBrace:public Instruction { |
182 | public: | 189 | public: |
183 | iOpenBrace (Engine *e):Instruction (100) {engine = e;}; | 190 | iOpenBrace (Engine *e):Instruction (100) {engine = e;}; |
184 | ~iOpenBrace () {}; | 191 | ~iOpenBrace () {}; |
185 | 192 | ||
186 | Data eval (Data num) { | 193 | Data eval (Data num) { |
187 | engine->decBraces(); | 194 | engine->decBraces(); |
188 | return num; | 195 | return num; |
189 | }; | 196 | }; |
190 | private: | 197 | private: |
191 | Engine *engine; | 198 | Engine *engine; |
192 | }; | 199 | }; |
193 | 200 | ||
194 | void Engine::openBrace() { | 201 | void Engine::openBrace() { |
195 | pushInstruction(new iOpenBrace(this)); | 202 | pushInstruction(new iOpenBrace(this)); |
196 | } | 203 | } |
197 | 204 | ||
198 | void Engine::closeBrace() { | 205 | void Engine::closeBrace() { |
199 | braces++;evalStack(num,TRUE); | 206 | braces++;evalStack(num,TRUE); |
200 | } | 207 | } |
201 | 208 | ||
202 | // will need to show and hide display widgets | 209 | // will need to show and hide display widgets |
203 | void Engine::setRepresentation(Representation r) { | 210 | void Engine::setRepresentation(Representation r) { |