From a03fc957185a0a409432d9bc8364c25fb3ff4af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 5 Feb 2023 19:50:24 -0300 Subject: Generate all definitions in target package --- cmamut.lisp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'cmamut.lisp') diff --git a/cmamut.lisp b/cmamut.lisp index 4bd4cad..c8d213f 100644 --- a/cmamut.lisp +++ b/cmamut.lisp @@ -1,11 +1,9 @@ (in-package #:cmamut) -; Ideas: -; Generate code that creates a package and puts all definitions there. -; To avoid collisions the created package shouldn't import any symbols. - ; TODO: Use c2ffi to extract the spec.json using the header file as parameter +(defvar *target-package* *package*) + (defun get-raw-spec (filename) (json:from-file filename)) @@ -51,9 +49,9 @@ (cook-type (gethash "type" raw-type) type-associations) (gethash "size" raw-type))) ((string= tag ":struct") - (list 'sb-alien:struct (intern (gethash "name" raw-type)))) + (list 'sb-alien:struct (intern (gethash "name" raw-type) *target-package*))) ((string= tag ":union") - (list 'sb-alien:union (intern (gethash "name" raw-type)))) + (list 'sb-alien:union (intern (gethash "name" raw-type) *target-package*))) ((string= tag ":enum") (list 'sb-alien:enum (gethash "name" raw-type))) ((or (string= tag "struct") (string= tag "union")) @@ -72,11 +70,11 @@ (queue:add cooked-params (list (if (string= (gethash "name" (aref raw-params j)) "") (gensym) - (intern (gethash "name" (aref raw-params j)))) + (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))) + (list function-name (intern function-name *target-package*))) ,(cook-type (gethash "return-type" raw-function) type-associations) ,@(queue:to-list cooked-params)))) @@ -85,16 +83,16 @@ (cooked-fields (queue:new))) (dotimes (j (length raw-fields)) (queue:add cooked-fields - (list (intern (gethash "name" (aref raw-fields j))) + (list (intern (gethash "name" (aref raw-fields j)) *target-package*) (cook-type (gethash "type" (aref raw-fields j)) type-associations)))) `(,(intern (string-upcase (gethash "tag" raw-composite)) 'sb-alien) ,(when (> (length (gethash "name" raw-composite)) 0) - (intern (gethash "name" raw-composite))) + (intern (gethash "name" raw-composite) *target-package*)) ,@(queue:to-list cooked-fields)))) (defun cook-const (raw-const) `(defparameter - ,(intern (gethash "name" raw-const)) + ,(intern (gethash "name" raw-const) *target-package*) ,(gethash "value" raw-const))) (defun cook-enum (raw-enum enum-references) @@ -103,7 +101,7 @@ (cooked-fields (queue:new))) (dotimes (j (length raw-fields)) (queue:add cooked-fields - (list (intern (gethash "name" (aref raw-fields j))) + (list (intern (gethash "name" (aref raw-fields j)) *target-package*) (gethash "value" (aref raw-fields j))))) `(sb-alien:define-alien-type nil @@ -247,9 +245,13 @@ :if-does-not-exist :create) (let ((*print-length* nil) (*print-level* nil)) + (format f "(cl:defpackage ~s)~&" *target-package*) (format f "~{~s~&~}" code)))) -(defun run (input output &optional function-filter) +(defun run (pkg input output &optional function-filter) + (setf *target-package* pkg) + (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 function-filter))) -- cgit v1.2.3