const MOTION_LAWS = { harmonic: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*((1-cos(pi*t/D))/2)", }, cycloid: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*((t/D)-(sin(2*pi*t/D)/(2*pi)))", }, poly345: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*(10*(t/D)^3-15*(t/D)^4+6*(t/D)^5)", }, poly4567: { parameters: ["h0", "h1", "D", "t"], expression: "h0+(h1-h0)*(35*(t/D)^4-84*(t/D)^5+70*(t/D)^6-20*(t/D)^7)", }, }; function escapeRegExp(value) { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function splitArguments(source) { const values = []; let start = 0; let depth = 0; for (let index = 0; index < source.length; index += 1) { const character = source[index]; if (character === "(") depth += 1; if (character === ")") depth -= 1; if (character === "," && depth === 0) { values.push(source.slice(start, index).trim()); start = index + 1; } } values.push(source.slice(start).trim()); return values; } function replaceTokens(source, replacements) { return Object.entries(replacements) .sort(([left], [right]) => right.length - left.length) .reduce( (text, [name, value]) => text.replace(new RegExp(`\\b${escapeRegExp(name)}\\b`, "g"), value), source, ); } function selectConditionalBranches(source) { const conditional = /@if\s+([^\r\n]+)\r?\n([\s\S]*?)(?:@else\r?\n([\s\S]*?))?@endif/g; let result = source; let previous; do { previous = result; result = result.replace(conditional, (_match, condition, yes, no = "") => { const [left, right] = condition.split("==").map((value) => value.trim()); return left === right ? yes : no; }); } while (result !== previous); return result; } function expandFollower(macroSource, callLine) { const declaration = macroSource.match(/^\s*@macro\s+([A-Za-z_]\w*)\(([^)]*)\)\s*\{/); const call = callLine.match(/^\s*@([A-Za-z_]\w*)\((.*)\)\s*$/); if (!declaration || !call || declaration[1] !== call[1]) { throw new Error("The generated follower call does not match its macro file."); } const parameters = splitArguments(declaration[2]).map((parameter) => { const equals = parameter.indexOf("="); return equals < 0 ? { name: parameter.trim(), fallback: undefined } : { name: parameter.slice(0, equals).trim(), fallback: parameter.slice(equals + 1).trim() }; }); const argumentsList = splitArguments(call[2]); const replacements = {}; parameters.forEach((parameter, index) => { const value = argumentsList[index] || parameter.fallback; if (!value) throw new Error(`The ${declaration[1]} call is missing ${parameter.name}.`); replacements[parameter.name] = value; }); const bodyStart = declaration.index + declaration[0].length; const bodyEnd = macroSource.lastIndexOf("}"); if (bodyEnd < bodyStart) throw new Error("The follower macro file is incomplete."); const substituted = replaceTokens(macroSource.slice(bodyStart, bodyEnd), replacements); return selectConditionalBranches(substituted) .split(/\r?\n/) .map((line) => line.trim()) .filter((line) => line && !line.startsWith("@")) .join("\n"); } function findClosingParenthesis(source, openingIndex) { let depth = 0; for (let index = openingIndex; index < source.length; index += 1) { if (source[index] === "(") depth += 1; if (source[index] === ")") { depth -= 1; if (depth === 0) return index; } } return -1; } function expandMotionLaw(source, name, law) { let result = source; while (result.includes(`${name}(`)) { const start = result.lastIndexOf(`${name}(`); const opening = start + name.length; const closing = findClosingParenthesis(result, opening); if (closing < 0) throw new Error(`The ${name} motion expression is incomplete.`); const argumentsList = splitArguments(result.slice(opening + 1, closing)); if (argumentsList.length !== law.parameters.length) { throw new Error(`The ${name} motion expression has the wrong number of arguments.`); } const replacements = Object.fromEntries( law.parameters.map((parameter, index) => [parameter, `(${argumentsList[index]})`]), ); const expanded = `(${replaceTokens(law.expression, replacements)})`; result = `${result.slice(0, start)}${expanded}${result.slice(closing + 1)}`; } return result; } function expandMotionLaws(source) { return Object.entries(MOTION_LAWS).reduce( (text, [name, law]) => expandMotionLaw(text, name, law), source, ); } export function compileProgram(macroSource, generatedBody) { const lines = generatedBody.split(/\r?\n/).map((line) => line.trim()).filter(Boolean); if (lines.length !== 3) throw new Error("The model must return exactly three CoreGX lines."); const geometry = expandFollower(macroSource, lines[0]); const motion = expandMotionLaws(lines[1]); return `${geometry}\n${motion}\n${lines[2]}\n`; }