-
Notifications
You must be signed in to change notification settings - Fork 6
/
rpn.js
192 lines (169 loc) · 4.4 KB
/
rpn.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
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
"use strict";
function jsFromRPN(string) {
try {
return jsFromRPNSource(string);
} catch (err) {
return jsFromURI(string);
}
}
function jsFromRPNSource(string) {
return translate(string.match(/\S+/g));
function translate(tokens) {
var stack = makeCyclicStack();
for (var i = 0; i < tokens.length; ++i)
simulate(tokens[i], opTable, stack);
return stack.compile();
}
}
function jsFromURI(string) {
var stack = makeCyclicStack();
var chars = string.substr(string.search(/!/) + 1).split('');
for (var i = 0; i < chars.length; ) {
var literal = '';
while (i < chars.length && chars[i].match(/[0123456789ABCDEF]/))
literal += chars[i++];
if (literal !== '')
stack.push(parseInt(literal, 16));
else if (chars[i] === '.' || chars[i] === '!')
++i;
else
simulate(chars[i++], shortOpTable, stack);
}
return stack.compile();
}
function makeCyclicStack() {
var stmts = [];
var mask = 0xFF;
var sp = -1;
function S() {
return 'this.S[' + (sp & mask) + ']';
}
function pop() {
var result = S();
--sp;
return result;
}
function push(expr) {
++sp;
stmts.push(S() + ' = ' + expr + ',');
}
return {
compile: function() {
if (sp < 0)
return '0';
return ('((this.S ? 0 : this.S = new Array(256)), '
+ stmts.concat(pop() + ')').join(' '));
},
pick: function() {
push('this.S[(' + (sp-1) + '-' + pop() + ')&' + mask + ']');
},
push: push,
pop: pop,
};
}
function simulate(op, table, stack) {
if (table[op])
table[op](stack);
else if (!isNaN(op))
stack.push(0xFFFFFFFF & op); // It's a number
else
throw "Unknown op: " + op;
}
function literalOp(op) {
return function(stack) {
stack.push(op);
};
}
// XXX use e|0 or e>>>0, depending on if we want signed or unsigned
function masked(e) {
return '(' + e + ')>>>0';
}
function unmasked(e) {
return '(' + e + ')';
}
function prefixOp(op, masker) {
return function(stack) {
var z = stack.pop();
stack.push(masker(op + z));
};
}
function infixOp(op, masker) {
return function(stack) {
var z = stack.pop();
var y = stack.pop();
stack.push(masker(y + op + z));
};
}
function infixRel(op) {
return function(stack) {
var z = stack.pop();
var y = stack.pop();
stack.push('(' + y + op + z + ' ? 0xFFFFFFFF : 0)');
};
}
// Based on http://paste.ubuntu.com/733764/
// and http://paste.ubuntu.com/733829/
var opTable = {
't': literalOp('t'),
'drop': function(stack) {
stack.pop();
},
'*': infixOp('*', masked),
'/': function(stack) {
var z = stack.pop();
var y = stack.pop();
stack.push('(' + z + '?(' + y+'/'+z + ')>>>0:0)');
},
'+': infixOp('+', masked),
'-': infixOp('-', masked),
'%': function(stack) {
var z = stack.pop();
var y = stack.pop();
stack.push('(' + z + '?(' + y+'%'+z + ')>>>0:0)');
},
'<<': infixOp('<<', unmasked), // XXX JS only looks at low 5 bits of count
'>>': infixOp('>>>', unmasked), // XXX JS only looks at low 5 bits of count
'&': infixOp('&', unmasked),
'|': infixOp('|', unmasked),
'^': infixOp('^', unmasked),
'~': prefixOp('~', unmasked),
'dup': function(stack) {
var z = stack.pop();
stack.push(z);
stack.push(z);
},
'pick': function(stack) {
stack.pick();
},
'swap': function(stack) {
var z = stack.pop();
var y = stack.pop();
stack.push(z);
stack.push(y);
},
'>': infixRel('>'),
'<': infixRel('<'),
'=': infixRel('==='),
};
https://github.com/erlehmann/libglitch/blob/master/FORMAT-draft-erlehmann
var shortOpTable = {
'a': opTable['t'],
'c': opTable['drop'],
'd': opTable['*'],
'e': opTable['/'],
'f': opTable['+'],
'g': opTable['-'],
'h': opTable['%'],
'j': opTable['<<'],
'k': opTable['>>'],
'l': opTable['&'],
'm': opTable['|'],
'n': opTable['^'],
'o': opTable['~'],
'p': opTable['dup'],
'q': opTable['pick'],
'r': opTable['swap'],
's': opTable['<'],
't': opTable['>'],
'u': opTable['='],
};