strudel/docs/dist/shapeshifter.js
Felix Roos 57d85ee7de build
2022-02-18 11:13:19 +01:00

30 lines
1.1 KiB
JavaScript

import { parseScript } from './shift-parser/index.js'; // npm module does not work in the browser
import traverser from './shift-traverser/index.js'; // npm module does not work in the browser
const { replace } = traverser;
import { LiteralStringExpression, IdentifierExpression } from '../_snowpack/pkg/shift-ast.js';
import codegen from '../_snowpack/pkg/shift-codegen.js';
const isNote = (name) => /^[a-gC-G][bs]?[0-9]$/.test(name);
export default (code) => {
const ast = parseScript(code);
const shifted = replace(ast, {
enter(node, parent) {
// replace identifiers that are a note with a note string
if (node.type === 'IdentifierExpression') {
if (isNote(node.name)) {
const value = node.name[1] === 's' ? node.name.replace('s', '#') : node.name;
return new LiteralStringExpression({ value });
}
if (node.name === 'r') {
return new IdentifierExpression({ name: 'silence' });
}
}
},
});
return codegen(shifted);
};
// TODO: turn x.groove['[~ x]*2'] into x.groove('[~ x]*2'.m)
// and ['c1*2'].xx into 'c1*2'.m.xx ??
// or just all templated strings?? x.groove(`[~ x]*2`)