precedence.lisp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. (import test ())
  2. (import tests/compiler/codegen/codegen-helpers ())
  3. (describe "The codegen"
  4. (section "will correctly wrap calls"
  5. (it "of conditions"
  6. (affirm-codegen
  7. '(((cond
  8. [foo bar]
  9. [true foo]) 1 2))
  10. "return (foo and bar)(1, 2)")
  11. (affirm-codegen
  12. '(((cond
  13. [foo foo]
  14. [true bar]) 1 2))
  15. "return (foo or bar)(1, 2)")
  16. (affirm-codegen
  17. '(((cond
  18. [foo false]
  19. [true true]) 1 2))
  20. "return (not foo)(1, 2)"))
  21. (it "of statements"
  22. (affirm-codegen
  23. '((foo ((lambda () 1))))
  24. "return foo((function()
  25. return 1
  26. end)())")
  27. (affirm-codegen
  28. '((((lambda (x) x) (cond [true 1])) 1 2))
  29. "return (function()
  30. local x
  31. do
  32. x = 1
  33. end
  34. return x
  35. end)()(1, 2)")
  36. (affirm-codegen
  37. '((((lambda (x) (cond [x x] [true 2])) (cond [true 1])) 1 2))
  38. "return (function()
  39. local x
  40. do
  41. x = 1
  42. end
  43. return x or 2
  44. end)()(1, 2)")
  45. (affirm-codegen
  46. '((((lambda (x) (cond [x 2] [true x])) (cond [true 1])) 1 2))
  47. "return (function()
  48. local x
  49. do
  50. x = 1
  51. end
  52. return x and 2
  53. end)()(1, 2)")
  54. (affirm-codegen
  55. '((((lambda (x) (cond [x false] [true true])) (cond [true 1])) 1 2))
  56. "return (function()
  57. local x
  58. do
  59. x = 1
  60. end
  61. return not x
  62. end)()(1, 2)"))
  63. (it "of quotes"
  64. (affirm-codegen
  65. '(('(1 2) 1 2))
  66. "return ({tag=\"list\", n=2, 1, 2})(1, 2)")
  67. (affirm-codegen
  68. '((`(1 2) 1 2))
  69. "return ({tag=\"list\", n=2, 1, 2})(1, 2)"))
  70. (it "of struct-literal"
  71. (affirm-codegen
  72. '(({ :a 1 } 1 2))
  73. "return ({a=1})(1, 2)"))
  74. (it "of constants"
  75. (affirm-codegen*
  76. '((1 1 2))
  77. "return (1)(1, 2)"))
  78. (it "of symbols"
  79. (affirm-codegen
  80. '((true 1 2))
  81. "return (true)(1, 2)")))
  82. (section "will correctly indexed"
  83. (it "constants"
  84. (affirm-codegen
  85. '((get-idx 1 2))
  86. "return (1)[2]")
  87. (affirm-codegen
  88. '((get-idx foo 2))
  89. "return foo[2]"))
  90. (it "quotes"
  91. (affirm-codegen
  92. '((get-idx '(1 2 3) 2))
  93. "return ({tag=\"list\", n=3, 1, 2, 3})[2]")))
  94. (section "will correctly wrap nested expressions"
  95. (it "with nots"
  96. (affirm-codegen
  97. '((cond
  98. [(+ 2 3) false]
  99. [true true]))
  100. "return not (2 + 3)"))
  101. (it "with and & or"
  102. (affirm-codegen
  103. '((+ (cond
  104. [foo bar]
  105. [true foo])
  106. (cond
  107. [foo foo]
  108. [true bar])))
  109. "return (foo and bar) + (foo or bar)"))
  110. (it "with operators"
  111. (affirm-codegen
  112. '((+ 2 3 (+ 4 5)))
  113. "return 2 + 3 + (4 + 5)")
  114. (affirm-codegen
  115. '((+ 2 3 (.. "foo" 5)))
  116. "return 2 + 3 + (\"foo\" .. 5)")
  117. (affirm-codegen
  118. '((.. "foo" (+ 2 3)))
  119. "return \"foo\" .. 2 + 3"))))