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
This commit is contained in:
66
app/imports/parser/parseTree/accessor.js
Normal file
66
app/imports/parser/parseTree/accessor.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import constant from './constant.js';
|
||||
|
||||
const accessor = {
|
||||
create({name, path}) {
|
||||
return {
|
||||
type: 'accessor',
|
||||
path,
|
||||
name,
|
||||
};
|
||||
},
|
||||
compile(node, scope, context){
|
||||
let value = scope && scope[node.name];
|
||||
// For objects, get their value
|
||||
node.path.forEach(name => {
|
||||
if (value === undefined) return;
|
||||
value = value[name];
|
||||
});
|
||||
let valueType = typeof value;
|
||||
if (valueType === 'string' || valueType === 'number' || valueType === 'boolean'){
|
||||
return {
|
||||
result: constant.create({
|
||||
value,
|
||||
valueType
|
||||
}),
|
||||
context,
|
||||
};
|
||||
} else if (valueType === 'undefined'){
|
||||
return {
|
||||
result: accessor.create({
|
||||
name: node.name,
|
||||
path: node.path,
|
||||
}),
|
||||
context,
|
||||
};
|
||||
} else {
|
||||
context.error(`${node.name} returned an unexpected type`);
|
||||
return {
|
||||
result: accessor.create({
|
||||
name: node.name,
|
||||
path: node.path,
|
||||
}),
|
||||
context,
|
||||
};
|
||||
}
|
||||
},
|
||||
reduce(node, scope, context){
|
||||
let { result } = accessor.compile(node, scope, context);
|
||||
if (result.type === 'accessor'){
|
||||
context.error(`${accessor.toString(result)} not found, set to 0`);
|
||||
return {
|
||||
result: constant.create({
|
||||
value: 0,
|
||||
valueType: 'number',
|
||||
}),
|
||||
context
|
||||
};
|
||||
} else {
|
||||
return {result, context};
|
||||
}
|
||||
},
|
||||
toString(node){
|
||||
return `${node.name}.${node.path.join('.')}`;
|
||||
}
|
||||
}
|
||||
|
||||
export default accessor;
|
||||
Reference in New Issue
Block a user