-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_asm.js
50 lines (41 loc) · 1.61 KB
/
json_asm.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
module.exports = (access) => {
const self = {};
self.add = (state, lvalue, operand1, operand2) => {
const result = (self.load(state, operand1) || 0) + (self.load(state, operand2) || 0);
self.store(state, lvalue, result);
},
self.load = (state, operand) => {
// Handle literals and convert to their true type if possible
if (operand === 'true' || operand === 'false') return operand === 'true';
if (!isNaN(operand)) return Number(operand);
if (operand.indexOf('"') === 0 || operand.indexOf("'") === 0) {
const endQuote = operand[operand.length - 1] === '"' || operand[operand.length - 1] === "'";
return operand.slice(1, endQuote ? operand.length - 1 : operand.length);
}
// Dereference pointers
return access.get(state, operand);
},
self.store = (state, path, value) => {
access.set(state, path, self.load(state, value));
},
self.sort = (state, path, property, order) => {
const array = access.get(state, path);
const get = (obj, property) => {
if (!property) return obj;
return obj[property];
};
array.sort((a,b) => get(a, property) >= get(b, property) ? order : -order);
};
self.splice = (state, path, start, deleteCount, ...newValues) => {
const array = access.get(state, path);
array.splice(self.load(state, start),
self.load(state, deleteCount),
...(newValues.map(v => self.load(state, v))));
};
self.if_not_equal = (state, operand1, operand2, command) => {
if (self.load(state, operand1) == self.load(state, operand2)) return;
const next_instr = self.load(state, '/__index__') + 1;
self.splice(state, '/__statements__', next_instr, 0, command);
};
return self;
};