diff options
Diffstat (limited to 'src/tg')
| -rw-r--r-- | src/tg/method-macros.lisp | 7 | ||||
| -rw-r--r-- | src/tg/type-macros.lisp | 30 |
2 files changed, 16 insertions, 21 deletions
diff --git a/src/tg/method-macros.lisp b/src/tg/method-macros.lisp index 215b2ab..b924e15 100644 --- a/src/tg/method-macros.lisp +++ b/src/tg/method-macros.lisp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | (defpackage :ukkoclot/tg/method-macros | 3 | (defpackage :ukkoclot/tg/method-macros |
| 4 | (:documentation "Macros for easy defining TG methods.") | 4 | (:documentation "Macros for easy defining TG methods.") |
| 5 | (:use :c2cl :iterate) | 5 | (:use :c2cl :iterate) |
| 6 | (:import-from :alexandria :with-gensyms) | ||
| 6 | (:import-from :com.dieggsy.f-string :enable-f-strings) | 7 | (:import-from :com.dieggsy.f-string :enable-f-strings) |
| 7 | (:import-from :serapeum :take) | 8 | (:import-from :serapeum :take) |
| 8 | (:import-from :ukkoclot/state :bot) | 9 | (:import-from :ukkoclot/state :bot) |
| @@ -51,13 +52,11 @@ | |||
| 51 | `(,(param-name param) ,(param-default param))) | 52 | `(,(param-name param) ,(param-default param))) |
| 52 | 53 | ||
| 53 | (defun emit-defun (name return-type params method) | 54 | (defun emit-defun (name return-type params method) |
| 54 | (let ((revparams (reverse params)) | 55 | (with-gensyms (args bot) |
| 55 | (args (gensym "ARGS")) | ||
| 56 | (bot (gensym "BOT"))) | ||
| 57 | `(defun ,name (,bot &key ,@(iter (for param in params) ; lint:suppress lambda-list-invalid | 56 | `(defun ,name (,bot &key ,@(iter (for param in params) ; lint:suppress lambda-list-invalid |
| 58 | (collect (emit-defun-arg param)))) | 57 | (collect (emit-defun-arg param)))) |
| 59 | (let (,args) | 58 | (let (,args) |
| 60 | ,@(iter (for param in revparams) | 59 | ,@(iter (for param in (reverse params)) |
| 61 | (collect (if (param-skip-if-default param) | 60 | (collect (if (param-skip-if-default param) |
| 62 | `(unless (equal ,(param-name param) | 61 | `(unless (equal ,(param-name param) |
| 63 | ,(param-default param)) | 62 | ,(param-default param)) |
diff --git a/src/tg/type-macros.lisp b/src/tg/type-macros.lisp index 390781f..5f99cba 100644 --- a/src/tg/type-macros.lisp +++ b/src/tg/type-macros.lisp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | (defpackage :ukkoclot/tg/type-macros | 3 | (defpackage :ukkoclot/tg/type-macros |
| 4 | (:documentation "Macros for easy defining TG types.") | 4 | (:documentation "Macros for easy defining TG types.") |
| 5 | (:use :c2cl :iterate) | 5 | (:use :c2cl :iterate) |
| 6 | (:import-from :alexandria :with-gensyms) | ||
| 6 | (:import-from :com.dieggsy.f-string :enable-f-strings) | 7 | (:import-from :com.dieggsy.f-string :enable-f-strings) |
| 7 | (:import-from :ukkoclot/serializing :parse-value) | 8 | (:import-from :ukkoclot/serializing :parse-value) |
| 8 | (:import-from :ukkoclot/hash-tables :gethash-lazy) | 9 | (:import-from :ukkoclot/hash-tables :gethash-lazy) |
| @@ -54,7 +55,7 @@ | |||
| 54 | `(list ,(field-hash-key field) ,value ',(field-type field))) | 55 | `(list ,(field-hash-key field) ,value ',(field-type field))) |
| 55 | 56 | ||
| 56 | (defun emit-collect-nondefault-fields (name fields obj collector) | 57 | (defun emit-collect-nondefault-fields (name fields obj collector) |
| 57 | (let ((value (gensym "VALUE"))) | 58 | (with-gensyms (value) |
| 58 | (iter (for field in (reverse fields)) | 59 | (iter (for field in (reverse fields)) |
| 59 | (collect | 60 | (collect |
| 60 | (if (field-skip-if-default field) | 61 | (if (field-skip-if-default field) |
| @@ -70,8 +71,7 @@ | |||
| 70 | `(gethash-lazy ,(field-hash-key field) ,source ,(field-default field))) | 71 | `(gethash-lazy ,(field-hash-key field) ,source ,(field-default field))) |
| 71 | 72 | ||
| 72 | (defun emit-jzon-coerced-fields (name fields) | 73 | (defun emit-jzon-coerced-fields (name fields) |
| 73 | (let ((obj (gensym "OBJ")) | 74 | (with-gensyms (obj result) |
| 74 | (result (gensym "RESULT"))) | ||
| 75 | `(defmethod jzon:coerced-fields ((,obj ,name)) | 75 | `(defmethod jzon:coerced-fields ((,obj ,name)) |
| 76 | (let (,result) | 76 | (let (,result) |
| 77 | ,@(emit-collect-nondefault-fields | 77 | ,@(emit-collect-nondefault-fields |
| @@ -85,9 +85,8 @@ | |||
| 85 | (parse-value ',(field-type field) ,(emit-gethash field source)))) | 85 | (parse-value ',(field-type field) ,(emit-gethash field source)))) |
| 86 | 86 | ||
| 87 | (defun emit-parse-value (name fields) | 87 | (defun emit-parse-value (name fields) |
| 88 | (let ((type-sym (gensym "TYPE-SYM")) | 88 | (with-gensyms (source type) |
| 89 | (source (gensym "SOURCE"))) | 89 | `(defmethod parse-value ((,type (eql ',name)) ,source) |
| 90 | `(defmethod parse-value ((,type-sym (eql ',name)) ,source) | ||
| 91 | (let (,@(iter (for field in fields) | 90 | (let (,@(iter (for field in fields) |
| 92 | (collect (emit-let-gethash field source)))) | 91 | (collect (emit-let-gethash field source)))) |
| 93 | (,(type-constructor name) | 92 | (,(type-constructor name) |
| @@ -95,10 +94,7 @@ | |||
| 95 | (appending (emit-constructor-args field)))))))) | 94 | (appending (emit-constructor-args field)))))))) |
| 96 | 95 | ||
| 97 | (defun emit-printer (name printer-name fields) | 96 | (defun emit-printer (name printer-name fields) |
| 98 | (let ((obj (gensym "OBJ")) | 97 | (with-gensyms (depth obj pprint-args stream) |
| 99 | (stream (gensym "STREAM")) | ||
| 100 | (depth (gensym "DEPTH")) | ||
| 101 | (pprint-args (gensym "PPRINT-ARGS"))) | ||
| 102 | `(defun ,printer-name (,obj ,stream ,depth) ; lint:suppress lambda-list-invalid | 98 | `(defun ,printer-name (,obj ,stream ,depth) ; lint:suppress lambda-list-invalid |
| 103 | (declare (ignore ,depth)) | 99 | (declare (ignore ,depth)) |
| 104 | (let (,pprint-args) | 100 | (let (,pprint-args) |
| @@ -117,10 +113,10 @@ | |||
| 117 | `(,(field-name field) ,(field-default field) :type ,(field-type field)))) | 113 | `(,(field-name field) ,(field-default field) :type ,(field-type field)))) |
| 118 | 114 | ||
| 119 | (defmacro define-tg-type (name &body field-specs) | 115 | (defmacro define-tg-type (name &body field-specs) |
| 120 | (let ((fields (parse-field-specs field-specs)) | 116 | (let ((fields (parse-field-specs field-specs))) |
| 121 | (printer-name (gensym "PRINTER"))) | 117 | (with-gensyms (printer) |
| 122 | `(progn | 118 | `(progn |
| 123 | ,(emit-struct name printer-name fields) | 119 | ,(emit-struct name printer fields) |
| 124 | ,(emit-printer name printer-name fields) | 120 | ,(emit-printer name printer fields) |
| 125 | ,(emit-parse-value name fields) | 121 | ,(emit-parse-value name fields) |
| 126 | ,(emit-jzon-coerced-fields name fields)))) | 122 | ,(emit-jzon-coerced-fields name fields))))) |