diff options
Diffstat (limited to 'cmamut.lisp')
-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+))) |