nodes.lisp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "This library provides a series of methods for interacting with the
  2. internal representation of nodes."
  3. (import core/prelude ())
  4. (define-native visit-node
  5. "Visit NODE with VISITOR.
  6. VISITOR should be a function which accepts the current node and the
  7. visitor. This is called before traversing the child nodes. You can
  8. return false to not visit them."
  9. :bind-to "_compiler['visit-node']")
  10. (define-native visit-nodes
  11. "Visit a list of NODES, starting at IDX, using the specified VISITOR.
  12. See [[visit-node]] for more information about the VISITOR."
  13. :bind-to "_compiler['visit-nodes']")
  14. (define-native traverse-node
  15. "Traverse NODE with VISITOR.
  16. VISITOR should be a function which accepts the current node and the
  17. visitor. It should return the replacement node, or the current node
  18. if no changes should be made."
  19. :bind-to "_compiler['traverse-node']")
  20. (define-native traverse-nodes
  21. "Traverse a list of NODES, starting at IDX, using the specified VISITOR.
  22. See [[traverse-node]] for more information about the VISITOR."
  23. :bind-to "_compiler['traverse-nodes']")
  24. (define-native symbol->var
  25. "Extract the variable from the given SYMBOL.
  26. This will work with quasi-quoted symbols, and those from resolved
  27. ASTs. You should not use this on macro arguments as it will not
  28. return anything useful."
  29. :bind-to "_compiler['symbol->var']")
  30. (define-native var->symbol
  31. "Create a new symbol referencing the given VARIABLE."
  32. :bind-to "_compiler['var->symbol']")
  33. (defun fix-symbol (symbol)
  34. "Convert the quasi-quoted SYMBOL into a fully resolved one."
  35. (assert-type! symbol symbol)
  36. (with (var (symbol->var symbol))
  37. (if var
  38. (var->symbol var)
  39. (error! "Variable is not defined"))))
  40. (define-native builtin?
  41. "Determine whether the specified NODE is the given BUILTIN.
  42. ### Example
  43. ```cl :no-test
  44. > (builtin? (symbol->var `lambda) :lambda)
  45. out = true
  46. ```"
  47. :bind-to "_compiler['builtin?']")
  48. (define-native builtin
  49. "Get the builtin with the given NAME."
  50. :bind-to "_compiler['builtin']")
  51. (define-native constant?
  52. "Determine whether the specified NODE is a constant."
  53. :bind-to "_compiler['constant?']")
  54. (define-native node->val
  55. "Gets the constant value of NODE."
  56. :bind-to "_compiler['node->val']")
  57. (define-native val->node
  58. "Gets the node representation of the constant VALUE."
  59. :bind-to "_compiler['val->node']")
  60. (define-native node-contains-var?
  61. "Determine whether NODE contains a reference to the given VAR."
  62. :bind-to "_compiler['node-contains-var?']")
  63. (define-native node-contains-vars?
  64. "Determine whether NODE contains a reference to any of the given VARS.
  65. VARS must be a struct, mapping variable names to `true`."
  66. :bind-to "_compiler['node-contains-vars?']")