Here is the complete, zero-dependency Node.js implementation of the compiler's
core engine.
This script parses the raw markdown, builds the dependency graph, validates
it, and uses a Depth-First Search (DFS) algorithm to topologically sort the
nodes so the offline AI will always be prompted in the correct bottom-up
order.
The AST Compiler (compiler.js)
You can save this as compiler.js and run it natively using node compiler.js.
* AST-MD Compiler Pipeline
* Parses "CEO Spec" Markdown into an execution order for AI coding agents.
*/
function parseAST(markdownText) {
const lines = markdownText.split('
');
const nodes = new Map();
let currentNode = null;
// Regex to match root "@UNIVERSE: NAME" or standard "## NAME"
const nodeRegex = /^(?:@UNIVERSE:\s+|#+\s+)([A-Z0-9_]+)/;
// Regex to match properties like "> depends_on: [NODE_A]"
const propRegex = /^>\s+([a-z_]+):\s+(.*)$/;
for (let line of lines) {
line = line.trim();
if (!line) continue;
const nodeMatch = line.match(nodeRegex);
if (nodeMatch) {
const nodeId = nodeMatch[1];
currentNode = {
id: nodeId,
description: '',
type: '',
tags: [],
depends_on: []
};
nodes.set(nodeId, currentNode);
continue;
}
if (currentNode) {
const propMatch = line.match(propRegex);
if (propMatch) {
const key = propMatch[1];
const value = propMatch[2];
// Parse array properties (tags, depends_on)
if (key === 'tags' || key === 'depends_on') {
const arrayMatch = value.match(/\[(.*?)\]/);
if (arrayMatch && arrayMatch[1]) {
currentNode[key] = arrayMatch[1].split(',').map(s =>
s.trim()).filter(Boolean);
}
} else {
// Parse string properties (description, type)
currentNode[key] = value;
}
}
}
}
return nodes;
}
function buildAndValidateGraph(nodesMap) {
console.log(`
Validating DAG containing ${nodesMap.size} nodes...`);
for (const [id, node] of nodesMap.entries()) {
for (const dep of node.depends_on) {
if (!nodesMap.has(dep)) {
// The CEO Spec referenced a node that doesn't exist. Fail
hard.
throw new Error(`[FATAL] Compilation failed: Node '${id}'
depends on missing node '${dep}'`);
}
}
}
console.log("-> Graph validation passed. No missing dependencies.");
return nodesMap;
}
function sortBottomUp(nodesMap) {
const executionOrder = [];
const visited = new Set();
const visiting = new Set(); // Used to detect circular dependencies (A ->
B -> A)
// Depth-First Search recursive function
function visit(nodeId) {
if (visiting.has(nodeId)) {
throw new Error(`[FATAL] Circular dependency detected at node
'${nodeId}'`);
}
if (!visited.has(nodeId)) {
visiting.add(nodeId);
const node = nodesMap.get(nodeId);
// Recursively visit all dependencies FIRST
for (const dep of node.depends_on) {
visit(dep);
}
visiting.delete(nodeId);
visited.add(nodeId);
// Push to execution list only AFTER all dependencies are resolved
executionOrder.push(node);
}
}
// Trigger the DFS for every node in the graph
for (const nodeId of nodesMap.keys()) {
visit(nodeId);
}
return executionOrder;
}
const mockCeoSpec = ''; // insert
try {
const rawNodes = parseAST(mockCeoSpec);
const validGraph = buildAndValidateGraph(rawNodes);
const buildOrder = sortBottomUp(validGraph);
console.log("
--- AI COMPILATION ORDER (Bottom-Up) ---");
buildOrder.forEach((node, index) => {
const deps = node.depends_on.length > 0 ? ` (Needs: ${node.depends_on.j
oin(', ')})` : ' (Leaf Node)';
console.log(`${index + 1}. Build: ${node.id}${deps}`);
});
} catch (error) {
console.error(error.message);
}