forked from alex-eg/sex
`.' now writes whichever of C's member operators the type calls for, so nothing needs the arrow spelled out and the name is free for the macro Clojure spells the same way: (-> 5 double-it (add 3)) reads in the order it happens rather than inside out. It has no business in the prelude, which every unit pays for whether or not it threads anything, so it opens the standard library instead and is imported like anything else. The compiler finds lib/ by asking the binary where it is -- beside sexc in a work tree, under ../share/sex once installed -- and looks there last, so a module of one's own by that name still wins. The C arrow becomes `%->', and that rename is what makes the rest possible. The closure lifter writes arrows of its own for the capture record, those forms go back through walk-statement, and a macro named `->' threads them: (-> ƛcaptures n) turns into a call to n, with nothing saying so until the C compiler. fmt-c rendered a bare `->' as the arrow, which left the one spelling that cannot be right -- threading with no import -- looking like the operator it stopped being. It goes through c-apply now, the way any other head nothing knows does.
38 lines
1.3 KiB
Scheme
38 lines
1.3 KiB
Scheme
(input)
|
|
(output "box { w=3 h=4 label=wide }")
|
|
(return 0)
|
|
|
|
;;; A macro generating code from the type database: it is handed a
|
|
;;; struct name, walks its fields with map-fields, and picks a printf
|
|
;;; conversion per field with type-match. Exercises semen registering
|
|
;;; the struct and the macro reading it back at expansion time.
|
|
|
|
(include stdio.h)
|
|
|
|
(defmacro (print-struct type-name)
|
|
(let ((printers
|
|
(map-fields type-name
|
|
(lambda (name type)
|
|
`(printf ,(string-append " " (symbol->string name) "="
|
|
(type-match type
|
|
(int "%d")
|
|
((* const char) "%s")
|
|
(else (error "print-struct: unsupported field type"
|
|
name type))))
|
|
(. v ,name))))))
|
|
(if (not printers)
|
|
(error "print-struct: no such struct" type-name)
|
|
`(fn ,(cat 'print- type-name) ((v (* const struct ,type-name))) void
|
|
(printf ,(string-append (symbol->string type-name) " {"))
|
|
,@printers
|
|
(printf " }\n")))))
|
|
|
|
(struct box ((w int) (h int) (label (* const char))))
|
|
|
|
(print-struct box)
|
|
|
|
(pub fn main () int
|
|
(var b (struct box) #(3 4 "wide"))
|
|
(print-box (& b))
|
|
(return 0))
|