-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.js
169 lines (122 loc) · 4.03 KB
/
solution.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
// Retrieved from the Codewars site - 2020-01-31
function Compiler () {
this.args = [];
this.tokens = [];
};
Compiler.prototype.compile = function (program) {
return this.pass3(this.pass2(this.pass1(program)));
};
Compiler.prototype.tokenize = function (program) {
// Turn a program string into an array of tokens. Each token
// is either '[', ']', '(', ')', '+', '-', '*', '/', a variable
// name or a number (as a string)
var regex = /\s*([-+*/\(\)\[\]]|[A-Za-z]+|[0-9]+)\s*/g;
this.tokens = program.replace(regex, ":$1").substring(1).split(':').map( function (tok) {
return isNaN(tok) ? tok : tok|0;
});
};
Compiler.prototype.pass1 = function (program) {
this.tokenize(program);
this.collectArglist();
// return un-optimized AST
return this.expression();
};
Compiler.prototype.pass2 = function (ast) {
return this.simplify_ast( ast )
};
Compiler.prototype.pass3 = function (ast) {
return this.generate( ast );
};
Compiler.prototype.collectArglist = function() {
this.tokens.shift(); // Suck up '['
var arg = this.tokens.shift();
while( this.tokens.length > 0 && arg != ']' ) {
this.args.push( arg );
arg = this.tokens.shift();
}
};
Compiler.prototype.expression = function() {
var apart = this.term(),
t0 = this.tokens[0],
now = null;
if( t0 == undefined || (t0 != '+' && t0 != '-') )
return apart;
while( t0 != undefined && (t0 == '+' || t0 == '-') ) {
var curop = this.tokens.shift(),
bpart = this.term();
if( now != null )
now = { op: curop, a: now, b: bpart }
else
now = { op: curop, a: apart, b: bpart }
t0 = this.tokens[0];
}
return now;
};
Compiler.prototype.term = function() {
var apart = this.factor(),
t0 = this.tokens[0],
now = null;
if( t0 == undefined || (t0 != '*' && t0 != '/') )
return apart;
while( t0 != undefined && (t0 == '*' || t0 == '/') ) {
var curop = this.tokens.shift(),
bpart = this.factor();
if( now != null )
now = { op: curop, a: now, b: bpart }
else
now = { op: curop, a: apart, b: bpart }
t0 = this.tokens[0];
}
return now;
};
Compiler.prototype.factor = function() {
var tok = this.tokens.shift();
if( typeof( tok ) == 'number' )
return { op: 'imm', n: tok };
if( tok.match( /^\w/ ) )
return { op: 'arg', n: this.args.indexOf( tok ) };
// tok should be a '('
var ret = this.expression();
this.tokens.shift(); // Suck up ')'
return ret;
};
Compiler.prototype.simplify_ast = function( expr ) {
var this_op = expr.op;
if( this_op == 'imm' || this_op == 'arg' )
return expr;
var left = this.simplify_ast( expr.a ),
right = this.simplify_ast( expr.b );
if( left.op == 'imm' && right.op == 'imm' ) {
var lval = left.n, rval = right.n;
var value;
switch( this_op ) {
case '+': value = lval + rval; break;
case '-': value = lval - rval; break;
case '*': value = lval * rval; break;
case '/': value = lval / rval; break;
}
return { op: 'imm', n: value };
}
return { op: this_op, a: left, b: right };
};
Compiler.prototype.generate = function( node ) {
var mc_ins = []
if( node.op == 'imm' || node.op == 'arg' ) {
if( node.op == 'imm' )
mc_ins.push( 'IM ' + node.n );
else
mc_ins.push( 'AR ' + node.n );
}
else {
mc_ins = this.generate( node.a );
mc_ins = mc_ins.concat( this.generate( node.b ) );
mc_ins = mc_ins.concat( ['PO', 'SW', 'PO'] );
switch( node.op ) {
case '+': mc_ins.push( 'AD' ); break;
case '-': mc_ins.push( 'SU' ); break;
case '*': mc_ins.push( 'MU' ); break;
case '/': mc_ins.push( 'DI' ); break;
}
}
return mc_ins.concat( ['PU'] );
};