hsx/src/element.lisp

41 lines
1,013 B
Common Lisp
Raw Normal View History

2024-05-25 12:48:35 +00:00
(defpackage #:hsx/element
2024-05-25 03:00:39 +00:00
(:use #:cl)
(:export #:element-kind
#:element-props
#:create-element
#:expand))
2024-05-25 12:48:35 +00:00
(in-package #:hsx/element)
2024-05-25 03:00:39 +00:00
(defclass element ()
((kind
:reader element-kind
:initarg :kind)
(props
:reader element-props
:initarg :props)))
(defun create-element (kind props &rest children)
(make-instance 'element
:kind kind
:props (append props
(and children
(list :children (flatten children))))))
(defmethod expand ((elm element))
(with-accessors ((kind element-kind)
(props element-props)) elm
(if (functionp kind)
(apply kind props)
elm)))
;;;; utils
2024-05-25 03:00:39 +00:00
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec
(car x)
(rec (cdr x) acc))))))
(rec x nil)))