js.l 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. (
  2. (def st-atom 0)
  3. (def st-num 1)
  4. (def st-str 2)
  5. (def st-arr 3)
  6. (def st-sym 4)
  7. (def curlies 0)
  8. (def brackets 0)
  9. (def parens 0)
  10. (def js-pst st-atom)
  11. (def cur-string "")
  12. (def cur-string-idx 0)
  13. (def cur-number 0)
  14. (def cur-symbol nil)
  15. (def js-opstate 0)
  16. (def js-argstate 0)
  17. (def cur-op 0)
  18. (def js-callable 0)
  19. (def js-stack (list))
  20. (def js-args (list))
  21. (def is-letter (fn c
  22. (or (eq c 95) ; _
  23. (or
  24. (and (gt c 96) (lt c 123)) ; a-z
  25. (and (gt c 64) (lt c 90)) ; A-Z
  26. ))))
  27. (def is-digit (fn c
  28. (and (gt c 47) (lt c 58)) ; 0-9
  29. ))
  30. (def is-space (fn c
  31. (or (eq c 32) (or (eq c 10) (eq c 13)))
  32. ))
  33. (def is-operator (fn c
  34. (if (eq c 43) (quote +)
  35. (if (eq c 45) (quote -)
  36. (if (eq c 42) (quote *)
  37. (if (eq c 47) (quote /)
  38. (if (eq c 37) (quote %)
  39. (if (eq c 60) (quote lt)
  40. (if (eq c 62) (quote gt)
  41. (if (eq c 124) (quote bitor)
  42. (if (eq c 38) (quote bitand)
  43. 0)))))))))
  44. ))
  45. (def js-emit-operator (fn c lhs rhs (do
  46. (def js-opstate 0)
  47. (def js-stack (cons (is-operator c) (list lhs rhs)))
  48. )))
  49. (def push-js (fn x (do
  50. (if (car js-stack)
  51. (def js-stack (cons x js-stack))
  52. (def js-stack x)
  53. )
  54. )))
  55. (def js-emit-value (fn v (do
  56. (if (eq 2 js-opstate) (do
  57. (js-emit-operator cur-op js-stack v)
  58. )
  59. (push-js v))
  60. )))
  61. ; if js-argstate, collect args instead
  62. (def js-emit-expr (fn (do
  63. (if js-argstate (do
  64. (def js-args (cons js-args js-args)))
  65. (do
  66. (print js-stack)
  67. (print (eval (cons js-stack nil))))
  68. )
  69. (def js-stack (list))
  70. (def js-pst st-atom)
  71. (def js-opstate 0)
  72. (def js-callable 0)
  73. )))
  74. (def js-parse-atom (fn c (do
  75. (if (is-operator c) (do
  76. (def cur-op c)
  77. (def js-opstate 2)
  78. )
  79. (if (eq c 59) (do
  80. (js-emit-expr)
  81. )
  82. (if (eq c 40) (do
  83. (def parens (+ parens 1))
  84. (if js-callable (do
  85. (def js-argstate 1)
  86. ) 0)
  87. )
  88. (if (eq c 41) (do
  89. (def parens (- parens 1))
  90. (if (lt parens 0) (print "syntax error") 0)
  91. (if js-argstate (do
  92. (print "function call!")
  93. (def js-argstate 0)
  94. (if (car js-args)
  95. (def js-stack (list js-stack js-args))
  96. (def js-stack (list js-stack)))
  97. )
  98. (do
  99. (print "parens")
  100. )
  101. )
  102. )
  103. 0))))
  104. )))
  105. (def js-parse-char (fn c (do
  106. (if (eq js-pst st-atom)
  107. (if (is-letter c) (do
  108. (def js-pst st-sym)
  109. (def cur-string (alloc-str 16))
  110. (put cur-string 0 c)
  111. (def cur-string-idx 1)
  112. (def js-callable 1)
  113. )
  114. (if (is-digit c) (do
  115. (def js-pst st-num)
  116. (def cur-number (- c 48))
  117. (def js-callable 0)
  118. )
  119. (js-parse-atom c)
  120. ))
  121. (if (eq js-pst st-sym)
  122. (if (or (is-letter c) (is-digit c)) (do
  123. (put cur-string cur-string-idx c)
  124. (def cur-string-idx (+ cur-string-idx 1))
  125. )
  126. (do
  127. (js-emit-value (read cur-string))
  128. (def js-pst st-atom)
  129. (js-parse-atom c)
  130. ))
  131. (if (eq js-pst st-num)
  132. (if (is-digit c) (do
  133. (def cur-number (+ (- c 48) (* 10 cur-number)))
  134. )
  135. (do
  136. (js-emit-value cur-number)
  137. (def js-pst st-atom)
  138. (js-parse-atom c)
  139. ))
  140. 0)))
  141. 0
  142. )))
  143. (def js (fn str (do
  144. (def js-pst st-atom)
  145. (let l (size str))
  146. (let i 0)
  147. (while (lt i l) (do
  148. (js-parse-char (get str i))
  149. (let i (+ i 1))
  150. ))
  151. (js-parse-char 0)
  152. (js-emit-expr)
  153. )))
  154. )