1
0
forked from alex-eg/sex

write a sum's constructors in place

A constructor was a static function per variant. One taking an array
field cannot exist, its parameter being a pointer, and building some
variants in place and others by call would make two kinds of
constructor. In place, every one is a constant where one is wanted, an
lvalue a method can take the address of, and nothing a pub sum has to
paste into its importer.

What the function did for free is done here instead: arity is checked,
since a short compound literal zero-fills quietly, and an argument is
converted against its field's closure type. The price is that a
constructor is no longer a function value; a lambda makes one.
This commit is contained in:
2026-10-14 17:11:38 +03:00
parent e91b44dbe1
commit 427177d50f
6 changed files with 109 additions and 47 deletions

110
semen.scm
View File

@@ -554,6 +554,21 @@
(parameterize ((operand-position? #t))
(walk-statement form env)))
;;; FORMS are values written inside PARENT, which is what a diagnostic
;;; about one of them points at -- a bare name has no location of its
;;; own:
;;;
;;; (var f (fn ((r float)) shape) shape/circle)
(define (walk-operands parent forms env)
(for-each (lambda (form)
(when (and (symbol? form) (get-constructor form))
(sex-error parent
"a constructor is not a function; call it from a lambda"
form)))
forms)
(parameterize ((operand-position? #t))
(walk-body forms env)))
(define (walk-statement form env)
(cond
((vector? form) (walk-literal form env))
@@ -612,8 +627,7 @@
form
(append prefix
(if (> (length form) 3)
(parameterize ((operand-position? #t))
(walk-body (drop form 3) env))
(walk-operands form (drop form 3) env)
(list))))
env))
(bound (if (>= (length walked) 4)
@@ -630,8 +644,7 @@
bound))
((return)
(let ((walked (parameterize ((operand-position? #t))
(walk-parts form env))))
(let ((walked (copy-form-source! form (walk-operands form form env))))
(if (>= (length walked) 2)
(copy-form-source!
walked
@@ -649,23 +662,44 @@
((dot-access) (walk-member-path form env))
(else
(if (method-head? (car form))
(walk-method-call form env)
(let ((closure (receiver-closure-type (car form) env)))
(if closure
;; the receiver is walked first: `((closure ((x int))
;; int () ...) 5)' registers `struct ƛint_int' on the
;; way, and the call helper's signature names it
(let ((receiver (walk-operand (car form) env)))
(copy-form-source!
form
`(,(register-closure-call! closure form)
,receiver
,@(map (lambda (a) (walk-operand a env))
(cdr form)))))
(convert-arguments (parameterize ((operand-position? #t))
(walk-parts form env))
env)))))))))
(cond
((method-head? (car form)) (walk-method-call form env))
((get-constructor (car form))
=> (lambda (constructor) (walk-construction form constructor env)))
((receiver-closure-type (car form) env)
=> (lambda (closure)
;; the receiver is walked first: `((closure ((x int))
;; int () ...) 5)' registers `struct ƛint_int' on the
;; way, and the call helper's signature names it
(let ((receiver (walk-operand (car form) env)))
(copy-form-source!
form
`(,(register-closure-call! closure form)
,receiver
,@(walk-operands form (cdr form) env))))))
(else
(convert-arguments (copy-form-source! form (walk-operands form form env))
env))))))))
;;; (shape/circle 1.0) is the value itself, written where it is used:
;;;
;;; (shape){.tag = shapeǂcircle, .u = {.circle = (struct shapeːcircle){1.0}}}
;;;
;;; so it is a constant where one is wanted, an lvalue a method can take
;;; the address of, and a pub sum pastes nothing into the importer but
;;; its declarations. A lambda argument is converted against its field
;;; the way a call converts one against its parameter.
(define (walk-construction form constructor env)
(let* ((name (car constructor))
(tag (cdr constructor))
(fields (variant-fields (get-sum name) tag))
(args (walk-operands form (cdr form) env)))
(unless (= (length fields) (length args))
(sex-error form "variant takes this many fields" tag (length fields)))
(sum-literal name tag
(map (lambda (field arg)
(convert-to-closure (second field) arg env form))
fields args))))
;;; The forms a macro call stands for, with `(type-of x)' answered from
;;; ENV
@@ -930,8 +964,7 @@
`(,method
,(method-receiver method (or target written) (and target #t)
(walk-operand receiver env) form)
,@(map (lambda (argument) (walk-operand argument env))
(cddr form))))))))
,@(walk-operands form (cddr form) env)))))))
;;; A method taking (* Self) wants an address and one taking Self wants
;;; a value; the receiver is one or the other, so one of the two gets
@@ -1051,6 +1084,8 @@
(and (pair? expansion) (null? (cdr expansion))
(expression-type (car expansion) env))))
((not (and (list? expr) (pair? expr))) #f)
;; (shape/circle 1.0) -> shape
((and (symbol? (car expr)) (get-constructor (car expr))) => car)
;; a call of no arguments is still a call
((< (length expr) 2)
(and (symbol? (car expr)) (get-return-type (car expr))))
@@ -1620,6 +1655,8 @@
(when (get-sum name)
(sex-error form "sum is already defined" name))
(add-sum! name variants)
(for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v)))
variants)
(fold match-sex-form acc
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants))))))
@@ -1644,31 +1681,19 @@
`((u (union ,(map (lambda (v)
`(,(car v)
(struct ,(qualify-op name (car v)))))
carried)))))))
,@(map (lambda (v) (sum-constructor name v)) variants))))
carried))))))))))
;;; (shape/circle 1.0) builds the value in one compound literal, so a
;;; constructor is an expression and not three statements
;;; Static even when the sum is pub: a pub sum is pasted whole into
;;; the importer, and two units defining shapeːcircle would not link.
(define (sum-constructor name variant)
(let* ((tag (car variant))
(fields (cdr variant))
(payload (qualify-op name tag)))
`(fn ,payload ,fields ,name
(return ,(sum-literal name tag payload fields)))))
;;; #(shape : .tag shapeǂcircle .u #(.circle #((struct shapeːcircle) : r)))
(define (sum-literal name tag payload fields)
;;; #(shape : .tag shapeǂcircle .u #(.circle #((struct shapeːcircle) : 1.0)))
(define (sum-literal name tag values)
(list->vector
(append (list name ': (dotted 'tag) (sum-tag-constant name tag))
(if (null? fields)
(if (null? values)
(list)
(list (dotted 'u)
(vector (dotted tag)
(list->vector
(append (list (list 'struct payload) ':)
(map car fields)))))))))
(append (list (list 'struct (qualify-op name tag)) ':)
values))))))))
(define (dotted name)
(string->symbol (string-append "." (symbol->string name))))
@@ -1745,8 +1770,7 @@
core
(if (> (length core) 3)
(append (take core 3)
(parameterize ((operand-position? #t))
(walk-body (drop core 3) env)))
(walk-operands core (drop core 3) env))
core))
env)))
(form (if qualifier

View File

@@ -196,8 +196,32 @@
(test-assert "the typedef comes before the payloads, for a recursive sum"
(emits? "(sum expr ((lit ((v float))) (neg ((e (* const expr))))))"
"typedef struct expr expr;"))
(test-assert "constructors are static even when the sum is pub"
(emits? "(pub sum t ((a ((x int)))))" "static t tːa"))
(test-assert "a constructor is written in place"
(emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))"
"(struct tːa){1}"))
(test-assert "and is no function"
(not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))"
"t tːa (")))
(test-assert "so it is a constant at file scope"
(emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" "static t g = (t){"))
(test-assert "and an lvalue a method can take the address of"
(emits? (string-append
"(sum t ((a ((x int)))))"
"(ops t (fn get ((self (* const t))) int (return 1)))"
"(fn f () int (return (.get (t/a 1))))")
"tːget(&(t){"))
(test-assert "a constructor takes as many arguments as its variant has fields"
(reports? "(sum t ((a ((x int)))))(fn f () t (return (t/a 1 2)))"
"variant takes this many fields"))
(test-assert "and is not a function value"
(reports? "(sum t ((a ((x int)))))(fn f () void (var g (fn ((x int)) t) t/a))"
"a constructor is not a function"))
(test-assert "a function argument becomes the closure its field holds"
(emits? (string-append
"(fn g ((n int)) int (return n))"
"(sum t ((a ((k (closure ((int)) int))))))"
"(fn f () t (return (t/a g)))")
"ƛint_int_fromfn(g)"))
(test-assert "a tag-only sum has no union at all"
(not (emits? "(sum light ((red ()) (green ())))" "union")))
(test-assert "a sum is declared once"

View File

@@ -80,8 +80,8 @@
(free self)))
;;; A pub sum is pasted whole, so the importer builds its own copy of
;;; the declarations -- and of the constructors, which is why those are
;;; static: two units defining colourːrgb would not link.
;;; the declarations; a constructor is written in place where it is
;;; called, so there is no function for two units to both define.
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
;;; A spec and a realization over the boundary. The written method

View File

@@ -11,6 +11,8 @@
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!

View File

@@ -11,6 +11,8 @@
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!

View File

@@ -105,6 +105,15 @@
(define (get-sum name)
(hash-table-ref/default +sum-db+ (type-key name) #f))
;;; shape/circle -> (shape . circle), for each variant of each sum
(define +constructor-db+ (make-hash-table))
(define (add-constructor! constructor sum variant)
(hash-table-set! +constructor-db+ constructor (cons sum variant)))
(define (get-constructor name)
(hash-table-ref/default +constructor-db+ name #f))
(define (variant-fields variants name)
(let ((entry (assq name variants)))
(and entry (cdr entry))))
@@ -141,6 +150,7 @@
;;; that compiles two units has to start the second one clean.
(define (reset-declarations!)
(set! +sum-db+ (make-hash-table))
(set! +constructor-db+ (make-hash-table))
(set! +ops-db+ (make-hash-table))
(set! +spec-db+ (make-hash-table))
(set! +realize-db+ (make-hash-table)))