const AST = require('./ast') module.exports = class Compiler { compile(tree) { let output = '' tree.forEach(node => { output += this.compileNode(node) }) return output } compileNode(node) { switch (node.constructor) { case AST.Number: case AST.String: return node.value case AST.Lambda: return '' case AST.HTMLElement: let el = `<${node.name}` if (node.attributes.length > 0) { el += ' ' el += node.attributes.map(attr => `${attr.name}="${this.compileNode(attr.value)}"`).join(' ') } el += '>' if (node.contents.length > 0) { el += node.contents.map(content => this.compileNode(content)).join('') } if (node.selfClosing === false) { el += `` } return el } } }