123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 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 '<lambda>'
- 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 += `</${node.name}>`
- }
-
- return el
- }
- }
- }
|