Expressions / refactor operators to avoid linting errors
Previously the utilities used the Operators dict before its definition.
This commit is contained in:
@@ -44,7 +44,8 @@ const style = {
|
|||||||
],
|
],
|
||||||
color: ['interpolate',
|
color: ['interpolate',
|
||||||
animRatio,
|
animRatio,
|
||||||
newColor, oldColor],
|
newColor, oldColor
|
||||||
|
],
|
||||||
opacity: ['-', 1.0, ['*', animRatio, 0.75]]
|
opacity: ['-', 1.0, ['*', animRatio, 0.75]]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -72,6 +72,22 @@ export const ValueTypes = {
|
|||||||
NONE: 0
|
NONE: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An operator declaration must contain two methods: `getReturnType` which returns a type based on
|
||||||
|
* the operator arguments, and `toGlsl` which returns a GLSL-compatible string.
|
||||||
|
* Note: both methods can process arguments recursively.
|
||||||
|
* @typedef {Object} Operator
|
||||||
|
* @property {function(Array<ExpressionValue>): ValueTypes|number} getReturnType Returns one or several types
|
||||||
|
* @property {function(ParsingContext, Array<ExpressionValue>, ValueTypes=): string} toGlsl Returns a GLSL-compatible string
|
||||||
|
* Note: takes in an optional type hint as 3rd parameter
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operator declarations
|
||||||
|
* @type {Object<string, Operator>}
|
||||||
|
*/
|
||||||
|
export const Operators = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the possible types for a given value (each type being a binary flag)
|
* Returns the possible types for a given value (each type being a binary flag)
|
||||||
* To test a value use e.g. `getValueType(v) & ValueTypes.BOOLEAN`
|
* To test a value use e.g. `getValueType(v) & ValueTypes.BOOLEAN`
|
||||||
@@ -258,22 +274,7 @@ function assertUniqueInferredType(args, types) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
Operators['get'] = {
|
||||||
* An operator declaration must contain two methods: `getReturnType` which returns a type based on
|
|
||||||
* the operator arguments, and `toGlsl` which returns a GLSL-compatible string.
|
|
||||||
* Note: both methods can process arguments recursively.
|
|
||||||
* @typedef {Object} Operator
|
|
||||||
* @property {function(Array<ExpressionValue>): ValueTypes|number} getReturnType Returns one or several types
|
|
||||||
* @property {function(ParsingContext, Array<ExpressionValue>, ValueTypes=): string} toGlsl Returns a GLSL-compatible string
|
|
||||||
* Note: takes in an optional type hint as 3rd parameter
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Operator declarations
|
|
||||||
* @type {Object<string, Operator>}
|
|
||||||
*/
|
|
||||||
export const Operators = {
|
|
||||||
'get': {
|
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.ANY;
|
return ValueTypes.ANY;
|
||||||
},
|
},
|
||||||
@@ -287,8 +288,8 @@ export const Operators = {
|
|||||||
const prefix = context.inFragmentShader ? 'v_' : 'a_';
|
const prefix = context.inFragmentShader ? 'v_' : 'a_';
|
||||||
return prefix + value;
|
return prefix + value;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'var': {
|
Operators['var'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.ANY;
|
return ValueTypes.ANY;
|
||||||
},
|
},
|
||||||
@@ -301,8 +302,8 @@ export const Operators = {
|
|||||||
}
|
}
|
||||||
return `u_${value}`;
|
return `u_${value}`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'time': {
|
Operators['time'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -310,8 +311,8 @@ export const Operators = {
|
|||||||
assertArgsCount(args, 0);
|
assertArgsCount(args, 0);
|
||||||
return 'u_time';
|
return 'u_time';
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'*': {
|
Operators['*'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -321,8 +322,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'/': {
|
Operators['/'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -332,8 +333,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'+': {
|
Operators['+'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -343,8 +344,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'-': {
|
Operators['-'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -354,8 +355,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'clamp': {
|
Operators['clamp'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -368,8 +369,8 @@ export const Operators = {
|
|||||||
const max = expressionToGlsl(context, args[2]);
|
const max = expressionToGlsl(context, args[2]);
|
||||||
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
|
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'stretch': {
|
Operators['stretch'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -386,8 +387,8 @@ export const Operators = {
|
|||||||
const high2 = expressionToGlsl(context, args[4]);
|
const high2 = expressionToGlsl(context, args[4]);
|
||||||
return `((clamp(${expressionToGlsl(context, args[0])}, ${low1}, ${high1}) - ${low1}) * ((${high2} - ${low2}) / (${high1} - ${low1})) + ${low2})`;
|
return `((clamp(${expressionToGlsl(context, args[0])}, ${low1}, ${high1}) - ${low1}) * ((${high2} - ${low2}) / (${high1} - ${low1})) + ${low2})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'mod': {
|
Operators['mod'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -397,8 +398,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'pow': {
|
Operators['pow'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.NUMBER;
|
return ValueTypes.NUMBER;
|
||||||
},
|
},
|
||||||
@@ -408,8 +409,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'>': {
|
Operators['>'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -419,8 +420,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'>=': {
|
Operators['>='] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -430,8 +431,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'<': {
|
Operators['<'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -441,8 +442,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'<=': {
|
Operators['<='] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -452,8 +453,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'==': {
|
Operators['=='] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -463,8 +464,8 @@ export const Operators = {
|
|||||||
assertNumber(args[1]);
|
assertNumber(args[1]);
|
||||||
return `(${expressionToGlsl(context, args[0])} == ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} == ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'!': {
|
Operators['!'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -473,8 +474,8 @@ export const Operators = {
|
|||||||
assertBoolean(args[0]);
|
assertBoolean(args[0]);
|
||||||
return `(!${expressionToGlsl(context, args[0])})`;
|
return `(!${expressionToGlsl(context, args[0])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'between': {
|
Operators['between'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.BOOLEAN;
|
return ValueTypes.BOOLEAN;
|
||||||
},
|
},
|
||||||
@@ -488,8 +489,8 @@ export const Operators = {
|
|||||||
const value = expressionToGlsl(context, args[0]);
|
const value = expressionToGlsl(context, args[0]);
|
||||||
return `(${value} >= ${min} && ${value} <= ${max})`;
|
return `(${value} >= ${min} && ${value} <= ${max})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'interpolate': {
|
Operators['interpolate'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
return ValueTypes.COLOR;
|
return ValueTypes.COLOR;
|
||||||
},
|
},
|
||||||
@@ -503,8 +504,8 @@ export const Operators = {
|
|||||||
const end = expressionToGlsl(newContext, args[2], ValueTypes.COLOR);
|
const end = expressionToGlsl(newContext, args[2], ValueTypes.COLOR);
|
||||||
return `mix(${start}, ${end}, ${expressionToGlsl(context, args[0])})`;
|
return `mix(${start}, ${end}, ${expressionToGlsl(context, args[0])})`;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
'match': {
|
Operators['match'] = {
|
||||||
getReturnType: function(args) {
|
getReturnType: function(args) {
|
||||||
let type = ValueTypes.ANY;
|
let type = ValueTypes.ANY;
|
||||||
for (let i = 2; i < args.length; i += 2) {
|
for (let i = 2; i < args.length; i += 2) {
|
||||||
@@ -531,5 +532,4 @@ export const Operators = {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user