A performance-oriented Lisp-like language where I can have my cake, and eat it (too)
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.

78 lines
3.9 KiB

  1. ;; TODO: This should be builtin to macros and generators
  2. (defmacro destructure-arguments ()
  3. (var end-invocation-index int (FindCloseParenTokenIndex tokens startTokenIndex))
  4. ;; Find the end invocation for the caller, not us
  5. (tokenize-push output
  6. (var destr-end-invocation-index int
  7. (FindCloseParenTokenIndex tokens startTokenIndex)))
  8. (var start-args-index int (+ 2 startTokenIndex))
  9. (var current-arg-index int start-args-index)
  10. ;; Invocation is 0, so skip it
  11. (var num-destructured-args int 1)
  12. (while (< current-arg-index end-invocation-index)
  13. (var current-arg (* (const Token)) (addr (at current-arg-index tokens)))
  14. (var num-destructured-args-token Token (array TokenType_Symbol (std::to_string num-destructured-args)
  15. "test/Macros.cake" 1 1 1))
  16. (unless (ExpectTokenType "destructure-arguments" (at current-arg-index tokens) TokenType_Symbol)
  17. (return false))
  18. (var destructured-arg-name-token Token (array TokenType_String (field (at current-arg-index tokens) contents)
  19. "test/Macros.cake" 1 1 1))
  20. (tokenize-push output
  21. (var (token-splice current-arg) int
  22. (getExpectedArgument
  23. ;; Use the name of the requested argument as the message
  24. (token-splice (addr destructured-arg-name-token))
  25. tokens startTokenIndex
  26. (token-splice (addr num-destructured-args-token))
  27. destr-end-invocation-index))
  28. (when (= -1 (token-splice current-arg)) (return false)))
  29. (++ num-destructured-args)
  30. (set current-arg-index
  31. (getNextArgument tokens current-arg-index end-invocation-index)))
  32. (return true))
  33. ;; Assumes tokens is the array of tokens
  34. (defmacro quick-token-at ()
  35. (destructure-arguments name index)
  36. (tokenize-push output (var (token-splice (addr (at name tokens))) (& (const Token))
  37. (at (token-splice (addr (at index tokens))) tokens)))
  38. (return true))
  39. ;; The first significant generator written in Cakelisp!
  40. ;; Now built-in
  41. ;; (defgenerator def-type-alias ()
  42. ;; (destructure-arguments name-index type-index)
  43. ;; (quick-token-at name-token name-index)
  44. ;; (quick-token-at invocation-token (+ 1 startTokenIndex))
  45. ;; ;; Make sure the type is valid before outputting anything
  46. ;; (var type-output (<> std::vector StringOutput))
  47. ;; (var type-after-name-output (<> std::vector StringOutput))
  48. ;; (unless (tokenizedCTypeToString_Recursive tokens type-index true type-output type-after-name-output)
  49. ;; (return false))
  50. ;; (addModifierToStringOutput (on-call type-output back) StringOutMod_SpaceAfter)
  51. ;; (addStringOutput (field output source) "typedef" StringOutMod_SpaceAfter (addr invocation-token))
  52. ;; ;; TODO: Add ability to define typedefs in header
  53. ;; (PushBackAll (field output source) type-output)
  54. ;; ;; Evaluate name
  55. ;; (var expressionContext EvaluatorContext context)
  56. ;; (set (field expressionContext scope) EvaluatorScope_ExpressionsOnly)
  57. ;; (unless (= 0 (EvaluateGenerate_Recursive environment expressionContext tokens name-index output))
  58. ;; (return false))
  59. ;; ;; Yep, believe it or not, C typedefs have the length of the array after the new type name
  60. ;; (PushBackAll (field output source) type-after-name-output)
  61. ;; (addLangTokenOutput (field output source) StringOutMod_EndStatement (addr invocation-token))
  62. ;; (return true))
  63. (defmacro array-size ()
  64. (destructure-arguments array-index)
  65. (quick-token-at array-token array-index)
  66. ;; This should evaluate its argument, but I'm just hacking it in right now anyways
  67. (unless (ExpectTokenType "array-size" array-token TokenType_Symbol)
  68. (return false))
  69. (tokenize-push output (/ (sizeof (token-splice (addr array-token)))
  70. (sizeof (at 0 (token-splice (addr array-token))))))
  71. (return true))