Add create-element
This commit is contained in:
parent
f086e13e04
commit
f8171dd47f
4 changed files with 87 additions and 1 deletions
src
38
src/element.lisp
Normal file
38
src/element.lisp
Normal file
|
@ -0,0 +1,38 @@
|
|||
(defpackage #:piccolo/element
|
||||
(:use #:cl)
|
||||
(:export #:element-kind
|
||||
#:element-props
|
||||
#:create-element
|
||||
#:expand))
|
||||
(in-package #:piccolo/element)
|
||||
|
||||
(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))))))
|
||||
|
||||
(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)))
|
||||
|
||||
(defmethod expand ((elm element))
|
||||
(with-accessors ((kind element-kind)
|
||||
(props element-props)) elm
|
||||
(if (functionp kind)
|
||||
(apply kind props)
|
||||
(error "element-kind is not a function."))))
|
Loading…
Add table
Add a link
Reference in a new issue