diff --git a/app/imports/parser/functions.js b/app/imports/parser/functions.js index a98dded5..b0d16561 100644 --- a/app/imports/parser/functions.js +++ b/app/imports/parser/functions.js @@ -1,3 +1,5 @@ +import resolve from '/imports/parser/resolve.js' + export default { 'abs': { comment: 'Returns the absolute value of a number', @@ -108,6 +110,18 @@ export default { } return arrayNode.values.length; } + }, + 'resolve': { + comment: 'Forces the given calcultion to resolve into a number', + examples: [ + {input: 'resolve(someUndefinedVariable + 3 + 4)', result: '7'}, + {input: 'resolve(3d6)', result: '2'}, + ], + arguments: ['parseNode'], + fn: function resolveFn(node){ + let {result} = resolve('reduce', node, this.scope, this.context); + return result; + } } } diff --git a/app/imports/parser/parseTree/call.js b/app/imports/parser/parseTree/call.js index 83cce223..f5b1c91e 100644 --- a/app/imports/parser/parseTree/call.js +++ b/app/imports/parser/parseTree/call.js @@ -61,8 +61,11 @@ const call = { } // Map contant nodes to constants before attempting to run the function - let mappedArgs = resolvedArgs.map(arg => { - if (arg.parseType === 'constant'){ + let mappedArgs = resolvedArgs.map((arg, index) => { + if ( + arg.parseType === 'constant' && + func.arguments[index] !== 'parseNode' + ){ return arg.value; } else { return arg; @@ -71,7 +74,7 @@ const call = { try { // Run the function - let value = func.fn.apply(null, mappedArgs); + let value = func.fn.apply({scope, context}, mappedArgs); let valueType = typeof value; if (valueType === 'number' || valueType === 'string' || valueType === 'boolean'){ @@ -132,6 +135,7 @@ const call = { } else { type = argumentsExpected[index]; } + if (type === 'parseNode') return; if (node.parseType !== type && node.valueType !== type) failed = true; if (failed && fn === 'reduce'){ let typeName = typeof type === 'string' ? type : type.constructor.name;