From b81dddf89cc00633ff125244baaecd5b467b1ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 5 Feb 2023 17:14:57 -0300 Subject: Generate consts from macro file --- cmamut.lisp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'cmamut.lisp') diff --git a/cmamut.lisp b/cmamut.lisp index 904241e..4bd4cad 100644 --- a/cmamut.lisp +++ b/cmamut.lisp @@ -4,7 +4,6 @@ ; Generate code that creates a package and puts all definitions there. ; To avoid collisions the created package shouldn't import any symbols. -; TODO: Get constants from macro file generated by c2ffi -M ; TODO: Use c2ffi to extract the spec.json using the header file as parameter (defun get-raw-spec (filename) @@ -66,7 +65,7 @@ (cook-type new-type type-associations) (error "Unknown type: ~a" tag))))))) -(defun cook-function (raw-function type-associations &optional name-transformer) +(defun cook-function (raw-function type-associations) (let ((raw-params (gethash "parameters" raw-function)) (cooked-params (queue:new))) (dotimes (j (length raw-params)) @@ -77,9 +76,7 @@ (cook-type (gethash "type" (aref raw-params j)) type-associations)))) `(sb-alien:define-alien-routine ,(let ((function-name (gethash "name" raw-function))) - (if name-transformer - (list function-name (funcall name-transformer function-name)) - (list function-name (intern function-name)))) + (list function-name (intern function-name))) ,(cook-type (gethash "return-type" raw-function) type-associations) ,@(queue:to-list cooked-params)))) @@ -95,6 +92,11 @@ (intern (gethash "name" raw-composite))) ,@(queue:to-list cooked-fields)))) +(defun cook-const (raw-const) + `(defparameter + ,(intern (gethash "name" raw-const)) + ,(gethash "value" raw-const))) + (defun cook-enum (raw-enum enum-references) (let ((name (gethash (gethash "id" raw-enum) enum-references)) (raw-fields (gethash "fields" raw-enum)) @@ -135,6 +137,7 @@ type-associations)) (defstruct spec + consts functions typedefs composite @@ -156,6 +159,9 @@ (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") (string= tag ":array") (string= tag ":pointer")) @@ -177,6 +183,7 @@ ((string= tag "enum") (push def (spec-enums spec))) + ((or (string= tag "struct") (string= tag "union")) (push def (spec-composite spec))))) spec) @@ -222,7 +229,8 @@ (defun codegen (spec function-filter) (let ((enum-references (generate-enum-references (spec-typedefs spec))) (type-associations (generate-type-associations (spec-typedefs spec)))) - (let ((enums (mapcar (lambda (x) (cook-enum x enum-references)) + (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)) (sort-composites-by-deps @@ -230,7 +238,7 @@ (spec-composite spec))))) (functions (mapcar (lambda (x) (cook-function x type-associations)) (remove-if-not function-filter (spec-functions spec))))) - (remove-duplicates (concatenate 'list enums composite functions) :test #'equal)))) + (remove-duplicates (concatenate 'list consts enums composite functions) :test #'equal)))) (defun to-file (code filename) (with-open-file (f filename -- cgit v1.2.3