Made undefined variable names zero in compile step

This commit is contained in:
Thaum Rystra
2024-04-12 11:36:11 +02:00
parent 3a7c3765c6
commit 4793b34a55
5 changed files with 23 additions and 16 deletions

View File

@@ -16,8 +16,8 @@ describe('Accessor Node', function () {
assert.isEmpty(context.errors);
assert.equal(
toString(result),
'unknownVariable + 8',
'Only known variables should be substituted during compilation step'
'8',
'Unknown variables should be be substituted with zero during compilation step'
);
});
it('reduces', async function () {
@@ -44,8 +44,8 @@ describe('Accessor Node', function () {
);
assert.isEmpty(compileContext.errors, 'compiling unknown variables should not have errors');
assert.deepEqual(
compileResult, { parseType: 'accessor', name: 'unknownVariable', isUndefined: true },
'Unknown variables should be marked as inUndefined in compile step'
compileResult, { parseType: 'constant', value: 0, valueType: 'number', isUndefined: true },
'Unknown variables should be zero and marked as inUndefined in compile step'
);
// At reduce step

View File

@@ -87,6 +87,15 @@ const accessor: AccessorFactory = {
};
}
if (valueType === 'undefined') {
// Replace unknown variables with zero marked isUndefined
return {
result: constant.create({
value: 0,
isUndefined: true,
}),
context
};
// Old Behavior
// We are only at compile, if it isn't defined in the scope, return a copy of the accessor
return {
result: accessor.create({

View File

@@ -1,5 +1,5 @@
import { serialMap } from '/imports/api/utility/asyncMap';
import constant from '/imports/parser/parseTree/constant';
import constant, { ConstantValueType } from '/imports/parser/parseTree/constant';
import ParseNode from '/imports/parser/parseTree/ParseNode';
import ResolveFunction from '/imports/parser/types/ResolveFunction';
import MapFunction from '/imports/parser/types/MapFunction';
@@ -29,17 +29,15 @@ const arrayFactory: ArrayFactory = {
},
fromConstantArray(constantArray) {
const values = constantArray.map(value => {
const valueType = typeof value;
if (
valueType === 'string' ||
valueType === 'number' ||
valueType === 'boolean' ||
valueType === 'undefined'
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean'
) {
return constant.create({ value });
} else {
// Gracefully create an empty constant in the array for unsupported types
return constant.create({ value: undefined });
return constant.create({ value: 0, isUndefined: true });
}
});
return arrayFactory.create({ values });

View File

@@ -9,7 +9,7 @@ describe('Call Node', function () {
const callNode = parse('min( unknownVariable, 1 + 2, 3d30 )');
const { result, context } = await resolve('compile', callNode, undefined, undefined, inputProviderForTests);
assert.isEmpty(context.errors)
assert.equal(toString(result), 'min(unknownVariable, 3, 3d30)');
assert.equal(toString(result), 'min(0, 3, 3d30)');
});
it('reduces', async function () {
const callNode = parse('min( unknownVariable, 1 + 2, 3d30 )');

View File

@@ -9,13 +9,13 @@
},
"author": "Thaum Rystra",
"scripts": {
"run": "meteor",
"serve": "meteor run --raw-logs",
"debug": "meteor --inspect",
"bundle-viz": "meteor --extra-packages bundle-visualizer --production",
"lint": "eslint .",
"test": "meteor test --driver-package meteortesting:mocha --port 3001",
"test:coverage": "COVERAGE=1 COVERAGE_OUT_LCOVONLY=1 COVERAGE_OUT_REMAP=1 COVERAGE_APP_FOLDER=$PWD/ meteor test --once --driver-package meteortesting:mocha",
"test:watch:coverage": "COVERAGE=1 COVERAGE_OUT_LCOVONLY=1 COVERAGE_OUT_REMAP=1 COVERAGE_APP_FOLDER=$PWD/ TEST_WATCH=1 meteor test --driver-package meteortesting:mocha",
"test": "meteor test --driver-package meteortesting:mocha --port 3001 --raw-logs",
"test:coverage": "COVERAGE=1 COVERAGE_OUT_LCOVONLY=1 COVERAGE_OUT_REMAP=1 COVERAGE_APP_FOLDER=$PWD/ meteor test --once --raw-logs --driver-package meteortesting:mocha",
"test:watch:coverage": "COVERAGE=1 COVERAGE_OUT_LCOVONLY=1 COVERAGE_OUT_REMAP=1 COVERAGE_APP_FOLDER=$PWD/ TEST_WATCH=1 meteor test --raw-logs --driver-package meteortesting:mocha",
"build": "meteor build ../build --architecture os.linux.x86_64"
},
"engines": {