infix.zy 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // infix will be contained within {}
  2. (assert (== "list" (type? (macexpand {}))))
  3. (assert (== "nil" (str (infix))))
  4. (assert (== {3 + 4} 7))
  5. {3 + 4}
  6. (assert (== {9 - 8} 1))
  7. (assert (== { 9 - {1 - 3}} 11))
  8. (assert (== {4 + 2 * 3} 10))
  9. (assert (== {4 - 4 / 2} 2))
  10. (assert (== {3 * 2 ** 3} 24))
  11. // and can mix in function calls to infix expressions
  12. (defn f [] 7)
  13. (assert (== -1020 {4 - 2 ** {3 + (f)}}))
  14. (assert (== {true and not false} {not false and true and true}))
  15. // can put infix as arguments to sexp calls
  16. (defn add [a b] (+ a b))
  17. (assert (== (add {4 + 1} {6 - 1}) 10))
  18. //(def a [3 4 5])
  19. //(assert (== "4" (str {a[1]})))
  20. //(assert {a[1] == 4})
  21. { newvar = 3}
  22. (assert (== newvar 3))
  23. (assert (== {5 mod 3} 2))
  24. {newvar++}
  25. (assert (== newvar 4))
  26. {newvar--}
  27. (assert (== newvar 3))
  28. // pow should be right associative
  29. (assert (== {2 ** 3 ** 3} 134217728))
  30. // lack of spacing between builtin operators should not matter,
  31. // expect that -1 is preferred over subtract 1 so 6-1 won't parse.
  32. (assert (== (add {4+1} {6 - 1}) 10))
  33. // debug help: infixExpand shows the conversion from infix to s-expression
  34. (assert (== "(quote (set a 4))" (str (infixExpand { a = 4})) ))
  35. (assert (== {4/10} 0.4))
  36. // comparisons
  37. (assert (== true { 2 < 3}))
  38. (assert (== true { 4 > 2}))
  39. (assert (== true { 2 <= 2}))
  40. (assert (== false { 3 <= 2}))
  41. (assert (== true { 2 >= 2}))
  42. (assert (== false { 1 >= 2}))
  43. (assert (== true { 1 == 1}))
  44. (assert (== true { 1 != 0}))
  45. (assert (== "(quote (<= 2 3))" (str (infixExpand {2<=3})) ))
  46. (assert {2 < 3})
  47. (def h (hash a:(hash b:[12 4 6])))
  48. (assert (== {h.a.b[0]} 12))
  49. // (quote (arrayidx h.a.b [0]))
  50. (infixExpand {h.a.b[2] = 99})
  51. {h.a.b[2] = 99}
  52. (assert (== {h.a.b[2]} 99))
  53. (infixExpand {})
  54. // single assignment
  55. {g = 12}
  56. (assert (== g 12))
  57. // multiple assignment
  58. //{a b = 5 6}
  59. //(infixExpand {a b = 5 6})
  60. // was giving errors having a function call follow a semicolon: Error calling 'infix': LeftBindingPower: unhandled sx :&zygo.SexpPair{Head:(*zygo.SexpSymbol)(0xc82022d530), Tail:(*zygo.SexpPair)(0xc820241aa0)}
  61. //
  62. {a = 10; b = 12; d = 3; (println {a+b*d})}
  63. // mixed parses fine, and takes the value from the e at the end; the last expression.
  64. (assert (== 126 { (println "one"); (println "two"); a = 10; d = (+ 1 2); e = 100 + (+ a d {5 + 6}) + 2; (println e); e}))