pre-process.scm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. (import
  2. (scheme base)
  3. (scheme read)
  4. (scheme write))
  5. (define symbol-terminators
  6. '(#\space #\newline #\tab
  7. #\. #\; #\= #\+ #\* #\/ #\-
  8. #\( #\) #\, #\< #\> #\[ #\]
  9. #\{ #\}))
  10. (define keywords
  11. '(break case continue else for if return while))
  12. (define operators
  13. '(+ - * / = += -= *= /= < > == != <= >=))
  14. (define (symbol-terminator? char)
  15. (not (not (memv char symbol-terminators))))
  16. (define (keyword? sym)
  17. (not (not (memq sym keywords))))
  18. (define (operator? sym)
  19. (not (not (memq sym operators))))
  20. (define (symbol-start? char)
  21. (define i (char->integer char))
  22. (or (<= 48 i 57)
  23. (<= 65 i 90)
  24. (<= 97 i 122)))
  25. (define (tokenize-expression str)
  26. (define p (open-input-string str))
  27. (let loop ((result '())
  28. (next-char (peek-char p)))
  29. (if (eof-object? next-char)
  30. (begin
  31. (close-input-port p)
  32. (reverse result))
  33. (cond
  34. ((symbol-start? next-char)
  35. (let ((sym (parse-symbol p)))
  36. (loop (cons sym result)
  37. (peek-char p))))
  38. (else
  39. ;; to do
  40. (read-char p)
  41. (loop result
  42. (peek-char p)))))))