Files
DiceCloud/app/imports/parser/resolve.js
Stefan Zermatten feffa45cf7 Began work on rewriting parser without object orientation
Parsing is expensive, if the parse tree can be stored on the DB it can 
save a lot of compute time, but mongo can't store Classes, so we 
re-write without classes
2021-10-01 13:41:22 +02:00

46 lines
1.4 KiB
JavaScript

import nodeTypeIndex from './parseTree/index.js';
import collate from '/imports/api/engine/computation/utility/collate.js';
import Context from './ResolveContext.js';
// Takes a parse ndoe and computes it to a set detail level
// returns {result, context}
export default function resolve(fn, node, scope, context = new Context()){
let type = nodeTypeIndex[node.type];
if (!type){
throw new Meteor.Error(`Parse node type: ${node.type} not implemented`);
}
if (type.resolve){
return type.resolve(fn, node, scope, context);
} else if (type[fn]) {
return type[fn](node, scope, context);
} else if (fn === 'reduce' && type.roll) {
return type.roll(node, scope, context)
} else if (type.compile){
return type.compile(node, scope, context)
} else {
throw new Meteor.Error('Compile not implemented on ' + node.type);
}
}
export function toString(node){
let type = nodeTypeIndex[node.type];
if (!type.toString){
throw new Meteor.Error('toString not implemented on ' + node.type);
}
return type.toString(node);
}
export function traverse(node, fn){
let type = nodeTypeIndex[node.type];
if (type.traverse){
return type.traverse(node, fn);
}
return fn(node);
}
export function mergeResolvedNodes(main, other){
main.errors = collate(main.errors, other.errors);
main.rolls = collate(main.rolls, other.rolls);
return main;
}