guile.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (module (arguile guile))
  2. (export _+ _* _= _length _apply grp expand-kwargs has-kwargs?)
  3. (export-syntax _let cut)
  4. (use (srfi srfi-1)
  5. (arguile loop))
  6. (define _+ +)
  7. (define _= =)
  8. (define _* *)
  9. (define _length length)
  10. (define _apply apply)
  11. (define-syntax _let
  12. (syntax-rules ()
  13. ((_let args e1 ...)
  14. (let args e1 ...))))
  15. (define-syntax cut
  16. (lambda (stx)
  17. (syntax-case stx ()
  18. ((cut slot0 slot1+ ...)
  19. (let loop ((slots #'(slot0 slot1+ ...))
  20. (params '())
  21. (args '()))
  22. (if (null? slots)
  23. #`(lambda #,(reverse params) #,(reverse args))
  24. (let ((s (car slots))
  25. (rest (cdr slots)))
  26. (with-syntax (((var) (generate-temporaries '(var))))
  27. (syntax-case s (_ ___)
  28. (_
  29. (loop rest (cons #'var params) (cons #'var args)))
  30. (___
  31. (if (pair? rest)
  32. (error "___ not on the end of cut expression"))
  33. #`(lambda #,(append (reverse params) #'var)
  34. (apply #,@(reverse (cons #'var args)))))
  35. (else
  36. (loop rest params (cons s args))))))))))))
  37. (define (grp lst n)
  38. (loop lp ((lst lst) (acc '()))
  39. (if (> n (length lst))
  40. (reverse (append lst acc))
  41. (lp (drop lst 2) (cons (take lst 2) acc)))))
  42. (define (expand-kwargs args ctx)
  43. (loop ((for arg (in-list args))
  44. (where args* '()
  45. (cons (let ((arg* (syntax->datum arg)))
  46. (if (and (keyword? arg*) (eq? #:o arg*))
  47. (datum->syntax ctx #:optional)
  48. arg))
  49. args*)))
  50. => (reverse args*)))
  51. (define (has-kwargs? args)
  52. (or-map (lambda (arg) (keyword? (syntax->datum arg)))
  53. args))