-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathInputPoc.html
156 lines (125 loc) · 6 KB
/
mathInputPoc.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="Assets/reset.css">
<link rel="stylesheet" href="Assets/style.css">
<!-- import external stuff -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Comfortaa:300,400" rel="stylesheet">
<!-- Local scripts -->
<script src="tools.js"></script>
<script src="parseExpr.js"></script>
<script src="math.js"></script>
<script src="exprMath.js"></script>
<script src="simpExpr.js"></script>
<script src="vars.js"></script>
<script src="http://fxtend.herokuapp.com/Lyrics/Scripts/panelTransform.js"></script>
</head>
<body>
<div id="mainPage">
<input type="text" id="input" value="√2"><br><div class="button btnEnter" onclick="parseAndCalc()">Enter<div class="cb cbAuto cbInactive" onclick="toggleEnter()"></div> </div><a id="ans">1.4142135623730951</a><div id="rerender"></div>
</div>
<!--<iframe id="userScript"></iframe>-->
<script id="userScript"></script>
<script>
var inputBox = document.getElementById("input");
var ansBox = document.getElementById("ans");
var btnEnter = document.getElementById("btnEnter")
var cbAuto = document.getElementById("cbAuto");
var rerendBox = document.getElementById("rerender");
var uvarInd = 0;
var autoCalc = true;
//inputBox.value.onchange = parseAndCalc()
$('#input').on('input', function() {
rerenderStr(inputBox.value);
if(autoCalc){
parseAndCalc();
}
});
function toggleEnter(){
autoCalc = !autoCalc;
$(".btnEnter").toggleClass("btnActive");
$(".cbAuto").toggleClass("cbInactive");
}
var failed = false;
function err(msg){
ansBox.innerHTML = msg;
failed = true;
}
function exprToString(expr, pref, suf){
// In case you don't want any separators
if(suf == undefined){
suf = "";
if(pref == undefined){
pref = "";
}
}
switch(expr.type){
case "num":
return pref + expr.num + suf;
case "unOp":
return pref + expr.op + exprToString(expr.expr, pref, suf) + suf;
case "binOp":
return pref + "(" + exprToString(expr.expr0) + expr.op + exprToString(expr.expr1) + ")" + suf;
}
}
function parseAndCalc(){
failed = false;
ansBox.innerHTML = "";
if(inputBox.value.length > 0){
var expr = purifyStr(inputBox.value);
expr = parseExpr(expr)
expr = simplify(expr); //TODO: fraction division simplification is wrong for bigInt but not JS numbers
var ans = calc(expr);
if(!failed)
ansBox.innerHTML = ans;
rerendBox.innerHTML = exprToString(expr, '<a class="expr">', '</a>');
}
}
function rerenderStr(str){
var oldCursorPos = inputBox.selectionStart;
var oldLen = str.length;
for(var i = 0; i < str.length; i++){
var newStr = "";
var oldLength = 0;
var found = false;
for(var replInd = 0; replInd < strRepl.length && !found; replInd++){
var from = strRepl[replInd].from;
for(var fromInd = 0; fromInd < from.length && !found; fromInd++){
if(begins(from[fromInd], str.substr(i))){
found = true;
oldLength = from[fromInd].length;
newStr = strRepl[replInd].to;
str = str.substr(0, i) + newStr + str.substr(i + oldLength)
}
}
}
}
inputBox.value = str;
var newCursorPos = oldCursorPos + str.length - oldLen;
inputBox.selectionStart = newCursorPos;
inputBox.selectionEnd = newCursorPos;
}
function purifyStr(str){
var jsInd = str.indexOf("js(");
while(jsInd > -1){
jsEnd = skipPars(str, jsInd + 2, true);
var jsStr = str.substring(jsInd + 3, jsEnd - 1);
jsStr = calcJs(jsStr);
str = str.substring(0, jsInd) + jsStr + str.substr(jsEnd);
jsInd = str.indexOf("js(")
}
return str;
}
function calcJs(str){
var scriptEl = document.getElementById("userScript");
scriptEl.innerHTML = "<script>function userFunc(){ return " + str + ";}</script"+">";
return userFunc();
}
</script>
</body>
</html>