implement new macro system

This commit is contained in:
2025-08-05 17:47:04 +03:00
committed by Pavel Kulyov
parent 4245463714
commit 6c6e00e6dc
9 changed files with 116 additions and 221 deletions

View File

@@ -1,37 +1,46 @@
(pub template (list-T ?T)
(struct list-?T
((?T value)
((* list-?T) next))))
(pub defmacro (list-T type)
(let ((list-type (cat 'list- type)))
`(struct ,list-type
((,type value)
((* ,list-type) next)))))
(pub 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))
(pub defmacro (make-list-T type is-public?)
(let ((list-type (cat 'list- type))
(fn-name (cat 'make-list- type)))
`(,@(if is-public? '(pub) '()) fn (* ,list-type) ,fn-name ()
(var (* ,list-type) list (cast (* ,list-type) (malloc (sizeof ,list-type))))
(= (-> list next) NULL)
(return list))))
(pub 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)))
(pub defmacro (add-value-list-T type is-public?)
(let ((list-type (cat 'list- type))
(fn-name (cat 'add-value-list- type)))
`(,@(if is-public? '(pub) '()) fn void ,fn-name ((,list-type *list) (,type value))
(while (!= (-> list next) NULL)
(= list (-> list next)))
(= (-> list next) (,(cat 'make-list- type)))
(= (-> list value) value))))
(pub 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))
(pub defmacro (length-list-T type is-public?)
(let ((fn-name (cat 'length-list- type))
(list-type (cat 'list- type)))
`(,@(if is-public? '(pub) '()) fn size-t ,fn-name ((,list-type *list))
(var size-t n 0)
(while (!= (-> list next) NULL)
(= list (-> list next))
(++ n))
(return n))))
(pub template (is-empty-list-T ?T is-public?)
(,@(if 'is-public? '(pub) '()) fn bool is-empty-list-?T ((list-?T *list))
(== (-> list next) NULL)))
(pub defmacro (is-empty-list-T type is-public?)
`(,@(if is-public? '(pub) '()) fn bool ,(cat 'is-empty-list- type)
((,(cat 'list- type) *list))
(return (== (-> list next) NULL))))
(pub template (list-for-each list-type list-var elt-type elt-var what-do)
(var (pointer list-type) list-var-2 list-var)
(var elt-type elt-var (-> list-var-2 value))
(while (!= (-> list-var-2 next) NULL)
what-do
(= list-var-2 (-> list-var-2 next))
(= elt-var (-> list-var-2 value))))
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
(let ((list-var-2 (cat list-var '-2)))
`((var (pointer ,list-type) ,list-var-2 ,list-var)
(var ,elt-type ,elt-var (-> ,list-var-2 value))
(while (!= (-> ,list-var-2 next) NULL)
,what-do
(= ,list-var-2 (-> ,list-var-2 next))
(= ,elt-var (-> ,list-var-2 value))))))

View File

@@ -25,7 +25,7 @@
(is-empty-list-T int #f)
(extern fn void puk ((int a) (float b)))
(fn int bar () ,(imports-test 10 20 30))
(fn int bar () (return ,(imports-test 10 20 30)))
(pub fn void baz () true)
(extern var int i)
@@ -43,7 +43,7 @@
(printf "\n")
(printf "Size of the list: %zu\n" (length-list-int l))
(printf "%p\n" l->next)
0)
(return 0))
(pub fn void print-list (((const list-int) *l))
(list-for-each (const list-int) l int v (printf "%d " v))