diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2023-02-25 02:56:07 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2023-02-25 02:56:07 -0300 |
commit | ea1b5a50c6e266326667c0552d9277651bf08b5a (patch) | |
tree | dd24bae474221fba939eb763fa4d271801de7838 | |
parent | c843e4abe4940fd280670f0ca19586f6c57b5e4a (diff) | |
download | cmamut-ea1b5a50c6e266326667c0552d9277651bf08b5a.tar.gz cmamut-ea1b5a50c6e266326667c0552d9277651bf08b5a.zip |
Revert changes from 343ff7a65e0db0f70e5c1253181e50e0e44986b2
-rw-r--r-- | cmamut.lisp | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/cmamut.lisp b/cmamut.lisp index fddf790..2b7628c 100644 --- a/cmamut.lisp +++ b/cmamut.lisp @@ -63,6 +63,21 @@ (cook-type new-type type-associations) (error "Unknown type: ~a" tag))))))) +(defun cook-function (raw-function type-associations) + (let ((raw-params (gethash "parameters" raw-function)) + (cooked-params (queue:new))) + (dotimes (j (length raw-params)) + (queue:add cooked-params + (list (if (string= (gethash "name" (aref raw-params j)) "") + (gensym) + (intern (gethash "name" (aref raw-params j)) +target-package+)) + (cook-type (gethash "type" (aref raw-params j)) type-associations)))) + `(sb-alien:define-alien-routine + ,(let ((function-name (gethash "name" raw-function))) + (list function-name (intern function-name +target-package+))) + ,(cook-type (gethash "return-type" raw-function) type-associations) + ,@(queue:to-list cooked-params)))) + (defun cook-composite (raw-composite type-associations) (let ((raw-fields (gethash "fields" raw-composite)) (cooked-fields (queue:new))) @@ -120,6 +135,7 @@ (defstruct spec consts + functions typedefs composite enums) @@ -137,7 +153,10 @@ (defun classify-definition (def spec) (let ((tag (gethash "tag" def))) - (cond ((and (string= tag "const") (gethash "value" def)) + (cond ((string= tag "function") + (push def (spec-functions spec))) + + ((and (string= tag "const") (gethash "value" def)) (push def (spec-consts spec))) ((or (string= tag "typedef") @@ -204,17 +223,21 @@ (filter-deps comps-and-deps)) (reverse result))) -(defun codegen (spec) +(defun codegen (spec function-filter) (let ((enum-references (generate-enum-references (spec-typedefs spec))) (type-associations (generate-type-associations (spec-typedefs spec)))) (let ((consts (mapcar #'cook-const (spec-consts spec))) (enums (mapcar (lambda (x) (cook-enum x enum-references)) (spec-enums spec))) - (composite (mapcar (lambda (x) `(sb-alien:define-alien-type nil ,x)) + (composite (mapcar (lambda (x) `(sb-alien:define-alien-type ,(cadr x) ,x)) (sort-composites-by-deps (mapcar (lambda (x) (cook-composite x type-associations)) - (spec-composite spec)))))) - (remove-duplicates (concatenate 'list consts enums composite) :test #'equal)))) + (spec-composite spec))))) + (functions (mapcar (lambda (x) (cook-function x type-associations)) + (if function-filter + (remove-if-not function-filter (spec-functions spec)) + (spec-functions spec))))) + (remove-duplicates (concatenate 'list consts enums composite functions) :test #'equal)))) (defun to-file (code filename) (with-open-file (f filename @@ -227,11 +250,11 @@ (*package* (find-package +target-package+))) (format f "~{~s~&~}" code)))) -(defun run (input output) +(defun run (input output &optional function-filter) (when (not (find-package +target-package+)) (make-package +target-package+)) (let* ((raw-spec (get-raw-spec input)) (spec (classify-definitions raw-spec)) - (code (codegen spec))) + (code (codegen spec function-filter))) (to-file code output)) (delete-package (find-package +target-package+))) |