123456789101112131415161718192021222324252627282930313233343536 |
- module.exports = class Compiler {
- constructor(tree, context) {
- this.tree = tree
- this.context = context
- this.result = ''
- }
-
- compile() {
- this.tree.forEach(node => {
- if (node.type === 'functionCall') {
- const attributes = node.args.map(arg => `${arg.attributeName}="${this.compileAttribute(arg.attributeValue)}"`)
- const compiler = new Compiler(node.subtree, this.context)
- const content = compiler.compile()
- this.result += `<${node.functionName}${attributes.length ? ' ' : ''}${attributes.join(' ')}>${content}</${node.functionName}>`
- } else if (node.type === 'string') {
- this.result += node.content
- } else if (node.type === 'identifier') {
- this.result += this.lookup(node.name)
- }
- })
-
- return this.result.trim()
- }
-
- compileAttribute(attribute) {
- if (attribute.type === 'string') {
- return attribute.content
- } else if (attribute.type === 'identifier') {
- return this.lookup(attribute.name)
- }
- }
-
- lookup(name) {
- return this.context[name]
- }
- }
|