Newer
Older
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>RPN calculator</title>
<style type="text/css">
#container {
display: flex;
flex-flow: row;
width: 700px;
margin: 10px auto;
/* border: 1px grey dotted; */
#stack-content {
margin-left: 10px;
font-size: 1.2em;
height: 400px;
overflow: auto;
}
#expand-stack {
display: none;
}
height: 100%;
border-left: 1px skyblue dotted;
border-right: 1px skyblue dotted;
}
#trail-content {
margin-left: 10px;
font-family: monospace;
height: 400px;
overflow: auto;
.keyboard {
font-size: 1.2em;
text-align: center;
}
.keyboard table {
margin: 2px auto;
}
.keyboard td {
width: 60px;
height: 60px;
max-width: 150px; /*just set to something small and it works in chrome.*/
overflow: auto;
}
#error-msg {
height: 30px;
min-width: 10px;
text-align: left;
}
.trail-active td.trail-op {
border-left: 2px lightblue solid;
}
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
@media only screen and (max-device-width: 480px) {
#container {
width: 300px;
flex-flow: column;
}
#stack {
height: 100%;
width: 300px;
order: 200;
}
#stack-content {
height: 100%;
max-height: 100px;
}
#expand-stack {
display: inline;
}
#keyboard {
height: 100%;
order: 100;
}
#trail {
height: 100%;
width: 300px;
order: 300;
}
#trail-content {
height: 100%;
max-height: 100px;
}
#expand-trail {
display: inline;
}
#error-msg {
height: 20px;
}
.keyboard td {
width: 60px;
height: 50px;
}
}
</style>
</head>
<body>
<div id="container">
<div id="stack">
<p>Stack <a title="clear stack" id="clear-stack" href="#">Clear Stack</a> <a title="expand stack" id="expand-stack" href="#">Expand Stack</a></p>
<div id="stack-content"></div>
</div>
<div id="keyboard">
<p>Keyboard
<a title="reset RPN calculator" id="reset" href="#">Reset All</a>
<a title="refresh page" id="refresh-page" href="#">Refresh Page</a>
</p>
<div class="keyboard">
<table>
<tr>
<td colspan="4" id="error-msg"></td>
</tr>
<tr>
<td colspan="4" id="number-display"></td>
<td data-bt-name="num7">7</td>
<td data-bt-name="num8">8</td>
<td data-bt-name="num9">9</td>
<td title="delete pending digit or last number in stack (q)" data-bt-name="backspace">←</td>
<td data-bt-name="num4">4</td>
<td data-bt-name="num5">5</td>
<td data-bt-name="num6">6</td>
<td title="times (*)" data-bt-name="times">×</td>
<td data-bt-name="num1">1</td>
<td data-bt-name="num2">2</td>
<td data-bt-name="num3">3</td>
<td title="minus" data-bt-name="minus">-</td>
<td title="change sign (n)" data-bt-name="change-sign">±</td>
<td title="dot" data-bt-name="dot">.</td>
<td title="plus" data-bt-name="plus">+</td>
<td title="swap top two numbers on stack" data-bt-name="swap">SWAP</td>
<td title="undo last operator (U)" data-bt-name="undo">UNDO</td>
<td title="divide (/)" data-bt-name="divide">÷</td>
<td title="push pending number to stack or duplicate top number on stack (Enter)"
<tr>
<td title="sum all numbers in stack" data-bt-name="sum-all">Σ</td>
<td title="square root (Q)" data-bt-name="square-root">√</td>
<td title="power (^)" data-bt-name="power">^</td>
</table>
</div>
</div>
<div id="trail">
<p>Trail <a title="clear trail" id="clear-trail" href="#">Clear Trail</a> <a title="expand trail" id="expand-trail" href="#">Expand Trail</a></p>
<div id="trail-content"></div>
</div>
</div>
<script type="text/javascript" src="vendor/localforage.min.js"></script>
<script type="text/javascript" src="vendor/jquery.min.js"></script>
<script type="text/javascript" src="fifo.js"></script>
<script type="text/javascript" src="fsm.js"></script> <!-- fifo.js is required by fsm.js -->
const testing = false;
const uiTesting = false;
// service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('sw.js', {scope: './'})
.then(function() {
console.log("Service Worker Registered");
});
}
/**
* show dumb data for UI testing.
*/
const showDumbData = function (rpnCalculator) {
var i;
rpnCalculator.sendKey("num1");
rpnCalculator.sendKey("return");
rpnCalculator.sendKey("num2");
rpnCalculator.sendKey("return");
rpnCalculator.sendKey("num3");
rpnCalculator.sendKey("return");
for (i = 0; i < 30; ++i) {
rpnCalculator.sendKey("return");
rpnCalculator.sendKey("plus");
}
for (i = 0; i < 30; ++i) {
rpnCalculator.sendKey("return");
}
rpnCalculator.sendKey("num7");
rpnCalculator.sendKey("num8");
rpnCalculator.sendKey("num9");
// for (i = 0; i < 10; ++i) {
// rpnCalculator.sendKey("num" + i);
// rpnCalculator.sendKey("num" + i);
// }
// $("#stack-content").append($('<pre>').text('3: 1'));
// $("#stack-content").append($('<pre>').text('2: 2'));
// $("#stack-content").append($('<pre>').text('1: 3'));
// $("#stack-content").append($('<pre>').text(' .'));
// $("#trail-content").append($('<pre>').text(' 1'));
// $("#trail-content").append($('<pre>').text(' 2'));
// $("#trail-content").append($('<pre>').text('> 3'));
const rpnTestBasic = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 2);
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 3);
const trail = m.trail.data; // the underlying array.
console.assert(trail.length === 3);
console.assert(trail[0][0] === null);
console.assert(trail[0][1] === 1);
console.assert(trail[1][0] === null);
console.assert(trail[1][1] === 2);
console.assert(trail[2][0] === '+');
console.assert(trail[2][1] === 3);
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
};
const rpnTestNumberHandling = function () {
var m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 12);
console.assert(m.currentNumber === "");
m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num0");
m.sendKey("dot");
m.sendKey("num1");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 0.12);
console.assert(m.currentNumber === "");
m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num0");
m.sendKey("dot");
m.sendKey("num1");
m.sendKey("num2");
m.sendKey("backspace");
console.assert(m.currentNumber === "0.1");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 0.1);
console.assert(m.currentNumber === "");
};
const rpnTestAutoCommitNumber = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 3);
console.assert(m.currentNumber === "");
console.assert(m.currentState === "idle");
};
const rpnTestOperator = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 2);
m.sendKey("minus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === -1);
m.sendKey("num3");
m.sendKey("num0");
m.sendKey("return");
console.assert(m.numberStack.length === 2);
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 29);
m.sendKey("return"); // duplicate
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 29 * 2);
m.sendKey("num9");
m.sendKey("change-sign");
m.sendKey("swap");
console.assert(m.numberStack.length === 2);
console.assert(m.numberStack[0] === -9);
console.assert(m.numberStack[1] === 29 * 2);
};
const rpnTestBackspaceOperator = function () {
const m = new fsm.RPNCalculator();
m.sendKey("num1");
m.sendKey("num2");
console.assert(m.currentNumber === "12");
m.sendKey("backspace"); // remove last digit in current number.
console.assert(m.currentNumber === "1");
m.sendKey("num3");
m.sendKey("return");
console.assert(m.currentNumber === "");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 13);
m.sendKey("backspace"); // delete last number in stack.
console.assert(m.numberStack.length === 0);
};
const rpnTestSumAllOperator = function () {
const m = new fsm.RPNCalculator();
m.sendKey("sum-all");
console.assert(m.lastError !== null);
m.sendKey("num1");
m.sendKey("sum-all");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 1);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("return");
m.sendKey("num3");
m.sendKey("return");
m.sendKey("num1");
m.sendKey("change-sign");
m.sendKey("return");
m.sendKey("sum-all");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 5);
};
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
const rpnTestNumberErrorHandling = function () {
var m = new fsm.RPNCalculator();
m.sendKey("num0");
m.sendKey("num0");
console.assert(m.currentNumber === "0");
m = new fsm.RPNCalculator();
m.sendKey("dot");
console.assert(m.currentNumber === "0.");
m.sendKey("backspace");
console.assert(m.currentNumber === "0");
m.sendKey("backspace");
console.assert(m.currentNumber === "");
m = new fsm.RPNCalculator();
m.sendKey("num1");
m.sendKey("dot");
m.sendKey("num2");
console.assert(m.currentNumber === "1.2");
m.sendKey("dot"); // future dot is ignored and show error.
console.assert(m.currentNumber === "1.2");
m.sendKey("num3");
console.assert(m.currentNumber === "1.23");
m.sendKey("dot");
console.assert(m.currentNumber === "1.23");
m.sendKey("return");
console.assert(m.numberStack[0] === 1.23);
};
const rpnTestNotEnoughElementOnStack = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 1);
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 1);
console.assert(m.lastError !== null);
m.sendKey("backspace");
console.assert(m.numberStack.length === 0);
console.assert(m.lastError === null);
m.sendKey("change-sign");
console.assert(m.lastError !== null);
m.sendKey("num1");
console.assert(m.lastError === null);
m.sendKey("change-sign");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === -1);
console.assert(m.lastError === null);
};
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
const rpnTestUndo = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
console.assert(m.snapshots.data.length === 1);
console.assert(m.snapshots.data[0].numberStack.length === 0);
m.sendKey("num2");
m.sendKey("return");
console.assert(m.snapshots.data.length === 2);
console.assert(m.snapshots.data[0].numberStack.length === 0);
console.assert(m.snapshots.data[1].numberStack.length === 1);
console.assert(m.snapshots.data[1].numberStack[0] === 1);
console.assert(m.numberStack.length === 2);
console.assert(m.numberStack[0] === 1);
console.assert(m.numberStack[1] === 2);
m.sendKey("undo");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 1);
m.sendKey("num3");
m.sendKey("return");
m.sendKey("plus");
m.sendKey("undo");
m.sendKey("times");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 3);
m.sendKey("undo");
m.sendKey("minus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === -2);
};
const rpnTestUndoWithErrors = function () {
var m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
m.sendKey("undo");
console.assert(m.numberStack.length === 0);
m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num8");
m.sendKey("return");
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num0");
m.sendKey("divide");
m.sendKey("undo");
console.assert(m.numberStack.length === 3);
console.assert(m.numberStack[0] === 8);
console.assert(m.numberStack[1] === 1);
console.assert(m.numberStack[2] === 0);
m.sendKey("undo");
console.assert(m.numberStack.length === 2);
console.assert(m.numberStack[0] === 8);
console.assert(m.numberStack[1] === 1);
};
/**
* run all tests.
*/
const runTests = function () {
console.assert(fsm.matchesSubstringAbb("123abb123") === true);
console.assert(fsm.matchesSubstringAbb("123ab123") === false);
console.assert(fsm.matchesSubstringAbb("123abaabb123") === true);
console.assert(fsm.matchesSubstringAbb("123abbb123") === true);
console.assert(fsm.matchesSubstringAbb("123ababab123") === false);
console.assert(fsm.matchesSubstringAbb("") === false);
console.assert(fsm.matchesSubstringAbb("123") === false);
console.assert(fsm.matchesSubstringAbb("123ab") === false);
console.assert(fsm.matchesSubstringAbb("123abb") === true);
console.assert(fsm.matchesSubstringAbb("abb") === true);
rpnTestBasic();
rpnTestNumberHandling();
rpnTestAutoCommitNumber();
rpnTestOperator();
rpnTestNotEnoughElementOnStack();
rpnTestBackspaceOperator();
rpnTestSumAllOperator();
/**
* update web UI to match the state of th rpnCalculator.
*
* currently it syncs rpnCalculator.currentNumber and rpnCalculator.numberStack.
*/
const updateUI = function (rpnCalculator) {
$('#number-display').text(rpnCalculator.currentNumber);
$('#error-msg').text(rpnCalculator.lastError);
const stack = rpnCalculator.numberStack;
const stackSize = stack.length;
$("#stack-content").children().remove();
for (i = 0; i < stackSize; ++i) {
$("#stack-content").append($('<pre>').text((stackSize - i) + ': ' + stack[i]));
}
$("#stack-content").append($('<pre>').text(' .')).scrollTop(9000); // TODO how to scroll to bottom?
const trail = rpnCalculator.trail.data;
const trailSize = trail.length;
const trailTable = $('<table>');
for (i = 0; i < trailSize; ++i) {
trailOp = trail[i][0];
trailNum = trail[i][1];
trNode = $('<tr>');
if (i === trailSize - 1) {
trNode.addClass("trail-active");
}
trNode.append($('<td>').text(trailOp).addClass("trail-op"));
trNode.append($('<td>').text(trailNum).addClass("trail-num"));
trailTable.append(trNode);
}
$("#trail-content").children().remove();
$("#trail-content").append(trailTable).scrollTop(9000); // TODO how to scroll to bottom?
const rpnCalculator = new fsm.RPNCalculator();
if (uiTesting) {
showDumbData(rpnCalculator);
}
// init web UI
updateUI(rpnCalculator);
// setup event handler
$('.keyboard td').click(function (evt) {
const keyName = $(target).attr('data-bt-name');
if (keyName !== undefined) {
console.log("user clicked on button: " + keyName);
rpnCalculator.sendKey(keyName);
// update UI
updateUI(rpnCalculator);
}
$('#clear-trail').click(function (evt) {
rpnCalculator.clearTrail();
updateUI(rpnCalculator);
return false;
});
$('#clear-stack').click(function (evt) {
rpnCalculator.clearStack();
updateUI(rpnCalculator);
return false;
});
$('#reset').click(function (evt) {
rpnCalculator.reset();
updateUI(rpnCalculator);
return false;
});
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
$('#refresh-page').click(function (evt) {
const urlJoin = function (baseUrl, path) {
const lastSlash = baseUrl.lastIndexOf("/");
return baseUrl.substring(0, lastSlash + 1) + path;
};
console.assert(urlJoin("http://localhost:8000/calc.html", "fifo.js") ===
"http://localhost:8000/fifo.js");
console.assert(urlJoin("http://localhost:8000/", "fifo.js") ===
"http://localhost:8000/fifo.js");
console.assert(urlJoin("http://localhost:8000/abc/t1.html", "fifo.js") ===
"http://localhost:8000/abc/fifo.js");
console.assert(urlJoin("https://www.example.com/calc.html", "fifo.js") ===
"https://www.example.com/fifo.js");
console.assert(urlJoin("https://www.example.com/", "fifo.js") ===
"https://www.example.com/fifo.js");
// set service worker cache to expire, thus fetching latest page on next refresh.
const htmlUrl = window.location.href;
const fifoJsUrl = urlJoin(htmlUrl, 'fifo.js');
const fsmJsUrl = urlJoin(htmlUrl, 'fsm.js');
// clear cache in page load order. the basic dependence should
// be cleared first before trigger a page reload.
localforage.setItem('timestamp:' + fsmJsUrl, new Date(2016,0,1).toJSON()).then(function (_) {
localforage.setItem('timestamp:' + fifoJsUrl, new Date(2016,0,1).toJSON()).then(function (_) {
localforage.setItem('timestamp:' + htmlUrl, new Date(2016,0,1).toJSON()).then(function (_) {
window.location.href = window.location.href;
});
});
});
return false;
});
$('#expand-stack').click(function (evt) {
const oldMaxHeight = $('#stack-content').css("max-height");
if (oldMaxHeight === "100px") {
$('#stack-content').css("max-height", "300px");
$('#expand-stack').text("Shrink Stack");
} else {
$('#stack-content').css("max-height", "100px");
$('#expand-stack').text("Expand Stack");
}
});
$('#expand-trail').click(function (evt) {
const oldMaxHeight = $('#trail-content').css("max-height");
if (oldMaxHeight === "300px") {
$('#trail-content').css("max-height", "100px");
$('#expand-trail').text("Expand Trail");
} else {
$('#trail-content').css("max-height", "300px");
$('#expand-trail').text("Shrink Trail");
}
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
$(document).keypress(function (evt) {
// map evt.key to RPNCalculator keyName.
const keyMap = {
"1": "num1",
"2": "num2",
"3": "num3",
"4": "num4",
"5": "num5",
"6": "num6",
"7": "num7",
"8": "num8",
"9": "num9",
"0": "num0",
".": "dot",
"Enter": "return",
"+": "plus",
"-": "minus",
"*": "times",
"/": "divide",
"n": "change-sign",
"^": "power",
"Q": "square-root",
"U": "undo",
// below is not from emacs calc
"q": "backspace",
};
console.log("charCode=" + evt.charCode + ", key=" + evt.key);
console.log(typeof(evt.key));
const keyName = keyMap[evt.key];
if (keyName !== undefined) {
rpnCalculator.sendKey(keyName);
updateUI(rpnCalculator);
}
});
</script>
</body>
</html>