forked from alex-eg/sex
(. v length) made the shape of a form depend on the receiver's type: (. a b c d e) was either the path a.b.c.d.e or a/b called on c d e, and nothing but the ops registry could say which. Resolution had to arbitrate, a field won, and so adding a field named length to vec2 turned every (. v length) in reach from a call into a read -- silently, and only sometimes into a type error. Resolution may need types; shape must not. (.length v) puts the method where an operator belongs, so (. v x) is always the field x, a field and a method of one name coexist, and a reader needs no symbol table to see where the arguments start. Which of &, * or nothing goes in front of the receiver now follows from its own type against the method's self, rather than from whether the caller wrote . or ->, so (.length v) and (.length p) are the same call and -> is member access again and nothing else. The reader already allowed it: only a standalone . becomes dot-access, and .foo is the ordinary symbol designated initializers are made of.
46 lines
1.4 KiB
Scheme
46 lines
1.4 KiB
Scheme
;;; Uses the greet module. Build both, then link them:
|
|
;;;
|
|
;;; ./sexc example/greet.sex -c -o greet.o
|
|
;;; ./sexc example/greet-app.sex -o greet-app -- greet.o
|
|
;;;
|
|
;;; `(import greet)' pastes greet's public declarations here: `greet'
|
|
;;; as a prototype and `greet-count' as an extern. Both keep external
|
|
;;; linkage, so they refer to the one definition in greet.o rather than
|
|
;;; to private copies.
|
|
;;;
|
|
;;; Also check that import populates type-database, by means of
|
|
;;; describe-fields macro, which should work on imported type.
|
|
|
|
(include stdio.h)
|
|
|
|
(import greet)
|
|
|
|
(pub fn main () int
|
|
(greet "world")
|
|
(greet "sex")
|
|
(printf "%d greetings\n" greet-count)
|
|
(describe-fields greeting)
|
|
(printf "\n")
|
|
;; ...and through the imported typedef for it
|
|
(describe-fields greeting-t)
|
|
(printf "\n")
|
|
(var m (enum mood) grumpy)
|
|
(printf "mood %d\n" m)
|
|
|
|
(var add-10 (closure ((int)) int) (make-adder 10))
|
|
(printf "closure %d %d" (add-10 5) (apply-twice add-10 1))
|
|
(var base int 100)
|
|
(var here (closure ((int)) int)
|
|
(closure ((x int)) int (base) (return (+ base x))))
|
|
(printf " %d\n" (apply-twice here 1))
|
|
|
|
;; the bare record name, then the method both ways round
|
|
(var v vec2 #(vec2 : 3 4))
|
|
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
|
|
|
(var c colour (colour/rgb 1 2 3))
|
|
(match c
|
|
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
|
((none ()) (printf "colour none\n")))
|
|
(return 0))
|