forked from alex-eg/sex
No more unquotes to instantiate templates. Also no need to pass quoted substitution lists, just use them as regular lisp macros.
37 lines
1.1 KiB
Plaintext
37 lines
1.1 KiB
Plaintext
(template (list-T ?T)
|
|
(struct list-?T
|
|
((?T value)
|
|
((* list-?T) next))))
|
|
|
|
(template (make-list-T ?T is-public?)
|
|
(,@(if 'is-public? '(pub) '()) fn (* list-?T) make-list-?T ()
|
|
(var (* list-?T) list (cast (* list-?T) (malloc (sizeof list-?T))))
|
|
(= (-> list next) NULL)
|
|
list))
|
|
|
|
(template (add-value-list-?T ?T is-public?)
|
|
(,@(if 'is-public? '(pub) '()) fn void add-value-list-?T ((list-?T *list) (T value))
|
|
(while (!= (-> list next) NULL)
|
|
(= list (-> list next)))
|
|
(= (-> list next) (make-list-?T))
|
|
(= (-> list value) value)))
|
|
|
|
(template (length-list-?T ?T is-public?)
|
|
(,@(if 'is-public? '(pub) '()) fn size-t length-list-?T ((list-?T *list))
|
|
(var size-t n 0)
|
|
(while (!= (-> list next) NULL)
|
|
(= list (-> list next))
|
|
(++ n))
|
|
n))
|
|
|
|
(template (is-empty-list-?T ?T is-public?)
|
|
(,@(if 'is-public? '(pub) '()) fn bool is-empty-list-?T ((list-?T *list))
|
|
(== (-> list next) NULL)))
|
|
|
|
(template (list-for-each list-var elt-var what-do)
|
|
(var int elt-var (-> list-var value))
|
|
(while (!= (-> list-var next) NULL)
|
|
what-do
|
|
(= list-var (-> list-var next))
|
|
(= elt-var (-> list-var value))))
|