From 4793b34a553389c0321ce4c6cc13b639a5cefc25 Mon Sep 17 00:00:00 2001 From: Thaum Rystra <9525416+ThaumRystra@users.noreply.github.com> Date: Fri, 12 Apr 2024 11:36:11 +0200 Subject: [PATCH] Made undefined variable names zero in compile step --- app/imports/parser/parseTree/accessor.test.ts | 8 ++++---- app/imports/parser/parseTree/accessor.ts | 9 +++++++++ app/imports/parser/parseTree/array.ts | 12 +++++------- app/imports/parser/parseTree/call.test.ts | 2 +- app/package.json | 8 ++++---- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/imports/parser/parseTree/accessor.test.ts b/app/imports/parser/parseTree/accessor.test.ts index 83919aa6..69d25c02 100644 --- a/app/imports/parser/parseTree/accessor.test.ts +++ b/app/imports/parser/parseTree/accessor.test.ts @@ -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 diff --git a/app/imports/parser/parseTree/accessor.ts b/app/imports/parser/parseTree/accessor.ts index 6b70658c..8cfbf51d 100644 --- a/app/imports/parser/parseTree/accessor.ts +++ b/app/imports/parser/parseTree/accessor.ts @@ -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({ diff --git a/app/imports/parser/parseTree/array.ts b/app/imports/parser/parseTree/array.ts index 6462a12d..529af3ce 100644 --- a/app/imports/parser/parseTree/array.ts +++ b/app/imports/parser/parseTree/array.ts @@ -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 }); diff --git a/app/imports/parser/parseTree/call.test.ts b/app/imports/parser/parseTree/call.test.ts index 9f911558..34f81ee5 100644 --- a/app/imports/parser/parseTree/call.test.ts +++ b/app/imports/parser/parseTree/call.test.ts @@ -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 )'); diff --git a/app/package.json b/app/package.json index c6bf5e29..bffca6f1 100644 --- a/app/package.json +++ b/app/package.json @@ -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": {