Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. (use json list-of srfi-13 regex)
  2.  
  3. (define (json-fix jro)
  4. (cond ((null? jro) jro)
  5. ((vector? jro)
  6. (alist->hash-table (json-fix (vector->list jro))))
  7. ((pair? jro)
  8. (cons (json-fix (car jro))
  9. (json-fix (cdr jro))))
  10. (else jro)))
  11.  
  12. (define (json->scm port)
  13. (json-fix (json-read port)))
  14.  
  15. (define (scm->json obj port)
  16. (json-write obj port))
  17.  
  18. (define (mapcat f l)
  19. (foldr append '() (map f l)))
  20.  
  21. (define (partial f . args)
  22. (lambda more (apply f (append args more))))
  23.  
  24. (define (jt-array ins ops table args)
  25. (let ((jt* (lambda (x) (jt (cons x (cdr ins)) ops '(()) args))))
  26. (map (partial apply append)
  27. (list-of (list i j) (i in table) (j in (mapcat jt* (car ins)))))))
  28.  
  29. (define (jt-op-u ins ops table args)
  30. (jt (cdr ins) ops table (cdr args)))
  31.  
  32. (define (jt-op-p ins ops table args)
  33. (jt ins ops (map (lambda (x) (append x (list (car ins)))) table) (cdr args)))
  34.  
  35. (define (jt-op-d ins ops table args)
  36. (let ((val (hash-table-ref (car ins) (cadr args))))
  37. (jt (cons val ins) ops table (cddr args))))
  38.  
  39. (define (jt-op-lb ins ops table args)
  40. (let ((os (if (equal? '() (car ops))
  41. ops
  42. (cons (cons 'no-print (car ops)) (cdr ops)))))
  43. (jt ins (cons '() os) table (cdr args))))
  44.  
  45. (define (jt-op-rb ins ops table args)
  46. (let ((ps (if (string? (caar ops)) '("-p") '()))
  47. (us (list-of "-u" (i in (filter string? (car ops))))))
  48. (jt ins (cdr ops) table (append ps us (cdr args)))))
  49.  
  50. (define (jt-op-arg ins ops table args)
  51. (let ((ops* (cons (cons (car args) (car ops)) (cdr ops))))
  52. (jt ins ops* table (cons "-d" args))))
  53.  
  54. (define (jt-obj ins ops table args)
  55. ((cond
  56. ((equal? "-u" (car args)) jt-op-u)
  57. ((equal? "-p" (car args)) jt-op-p)
  58. ((equal? "-d" (car args)) jt-op-d)
  59. ((equal? "[" (car args)) jt-op-lb)
  60. ((equal? "]" (car args)) jt-op-rb)
  61. (else jt-op-arg)) ins ops table args))
  62.  
  63. (define (jt ins ops table args)
  64. (cond
  65. ((equal? '() args) table)
  66. ((list? (car ins)) (jt-array ins ops table args))
  67. (else (jt-obj ins ops table args))))
  68.  
  69. (define (json-table port args)
  70. (jt (list (json->scm port)) '(()) '(()) args))
  71.  
  72. (define (str x)
  73. (cond
  74. ((string? x) x)
  75. ((number? x) (number->string x))
  76. (else (call-with-output-string (partial scm->json x)))))
  77.  
  78. (define (print-row fs rs row)
  79. (display (string-append (string-join (map str row) fs) rs)))
  80.  
  81. (define (print-table table fs rs)
  82. (for-each (partial print-row fs rs) table))
  83.  
  84. (let lp ((args (command-line-arguments)) (fs "\t") (rs "\n"))
  85. (let* ((r (regexp "^-(F|R)(.)?$"))
  86. (m (string-search r (car args))))
  87. (if m
  88. (let* ((arg? (caddr m))
  89. (arg (or arg? (cadr args)))
  90. (opt (string->symbol (cadr m)))
  91. (args (if arg? (cdr args) (cddr args))))
  92. (case opt
  93. ('F (lp args arg rs))
  94. ('R (lp args fs arg))))
  95. (print-table (json-table (current-input-port) args) fs rs))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement