xtmdoc-write-alist-args-as-json   scheme


Defined in:  https://github.com/digego/extempore/tree/v0.8.9/runtime/llvmti.xtm

Implementation

;; we handle the args field separately, since there are a few special
;; cases to handle
(define xtmdoc-write-alist-args-as-json
  (lambda (xtmdoc-alist file-port)
    ;; (println (cdr (assoc-strcmp 'name xtmdoc-alist)))
    (display ",\n  \"args\": " file-port)
    (let ((category (cdr (assoc-strcmp 'category xtmdoc-alist)))
          (args (cdr (assoc-strcmp 'args xtmdoc-alist)))
          (type (cdr (assoc-strcmp 'type xtmdoc-alist))))
      ;; arg names and types
      (cond
       ((member category '("closure" "generic closure"))
        (display
         (string-append
          "["
          (string-join (map (lambda (name type)
                              (string-append "[\"" (symbol->string name) "\", \"" type "\"]"))
                            (cons 'RETURN args)
                            (impc:ir:get-pretty-closure-arg-strings type))
                       ", ")
          "]")
         file-port))
       ((string=? category "builtin")
        (if (or (not (string? type))
                (string=? type "")
                (<> (length (cdr (impc:ir:get-pretty-closure-arg-strings type)))
                    (length args)))
            ;; allow builtins have malformed arg/type relationships
            (display
             (string-append
              "["
              (string-join (map (lambda (name)
                                  (string-append "[\"" (symbol->string name) "\", null]"))
                                args)
                           ", ")
              "]")
             file-port)
            (display
             (string-append
              "["
              (string-join (map (lambda (name type)
                                  (string-append "[\"" (symbol->string name) "\", \"" type "\"]"))
                                (cons 'RETURN args)
                                (impc:ir:get-pretty-closure-arg-strings type))
                           ", ")
              "]")
             file-port)))
       ((string=? category "C function")
        (display
         (string-append
          "["
          ;; at the moment, there's no way of telling the xtlang
          ;; compiler about the names of the arguments to a C function
          (string-join (map (lambda (type)
                              (string-append "[null, \"" type "\"]"))
                            (impc:ir:get-pretty-closure-arg-strings type))
                       ", ")
          "]")
         file-port))
       ;; these are the ones for which "args" doesn't make sense
       ;; "named type"
       ;; "generic type"
       ;; "polymorphic closure"
       ;; "global var"
       ;; "polymorphic type"
       ;; "type alias"
       (else (write 'null file-port))))))


Back to Index