1
0
forked from alex-eg/sex

implement modules

This commit is contained in:
2025-07-28 19:41:11 +03:00
parent 93a4d181d0
commit 44a352849a
5 changed files with 223 additions and 112 deletions

View File

@@ -1,36 +0,0 @@
(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))))

37
example/list.sex Normal file
View File

@@ -0,0 +1,37 @@
(pub template (list-T ?T)
(struct list-?T
((?T value)
((* list-?T) 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 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 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 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 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))))

View File

@@ -5,7 +5,7 @@
(chicken-import srfi-1 brev-separate) ; list routines, e.g. fold
(chicken-load "list.seh")
(import list)
(chicken-define (imports-test a b c)
(fold + 0 (list 1 2 3 a b c)))
@@ -19,10 +19,10 @@
(var foo f #((= .a-field 1.2)))
(list-T int)
(make-list-T int)
(add-value-list-T int)
(length-list-T int)
(is-empty-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)
(extern fn void puk ((int a) (float b)))
(fn int bar () ,(imports-test 10 20 30))
@@ -38,32 +38,13 @@
(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
(list-for-each list-int l int v
(printf "%d " v))
(printf "\n")
(printf "%zu\n" l->next)
(printf "Size of the list: %zu\n" (length-list-int l))
(printf "%p\n" l->next)
0)
(pub fn segs-renderer* create-renderer ())
(pub fn void clear-command-buffer ((segs-renderer *r)))
(pub fn void add-render-command ((segs-renderer *r) ((fn void ()) command)))
(pub fn void commit-command-buffer ((segs-renderer *r)))
(template (list-T ?T)
(struct list-?T
((?T value)
((* list-?T) next))))
(template (list-for-each type list-var elt-var body)
(var type elt-var (-> list-var value))
(while (!= (-> list-var next) NULL)
body
(= list-var (-> list-var next))
(= elt-var (-> list-var value))))
; ... somewhere later
(list-T int)
(pub fn void print-list (((const list-int) *l))
(list-for-each int l v (printf "%d " v))
(list-for-each (const list-int) l int v (printf "%d " v))
(printf "\n"))