forked from alex-eg/sex
unshittify templates
This commit is contained in:
@@ -1,40 +1,36 @@
|
||||
(template (T)
|
||||
struct list-T
|
||||
((T value)
|
||||
((list-T *) next)))
|
||||
(template (list-T (T))
|
||||
(struct list-T
|
||||
((T value)
|
||||
((* list-T) next))))
|
||||
|
||||
(template (T)
|
||||
pub fn (list-T *) make-list-T ()
|
||||
(var (list-T *) list (malloc (sizeof list-T)))
|
||||
(= (-> list next) NULL)
|
||||
list)
|
||||
(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 (T)
|
||||
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 (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 (T)
|
||||
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 (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 (T)
|
||||
pub fn bool is-empty-list-T ((list-T *list))
|
||||
(== (-> list next) NULL))
|
||||
(template (is-empty-list-T (T) is-public?)
|
||||
(,@(if is-public? '(pub) '()) fn bool is-empty-list-T ((list-T *list))
|
||||
(== (-> list next) NULL)))
|
||||
|
||||
(define-syntax list-for-each
|
||||
(syntax-rules ()
|
||||
((_ list-var elt-var what-do ...)
|
||||
'(begin
|
||||
;; todo: that int here is not good
|
||||
(var int elt-var (-> list-var value))
|
||||
(while (!= (-> list-var next) NULL)
|
||||
what-do ...
|
||||
(= list-var (-> list-var next))
|
||||
(= elt-var (-> list-var value)))))))
|
||||
(template (list-for-each (what-do list-var elt-var))
|
||||
(var int elt-var (-> list-var value))
|
||||
(while (!= (-> list-var next) NULL)
|
||||
what-do
|
||||
(= list-var (-> list-var next))
|
||||
(= elt-var (-> list-var value))))
|
||||
|
||||
@@ -5,11 +5,17 @@
|
||||
|
||||
(load "list.hsex")
|
||||
|
||||
(instance list-int list-T (int))
|
||||
(instance make-list-int make-list-T (int))
|
||||
(instance add-value-list-int add-value-list-T (int))
|
||||
(instance length-list-int length-list-T (int))
|
||||
(instance is-empty-list-int is-empty-list-T (int))
|
||||
(struct foo
|
||||
((float a)
|
||||
(int b)))
|
||||
|
||||
,(list-T '(int))
|
||||
,(make-list-T '(int) #f)
|
||||
,(add-value-list-T '(int) #f)
|
||||
,(length-list-T '(int) #f)
|
||||
,(is-empty-list-T '(int) #f)
|
||||
|
||||
(fn bool bar () false)
|
||||
|
||||
(pub fn int main ()
|
||||
(var (* list-int) l (make-list-int))
|
||||
@@ -17,6 +23,6 @@
|
||||
(add-value-list-int l 3)
|
||||
(add-value-list-int l 4)
|
||||
(printf "Size of the list: %zu\n" (length-list-int l))
|
||||
,(list-for-each l v (printf "%d " v))
|
||||
,(list-for-each '((printf "%d " v) l v))
|
||||
(printf "\n")
|
||||
0)
|
||||
|
||||
Reference in New Issue
Block a user