~ahayzen/ubuntu-calculator-app/autopilot-move-py3

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This file is part of ubuntu-calculator-app.
 *
 * ubuntu-calculator-app 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; version 3.
 *
 * ubuntu-calculator-app 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, see <http://www.gnu.org/licenses/>.
 */

Qt.include("engine.js");

var context = new Context;

function Formula() {
    var previous = new Token(0, T_NUMBER);
    var formula = '';
    var previousFormula = {};
    var previousFormulaCounter = -1;
    var lookupTbl = {
        '-': '−',
        '/': '÷',
        '*': '×'
    };
    var table = {
        "65" : "+",
        "66" : "-",
        "67" : "*",
        "68" : "/"
    }

    var isLastResult = false;   // This var becames true if the last button pressed by user is equal
    var isChangeSign = false;   // This var becames true if user want to change the sign of a result

    function operatorToString(digit) {
        if(lookupTbl[digit] !== undefined) {
            return lookupTbl[digit];
        } else {
            return digit;
        }
    }

    // Check if the given digit is one of the valid operators or not.
    function isOperator(digit){
        switch(digit){
        case "+":
        case "-":
        case "*":
        case "/":
        case "%":
        case "^":
            return true;
        default:
            return false;
        }
    }

    var _calculate = function(func){
        try{
            var result = parse(func, context);
            if (result === Number.POSITIVE_INFINITY)
                result = '∞';
            else if (result === Number.NEGATIVE_INFINITY)
                result = '-∞';
        }catch(exception){
            if(exception instanceof DivisionByZeroError){
                result = "division by zero error";
            } else{
                throw new SyntaxError("malformed calculation");
            }
        }
        return result;
    }

    this.push = function(digit){
        var last = (new Scanner(formula + digit, context)).tokens.pop();

        // After a calc you have to add a sign before add a number
        if (!isOperator(digit) && isLastResult) return;

        switch(last.type){
        case T_PLUS:
        case T_MINUS:
        case T_DIV:
        case T_TIMES:
            if (previous.type & T_OPERATOR && !(previous.type !== '67' && hasToAddDot)) return 'invalidOperation'; // Not two operator below
            break;
        case T_UNARY_MINUS:
            // An unary minus can not be after an unary minus or an unary plus
            if (previous.type === T_UNARY_MINUS || previous.type === T_UNARY_PLUS) return 'invalidOperation';
            if (previous.type === T_MINUS && formula.length === 1) {
                // If formula.lenght is 1 it's the first minus, so we have to delete it. In all other cases we add it
                formulaPop();
                return 'invalidOperation';
            }

            break;
        case T_UNARY_PLUS:
            // An unary plus can not be after a minus or a plus or a * or a /
            if (previous.type === T_UNARY_MINUS
                    || previous.type === T_UNARY_PLUS
                    || previous.type === T_PLUS
                    || previous.type === T_TIMES
                    || previous.type === T_DIV
                    || previous.type === T_MINUS
                ) return 'invalidOperation';
            break;
        }

        formulaView.headerItem.state = "newPush"; // This is to change "AC" to "C" when user continue a calc

        formula += digit;
        isLastResult = false;
        previous = last;
        var ret = {value: '', type: last.type};
        //formula Shouldn't start with operators.
        var firstChar = formula.substring(0,1);
        if(isNaN(firstChar) && firstChar !== "(" && firstChar !== "-"){ // First letter should be digit or open bracket or minus
            ret.type = T_OPERATOR // Return
            formula = ""; // Make formula var empty
        }
        ret.value = operatorToString(digit);
        return ret;
    };

    this.pop = function(){
        var scannedFormula = new Scanner(formula, context);
        var last = {type:T_NUMBER};
        var endRemoveSize = 0;
        var beginRemoveSize = 0;
        do {
            last = scannedFormula.tokens.pop();
            endRemoveSize += last.value.toString().length;
            //If we found close bracket, then we must look for opening bracket
            if (last.type === T_PCLOSE) {
                //Let's check if we have opening bracket at beginning
                if (formula[0] === '(') {
                    beginRemoveSize = 1;
                } else {
                    console.log("Error: Expecting that first character in formula will be '(' in formula: " +formula);
                }
            }
        } while (scannedFormula.tokens.length > 0 &&
                (last.type === T_NUMBER || last.type === T_UNARY_PLUS || last.type === T_UNARY_MINUS || last.type === T_PCLOSE));
        formula = formula.substring(beginRemoveSize, formula.length - endRemoveSize);
        previous = scannedFormula.tokens.pop();
    };

    this.numeralPop = function() {
        var scannedFormula = new Scanner(formula, context);
        var last = {type:T_NUMBER};
        var endRemoveSize = 0;
        var beginRemoveSize = 0;
        do {
            last = scannedFormula.tokens.pop();
            //If the token is the number, then delete only last numeral
            if (last.type === T_NUMBER) {
                endRemoveSize += 1;
            } else {
                endRemoveSize += last.value.toString().length;
            }
            //If we found close bracket, then we must look for opening bracket
            if (last.type === T_PCLOSE) {
                //Let's check if we have opening bracket at beginning
                if (formula[0] === '(') {
                    beginRemoveSize = 1;
                } else {
                    console.log("Error: Expecting that first character in formula will be '(' in formula: " +formula);
                }
            }
        } while (scannedFormula.tokens.length > 0 &&
                (last.type === T_UNARY_PLUS || last.type === T_UNARY_MINUS || last.type === T_PCLOSE));
        formula = formula.substring(beginRemoveSize, formula.length - endRemoveSize);
        previous = scannedFormula.tokens.pop();
    };


    this.calculate = function(){
        var result;
        var previousSize = previous.value.toString().length;
        if(formula.length <= previousSize) return;
        var scannedFormula = new Scanner(formula, context);

        // Last and secondLast are one a number and the other an operator. Both can be both, it depends on user input
        var last = scannedFormula.tokens.pop();
        var secondLast = scannedFormula.tokens.pop();

        // In this case user press equal twice E.G. 2*2=4=8
        if (isLastResult && !isChangeSign && table[secondLast.type] !== undefined) {
            screenFormula.push({_text:'', _operation: operatorToString(table[secondLast.type]), _number: last.value.toString()});
            formula = '(' + formula + ')' + table[secondLast.type] + last.value.toString();
        }
        // In this case user press a number, and operator (or only an operator, if it isn't the first calc), and then equal
        else if (last.type & T_OPERATOR) {
            //if user has not put any number after last sign then ignore it
            formulaPop()
        }
        result = _calculate(formula);

        // If user want to change sign to the result
        if (isLastResult && isChangeSign) {
            result = -result;   // Change the sign
        }

        // Reset formula and add previous result to the array
        previousFormulaCounter++;
        previousFormula[previousFormulaCounter] = formula;
        formula = result;

        isLastResult = true;
        return result;
    }

    this.changeSign = function() {
        // If user want to change the sign of the result
        // previous.type is 16 if user do a calc, press equal, press a sign, press c and then press +/-
        if (isLastResult || previous.type === 16) {
            isChangeSign = true;
            screenFormula.pop(); // Delete last result from the screen
            calculate();

            if (previous.type === 16) { // If user pressed +/- after c we have to delete the do calc twice to remove the close bracket
                screenFormula.pop(); // Delete last result from the screen
                calculate();
            }

            isChangeSign = false;
            return;
        }

        switch(previous.type){
        case T_PLUS:
        case T_MINUS:
        case T_DIV:
        case T_TIMES:
            // After an operator, push minus
            formulaPush("-");
            break;
        default:
            var scannedFormula = new Scanner(formula, context);
            var last = {type: undefined};
            var toAdd = [];
            var addMinus = true;

            if (scannedFormula.tokens.length === 0) {   // If is the first pressed button
                formulaPush('-');
                return;
            }

            // Create a queue with all element to re-add to var formula
            while(scannedFormula.tokens.length > 0 && last.type !== T_PLUS &&
                  last.type !== T_MINUS &&
                  last.type !== T_DIV &&
                  last.type !== T_TIMES ){
                last = scannedFormula.tokens.pop();
                if(last.type !== T_UNARY_MINUS && last.type !== T_UNARY_PLUS){
                    toAdd.push(last);
                } else {
                    if(last.type === T_UNARY_MINUS)
                        addMinus = false;
                }
            }

            formulaPop();

            // This is for the first number only
            if (toAdd.length === 1) {
                var number = toAdd.pop();
                if (!isNaN(number.value)) { // Check if is a number
                    if (addMinus)
                        formulaPush('-');   // We add a minus
                    formulaPush(number.value.toString());

                    if (hasToAddDot)
                        formulaPush('.');

                    return; // We have finished
                }
                toAdd.push(number); // If number is not a number, we push it in queue for next function
            }


            while(toAdd.length > 0){
                var el = toAdd.pop()
                formulaPush(el.value.toString());
                if((el.type & T_OPERATOR) && addMinus){
                    formulaPush('-');
                }
            }
            if (hasToAddDot)
                formulaPush('.');
        }
    }

    this.hasResults = function(){
        if(results.length > 0){
            return true;
        }
        return false;
    }

    this.restorePreviousFormula = function() {
        if (previousFormulaCounter > -1) {
            formula = previousFormula[previousFormulaCounter];
            previousFormulaCounter--;
        }
    }
}