tested and fixed constant node computations

This commit is contained in:
Stefan Zermatten
2021-09-23 12:03:25 +02:00
parent 347bd8e476
commit e63dd2560a
3 changed files with 42 additions and 6 deletions

View File

@@ -0,0 +1,28 @@
import { buildComputationFromProps } from '/imports/api/creature/computation/newEngine/buildCreatureComputation.js';
import { assert } from 'chai';
import computeCreatureComputation from '../../computeCreatureComputation.js';
import clean from '../../utility/cleanProp.testFn.js';
export default function(){
const computation = buildComputationFromProps(testProperties);
computeCreatureComputation(computation);
const prop = id => computation.propsById[id];
assert.equal(prop('attId').value, 6);
}
var testProperties = [
clean({
_id: 'constId',
type: 'constant',
variableName: 'arrayConstant',
calculation: '[2, 4, 6, 8, 10]',
}),
clean({
_id: 'attId',
type: 'attribute',
baseValue: {
calculation: 'arrayConstant[3]',
},
ancestors: [{id: 'charId'}],
}),
];

View File

@@ -1,6 +1,7 @@
import computeAction from './computeAction.testFn.js';
import computeAttribute from './computeAttribute.testFn.js';
import computeClasses from './computeClasses.testFn.js';
import computeConstants from './computeConstants.testFn.js';
export default [{
text: 'Computes actions',
@@ -11,4 +12,7 @@ export default [{
},{
text: 'Computes classes',
fn: computeClasses,
},{
text: 'Computes constants',
fn: computeConstants,
}];

View File

@@ -9,18 +9,22 @@ export default class SymbolNode extends ParseNode {
toString(){
return `${this.name}`
}
compile(scope, context){
compile(scope, context, calledFromReduce = false){
let value = scope && scope[this.name];
let type = typeof value;
// For parse nodes, compile and return
if (value instanceof ParseNode){
return value.compile(scope, context);
}
// For objects, default to their .value
if (type === 'object'){
value = value.value;
type = typeof value;
}
// For parse nodes, compile and return
if (value instanceof ParseNode){
if (calledFromReduce){
return value.reduce(scope, context);
} else {
return value.compile(scope, context);
}
}
if (type === 'string' || type === 'number' || type === 'boolean'){
return new ConstantNode({value, type});
} else if (type === 'undefined'){
@@ -32,7 +36,7 @@ export default class SymbolNode extends ParseNode {
}
}
reduce(scope, context){
let result = this.compile(scope);
let result = this.compile(scope, context, true);
if (result instanceof SymbolNode){
if (context) context.storeError({
type: 'info',