-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacker.js
144 lines (131 loc) · 3.8 KB
/
Stacker.js
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
function add(a, b) {
return a + b;
}
function sub(a, b) {
return a - b;
}
function mul(a, b) {
return a * b;
}
function div(a, b) {
return a / b;
}
function not(a) {
return ~a;
}
function unsignedRightShift(a, b) {
return a >>> b;
}
// TODO:
// remainder %
// exponentiation **
// logical not !
// bitwise left/right << >>
// bitwise and/or/xor & | ^
// todo bits(0.1) is wrong
function bits(x) {
x = x.toString(2);
x = x.padStart(32, '0');
x = '0b' + x;
return x;
}
class Stacker {
ops = [];
constructor() {
this.div = document.createElement('div');
this.input = document.createElement('input');
this.input.value = '100';
this.table = document.createElement('table');
document.body.append(
this.div,
this.input,
this.table
);
this.buttonAdd = this.addFunc(add , '+' , 0 );
this.buttonSub = this.addFunc(sub , '-' , 0 );
this.buttonMul = this.addFunc(mul , '*' , 1 );
this.buttonDiv = this.addFunc(div , '/' , 1 );
this.buttonNot = this.addFunc(not , '~' , null);
this.buttonSin = this.addFunc(Math.sin , 'sin' , null);
this.buttonCos = this.addFunc(Math.cos , 'cos' , null);
this.buttonAbs = this.addFunc(Math.abs , 'abs' , null);
this.buttonUnsignedRightShift = this.addFunc(unsignedRightShift, '>>>' , 0 );
this.buttonUnsignedRightShift = this.addFunc(bits , 'bits', 0 );
}
addFunc(func, name, identityValue) {
var button;
button = document.createElement('button');
button.innerText = name;
button.onclick = function(e) {
var opInput = this.newInput();
opInput.tdMiddle.innerText = name;
if (this.ops.length == 0) {
opInput.inputLeft.value = this.input.value;
} else {
opInput.inputLeft.value = this.ops[this.ops.length - 1].opInput.inputResult.value;
}
if (identityValue != null) {
opInput.inputRight.value = identityValue;
}
this.ops.push({
func,
opInput
});
this.update();
}.bind(this);
document.body.append(button);
}
newInput() {
var tr = document.createElement('tr');
var tdLeft = document.createElement('td');
var tdMiddle = document.createElement('td');
var tdRight = document.createElement('td');
var tdResult = document.createElement('td');
this.table.append(tr);
tr.append(
tdLeft,
tdMiddle,
tdRight,
tdResult
);
var inputLeft = document.createElement('input');
var inputRight = document.createElement('input');
var inputResult = document.createElement('input');
tdLeft.append(inputLeft);
tdRight.append(inputRight);
tdRight.append('=');
tdResult.append(inputResult);
inputLeft.oninput = this.update.bind(this);
inputRight.oninput = this.update.bind(this);
return {
tr,
tdLeft,
tdMiddle,
tdRight,
tdResult,
inputLeft,
inputRight,
inputResult
};
}
update() {
this.ops.forEach((op, i) => {
op.opInput.inputResult.value = op.func(
eval(op.opInput.inputLeft.value),
eval(op.opInput.inputRight.value)
);
// Activate second input (might be deactivated again)
op.opInput.inputRight.disabled = false;
// An op with one argument doesn't need a second input
if (op.func.length == 1) {
op.opInput.inputRight.disabled = true;
op.opInput.inputRight.value = '';
}
// copy result to next row (if it exists)
var nextOp = this.ops[i + 1];
if (nextOp) {
nextOp.opInput.inputLeft.value = op.opInput.inputResult.value;
}
});
}
}