~rpadovani/ubuntu-calculator-app/dbupgrade

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
/*
 * 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 results = [];
    var lookupTbl = {
        '-': '−',
        '/': '÷',
        '*': '×'
    };

    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){
        if(results.length > 0) throw new SyntaxError("cannot push");
        var last = (new Scanner(previous.value.toString()+digit, context)).tokens.pop();
        switch(last.type){
        case T_PLUS:
        case T_MINUS:
        case T_DIV:
        case T_TIMES:
            if(previous.type & T_OPERATOR) return;
            break;
        case T_UNARY_PLUS:
        case T_UNARY_MINUS:
            if(previous.type === T_UNARY_MINUS || previous.type === T_UNARY_PLUS ) return;
            break;
        }
        formula += digit;
        previous = last;
        var ret = {value: '', type: last.type};
        if(lookupTbl[digit] !== undefined){
            ret.value = lookupTbl[digit];
        } else {
            ret.value = digit;
        }
        return ret;
    };

    this.pop = function(){
        if(results.length > 0){
            results.pop();
            return;
        }
        var scannedFormula = new Scanner(formula, context);
        var last = {type:T_NUMBER};
        var removeSize = 0;
        while(scannedFormula.tokens.length > 0 && (last.type === T_NUMBER || last.type === T_UNARY_PLUS || last.type === T_UNARY_MINUS)){
            last = scannedFormula.tokens.pop();
            removeSize += last.value.toString().length
        }
        formula = formula.substring(0, formula.length - removeSize);
        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);
        var last = scannedFormula.tokens.pop();
        if(results.length > 0){
            var newFormula = last.value.toString();
            while(last.type !== T_PLUS &&
                  last.type !== T_MINUS &&
                  last.type !== T_DIV &&
                  last.type !== T_TIMES ){
                last = scannedFormula.tokens.pop();
                newFormula = last.value.toString() + newFormula;
            }
            result = _calculate(results[results.length-1]+newFormula);
        } else{
            // check previous input
            if(last.type === T_PLUS ||
                    last.type === T_MINUS ||
                    last.type === T_DIV ||
                    last.type === T_TIMES){
                // getting the first value
                var firstVal = scannedFormula.tokens.pop();
                // adding the first value to formula
                formula = formula + firstVal.value.toString()
            }
            result = _calculate(formula);

        }
        results.push(result.toString());
        return result;
    }

    this.changeSign = function(){
        if(results.length > 0) throw new SyntaxError("cannot edit formula");
        switch(previous.type){
        case T_PLUS:
        case T_MINUS:
        case T_DIV:
        case T_TIMES:
            formulaPush("-");
            break;
        default:
            var scannedFormula = new Scanner(formula, context);
            var last = {type: undefined};
            var toAdd = [];
            var addMinus = true;
            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();
            while(toAdd.length > 0){
                var el = toAdd.pop()
                formulaPush(el.value.toString());
                if((el.type & T_OPERATOR) && addMinus){
                    formulaPush('-');
                }
            }
        }
    }

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