A templating language that looks like Lisp and compiles to HTML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiler.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const AST = require('./ast')
  2. const Env = require('./env')
  3. module.exports = class Compiler {
  4. constructor(env = null) {
  5. this.result = ''
  6. if (env) {
  7. this.env = env
  8. } else {
  9. this.env = new Env()
  10. }
  11. }
  12. compile(tree) {
  13. tree.forEach(node => {
  14. this.result += this.compileNode(node)
  15. })
  16. return this.result
  17. }
  18. compileNode(node) {
  19. switch (node.constructor) {
  20. case AST.Number:
  21. case AST.String:
  22. return node.value
  23. case AST.Boolean:
  24. return node.value === true ? '#t' : '#f'
  25. case AST.Identifier:
  26. return this.compileNode(this.env.get(node.name))
  27. case AST.Conditional:
  28. let condition = this.compileNode(node.condition)
  29. if (condition) {
  30. return this.compileNode(node.ifCase)
  31. } else {
  32. return this.compileNode(node.elseCase)
  33. }
  34. case AST.Application:
  35. if (node.function.constructor === AST.Identifier) {
  36. let f = this.env.get(node.function.name)
  37. return this.compileNode(f(...node.args.map(arg => this.compileNode(arg))))
  38. } else if (node.function.constructor === AST.Lambda) {
  39. let env = new Env(this.env)
  40. node.function.parameters.forEach((param, index) => {
  41. env.set(param.name, node.args[index])
  42. })
  43. let compiler = new Compiler(env)
  44. return compiler.compileNode(node.function.body)
  45. }
  46. return ''
  47. }
  48. }
  49. }