Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64
2
Makefile
2
Makefile
@@ -104,7 +104,7 @@ sextest:
|
|||||||
$(MAKE) -C ./tools/sextest sextest
|
$(MAKE) -C ./tools/sextest sextest
|
||||||
cp ./tools/sextest/sextest .
|
cp ./tools/sextest/sextest .
|
||||||
|
|
||||||
SEX_TEST_PROGRAMS = c99 \
|
SEX_TEST_PROGRAMS = c11 \
|
||||||
closure-signatures \
|
closure-signatures \
|
||||||
closures \
|
closures \
|
||||||
comments \
|
comments \
|
||||||
|
|||||||
14
semen.scm
14
semen.scm
@@ -1182,19 +1182,13 @@
|
|||||||
|
|
||||||
(define +closure-env-bytes+ 16)
|
(define +closure-env-bytes+ 16)
|
||||||
|
|
||||||
;;; +closure-env-bytes+ for capacity, the widest built-ins for
|
;;; +closure-env-bytes+ for capacity, max_align_t for alignment, hence
|
||||||
;;; alignment, hence union -- a union takes the strictest alignment of
|
;;; union -- a union takes the strictest alignment of its members.
|
||||||
;;; its members. `max_align_t' would say the second in one word:
|
|
||||||
;;;
|
|
||||||
;;; sexc hello-world.sex -- -std=c99 unknown type name 'max_align_t'
|
|
||||||
(define +closure-env-type+ 'ƛenv)
|
(define +closure-env-type+ 'ƛenv)
|
||||||
|
|
||||||
(define (closure-env-declaration)
|
(define (closure-env-declaration)
|
||||||
`(union ,+closure-env-type+ ((bytes (¤ char ,+closure-env-bytes+))
|
`(union ,+closure-env-type+ ((align max-align-t)
|
||||||
(align-integer (long long))
|
(bytes (¤ char ,+closure-env-bytes+)))))
|
||||||
(align-real (long double))
|
|
||||||
(align-pointer (* void))
|
|
||||||
(align-code (fn ((* void)) void)))))
|
|
||||||
|
|
||||||
(define +closure-structs+ (make-hash-table))
|
(define +closure-structs+ (make-hash-table))
|
||||||
(define +closure-forwards+ (make-hash-table))
|
(define +closure-forwards+ (make-hash-table))
|
||||||
|
|||||||
39
tests/sex-programs/c11.sex
Normal file
39
tests/sex-programs/c11.sex
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
(compilation "-- -std=c11 -pedantic-errors")
|
||||||
|
(input)
|
||||||
|
(output "c11: 42 43 6")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; C11 is the floor, and this is the program that holds it there: the
|
||||||
|
;;; C every feature emits has to build under it with no extension, so
|
||||||
|
;;; one that reaches past it -- a GNU statement expression, a C23
|
||||||
|
;;; keyword -- fails here rather than on a user's compiler.
|
||||||
|
;;;
|
||||||
|
;;; A capturing closure is the one that leans on C11 itself, through
|
||||||
|
;;; max_align_t in the environment and `_Static_assert' on its size;
|
||||||
|
;;; the record, the method and the match are here for what they emit.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(record point ((x int) (y int)))
|
||||||
|
|
||||||
|
(ops point
|
||||||
|
(fn area ((self (* const point))) int
|
||||||
|
(return (* (. self x) (. self y)))))
|
||||||
|
|
||||||
|
(sum shape ((square ((side int))) (dot ())))
|
||||||
|
|
||||||
|
(fn sides ((s shape)) int
|
||||||
|
(var n int 0)
|
||||||
|
(match s
|
||||||
|
((square (side)) (= n (* 4 side)))
|
||||||
|
((dot ()) (= n 0)))
|
||||||
|
(return n))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var p point #(point : 6 7))
|
||||||
|
(var k int 1)
|
||||||
|
(var add-k (closure ((int)) int)
|
||||||
|
(closure ((v int)) int (k) (return (+ v k))))
|
||||||
|
(printf "c11: %d %d %d\n"
|
||||||
|
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2))
|
||||||
|
(return 0))
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
(compilation "-- -std=c99 -pedantic-errors")
|
|
||||||
(input)
|
|
||||||
(output "c99: 42")
|
|
||||||
(return 0)
|
|
||||||
|
|
||||||
;;; The closure environment is part of the ABI, so its union is declared
|
|
||||||
;;; in every translation unit whether or not one is used. That put
|
|
||||||
;;; whatever it was written with into every program: `max_align_t' named
|
|
||||||
;;; the alignment in one word, and made C11 the floor for a program with
|
|
||||||
;;; no closure in it at all.
|
|
||||||
;;;
|
|
||||||
;;; The widest built-ins say the same thing -- a union is aligned for the
|
|
||||||
;;; strictest of its members -- and say it in C99.
|
|
||||||
;;;
|
|
||||||
;;; Closures themselves still want C11 for the `_Static_assert' that
|
|
||||||
;;; checks the captures fit, so this program keeps clear of them.
|
|
||||||
|
|
||||||
(include stdio.h)
|
|
||||||
|
|
||||||
(struct point ((x int) (y int)))
|
|
||||||
|
|
||||||
(fn area ((p (struct point))) int
|
|
||||||
(return (* (. p x) (. p y))))
|
|
||||||
|
|
||||||
(pub fn main () int
|
|
||||||
(var p (struct point) #((struct point) : 6 7))
|
|
||||||
(printf "c99: %d\n" (area p))
|
|
||||||
(return 0))
|
|
||||||
Reference in New Issue
Block a user