fork(1) download
  1. ;; Define helper functions
  2. (define (drop-while pred lis)
  3. (if (or (null? lis) (not (pred (car lis))))
  4. lis
  5. (drop-while pred (cdr lis))))
  6.  
  7. (define (take-while pred lis)
  8. (define (take-while-helper pred lis result)
  9. (if (and (not (null? lis)) (pred (car lis)))
  10. (take-while-helper pred (cdr lis) (cons (car lis) result))
  11. (reverse result)))
  12. (take-while-helper pred lis '()))
  13.  
  14. (define (char-other? ch)
  15. (not (or (= (char->integer ch) 91)
  16. (= (char->integer ch) 93)
  17. (= (char->integer ch) 40)
  18. (= (char->integer ch) 41)
  19. (= (char->integer ch) 32)
  20. (= (char->integer ch) 10))))
  21.  
  22. (define (process-other chars)
  23. (and (not (null? chars))
  24. (let* ((token-chars (take-while char-other? chars))
  25. (rest (drop-while char-other? chars)))
  26. (list token-chars rest))))
  27.  
  28. ;; Custom function to check if all elements in a list are characters
  29. (define (all-char? lis)
  30. (if (null? lis)
  31. #t
  32. (and (char? (car lis)) (all-char? (cdr lis)))))
  33.  
  34. ;; Function to tag tokens with their type
  35. (define (tag-token type value)
  36. (list type value))
  37.  
  38. ;; Functions to take and drop elements from the list
  39. (define (take n lis)
  40. (if (or (<= n 0) (null? lis))
  41. '()
  42. (cons (car lis) (take (- n 1) (cdr lis)))))
  43.  
  44. (define (drop n lis)
  45. (if (or (<= n 0) (null? lis))
  46. lis
  47. (drop (- n 1) (cdr lis))))
  48.  
  49. ;; Functions for Dot Count
  50. (define (process-dots chars)
  51. (let* ((dot-chars (take-while (lambda (ch) (char=? ch (integer->char 46))) chars)) ; char 46 is '.'
  52. (num-dots (length dot-chars))
  53. (rest (drop-while (lambda (ch) (char=? ch (integer->char 46))) chars)))
  54. (list num-dots rest)))
  55.  
  56. ;; Define main tokenize function with integrated new functions
  57. (define (tokenize input)
  58. (let loop ((chars (string->list input)) (tokens '()))
  59. (cond
  60. ((null? chars) (reverse tokens)) ; Return the reversed tokens
  61. ((char=? (car chars) (integer->char 91)) ; Handle opening bracket [
  62. (loop (cdr chars) (cons (tag-token 'open-bracket "[") tokens)))
  63. ((char=? (car chars) (integer->char 93)) ; Handle closing bracket ]
  64. (loop (cdr chars) (cons (tag-token 'close-bracket "]") tokens)))
  65. ((char=? (car chars) (integer->char 40)) ; Handle opening parenthesis
  66. (loop (cdr chars) (cons (tag-token 'open-parenthesis "(") tokens)))
  67. ((char=? (car chars) (integer->char 41)) ; Handle closing parenthesis
  68. (loop (cdr chars) (cons (tag-token 'close-parenthesis ")") tokens)))
  69. ((char=? (car chars) (integer->char 32)) ; Skip spaces
  70. (loop (cdr chars) tokens))
  71. ((char=? (car chars) (integer->char 46)) ; Handle dots (.)
  72. (let* ((result (process-dots chars))
  73. (num-dots (car result))
  74. (rest (cadr result)))
  75. (loop rest (cons (tag-token 'dots num-dots) tokens)))) ; Use a tagged list structure
  76. (else
  77. (let* ((result (process-other chars))
  78. (token (car result))
  79. (rest (cadr result)))
  80. (loop rest (cons (tag-token 'other token) tokens))))))
  81. ) ; End of tokenize function
  82.  
  83. (define (simplify-for-output x) x)
  84.  
  85. ;; Function to check if a token represents dots
  86. (define (dots? x) (and (pair? x) (pair? (car x))
  87. (eq? 'dots (caar x))))
  88.  
  89. (define (dot-count x) (cadr x))
  90.  
  91. ;; Function to gather tokens over nested parentheses
  92. (define (gather-tokens tokens)
  93. (define (helper tokens collected depth)
  94. (cond
  95. ((null? tokens) (list (reverse collected) tokens))
  96. ((and (equal? (car (car tokens)) 'close-parenthesis) (= depth 1))
  97. (list (reverse (cons (car tokens) collected)) (cdr tokens)))
  98. ((equal? (car (car tokens)) 'close-parenthesis)
  99. (helper (cdr tokens) (cons (car tokens) collected) (- depth 1)))
  100. ((equal? (car (car tokens)) 'open-parenthesis)
  101. (helper (cdr tokens) (cons (car tokens) collected) (+ depth 1)))
  102. (else
  103. (helper (cdr tokens) (cons (car tokens) collected) depth))))
  104. (helper tokens '() 0))
  105.  
  106. ;; Function to gather and tag tokens based on dots
  107. (define (process-final tokens)
  108. (define (process-helper reversed-tokens forward-tail)
  109. (cond
  110. ((null? reversed-tokens)
  111. (simplify-for-output forward-tail))
  112. ((dots? reversed-tokens)
  113. (let* ((count (dot-count (car reversed-tokens)))
  114. (gathered (gather-tokens forward-tail))
  115. (the-tail (cons (tag-token 'final (car gathered)) (cdr gathered))))
  116. (process-helper (cdr reversed-tokens) the-tail)))
  117. (else
  118. (process-helper (cdr reversed-tokens) (cons (car reversed-tokens) forward-tail)))))
  119. (process-helper (reverse tokens) '()))
  120.  
  121. ;; Example usage
  122. (define input "(define) f g (). 1 2")
  123.  
  124. ;; Tokenize the input
  125. (let ((tokens (tokenize input)))
  126. (write tokens) ; Write the intermediate tokenized output for debugging
  127. (newline)(newline)
  128. (write (process-final tokens))) ; Return the final processed output
  129.  
Success #stdin #stdout 0.01s 8128KB
stdin
Standard input is empty
stdout
((open-parenthesis "(") (other (#\d #\e #\f #\i #\n #\e)) (close-parenthesis ")") (other (#\f)) (other (#\g)) (open-parenthesis "(") (close-parenthesis ")") (dots 1) (other (#\1)) (other (#\2)))

((open-parenthesis "(") (other (#\d #\e #\f #\i #\n #\e)) (close-parenthesis ")") (other (#\f)) (other (#\g)) (open-parenthesis "(") (close-parenthesis ")") (final ((other (#\1)) (other (#\2)))) ())