Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel arithmetics #1

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dist:esm": "tsc -p tsconfig.esm.json",
"dist:cjs": "tsc -p tsconfig.cjs.json",
"dist": "npm run parseTypeDocs && rm -rf ./dist/* && npm run dist:esm && npm run dist:cjs && node ./scripts/setVersionConstant.mjs",
"test": "c8 mocha './dist/cjs/**/*.spec.js'",
"test": "c8 mocha './dist/cjs/**/*.spec.js' --reporter-option maxDiffSize=0",
"test:single": "mocha './dist/cjs/**/*.spec.js' --reporter-option maxDiffSize=0 --grep",
"check": "eslint ./src && tsc --noEmit",
"parseTypeDocs": "node ./scripts/parseTypeDocs.mjs"
Expand Down
8 changes: 8 additions & 0 deletions compiler/src/js2eel/compiler/Js2EelCompiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ someVar = 1;

/* Channel 0 */

CH__0 = 0;

someVar += ;

/* Channel 1 */

CH__1 = 1;

someVar += ;


Expand Down Expand Up @@ -326,10 +330,14 @@ out_pin:In 1

/* Channel 0 */

CH__0 = 0;

someVar__S4 = 1;

/* Channel 1 */

CH__1 = 1;

someVar__S5 = 1;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,15 @@ out_pin:In 1

/* Channel 0 */

CH__0 = 0;

spl0 = 4;
= 3;

/* Channel 1 */

CH__1 = 1;

spl1 = 4;
= 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { evaluateUserFunctionCall } from './utils/evaluateUserFunctionCall.js';
import { suffixInlineReturn } from '../../suffixersAndPrefixers/suffixInlineReturn.js';
import { prefixParam } from '../../suffixersAndPrefixers/prefixParam.js';
import { stripChannelPrefix } from '../../suffixersAndPrefixers/prefixChannel.js';
import { addSemicolonIfNone } from '../../suffixersAndPrefixers/addSemicolonIfNone.js';
import { EEL_LIBRARY_FUNCTION_NAMES } from '../../constants.js';

Expand Down Expand Up @@ -99,16 +100,22 @@
let replacedReturnSource = returnSrc?.src;

for (const [_key, arg] of Object.entries(args)) {
let cleanValue = arg.value;

if (typeof cleanValue === 'string') {
cleanValue = stripChannelPrefix(arg.value);
}

// FIXME this might hurt performance if functions get large

Check warning on line 109 in compiler/src/js2eel/generatorNodes/callExpression/callExpression.ts

View workflow job for this annotation

GitHub Actions / compiler (18.x)

Unexpected 'fixme' comment: 'FIXME this might hurt performance if...'
replacedBodySrc = replacedBodySrc.replaceAll(
`${prefixParam(arg.scopedName)}`,
arg.value
cleanValue
);

if (replacedReturnSource) {
replacedReturnSource = replacedReturnSource.replaceAll(
`${prefixParam(arg.scopedName)}`,
arg.value
cleanValue
);
}
}
Expand Down Expand Up @@ -156,7 +163,7 @@
break;
}
/* c8 ignore start */
// FIXME: Don't know how to test that

Check warning on line 166 in compiler/src/js2eel/generatorNodes/callExpression/callExpression.ts

View workflow job for this annotation

GitHub Actions / compiler (18.x)

Unexpected 'fixme' comment: 'FIXME: Don't know how to test that'
default: {
instance.error(
'UnknownSymbolError',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ out_pin:In 1

/* Channel 0 */

myVar__S4 = spl(0);
CH__0 = 0;

myVar__S4 = spl(CH__0);

/* Channel 1 */

myVar__S5 = spl(1);
CH__1 = 1;

myVar__S5 = spl(CH__1);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,19 @@

/* Channel 0 */

CH__0 = 0;


/* Channel 1 */

CH__1 = 1;




`)
);
expect(result.errors.length).to.equal(2); // FIXME Do not check twice

Check warning on line 131 in compiler/src/js2eel/generatorNodes/callExpression/js2EelLib/eachChannel.spec.ts

View workflow job for this annotation

GitHub Actions / compiler (18.x)

Unexpected 'fixme' comment: 'FIXME Do not check twice'
expect(result.errors[0].type).to.equal('TypeError');
expect(result.errors[1].type).to.equal('TypeError');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const evaluateUserFunctionCall = <ArgName extends string>(
break;
}
case 'Identifier': {
const id = identifier(givenArg, instance);
const id = identifier(givenArg, instance, { isParam: true });
value = id;
rawValue = id;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,87 @@ out_pin:In 1
spl3 = spl0 == 1 ? 3 : -4 * 2;


`)
);
expect(result.errors.length).to.equal(0);
});

it('Consequent is member expression', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: "conditional", inChannels: 2, outChannels: 2 });

const myEelbuf = new EelBuffer(2, 2);

onSample(() => {
spl3 = spl0 === 1 ? myEelbuf[0][1] : -4 * 2;
});`);

expect(result.success).to.equal(true);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL v0.9.1 */

desc:conditional

in_pin:In 0
in_pin:In 1
out_pin:In 0
out_pin:In 1


@init

myEelbuf__B0 = 0 * 2;
myEelbuf__B1 = 1 * 2;
myEelbuf__size = 2;


@sample

spl3 = spl0 == 1 ? myEelbuf__B0[1] : -4 * 2;


`)
);
expect(result.errors.length).to.equal(0);
});

it('Alternate is member expression', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'conditional', inChannels: 2, outChannels: 2 });

const myEelbuf = new EelBuffer(2, 2);

onSample(() => {
spl3 = spl0 === 1 ? 3 : myEelbuf[1][1];
});
`);

expect(result.success).to.equal(true);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL v0.9.1 */

desc:conditional

in_pin:In 0
in_pin:In 1
out_pin:In 0
out_pin:In 1


@init

myEelbuf__B0 = 0 * 2;
myEelbuf__B1 = 1 * 2;
myEelbuf__size = 2;


@sample

spl3 = spl0 == 1 ? 3 : myEelbuf__B1[1];


`)
);
expect(result.errors.length).to.equal(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { literal } from '../literal/literal';
import { identifier } from '../identifier/identifier';
import { unaryExpression } from '../unaryExpression/unaryExpression';
import { binaryExpression } from '../binaryExpression/binaryExpression';
import { memberExpression } from '../memberExpression/memberExpression';

import type { ConditionalExpression } from 'estree';
import type { Js2EelCompiler } from '../../compiler/Js2EelCompiler';
Expand Down Expand Up @@ -51,10 +52,18 @@ export const conditionalExpression = (
);
break;
}
case 'MemberExpression': {
conditionalExpressionSrc += memberExpression(
conditionalExpressionNode,
conditionalExpressionNode.consequent,
instance
);
break;
}
default: {
instance.error(
'TypeError',
`Type ${conditionalExpressionNode.consequent.type} not allowed`,
`Conditional expression: Consequent: Type ${conditionalExpressionNode.consequent.type} not allowed`,
conditionalExpressionNode.consequent
);
}
Expand Down Expand Up @@ -88,10 +97,18 @@ export const conditionalExpression = (
);
break;
}
case 'MemberExpression': {
conditionalExpressionSrc += memberExpression(
conditionalExpressionNode,
conditionalExpressionNode.alternate,
instance
);
break;
}
default: {
instance.error(
'TypeError',
`Type ${conditionalExpressionNode.alternate.type} not allowed`,
`Conditional expression: Alternate: Type ${conditionalExpressionNode.alternate.type} not allowed`,
conditionalExpressionNode.alternate
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { blockStatement } from '../blockStatement/blockStatement.js';

import { registerDeclarationParam } from '../../declarationParams/registerDeclarationParam.js';
import { prefixChannel } from '../../suffixersAndPrefixers/prefixChannel.js';

import type { ArrowFunctionExpression, FunctionExpression } from 'estree';
import type { Js2EelCompiler } from '../../compiler/Js2EelCompiler.js';
Expand Down Expand Up @@ -103,7 +104,11 @@ export const functionExpression = <ParamName extends string>(
i < instance.getChannels().inChannels;
/* FIXME is this sufficient? what about out? */ i++
) {
callbackSrc += `/* Channel ${i} */\n\n`;
callbackSrc += `/* Channel ${i} */

${prefixChannel(i)} = ${i};

`;

instance.setCurrentChannel(i);

Expand Down Expand Up @@ -190,7 +195,7 @@ export const functionExpression = <ParamName extends string>(
default: {
instance.error(
'GenericError',
"Function expressions are only allowed as arguments to onInit(), onBlock(), onSample(), eachChannel() and onSlider()",
'Function expressions are only allowed as arguments to onInit(), onBlock(), onSample(), eachChannel() and onSlider()',
functionExpression
);
}
Expand Down
37 changes: 37 additions & 0 deletions compiler/src/js2eel/generatorNodes/identifier/identifier.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect } from 'chai';
import { Js2EelCompiler } from '../../compiler/Js2EelCompiler';
import { testEelSrc } from '../../test/helpers';

describe('identifier()', () => {
it('error if using EelArray or EelBuffer without accessors', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'mono_delay', inChannels: 2, outChannels: 2 });

const myEelArray = new EelArray(2, 2);
const myVar = myEelArray;`);

expect(result.success).to.equal(false);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL v0.9.1 */

desc:mono_delay

in_pin:In 0
in_pin:In 1
out_pin:In 0
out_pin:In 1


@init

myVar = ?ä__DENY_COMPILATION;


`)
);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('GenericError');
});

});
26 changes: 23 additions & 3 deletions compiler/src/js2eel/generatorNodes/identifier/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import {
suffixScopeBySymbol
} from '../../suffixersAndPrefixers/suffixScope.js';
import { prefixParam } from '../../suffixersAndPrefixers/prefixParam.js';
import { EEL_LIBRARY_VARS } from '../../constants.js';
import { prefixChannel } from '../../suffixersAndPrefixers/prefixChannel.js';
import { EEL_LIBRARY_VARS, JSFX_DENY_COMPILATION } from '../../constants.js';

import type { Identifier } from 'estree';
import type { Js2EelCompiler } from '../../compiler/Js2EelCompiler.js';

export const identifier = (identifier: Identifier, instance: Js2EelCompiler): string => {
export const identifier = (
identifier: Identifier,
instance: Js2EelCompiler,
additionalData?: { isObjectInMemberExpression?: boolean; isParam?: boolean }
): string => {
if (EEL_LIBRARY_VARS.has(identifier.name.toLowerCase())) {
// We can lowercase here because there's only lowercase symbols in EEL
return identifier.name;
Expand All @@ -20,6 +25,21 @@ export const identifier = (identifier: Identifier, instance: Js2EelCompiler): st

const declaredSymbol = getSymbolInNextUpScope(identifier.name, instance);

if (
(declaredSymbol?.symbol.currentAssignment?.type === 'EelArray' ||
declaredSymbol?.symbol.currentAssignment?.type === 'EelBuffer') &&
(!additionalData?.isObjectInMemberExpression &&
!additionalData?.isParam)
) {
instance.error(
'GenericError',
'EelBuffer/EelArray cannot be used without accessors: ' + identifier.name,
identifier
);

return JSFX_DENY_COMPILATION;
}

const sampleParamsMap = instance.getEachChannelParams();
const inSampleParamsMap = Object.values(sampleParamsMap).includes(identifier.name);

Expand All @@ -39,7 +59,7 @@ export const identifier = (identifier: Identifier, instance: Js2EelCompiler): st
const currentChannel = instance.getCurrentChannel();

if (sampleParamsMap.channelIdentifier === identifier.name) {
identifierSrc = currentChannel.toString();
identifierSrc = `${prefixChannel(currentChannel)}`;
} else {
identifierSrc = `spl${currentChannel}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { EelArray } from '../../../types.js';

export const eelArrayMemberCall = (
eelArray: EelArray,
calleeObject: Identifier,
calleeProperty: Identifier | PrivateIdentifier,
instance: Js2EelCompiler
): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const memberExpressionCall = (
} else if (eelArray) {
parentCallExpressionSrc += eelArrayMemberCall(
eelArray,
calleeObject,
calleeProperty,
instance
);
Expand Down
Loading
Loading