This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.js
104 lines (84 loc) · 2.95 KB
/
index.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
var jsep = require('jsep');
/**
* Evaluation code from JSEP project, under MIT License.
* Copyright (c) 2013 Stephen Oney, http://jsep.from.so/
*/
var binops = {
'||': function (a, b) { return a || b; },
'&&': function (a, b) { return a && b; },
'|': function (a, b) { return a | b; },
'^': function (a, b) { return a ^ b; },
'&': function (a, b) { return a & b; },
'==': function (a, b) { return a == b; }, // jshint ignore:line
'!=': function (a, b) { return a != b; }, // jshint ignore:line
'===': function (a, b) { return a === b; },
'!==': function (a, b) { return a !== b; },
'<': function (a, b) { return a < b; },
'>': function (a, b) { return a > b; },
'<=': function (a, b) { return a <= b; },
'>=': function (a, b) { return a >= b; },
'<<': function (a, b) { return a << b; },
'>>': function (a, b) { return a >> b; },
'>>>': function (a, b) { return a >>> b; },
'+': function (a, b) { return a + b; },
'-': function (a, b) { return a - b; },
'*': function (a, b) { return a * b; },
'/': function (a, b) { return a / b; },
'%': function (a, b) { return a % b; }
};
var unops = {
'-' : function (a) { return -a; },
'+' : function (a) { return a; },
'~' : function (a) { return ~a; },
'!' : function (a) { return !a; },
};
function evaluate_array( list, context ) {
return list.map( function(v,i,a) { return evaluate( v, context ); } );
}
function evaluate ( node, context ) {
switch ( node.type ) {
case 'ArrayExpression':
return evaluate_array( node.elements, context );
case 'BinaryExpression':
return binops[ node.operator ]( evaluate( node.left, context ), evaluate( node.right, context ) );
case 'CallExpression':
let fn = evaluate( node.callee, context );
if ( typeof( fn ) === "function" ) {
return fn.apply( null, evaluate_array( node.arguments, context ) );
} else {
return undefined;
}
case 'ConditionalExpression':
if ( evaluate( node.test, context ) ) {
return evaluate( node.consequent, context );
} else {
return evaluate( node.alternate, context );
}
case 'Identifier':
return context[ node.name ];
case 'Literal':
return node.value;
case 'LogicalExpression':
return binops[ node.operator ]( evaluate( node.left, context ), evaluate( node.right, context ) );
case 'MemberExpression':
if ( node.computed ) {
return evaluate(node.object, context)[evaluate(node.property, context)];
} else {
return evaluate(node.object, context)[node.property.name];
}
case 'ThisExpression':
return this;
case 'UnaryExpression':
return unops[ node.operator ]( evaluate( node.argument, context ) );
default:
return undefined;
}
}
function compile (expression) {
return evaluate.bind(null, jsep(expression));
}
module.exports = {
parse: jsep,
eval: evaluate,
compile: compile
};