mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-26 04:58:27 +00:00
fix mini notation multiline highlight
+ started operator overloading
This commit is contained in:
parent
26fe6c8f61
commit
3b248ae94c
@ -32,6 +32,7 @@ Object.assign(globalThis, bootstrapped, Tone, toneHelpers);
|
|||||||
|
|
||||||
export const evaluate: any = (code: string) => {
|
export const evaluate: any = (code: string) => {
|
||||||
const shapeshifted = shapeshifter(code); // transform syntactically correct js code to semantically usable code
|
const shapeshifted = shapeshifter(code); // transform syntactically correct js code to semantically usable code
|
||||||
|
// console.log('shapeshifted', shapeshifted);
|
||||||
let evaluated = eval(shapeshifted);
|
let evaluated = eval(shapeshifted);
|
||||||
if (typeof evaluated === 'function') {
|
if (typeof evaluated === 'function') {
|
||||||
evaluated = evaluated();
|
evaluated = evaluated();
|
||||||
|
|||||||
@ -10,7 +10,7 @@ const { Pattern } = strudel;
|
|||||||
const isNote = (name) => /^[a-gC-G][bs]?[0-9]$/.test(name);
|
const isNote = (name) => /^[a-gC-G][bs]?[0-9]$/.test(name);
|
||||||
|
|
||||||
const addLocations = true;
|
const addLocations = true;
|
||||||
export const addMiniLocations = false;
|
export const addMiniLocations = true;
|
||||||
|
|
||||||
export default (code) => {
|
export default (code) => {
|
||||||
const ast = parseScriptWithLocation(code);
|
const ast = parseScriptWithLocation(code);
|
||||||
@ -24,16 +24,39 @@ export default (code) => {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
const grandparent = parents[parents.length - 2];
|
const grandparent = parents[parents.length - 2];
|
||||||
const isPatternArg = (parent) =>
|
const isTimeCat = parent?.type === 'ArrayExpression' && isPatternFactory(grandparent);
|
||||||
parent?.type === 'CallExpression' && Object.keys(Pattern.prototype.factories).includes(parent.callee.name);
|
const isMarkable = isPatternFactory(parent) || isTimeCat;
|
||||||
const isTimeCat = parent?.type === 'ArrayExpression' && isPatternArg(grandparent);
|
// operator overloading => still not done
|
||||||
const isMarkable = isPatternArg(parent) || isTimeCat;
|
const operators = {
|
||||||
|
'*': 'fast',
|
||||||
|
'/': 'slow',
|
||||||
|
'&': 'stack',
|
||||||
|
'&&': 'append',
|
||||||
|
};
|
||||||
|
if (
|
||||||
|
node.type === 'BinaryExpression' &&
|
||||||
|
operators[node.operator] &&
|
||||||
|
['LiteralNumericExpression', 'LiteralStringExpression', 'IdentifierExpression'].includes(node.right?.type) &&
|
||||||
|
canBeOverloaded(node.left)
|
||||||
|
) {
|
||||||
|
let arg = node.left;
|
||||||
|
if (node.left.type === 'IdentifierExpression') {
|
||||||
|
arg = wrapReify(node.left);
|
||||||
|
}
|
||||||
|
return new CallExpression({
|
||||||
|
callee: new StaticMemberExpression({
|
||||||
|
property: operators[node.operator],
|
||||||
|
object: wrapReify(arg),
|
||||||
|
}),
|
||||||
|
arguments: [node.right],
|
||||||
|
});
|
||||||
|
}
|
||||||
// replace pseudo note variables
|
// replace pseudo note variables
|
||||||
if (node.type === 'IdentifierExpression') {
|
if (node.type === 'IdentifierExpression') {
|
||||||
if (isNote(node.name)) {
|
if (isNote(node.name)) {
|
||||||
const value = node.name[1] === 's' ? node.name.replace('s', '#') : node.name;
|
const value = node.name[1] === 's' ? node.name.replace('s', '#') : node.name;
|
||||||
if (addLocations && isMarkable) {
|
if (addLocations && isMarkable) {
|
||||||
return addPureWithLocation(value, node, ast.locations, nodesWithLocation);
|
return reifyWithLocation(value, node, ast.locations, nodesWithLocation);
|
||||||
}
|
}
|
||||||
return new LiteralStringExpression({ value });
|
return new LiteralStringExpression({ value });
|
||||||
}
|
}
|
||||||
@ -43,7 +66,7 @@ export default (code) => {
|
|||||||
}
|
}
|
||||||
if (addLocations && node.type === 'LiteralStringExpression' && isMarkable) {
|
if (addLocations && node.type === 'LiteralStringExpression' && isMarkable) {
|
||||||
// console.log('add', node);
|
// console.log('add', node);
|
||||||
return addPureWithLocation(node.value, node, ast.locations, nodesWithLocation);
|
return reifyWithLocation(node.value, node, ast.locations, nodesWithLocation);
|
||||||
}
|
}
|
||||||
if (!addMiniLocations) {
|
if (!addMiniLocations) {
|
||||||
return node;
|
return node;
|
||||||
@ -73,6 +96,24 @@ export default (code) => {
|
|||||||
return codegen(shifted);
|
return codegen(shifted);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function wrapReify(node) {
|
||||||
|
return new CallExpression({
|
||||||
|
callee: new IdentifierExpression({
|
||||||
|
name: 'reify',
|
||||||
|
}),
|
||||||
|
arguments: [node],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function isPatternFactory(node) {
|
||||||
|
return node?.type === 'CallExpression' && Object.keys(Pattern.prototype.factories).includes(node.callee.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function canBeOverloaded(node) {
|
||||||
|
return (node.type === 'IdentifierExpression' && isNote(node.name)) || isPatternFactory(node);
|
||||||
|
// TODO: support sequence(c3).transpose(3).x.y.z
|
||||||
|
}
|
||||||
|
|
||||||
// turn node into withLocationOffset(node, location)
|
// turn node into withLocationOffset(node, location)
|
||||||
function wrapLocationOffset(node, stringNode, locations, nodesWithLocation) {
|
function wrapLocationOffset(node, stringNode, locations, nodesWithLocation) {
|
||||||
// console.log('wrapppp', stringNode);
|
// console.log('wrapppp', stringNode);
|
||||||
@ -89,15 +130,15 @@ function wrapLocationOffset(node, stringNode, locations, nodesWithLocation) {
|
|||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
// turns node in pure(value).withLocation(location), where location is the node's location in the source code
|
// turns node in reify(value).withLocation(location), where location is the node's location in the source code
|
||||||
// with this, the pure pattern can pass its location to the event, to know where to highlight when it's active
|
// with this, the reified pattern can pass its location to the event, to know where to highlight when it's active
|
||||||
function addPureWithLocation(value, node, locations, nodesWithLocation) {
|
function reifyWithLocation(value, node, locations, nodesWithLocation) {
|
||||||
// console.log('addPure', value, node);
|
// console.log('reifyWithLocation', value, node);
|
||||||
const withLocation = new CallExpression({
|
const withLocation = new CallExpression({
|
||||||
callee: new StaticMemberExpression({
|
callee: new StaticMemberExpression({
|
||||||
object: new CallExpression({
|
object: new CallExpression({
|
||||||
callee: new IdentifierExpression({
|
callee: new IdentifierExpression({
|
||||||
name: 'pure',
|
name: 'reify',
|
||||||
}),
|
}),
|
||||||
arguments: [new LiteralStringExpression({ value })],
|
arguments: [new LiteralStringExpression({ value })],
|
||||||
}),
|
}),
|
||||||
@ -118,7 +159,7 @@ function getLocationObject(node, locations) {
|
|||||||
console.log("locationAST", locationAST);*/
|
console.log("locationAST", locationAST);*/
|
||||||
|
|
||||||
/*const callAST = parseScript(
|
/*const callAST = parseScript(
|
||||||
`pure(${node.name}).withLocation(${JSON.stringify(
|
`reify(${node.name}).withLocation(${JSON.stringify(
|
||||||
ast.locations.get(node)
|
ast.locations.get(node)
|
||||||
)})`
|
)})`
|
||||||
).statements[0].expression;*/
|
).statements[0].expression;*/
|
||||||
|
|||||||
14
strudel.mjs
14
strudel.mjs
@ -875,21 +875,25 @@ Pattern.prototype.bootstrap = () => {
|
|||||||
// this is wrapped around mini patterns to offset krill parser location into the global js code space
|
// this is wrapped around mini patterns to offset krill parser location into the global js code space
|
||||||
function withLocationOffset(pat, offset) {
|
function withLocationOffset(pat, offset) {
|
||||||
// console.log('with offfset',pat,offset);
|
// console.log('with offfset',pat,offset);
|
||||||
|
let startLine;
|
||||||
return pat.fmap((value) => {
|
return pat.fmap((value) => {
|
||||||
value = typeof value === 'object' && !Array.isArray(value) ? value : { value };
|
value = typeof value === 'object' && !Array.isArray(value) ? value : { value };
|
||||||
const locations = (value.locations || []).map(({ start, end }) => ({
|
let locations = (value.locations || []);
|
||||||
|
startLine = startLine || locations[0].start.line;
|
||||||
|
locations = locations.map(({ start, end }) => {
|
||||||
|
const colOffset = startLine === end.line ? offset.start.column : 0;
|
||||||
|
return {
|
||||||
start: {
|
start: {
|
||||||
...start,
|
...start,
|
||||||
line: start.line - 1 + (offset.start.line - 1) + 1,
|
line: start.line - 1 + (offset.start.line - 1) + 1,
|
||||||
column: start.column - 1 + offset.start.column,
|
column: start.column - 1 + colOffset,
|
||||||
},
|
},
|
||||||
end: {
|
end: {
|
||||||
...end,
|
...end,
|
||||||
line: end.line - 1 + (offset.start.line - 1) + 1,
|
line: end.line - 1 + (offset.start.line - 1) + 1,
|
||||||
column: end.column - 1 + offset.start.column,
|
column: end.column - 1 + colOffset,
|
||||||
},
|
},
|
||||||
}));
|
}});
|
||||||
// console.log(value.value, 'location', locations[0]);
|
|
||||||
return {...value, locations }
|
return {...value, locations }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user