fork 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.  
  29. ;;;;;;;;;;;;;;;;;;
  30. ;; Custom function to check if all elements in a list are characters
  31. '(define (all-char? lis)
  32. (if (null? lis)
  33. #t
  34. (and (char? (car lis)) (all-char? (cdr lis)))))
  35.  
  36. ;; Function to tag tokens with their type
  37. (define (tag-token type value)
  38. (list type value))
  39.  
  40. ;; Functions to take and drop elements from the list
  41. (define (take n lis)
  42. (if (or (<= n 0) (null? lis))
  43. '()
  44. (cons (car lis) (take (- n 1) (cdr lis)))))
  45.  
  46. (define (drop n lis)
  47. (if (or (<= n 0) (null? lis))
  48. lis
  49. (drop (- n 1) (cdr lis))))
  50.  
  51. ;; Functions for Dot Count
  52. (define (process-dots chars)
  53. (let* ((dot-chars (take-while (lambda (ch) (char=? ch (integer->char 46))) chars)) ; char 46 is '.'
  54. (num-dots (length dot-chars))
  55. (rest (drop-while (lambda (ch) (char=? ch (integer->char 46))) chars)))
  56. (list num-dots rest)))
  57.  
  58. ;; Define main tokenize function with integrated new functions
  59. (define (tokenize input)
  60. (let loop ((chars (string->list input)) (tokens '()))
  61. (cond
  62. ((null? chars) (reverse tokens)) ; Return the reversed tokens
  63. ((char=? (car chars) (integer->char 91)) ; Handle opening bracket [
  64. (loop (cdr chars) (cons (tag-token 'open-bracket "[") tokens)))
  65. ((char=? (car chars) (integer->char 93)) ; Handle closing bracket ]
  66. (loop (cdr chars) (cons (tag-token 'close-bracket "]") tokens)))
  67. ((char=? (car chars) (integer->char 40)) ; Handle opening parenthesis
  68. (loop (cdr chars) (cons (tag-token 'open-parenthesis "(") tokens)))
  69. ((char=? (car chars) (integer->char 41)) ; Handle closing parenthesis
  70. (loop (cdr chars) (cons (tag-token 'close-parenthesis ")") tokens)))
  71. ((char=? (car chars) (integer->char 32)) ; Skip spaces
  72. (loop (cdr chars) tokens))
  73. ((char=? (car chars) (integer->char 46)) ; Handle dots (.)
  74. (let* ((result (process-dots chars))
  75. (num-dots (car result))
  76. (rest (cadr result)))
  77. (loop rest (cons (tag-token 'dots num-dots) tokens)))) ; Use a tagged list structure
  78. (else
  79. (let* ((result (process-other chars))
  80. (token (car result))
  81. (rest (cadr result)))
  82. (loop rest (cons (tag-token 'other token) tokens)))))) ); End of tokenize function
  83.  
  84. (define (simplify-for-output x) x)
  85.  
  86. ;; Function to check if a token represents dots
  87. (define (dots? x) (and (pair? x) (pair? (car x))
  88. (eq? 'dots (caar x))))
  89.  
  90. (define (dot-count x) (cadr x))
  91.  
  92. ;; Function to gather up the tokens based on dots
  93. (define (process-final tokens) #f)
  94.  
  95. ;; DEBUG
  96. (define (debug procedure-name args)
  97. (define (debug-helper args)
  98. (if (null? (cdr args))
  99. args
  100. (cons (car args)
  101. (cons '--- (debug-helper (cdr args))))))
  102. (write (cons procedure-name (debug-helper args)))(newline)(newline))
  103.  
  104. ;; Example usage
  105. (define input "(define) f g (). 1 2")
  106.  
  107. ;; Tokenize the input
  108. (let ((tokens (tokenize input)))
  109. (write tokens) ; Write the intermediate tokenized output for debugging
  110. (newline)(newline)
  111. (write (process-final tokens))) ; Return the final processed output
  112.  
Success #stdin #stdout 0.01s 7984KB
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)))

#f