syntax.lisp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (import test ())
  2. (import tests/compiler/codegen/codegen-helpers ())
  3. (describe "The codegen will generate syntactically valid Lua for"
  4. (section "symbols that are"
  5. (it "empty"
  6. (affirm-codegen
  7. ~((define ,(string->symbol "") 2))
  8. "_e = 2"))
  9. (it "numbers"
  10. (affirm-codegen
  11. ~((define ,(string->symbol "23") 2))
  12. "_e23 = 2")
  13. (affirm-codegen
  14. ~((define ,(string->symbol "23.0") 2))
  15. "_e23_2e_0 = 2"))
  16. (it "underscores"
  17. (affirm-codegen
  18. ~((define ,(string->symbol "_G") 2))
  19. "_5f_G = 2")
  20. (affirm-codegen
  21. ~((define ,(string->symbol "G_G") 2))
  22. "G_5f_G = 2"))
  23. (it "keywords"
  24. (affirm-codegen
  25. ~((define ,(string->symbol "and") 2))
  26. "_eand = 2"))
  27. (it "symbols"
  28. (affirm-codegen
  29. ~((define ,(string->symbol "a+") 2))
  30. "a_2b_ = 2")
  31. (affirm-codegen
  32. ~((define ,(string->symbol "+-") 2))
  33. "_2b2d_ = 2"))
  34. (it "hyphenated"
  35. (affirm-codegen
  36. ~((define ,(string->symbol "foo-bar") 2))
  37. "fooBar = 2")))
  38. (pending "semi-colons between function calls"
  39. (affirm-codegen
  40. '((foo) ((cond [foo foo] [true bar])) 1)
  41. "foo();
  42. (foo or bar)()
  43. return 1"))
  44. (section "setters in"
  45. (it "arguments"
  46. (affirm-codegen
  47. '((lambda (x)
  48. (foo (set! x 1))))
  49. "return function(x)
  50. return foo((function()
  51. x = 1
  52. end)())
  53. end"))
  54. (it "functions"
  55. (affirm-codegen
  56. '((lambda (x)
  57. ((set! x 1) 1)))
  58. "return function(x)
  59. return (function()
  60. x = 1
  61. end)()(1)
  62. end"))
  63. (it "arguments"
  64. (affirm-codegen
  65. '((lambda (x)
  66. (set! x (set! x 1))))
  67. "return function(x)
  68. x = 1
  69. x = nil
  70. return nil
  71. end")))
  72. (section "tables"
  73. (it "with identifier keys"
  74. (affirm-codegen
  75. '({ :key 123 :! 567})
  76. "return {key=123, [\"!\"]=567}"))
  77. (it "with keyword keys"
  78. (affirm-codegen
  79. '({ :end 123 })
  80. "return {[\"end\"]=123}"))
  81. (it "with identifier strings"
  82. (affirm-codegen
  83. '({ "key" 123 "!" 567})
  84. "return {key=123, [\"!\"]=567}"))))