Advertisement
Guest User

Untitled

a guest
Feb 18th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.24 KB | None | 0 0
  1. (defmacro emit (language file &body body)
  2.   `(with-open-file (str ,file
  3.             :direction :output
  4.             :if-exists :supersede)
  5.            (format str "~A" (cond ((eq ',language 'html)
  6.                        (with-html-output-to-string (s nil :prologue t :indent t) ,@body))
  7.                       ((eq ',language 'javascript)
  8.                        (ps ,@body))
  9.                       ((eq ',language 'json)
  10.                        (remove #\; (ps ,@body)))))))
  11.  
  12. (emit json "blah.txt"
  13.       (create "name" "Blah"
  14.           "description" "Blah."
  15.           "version" "1.0"
  16.           "manifest_version" 3
  17.           "background" (create "service_worker" "blah.js")
  18.           "permissions" (array "storage")
  19.           "action" (create "default_title" "Blah"
  20.                    "default_popup" "blah.html")))
  21.  
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;; slime-expand-1
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25.  
  26. (WITH-OPEN-FILE
  27.  (STR "blah.txt" :DIRECTION :OUTPUT :IF-EXISTS
  28.       :SUPERSEDE)
  29.  (FORMAT STR "~A"
  30.      (COND
  31.       ((EQ 'JSON 'HTML)
  32.        (WITH-HTML-OUTPUT-TO-STRING (S NIL :PROLOGUE T :INDENT T)
  33.                        (CREATE "name" "Blah" "description"
  34.                            "Blah." "version" "1.0"
  35.                            "manifest_version" 3 "background"
  36.                            (CREATE "service_worker" "blah.js") "permissions"
  37.                            (ARRAY "storage") "action"
  38.                            (CREATE "default_title" "Blah" "default_popup"
  39.                                "blah.html"))))
  40.       ((EQ 'JSON 'JAVASCRIPT)
  41.        (PS
  42.         (CREATE "name" "Blah" "description"
  43.             "Blah." "version" "1.0"
  44.             "manifest_version" 3 "background"
  45.             (CREATE "service_worker" "blah.js") "permissions"
  46.             (ARRAY "storage") "action"
  47.             (CREATE "default_title" "Blah" "default_popup"
  48.                 "blah.html"))))
  49.       ((EQ 'JSON 'JSON)
  50.        (REMOVE #\;
  51.            (PS
  52.             (CREATE "name" "Blah" "description"
  53.                 "Blah." "version"
  54.                 "1.0" "manifest_version" 3 "background"
  55.                 (CREATE "service_worker" "blah.js") "permissions"
  56.                 (ARRAY "storage") "action"
  57.                 (CREATE "default_title" "Blah" "default_popup"
  58.                     "blah.html"))))))))
  59.  
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; compilation output
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63.  
  64. ; processing (DEFMACRO EMIT ...)
  65. ; processing (EMIT JSON ...)
  66.  
  67. ; file: /tmp/slimeNopc75
  68. ; in: EMIT JSON
  69. ;     (PARENSCRIPT:CREATE "name" "Blah" "description" "Blah." "version" "1.0"
  70. ;      "manifest_version" 3 "background"
  71. ;      (PARENSCRIPT:CREATE "service_worker" "blah.js") "permissions" ...)
  72. ;
  73. ; note: deleting unreachable code
  74.  
  75. ; file: /tmp/slimeNopc75
  76. ; in: EMIT JSON
  77. ;     (ARRAY "storage")
  78. ;
  79. ; caught WARNING:
  80. ;   The function ARRAY is undefined, and its name is reserved by ANSI CL so that
  81. ;   even if it were defined later, the code doing so would not be portable.
  82.  
  83. ;     (PARENSCRIPT:CREATE "name" "Blah" "description" "Blah." "version" "1.0"
  84. ;      "manifest_version" 3 "background"
  85. ;      (PARENSCRIPT:CREATE "service_worker" "blah.js") "permissions" ...)
  86. ;
  87. ; caught STYLE-WARNING:
  88. ;   undefined function: PARENSCRIPT:CREATE
  89. ;
  90. ; compilation unit finished
  91. ;   Undefined functions:
  92. ;     ARRAY CREATE
  93. ;   caught 1 WARNING condition
  94. ;   caught 1 STYLE-WARNING condition
  95. ;   printed 1 note
  96.  
  97. CL-USER>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement