UNPKG

awv3

Version:
29 lines (25 loc) 796 B
/* * Abstract syntax tree of the ClassCad language. * API is closely based on python ast module, see there for class/method help. */ export default class Ast { constructor(...args) { let i = 0; for (let [name, field] of this.iter_fields()) this[name] = args[i++]; } *iter_fields() { for (let field of this.constructor._fields) yield [field, this[field]]; } *iter_child_nodes() { for (let [name, field] of this.iter_fields()) if (field instanceof Ast) yield field; else if (field instanceof Array) for (let item of field) if (item instanceof Ast) yield item; } *walk() { yield this; for (let child of this.iter_child_nodes()) yield* child.walk(); } }