import { compile, nodesToObjects } from '@datamoc/mwg/mwl';
import type { MwlTagSchema } from '@datamoc/mwg/mwl';
const schemas = {
bridge: { attributes: { budget: 'number' }, children: ['anchor'] },
anchor: { attributes: { x: 'number', fixed: 'boolean' }, children: [] },
} satisfies Record<string, MwlTagSchema>;
const game = compile("[{ tag: 'bridge', budget: 120, children: [{ tag: 'anchor', x: 4, fixed: true }] }]", { schemas });
const result = nodesToObjects(game.roots, schemas);
if (result.diagnostics.length) throw new Error(JSON.stringify(result.diagnostics));
console.log(result.value[0].budget); // 120 as a number
Converts compiled nodes into plain objects using the same schema as
compile. Attributes become fields; numbers and booleans are converted by their declared types. Children are grouped by tag in source order. A tag withcardinality.max: 1becomes one object, otherwise an array. Absent children and optional attributes stay absent. Strings, references, enum values and coordinates stay strings. The result carries no parser metadata and can be written directly as JSON. Diagnostics must be checked before use.