12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const AST = require('./ast')
- const Env = require('./env')
-
- module.exports = class Compiler {
- constructor(env = null) {
- this.result = ''
-
- if (env) {
- this.env = env
- } else {
- this.env = new Env()
- }
- }
-
- compile(tree) {
- tree.forEach(node => {
- this.result += this.compileNode(node)
- })
-
- return this.result
- }
-
- compileNode(node) {
- switch (node.constructor) {
- case AST.Number:
- case AST.String:
- return node.value
- case AST.Identifier:
- return this.compileNode(this.env.get(node.name))
- case AST.Conditional:
- let condition = this.compileNode(node.condition)
-
- if (condition) {
- return this.compileNode(node.ifCase)
- } else {
- return this.compileNode(node.elseCase)
- }
- case AST.Application:
- if (node.function.constructor === AST.Identifier) {
- let f = this.env.get(node.function.name)
- return f(...node.args.map(arg => this.compileNode(arg)))
- } else if (node.function.constructor === AST.Lambda) {
- let env = new Env(this.env)
- node.function.parameters.forEach((param, index) => {
- env.set(param.name, node.args[index])
- })
-
- let compiler = new Compiler(env)
- return compiler.compileNode(node.function.body)
- }
-
- return ''
- }
- }
- }
|