Advertisement
Void-voiD

Untitled

Dec 20th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. (define (iter1 s len res)
  2. (if (null? s)
  3. #t
  4. (if (and (>= (char->integer (car s)) 48) (<= (char->integer (car s)) 57))
  5. (iter1 (cdr s) (- len 1) (+ res (* (- (char->integer (car s)) 48) (expt 16 (- len 1)))))
  6. (if (or (equal? (car s) '#\a) (equal? (car s) '#\A))
  7. (iter1 (cdr s) (- len 1) (+ res (* 10 (expt 16 (- len 1)))))
  8. (if (or (equal? (car s) '#\b) (equal? (car s) '#\B))
  9. (iter1 (cdr s) (- len 1) (+ res (* 11 (expt 16 (- len 1)))))
  10. (if (or (equal? (car s) '#\c) (equal? (car s) '#\C))
  11. (iter1 (cdr s) (- len 1) (+ res (* 12 (expt 16 (- len 1)))))
  12. (if (or (equal? (car s) '#\d) (equal? (car s) '#\D))
  13. (iter1 (cdr s) (- len 1) (+ res (* 13 (expt 16 (- len 1)))))
  14. (if (or (equal? (car s) '#\e) (equal? (car s) '#\E))
  15. (iter1 (cdr s) (- len 1) (+ res (* 14 (expt 16 (- len 1)))))
  16. (if (or (equal? (car s) '#\f) (equal? (car s) '#\F))
  17. (iter1 (cdr s) (- len 1) (+ res (* 15 (expt 16 (- len 1)))))
  18. #f)))))))))
  19.  
  20. (define (iter2 s len res)
  21. (if (null? s)
  22. res
  23. (if (and (>= (char->integer (car s)) 48) (<= (char->integer (car s)) 57))
  24. (iter2 (cdr s) (- len 1) (+ res (* (- (char->integer (car s)) 48) (expt 16 (- len 1)))))
  25. (if (or (equal? (car s) '#\a) (equal? (car s) '#\A))
  26. (iter2 (cdr s) (- len 1) (+ res (* 10 (expt 16 (- len 1)))))
  27. (if (or (equal? (car s) '#\b) (equal? (car s) '#\B))
  28. (iter2 (cdr s) (- len 1) (+ res (* 11 (expt 16 (- len 1)))))
  29. (if (or (equal? (car s) '#\c) (equal? (car s) '#\C))
  30. (iter2 (cdr s) (- len 1) (+ res (* 12 (expt 16 (- len 1)))))
  31. (if (or (equal? (car s) '#\d) (equal? (car s) '#\D))
  32. (iter2 (cdr s) (- len 1) (+ res (* 13 (expt 16 (- len 1)))))
  33. (if (or (equal? (car s) '#\e) (equal? (car s) '#\E))
  34. (iter2 (cdr s) (- len 1) (+ res (* 14 (expt 16 (- len 1)))))
  35. (if (or (equal? (car s) '#\f) (equal? (car s) '#\F))
  36. (iter2 (cdr s) (- len 1) (+ res (* 15 (expt 16 (- len 1)))))
  37. #f)))))))))
  38.  
  39. (define (search s cur)
  40. (if (null? s)
  41. -1
  42. (if (or (equal? (car s) #\tab) (equal? (car s) #\newline) (equal? (car s) #\space))
  43. cur
  44. (search (cdr s) (+ cur 1)))))
  45.  
  46. (define (super-car s x res)
  47. (if (> x 0)
  48. (super-car (cdr s) (- x 1) (cons (car s) res))
  49. (reverse res)))
  50.  
  51. (define (super-cdr s x)
  52. (if (> x 0)
  53. (super-cdr (cdr s) (- x 1))
  54. s))
  55.  
  56. (define (iter3 s res)
  57. (if (null? s)
  58. (reverse res)
  59. (if (or (equal? (car s) #\tab) (equal? (car s) #\newline) (equal? (car s) #\space))
  60. (iter3 (cdr s) res)
  61. (begin
  62. (if (not (equal? -1 (search s 0)))
  63. (let* ((x (search s 0))
  64. (cur (super-car s x '())))
  65. (if (check-hex (list->string cur))
  66. (iter3 (super-cdr s x) (cons (scan-hex (list->string cur)) res))
  67. #f))
  68. (if (check-hex (list->string s))
  69. (iter3 '() (cons (scan-hex (list->string s)) res))
  70. #f))))))
  71.  
  72. ;<Body> ::= <Specs> <Digits> <Symbols> .
  73. ;<Specs> ::= <Specs> <\t> < > <\n> | .
  74. ;<Digits> ::= <Digit> <Digits> | .
  75. ;<Digit> ::= <0> | <1> | <2> | <3> | <4> | <5> | <6> | <7> | <8> | <9> .
  76. ;<Symbols> ::= <Symbols> <Symbol> | .
  77. ;<Symbol> ::= <a> | <b> | <c> | <d> | <e> | <f> | <A> | <B> | <C> | <D> | <E> | <F> | .
  78.  
  79. (define (check-hex x)
  80. (let ((s (string->list x)))
  81. (if (and (>= (length s) 3) (equal? (car s) '#\0) (or (equal? (cadr s) '#\x) (equal? (cadr s) '#\X)))
  82. (iter1 (cddr s) (- (length s) 2) 0)
  83. #f)))
  84.  
  85. (define (scan-hex x)
  86. (if (check-hex x)
  87. (iter2 (cddr (string->list x)) (- (length (string->list x)) 2) 0)
  88. #f))
  89.  
  90. (define (scan-many-hexs x)
  91. (let ((res '())
  92. (s (string->list x)))
  93. (iter3 s res)))
  94.  
  95. (define (super-cons s x res)
  96. (if (and (null? s) (null? x))
  97. res
  98. (if (null? x)
  99. (super-cons (cdr s) x (cons (car s) res))
  100. (super-cons s (cdr x) (cons (car x) res)))))
  101.  
  102. (define (body program wc res ifs)
  103. (if ifs
  104. (if (equal? 'endif (vector-ref program wc))
  105. (list (+ wc 1) (list 'if (reverse res)))
  106. (if (equal? 'if (vector-ref program wc))
  107. (let ((cur (body program (+ wc 1) '() (+ wc 1))))
  108. (body program (car cur) (cons (cadr cur) res) (car cur)))
  109. (body program (+ wc 1) (cons (vector-ref program wc) res) ifs)))
  110. (if (>= wc (vector-length program))
  111. (list wc (reverse res))
  112. (if (equal? 'if (vector-ref program wc))
  113. (let ((cur (body program (+ wc 1) '() (+ wc 1))))
  114. (body program (car cur) (cons (cadr cur) res) #f))
  115. (body program (+ wc 1) (cons (vector-ref program wc) res) ifs)))))
  116.  
  117. (define (article program dic wc)
  118. (if (= wc (vector-length program))
  119. (list #f #f)
  120. (let ((cur (vector-ref program wc)))
  121. (if (equal? 'end cur)
  122. (list (+ wc 1) (cadr (body (list->vector (reverse dic)) 0 '() #f)))
  123. (article program (cons cur dic) (+ wc 1))))))
  124.  
  125. (define (articles program wc res)
  126. (let ((cur (vector-ref program wc)))
  127. (if (equal? cur 'define)
  128. (let ((x (article program '() (+ wc 2))))
  129. (begin
  130. (if (car x)
  131. (articles program (car x) (cons (list (vector-ref program (+ wc 1)) (cadr x)) res))
  132. (list #f #f))))
  133. (list wc (reverse res)))))
  134.  
  135. (define (parse program)
  136. (let* ((artres (articles program 0 '()))
  137. (art (cadr artres))
  138. (wc (car artres)))
  139. (if wc
  140. (list art (cadr (body program wc '() #f)))
  141. #f)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement