Advertisement
Void-voiD

Untitled

Dec 9th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. (define-syntax trace
  2. (syntax-rules ()
  3. ((trace x)
  4. (begin
  5. (write (quote x))
  6. (display " => ")
  7. (write x)
  8. (newline)
  9. x))))
  10.  
  11. (define-syntax use-assertions
  12. (syntax-rules ()
  13. ((_) (eval '(define exit
  14. (call-with-current-continuation
  15. (lambda (escape) escape))) (interaction-environment)))))
  16.  
  17. (define-syntax assert
  18. (syntax-rules ()
  19. ((_ x)
  20. (if (not x)
  21. (exit 'x)))))
  22.  
  23. (use-assertions)
  24.  
  25. (define (make-source s . end)
  26. (if (string? s)
  27. (if (null? end)
  28. (let ((static (reverse (cons #f (reverse (string->list s))))))
  29. (lambda (x)
  30. (if (= (length static) 1)
  31. (car static)
  32. (if (equal? x 'peek)
  33. (car static)
  34. (if (equal? x 'next)
  35. (begin
  36. (let ((cur (car static)))
  37. (begin
  38. (set! static (cdr static))
  39. cur)))
  40. static)))))
  41. (let ((static (reverse (cons (car end) (reverse (string->list s))))))
  42. (lambda (x)
  43. (if (= (length static) 1)
  44. (car static)
  45. (if (equal? x 'peek)
  46. (car static)
  47. (if (equal? x 'next)
  48. (begin
  49. (let ((cur (car static)))
  50. (begin
  51. (set! static (cdr static))
  52. cur)))
  53. static))))))
  54. (if (vector? s)
  55. (if (null? end)
  56. (let ((static (reverse (cons #f (reverse (vector->list s))))))
  57. (lambda (x)
  58. (if (= (length static) 1)
  59. (car static)
  60. (if (equal? x 'peek)
  61. (car static)
  62. (if (equal? x 'next)
  63. (begin
  64. (let ((cur (car static)))
  65. (begin
  66. (set! static (cdr static))
  67. cur)))
  68. static)))))
  69. (let ((static (reverse (cons (car end) (reverse (vector->list s))))))
  70. (lambda (x)
  71. (if (= (length static) 1)
  72. (car static)
  73. (if (equal? x 'peek)
  74. (car static)
  75. (if (equal? x 'next)
  76. (begin
  77. (let ((cur (car static)))
  78. (begin
  79. (set! static (cdr static))
  80. cur)))
  81. static))))))
  82. (if (null? end)
  83. (let ((static (reverse (cons #f (reverse s)))))
  84. (lambda (x)
  85. (if (= (length static) 1)
  86. (car static)
  87. (if (equal? x 'peek)
  88. (car static)
  89. (if (equal? x 'next)
  90. (begin
  91. (let ((cur (car static)))
  92. (begin
  93. (set! static (cdr static))
  94. cur)))
  95. static)))))
  96. (let ((static (reverse (cons (car end) (reverse s)))))
  97. (lambda (x)
  98. (if (= (length static) 1)
  99. (car static)
  100. (if (equal? x 'peek)
  101. (car static)
  102. (if (equal? x 'next)
  103. (begin
  104. (let ((cur (car static)))
  105. (begin
  106. (set! static (cdr static))
  107. cur)))
  108. static)))))))))
  109.  
  110. (define (peek x)
  111. (x 'peek))
  112.  
  113. (define (next x)
  114. (x 'next))
  115.  
  116. ;expr ::= letters numbers sk operations | .
  117. ;letters ::= letter | .
  118. ;numbers ::= <0> | <1> | <2> | <3> | <4> | <5> | <6> | <7> | <8> | <9> .
  119. ;sk ::= <(> | <)> | < > | .
  120.  
  121. (define (letters x res)
  122. (if (and (not (null? x)) (char-alphabetic? (car x)))
  123. (letters (cdr x) (string-append res (list->string (cons (car x) '()))))
  124. (list x res)))
  125.  
  126. (define (tokenize str)
  127. (define (tokens x res)
  128. (if (null? x)
  129. (reverse res)
  130. (let ((cur (car x)))
  131. (if (char-numeric? cur)
  132. (tokens (cdr x) (cons (- (char->integer cur) 48) res))
  133. (if (char-alphabetic? cur)
  134. (let ((s (letters x "")))
  135. (tokens (car s) (cons (string->symbol (cadr s)) res)))
  136. (if (equal? cur #\space)
  137. (tokens (cdr x) res)
  138. (if (or (equal? cur #\() (equal? cur #\)))
  139. (tokens (cdr x) (cons (list->string (cons cur '())) res))
  140. (if (or (equal? cur #\-) (equal? cur #\+) (equal? cur #\*) (equal? cur #\/) (equal? cur #\^))
  141. (tokens (cdr x) (cons (string->symbol (list->string (cons cur '()))) res))
  142. #f))))))))
  143. (tokens (string->list str) '()))
  144.  
  145. (define (power lst)
  146. (if (list? (peek lst))
  147. (expr lst)
  148. (next lst)))
  149.  
  150. (define (factors lst now)
  151. (if (equal? '^ (peek lst))
  152. (factors lst (list (if (= (length now) 1) (car now) now) (next lst) (let ((nowt (power lst))) (if (= (lenght nowt) 1) (car nowt) nowt))))
  153. (let ((cur (peek lst)))
  154. (if (or (equal? cur ")") (equal? cur 'HELL))
  155. now
  156. (assert #f)))))
  157.  
  158. (define (factor lst)
  159. (if (equal? 'HELL (peek lst))
  160. (assert #f)
  161. (factors lst (power lst))))
  162.  
  163. (define (terms lst now)
  164. (if (or (equal? '* (peek lst)) (equal? '/ (peek lst)))
  165. (terms lst (list (if (= (length now) 1) (car now) now) (next lst) (let ((nowt (factor lst))) (if (= (lenght nowt) 1) (car nowt) nowt))))
  166. (let ((cur (peek lst)))
  167. (if (or (equal? cur '^) (equal? cur ")") (equal? cur 'HELL))
  168. now
  169. (assert #f)))))
  170.  
  171. (define (term lst)
  172. (if (equal? 'HELL (peek lst))
  173. (assert #f)
  174. (terms lst (factor lst))))
  175.  
  176. (define (exprs lst now)
  177. (if (or (equal? '+ (peek lst)) (equal? '- (peek lst)))
  178. (exprs lst (list (if (= (length now) 1) (car now) now) (next lst) (let ((nowt (term lst))) (if (= (lenght nowt) 1) (car nowt) nowt))))
  179. (let ((cur (peek lst)))
  180. (if (or (equal? cur '*) (equal? cur '/) (equal? cur '^) (equal? cur ")") (equal? cur 'HELL))
  181. now
  182. (assert #f)))))
  183.  
  184. (define (expr lst)
  185. (if (equal? 'HELL (peek lst))
  186. (assert #f)
  187. (exprs lst (term lst))))
  188.  
  189. (define (parse list)
  190. (define xex (make-source list 'HELL))
  191. (expr xex))
  192.  
  193. (parse (tokenize "a/b/c/d"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement