move Sex to better types

Types, fns and vars are now written in another, better, more intuitive
and readable way.

Types:
"pointer to const char" is "* const char"
"array of pointers to volatile int" is "[* volatile int]"
Read left to right

Vars (as well as fn args, struct fields):
(var name type)
(struct vec3 ((x float) (y float) (z float))
(fn vec3-add ((v1 vec3) (v2 vec3)) vec3 ...)

Also fns are now have return types after arg list.

* is now must not be attached to any type name (or variable for that
matter)
This commit is contained in:
2025-10-21 21:38:41 +03:00
committed by Pavel Kulyov
parent 2228ab3b50
commit 48a3d06925
8 changed files with 318 additions and 59 deletions

View File

@@ -1,10 +1,10 @@
;;; Prototypes
(fn void puk ())
(fn puk () void)
(pub fn void plak ())
(pub fn plak () void)
;;; Functions
(fn int foo () (return 1))
(fn foo () int (return 1))
(pub fn void bar ((int a) (int b))
(pub fn bar ((a int) (b int)) void
(printf "%d\n" (+ a b)))

View File

@@ -1,9 +1,9 @@
(include stdio.h)
(pub fn int main ((int argc) (char **argv))
(pub fn main ((argc int) (argv [* const char])) int
(puts "Hello from Sex!")
(var (array char 512) name)
(var name [char 512])
(puts "What is your name?")
(scanf "%s" (cast char* &name))
(scanf "%s" (cast (& name) (* char)))
(printf "Hello, %s!\n" name)
(return 0))

View File

@@ -1,21 +1,21 @@
(include stdio.h)
(fn int sum ((int a) (int b))
(fn sum ((a int) (b int)) int
(return (+ a b)))
(pub fn int main ()
(var int a 10)
(var int b 20)
(var (fn int ((int) (int))) sum-fn sum)
(pub fn main () int
(var a int 10)
(var b int 20)
(var (fn ((int) (int)) int) sum-fn sum)
(var (fn int ((int) (int))) sum-lambda
(var (fn ((int) (int)) int) sum-lambda
(lambda int ((int a) (int b)) ()
(lambda ((a int) (b int)) int ()
(return (+ a b))))
(var (fn int ((int))) sum-lambda-2
(var (fn ((int)) int) sum-lambda-2
(lambda int ((int a)) ()
(lambda ((a int)) int ()
(return (+ a 20))))
(printf "Hello from main fn!\n")
@@ -24,15 +24,28 @@
(printf "Calling fn ptr: %d\n" (sum-fn a b))
(printf "Calling lambda: %d\n" (sum-lambda a b))
(printf "Calling other lambda: %d\n" (sum-lambda-2 a))
(printf "Calling lambda inplace: %d\n" ((lambda int ((int a) (int b)) ()
(printf "Calling lambda inplace: %d\n" ((lambda ((a int) (b int)) int ()
(return (+ a b 100)))
a b))
(var (fn int ((int))) l-1
(lambda int ((int a)) ()
(var (fn int ((int))) l-2
(lambda int ((int a)) ()
(var (fn ((int)) int) l-1
(lambda ((a int)) int ()
(var (fn ((int)) int) l-2
(lambda ((a int)) int ()
(return (+ 60 a))))
(return (+ 600 (l-2 a)))))
(printf "Calling nested lambdas: %d\n" (l-1 6))
;; Not supported yet
;; Closure
;; (var (fn (fn ((int)) int) ((int))) make-adder
;; (lambda (fn int ((int a))) ()
;; (return (lambda int ((int b)) (a)
;; (return (+ a b))))))
;;
;; (var (fn int ((int))) add-10
;; (make-adder 10))
;; (var (fn int ((int))) add-20
;; (make-adder 20))
;; (printf "Calling closures: %d\n" (add-10 24))
(return 0))

View File

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

View File

@@ -5,12 +5,12 @@
(import list)
(struct foo
((float a-field)
(int b)
((const char *) c)
((fn bool ((bool val))) not)))
((a-field float)
(b int)
(c (* const char))
(not (fn ((val bool)) bool))))
(var (struct foo) f)
(var f (struct foo))
(list-T int)
(make-list-T int #f)
@@ -18,15 +18,16 @@
(length-list-T int #f)
(is-empty-list-T int #f)
(extern fn void puk ((int a) (float b)))
(pub fn bool baz () (return true))
(extern fn puk ((a int) (b float)) void)
(pub fn baz () bool
(return true))
(extern var int i)
(var int j)
(pub var int k)
(extern var i int)
(var j int)
(pub var k int)
(pub fn int main ()
(var (struct list-int) *l (make-list-int))
(pub fn main () int
(var l (* struct list-int) (make-list-int))
(printf "Size of the list: %lu\n" (length-list-int l))
(add-value-list-int l 3)
(add-value-list-int l 4)
@@ -35,9 +36,9 @@
(printf "%d " v))
(printf "\n")
(printf "Size of the list: %lu\n" (length-list-int l))
(printf "%p\n" (cast void* l->next))
(printf "%p\n" (cast l->next (* void)))
(return 0))
(pub fn void print-list (((const struct list-int) *l))
(pub fn print-list ((l (* const struct list-int))) void
(list-for-each (const struct list-int) l int v (printf "%d " v))
(printf "\n"))