add auto typedefs for structs

This commit is contained in:
2025-06-09 00:21:42 +03:00
parent f74d1b0c1c
commit 469db7b92e
3 changed files with 37 additions and 19 deletions

View File

@@ -73,6 +73,9 @@ Sex offers automatic unkebabification of all symbols, i.e. no more
ugly ~GL_ARRAY_BUFFER~ s in your code, they may be written in their
proper form: ~GL-ARRAY-BUFFER~.
** Auto typedef for structs
Probably harmless idk.
** Use an established environment for development
As Sex is S-expressions, you always have Emacs with paredit as your
best option.

View File

@@ -3,7 +3,12 @@
(define (foo)
"Hello from Chicken code!\n")
(struct foo
((int a)
(float b)))
(pub fn int main ((int argc) (char **argv))
(var int a 10)
(puts "Hello from Sex!")
(var (array char 512) name)
(puts "What is your name?")

View File

@@ -34,39 +34,49 @@
#f)))
(define (walk-generic form)
(let loop ((tree (tree-map atom-to-fmt-c form)))
(let ((unquote-form (tree-find (tree-finder 'unquote) tree #f)))
(if unquote-form
(let ((inv (invert-tree tree)))
(loop (tree-replace inv unquote-form (eval (cadr unquote-form)))))
tree))))
(tree-map
atom-to-fmt-c
(let ((ret-form form))
(let loop ((unquote-form (tree-find (tree-finder 'unquote) form #f)))
(if unquote-form
(begin
(set! ret-form (tree-replace (invert-tree form)
unquote-form
(eval (cadr unquote-form))))
(loop (tree-find (tree-finder 'unquote)
ret-form
#f)))
ret-form)))))
(define (walk-function form static)
(if static
(list 'static (walk-generic form))
(walk-generic (cdr form))))
(define (walk-sex-tree form)
(case (car form)
((fn) (walk-function form #t))
((pub) (walk-function form #f))
((template) '()) ; TODO
((struct) (walk-generic form))
(else (walk-generic form))))
(define (walk-struct form acc)
(let ((name (unkebabify (cadr form))))
(cons (walk-generic form)
(cons `(typedef struct ,name ,name) acc))))
(define (process-form form)
(define (walk-sex-tree form acc)
(case (car form)
((define)
(eval form) '())
((fn) (cons (walk-function form #t) acc))
((pub) (cons (walk-function form #f) acc))
((struct) (walk-struct form acc))
(else (cons (walk-generic form) acc))))
(define (process-form form acc)
(case (car form)
((define) (eval form) acc)
((load) (eval form) acc)
(else
(walk-sex-tree form))))
(walk-sex-tree form acc))))
(define (process-raw-forms raw-forms acc)
(if (null? raw-forms) (filter (fn (not (null? x)))
(reverse acc))
(process-raw-forms (cdr raw-forms)
(cons (process-form (car raw-forms))
acc))))
(process-form (car raw-forms) acc))))
(define (read-forms acc)
(let ((r (read)))