forked from alex-eg/sex
A closure was a code pointer beside an inline environment: capture records, per-signature structs, conversion thunks and call helpers. It is gone; a lambda still lifts to an ordinary function and a (fn ...) value is still a bare code pointer. The capture budget, the environment union and the closure glyph go with it. The implementation is kept at tag 0.0.1.
97 lines
3.0 KiB
Scheme
97 lines
3.0 KiB
Scheme
(input)
|
|
(output "logical: 1 1 0"
|
|
"bitwise: 7 2 5"
|
|
"shifts: 48 0 12"
|
|
"increment: 7"
|
|
"decayed: 2 2 there"
|
|
"unsigned wins either way: 4294967295 4294967295"
|
|
"toplevel: 1 2.5 hi 12"
|
|
"a type is not a call: 7"
|
|
"radix literals: 224 5 128"
|
|
"a double reaches C whole: 1"
|
|
"negated literals: 1 2.5")
|
|
(return 0)
|
|
|
|
;;; The walk types an expression by its head, and the heads it had a
|
|
;;; rule for were the ones inference was written against. `&&', the
|
|
;;; bitwise operators, the shifts and `++' were not among them and each
|
|
;;; stopped with `cannot infer'.
|
|
;;;
|
|
;;; The rest of this is the same mistake in three other places: an array
|
|
;;; is a pointer the moment it is an operand, a rank tie is not decided
|
|
;;; by which operand was written first, and `_' is not a local's
|
|
;;; privilege.
|
|
|
|
(include stdio.h)
|
|
|
|
(fn area ((w int) (h int)) int
|
|
(return (* w h)))
|
|
|
|
;;; a toplevel `_' reads the same table a local's does, so it can name
|
|
;;; anything declared above it
|
|
(var n _ 1)
|
|
(var d _ 2.5)
|
|
(var s _ "hi")
|
|
(var a _ (+ 3 (* 3 3)))
|
|
|
|
(pub fn main () int
|
|
(var x int 6)
|
|
(var y int 3)
|
|
(var ok _ (&& x y))
|
|
(var orr _ (|| x y))
|
|
(var neg _ (! x))
|
|
(printf "logical: %d %d %d\n" ok orr neg)
|
|
|
|
(var bor _ (| x y))
|
|
(var band _ (& x y))
|
|
(var bxor _ (^ x y))
|
|
(printf "bitwise: %d %d %d\n" bor band bxor)
|
|
|
|
;; a shift is the promoted left operand, not a join: the right one
|
|
;; says only how far
|
|
(var c char 12)
|
|
(var shl _ (<< x y))
|
|
(var shr _ (>> x y))
|
|
(var wide _ (>> c 0))
|
|
(printf "shifts: %d %d %d\n" shl shr wide)
|
|
|
|
;; ...and an increment is the operand, unpromoted
|
|
(var inc _ (++ x))
|
|
(printf "increment: %d\n" inc)
|
|
|
|
;; an array operand decays, so this is a pointer and not an array
|
|
(var xs [int 4] #(1 2 3 4))
|
|
(var p _ (+ xs 1))
|
|
(var q _ (+ 1 xs))
|
|
(var names [(* const char) 2] #("hi" "there"))
|
|
(var np _ (+ names 1))
|
|
(printf "decayed: %d %d %s\n" (* p) (* q) (* np))
|
|
|
|
;; at equal rank C takes the unsigned operand, whichever side it is on
|
|
(var i int -1)
|
|
(var u (unsigned int) 1)
|
|
(var u1 _ (+ i u))
|
|
(var u2 _ (+ u i))
|
|
(printf "unsigned wins either way: %u %u\n" (- u1 1) (- u2 1))
|
|
|
|
(printf "toplevel: %d %g %s %d\n" n d s a)
|
|
|
|
;; a `fn' type's parameter list looks exactly like a call; with a
|
|
;; function named `f' in scope it used to be read as one
|
|
(var f (fn ((int)) int) (lambda ((v int)) int (return (+ v 1))))
|
|
(var fp (fn ((f int) (g int)) int) area)
|
|
(printf "a type is not a call: %d\n" (f 6))
|
|
|
|
;; a mask reads best in the radix it is a mask in
|
|
(var lead int 0xE5)
|
|
(printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80)
|
|
|
|
;; ...and a double in all the digits it was written with
|
|
(var sixth double (/ 1.0 6.0))
|
|
(printf "a double reaches C whole: %d\n" (== 0.16666666666666666 sixth))
|
|
|
|
;; a negative literal is one token in Sex and two in C, and C reads a
|
|
;; minus written before them as `--'
|
|
(printf "negated literals: %d %g\n" (- -1) (- -2.5))
|
|
(return 0))
|