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:
110
semen.scm
110
semen.scm
@@ -554,6 +554,21 @@
|
|||||||
(parameterize ((operand-position? #t))
|
(parameterize ((operand-position? #t))
|
||||||
(walk-statement form env)))
|
(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)
|
(define (walk-statement form env)
|
||||||
(cond
|
(cond
|
||||||
((vector? form) (walk-literal form env))
|
((vector? form) (walk-literal form env))
|
||||||
@@ -612,8 +627,7 @@
|
|||||||
form
|
form
|
||||||
(append prefix
|
(append prefix
|
||||||
(if (> (length form) 3)
|
(if (> (length form) 3)
|
||||||
(parameterize ((operand-position? #t))
|
(walk-operands form (drop form 3) env)
|
||||||
(walk-body (drop form 3) env))
|
|
||||||
(list))))
|
(list))))
|
||||||
env))
|
env))
|
||||||
(bound (if (>= (length walked) 4)
|
(bound (if (>= (length walked) 4)
|
||||||
@@ -630,8 +644,7 @@
|
|||||||
bound))
|
bound))
|
||||||
|
|
||||||
((return)
|
((return)
|
||||||
(let ((walked (parameterize ((operand-position? #t))
|
(let ((walked (copy-form-source! form (walk-operands form form env))))
|
||||||
(walk-parts form env))))
|
|
||||||
(if (>= (length walked) 2)
|
(if (>= (length walked) 2)
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
walked
|
walked
|
||||||
@@ -649,23 +662,44 @@
|
|||||||
((dot-access) (walk-member-path form env))
|
((dot-access) (walk-member-path form env))
|
||||||
|
|
||||||
(else
|
(else
|
||||||
(if (method-head? (car form))
|
(cond
|
||||||
(walk-method-call form env)
|
((method-head? (car form)) (walk-method-call form env))
|
||||||
(let ((closure (receiver-closure-type (car form) env)))
|
((get-constructor (car form))
|
||||||
(if closure
|
=> (lambda (constructor) (walk-construction form constructor env)))
|
||||||
;; the receiver is walked first: `((closure ((x int))
|
((receiver-closure-type (car form) env)
|
||||||
;; int () ...) 5)' registers `struct ƛint_int' on the
|
=> (lambda (closure)
|
||||||
;; way, and the call helper's signature names it
|
;; the receiver is walked first: `((closure ((x int))
|
||||||
(let ((receiver (walk-operand (car form) env)))
|
;; int () ...) 5)' registers `struct ƛint_int' on the
|
||||||
(copy-form-source!
|
;; way, and the call helper's signature names it
|
||||||
form
|
(let ((receiver (walk-operand (car form) env)))
|
||||||
`(,(register-closure-call! closure form)
|
(copy-form-source!
|
||||||
,receiver
|
form
|
||||||
,@(map (lambda (a) (walk-operand a env))
|
`(,(register-closure-call! closure form)
|
||||||
(cdr form)))))
|
,receiver
|
||||||
(convert-arguments (parameterize ((operand-position? #t))
|
,@(walk-operands form (cdr form) env))))))
|
||||||
(walk-parts form env))
|
(else
|
||||||
env)))))))))
|
(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
|
;;; The forms a macro call stands for, with `(type-of x)' answered from
|
||||||
;;; ENV
|
;;; ENV
|
||||||
@@ -930,8 +964,7 @@
|
|||||||
`(,method
|
`(,method
|
||||||
,(method-receiver method (or target written) (and target #t)
|
,(method-receiver method (or target written) (and target #t)
|
||||||
(walk-operand receiver env) form)
|
(walk-operand receiver env) form)
|
||||||
,@(map (lambda (argument) (walk-operand argument env))
|
,@(walk-operands form (cddr form) env)))))))
|
||||||
(cddr form))))))))
|
|
||||||
|
|
||||||
;;; A method taking (* Self) wants an address and one taking Self wants
|
;;; 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
|
;;; 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))
|
(and (pair? expansion) (null? (cdr expansion))
|
||||||
(expression-type (car expansion) env))))
|
(expression-type (car expansion) env))))
|
||||||
((not (and (list? expr) (pair? expr))) #f)
|
((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
|
;; a call of no arguments is still a call
|
||||||
((< (length expr) 2)
|
((< (length expr) 2)
|
||||||
(and (symbol? (car expr)) (get-return-type (car expr))))
|
(and (symbol? (car expr)) (get-return-type (car expr))))
|
||||||
@@ -1620,6 +1655,8 @@
|
|||||||
(when (get-sum name)
|
(when (get-sum name)
|
||||||
(sex-error form "sum is already defined" name))
|
(sex-error form "sum is already defined" name))
|
||||||
(add-sum! name variants)
|
(add-sum! name variants)
|
||||||
|
(for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v)))
|
||||||
|
variants)
|
||||||
(fold match-sex-form acc
|
(fold match-sex-form acc
|
||||||
(map (lambda (f) (copy-form-source! form f))
|
(map (lambda (f) (copy-form-source! form f))
|
||||||
(sum-forms name variants))))))
|
(sum-forms name variants))))))
|
||||||
@@ -1644,31 +1681,19 @@
|
|||||||
`((u (union ,(map (lambda (v)
|
`((u (union ,(map (lambda (v)
|
||||||
`(,(car v)
|
`(,(car v)
|
||||||
(struct ,(qualify-op name (car v)))))
|
(struct ,(qualify-op name (car v)))))
|
||||||
carried)))))))
|
carried))))))))))
|
||||||
,@(map (lambda (v) (sum-constructor name v)) variants))))
|
|
||||||
|
|
||||||
;;; (shape/circle 1.0) builds the value in one compound literal, so a
|
;;; #(shape : .tag shapeǂcircle .u #(.circle #((struct shapeːcircle) : 1.0)))
|
||||||
;;; constructor is an expression and not three statements
|
(define (sum-literal name tag values)
|
||||||
;;; 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)
|
|
||||||
(list->vector
|
(list->vector
|
||||||
(append (list name ': (dotted 'tag) (sum-tag-constant name tag))
|
(append (list name ': (dotted 'tag) (sum-tag-constant name tag))
|
||||||
(if (null? fields)
|
(if (null? values)
|
||||||
(list)
|
(list)
|
||||||
(list (dotted 'u)
|
(list (dotted 'u)
|
||||||
(vector (dotted tag)
|
(vector (dotted tag)
|
||||||
(list->vector
|
(list->vector
|
||||||
(append (list (list 'struct payload) ':)
|
(append (list (list 'struct (qualify-op name tag)) ':)
|
||||||
(map car fields)))))))))
|
values))))))))
|
||||||
|
|
||||||
(define (dotted name)
|
(define (dotted name)
|
||||||
(string->symbol (string-append "." (symbol->string name))))
|
(string->symbol (string-append "." (symbol->string name))))
|
||||||
@@ -1745,8 +1770,7 @@
|
|||||||
core
|
core
|
||||||
(if (> (length core) 3)
|
(if (> (length core) 3)
|
||||||
(append (take core 3)
|
(append (take core 3)
|
||||||
(parameterize ((operand-position? #t))
|
(walk-operands core (drop core 3) env))
|
||||||
(walk-body (drop core 3) env)))
|
|
||||||
core))
|
core))
|
||||||
env)))
|
env)))
|
||||||
(form (if qualifier
|
(form (if qualifier
|
||||||
|
|||||||
@@ -196,8 +196,32 @@
|
|||||||
(test-assert "the typedef comes before the payloads, for a recursive sum"
|
(test-assert "the typedef comes before the payloads, for a recursive sum"
|
||||||
(emits? "(sum expr ((lit ((v float))) (neg ((e (* const expr))))))"
|
(emits? "(sum expr ((lit ((v float))) (neg ((e (* const expr))))))"
|
||||||
"typedef struct expr expr;"))
|
"typedef struct expr expr;"))
|
||||||
(test-assert "constructors are static even when the sum is pub"
|
(test-assert "a constructor is written in place"
|
||||||
(emits? "(pub sum t ((a ((x int)))))" "static t tːa"))
|
(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"
|
(test-assert "a tag-only sum has no union at all"
|
||||||
(not (emits? "(sum light ((red ()) (green ())))" "union")))
|
(not (emits? "(sum light ((red ()) (green ())))" "union")))
|
||||||
(test-assert "a sum is declared once"
|
(test-assert "a sum is declared once"
|
||||||
|
|||||||
@@ -80,8 +80,8 @@
|
|||||||
(free self)))
|
(free self)))
|
||||||
|
|
||||||
;;; A pub sum is pasted whole, so the importer builds its own copy of
|
;;; 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
|
;;; the declarations; a constructor is written in place where it is
|
||||||
;;; static: two units defining colourːrgb would not link.
|
;;; called, so there is no function for two units to both define.
|
||||||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||||||
|
|
||||||
;;; A spec and a realization over the boundary. The written method
|
;;; A spec and a realization over the boundary. The written method
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
add-sum!
|
add-sum!
|
||||||
get-sum
|
get-sum
|
||||||
|
add-constructor!
|
||||||
|
get-constructor
|
||||||
variant-fields
|
variant-fields
|
||||||
reset-declarations!
|
reset-declarations!
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
add-sum!
|
add-sum!
|
||||||
get-sum
|
get-sum
|
||||||
|
add-constructor!
|
||||||
|
get-constructor
|
||||||
variant-fields
|
variant-fields
|
||||||
reset-declarations!
|
reset-declarations!
|
||||||
|
|
||||||
|
|||||||
10
types.scm
10
types.scm
@@ -105,6 +105,15 @@
|
|||||||
(define (get-sum name)
|
(define (get-sum name)
|
||||||
(hash-table-ref/default +sum-db+ (type-key name) #f))
|
(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)
|
(define (variant-fields variants name)
|
||||||
(let ((entry (assq name variants)))
|
(let ((entry (assq name variants)))
|
||||||
(and entry (cdr entry))))
|
(and entry (cdr entry))))
|
||||||
@@ -141,6 +150,7 @@
|
|||||||
;;; that compiles two units has to start the second one clean.
|
;;; that compiles two units has to start the second one clean.
|
||||||
(define (reset-declarations!)
|
(define (reset-declarations!)
|
||||||
(set! +sum-db+ (make-hash-table))
|
(set! +sum-db+ (make-hash-table))
|
||||||
|
(set! +constructor-db+ (make-hash-table))
|
||||||
(set! +ops-db+ (make-hash-table))
|
(set! +ops-db+ (make-hash-table))
|
||||||
(set! +spec-db+ (make-hash-table))
|
(set! +spec-db+ (make-hash-table))
|
||||||
(set! +realize-db+ (make-hash-table)))
|
(set! +realize-db+ (make-hash-table)))
|
||||||
|
|||||||
Reference in New Issue
Block a user