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:
Stefan Zermatten
2021-10-01 13:41:22 +02:00
parent cb1fd38df3
commit feffa45cf7
19 changed files with 521 additions and 305 deletions

View File

@@ -0,0 +1,46 @@
import constant from './constant.js';
import resolve, { toString, traverse } from '../resolve.js';
const array = {
create({values}) {
return {
type: 'array',
values,
};
},
fromConstantArray(array){
let values = array.map( value => {
let valueType = typeof value;
if (
valueType === 'string' ||
valueType === 'number' ||
valueType === 'boolean' ||
valueType === 'undefined'
){
return constant.create({value, valueType});
} else {
throw `Unexpected type in constant array: ${valueType}`
}
});
return array.create({values});
},
resolve(fn, node, scope){
let values = node.values.map(node => {
let { result } = resolve(fn, node, scope, context);
return result;
});
return {
result: array.create({values}),
context,
};
},
toString(node){
return `[${node.values.map(value => toString(value)).join(', ')}]`;
},
traverse(node, fn){
fn(node);
node.values.forEach(value => traverse(value, fn));
},
}
export default array;