-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathsubstitute.js
30 lines (29 loc) · 1.1 KB
/
substitute.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
import { Instruction, IOP1, IOP2, IOP3, IVAR, IEXPR, ternaryInstruction, binaryInstruction, unaryInstruction } from './instruction';
export default function substitute(tokens, variable, expr) {
var newexpression = [];
for (var i = 0; i < tokens.length; i++) {
var item = tokens[i];
var type = item.type;
if (type === IVAR && item.value === variable) {
for (var j = 0; j < expr.tokens.length; j++) {
var expritem = expr.tokens[j];
var replitem;
if (expritem.type === IOP1) {
replitem = unaryInstruction(expritem.value);
} else if (expritem.type === IOP2) {
replitem = binaryInstruction(expritem.value);
} else if (expritem.type === IOP3) {
replitem = ternaryInstruction(expritem.value);
} else {
replitem = new Instruction(expritem.type, expritem.value);
}
newexpression.push(replitem);
}
} else if (type === IEXPR) {
newexpression.push(new Instruction(IEXPR, substitute(item.value, variable, expr)));
} else {
newexpression.push(item);
}
}
return newexpression;
}