-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRuntimeCompiler.js
56 lines (51 loc) · 1.5 KB
/
RuntimeCompiler.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
const Compiler = require("../Compiler");
const { Animated } = require("react-native");
const ANIMATED_UNARY_OPS = {
"+": a => a,
"-": a => Animated.multiply(-1, a)
};
const ANIMATED_BINARY_OPS = {
"+": Animated.add.bind(Animated),
"-": (a, b) => Animated.add(a, ANIMATED_UNARY_OPS["-"](b)),
"/": Animated.divide.bind(Animated),
"*": Animated.multiply.bind(Animated),
"%": Animated.modulo.bind(Animated)
};
function ensureAnimated(animatedOrNumber) {
if (typeof animatedOrNumber === "number") {
return new Animated.Value(animatedOrNumber);
}
return animatedOrNumber;
}
module.exports = class RuntimeCompiler extends Compiler {
evaluateAst(node) {
switch (node.type) {
case "BinaryExpression":
return ANIMATED_BINARY_OPS[node.operator](
this.evaluateAst(node.left),
this.evaluateAst(node.right)
);
break;
case "UnaryExpression":
return ANIMATED_UNARY_OPS[node.operator](
this.evaluateAst(node.argument)
);
case "NumericLiteral": {
const value = parseFloat(node.value);
if (node.exprType === "Animated.Value") {
return new Animated.Value(value);
}
return value;
}
case "Placeholder":
const value = node.value;
if (node.exprType === "Animated.Value") {
return ensureAnimated(value);
}
return value;
}
}
evaluateTemplate(strings, ...values) {
return this.evaluateAst(this.parseTemplate(strings, ...values));
}
};