implement sum and match
A sum is the tag enum, a payload struct per variant that carries fields, the record holding both, and a constructor each. match is a switch over the tag whose arms open a block and bind the payload as locals, so the shape of the generated C is the shape a C programmer would have written by hand. What it is for is the refusal: a match that leaves a variant out is an error naming the ones missing, and so are an arm the sum does not have, a wrong number of binders, and the same arm written twice. C cannot say any of that, which is why every switch over a tag in the wild ends in a defensive default nobody can tell from a real case. Only the sum is typedef'd. A payload struct would put shapeːcircle in the ordinary namespace, where the constructor of that name already lives; as a bare tag it collides with nothing. The sum's own typedef is emitted ahead of the payload structs, since a variant may hold a pointer back to the sum, and constructors are static even for a pub sum, which is pasted into the importer whole. The scrutinee is read once per binding, so one that is not a path of names is held in a local first -- otherwise (match (f) ...) would call f once for the tag and again for every field it binds.
This commit is contained in:
170
semen.scm
170
semen.scm
@@ -85,6 +85,8 @@
|
||||
(('ops . _) (process-ops sex-form acc))
|
||||
(('pub 'ops . _)
|
||||
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
|
||||
((or ('sum . _)
|
||||
('pub 'sum . _)) (process-sum sex-form acc))
|
||||
((or ('record . _)
|
||||
('pub 'record . _)) (process-record sex-form acc))
|
||||
((or ('struct . _)
|
||||
@@ -466,6 +468,8 @@
|
||||
(cddr walked))))
|
||||
walked)))
|
||||
|
||||
((match) (walk-match form env))
|
||||
|
||||
((dot-access ->) (walk-member-access form env))
|
||||
|
||||
(else
|
||||
@@ -594,6 +598,94 @@
|
||||
(define (walk-parts form env)
|
||||
(copy-form-source! form (walk-body form env)))
|
||||
|
||||
;;; (match s ((circle (r)) ...) ((empty ()) ...)) is a switch over the
|
||||
;;; tag, each arm a block binding that variant's fields as locals. `_'
|
||||
;;; drops one. An arm the sum does not have, a wrong number of
|
||||
;;; binders, or a variant left out with no `else' are all errors here:
|
||||
;;; a missing case is the thing this form exists to catch.
|
||||
(define (walk-match form env)
|
||||
(let* ((scrutinee (and (pair? (cdr form)) (second form)))
|
||||
;; a comment between arms goes the way the writer already
|
||||
;; drops one between switch clauses
|
||||
(arms (remove comment-form? (cddr form)))
|
||||
(type (expression-type scrutinee env))
|
||||
(name (ops-key type))
|
||||
(variants (and name (get-sum name))))
|
||||
(unless variants
|
||||
(sex-error form "match wants a sum, and this is not one"
|
||||
(or type 'unknown)))
|
||||
(check-match form name variants arms)
|
||||
(if (simple-path? scrutinee)
|
||||
(walk-statement (match-switch name scrutinee variants arms) env)
|
||||
;; the scrutinee is read once per binding, so anything that
|
||||
;; could be read twice is read into a local first
|
||||
(let ((held (aux-name! env make-match-name)))
|
||||
(walk-statement
|
||||
`(do (var ,held ,type ,scrutinee)
|
||||
,(match-switch name held variants arms))
|
||||
env)))))
|
||||
|
||||
(define (make-match-name fn-name counter)
|
||||
(string->symbol (string-append "ƛm" (number->string counter))))
|
||||
|
||||
;;; A path of names reads the same however often it is written; a call
|
||||
;;; in there does not
|
||||
(define (simple-path? expr)
|
||||
(cond ((symbol? expr) #t)
|
||||
((not (pair? expr)) #f)
|
||||
(else (case (car expr)
|
||||
((dot-access ->) (simple-path? (second expr)))
|
||||
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
|
||||
(else #f)))))
|
||||
|
||||
(define (match-arm-variant arm) (and (pair? (car arm)) (car (car arm))))
|
||||
|
||||
(define (check-match form name variants arms)
|
||||
(let* ((else? (lambda (arm) (eq? (car arm) 'else)))
|
||||
(named (remove else? arms))
|
||||
(covered (map match-arm-variant named)))
|
||||
(for-each
|
||||
(lambda (arm)
|
||||
(let* ((variant (match-arm-variant arm))
|
||||
(fields (variant-fields variants variant)))
|
||||
(unless (and (list? (car arm)) (= 2 (length (car arm))))
|
||||
(sex-error form "an arm is ((variant (binders ...)) body ...)" arm))
|
||||
(unless fields
|
||||
(sex-error form "not a variant of" name variant))
|
||||
(unless (= (length fields) (length (second (car arm))))
|
||||
(sex-error form "variant takes this many fields" variant
|
||||
(length fields)))))
|
||||
named)
|
||||
(let ((twice (find (lambda (v) (> (count (cut eq? <> v) covered) 1))
|
||||
covered)))
|
||||
(when twice
|
||||
(sex-error form "arm is written twice" twice)))
|
||||
(let ((missing (lset-difference eq? (map car variants) covered)))
|
||||
(when (and (pair? missing) (not (any else? arms)))
|
||||
(apply sex-error form "match leaves a variant out:" missing)))))
|
||||
|
||||
(define (match-switch name scrutinee variants arms)
|
||||
`(switch (dot-access ,scrutinee tag)
|
||||
,@(map (lambda (arm)
|
||||
(if (eq? (car arm) 'else)
|
||||
`(default (do ,@(cdr arm)))
|
||||
(let* ((variant (match-arm-variant arm))
|
||||
(binders (second (car arm)))
|
||||
(fields (variant-fields variants variant)))
|
||||
`(case ,(sum-tag-constant name variant)
|
||||
(do ,@(match-bindings scrutinee variant binders fields)
|
||||
,@(cdr arm))))))
|
||||
arms)))
|
||||
|
||||
(define (match-bindings scrutinee variant binders fields)
|
||||
(filter-map
|
||||
(lambda (binder field)
|
||||
(and (not (eq? binder '_))
|
||||
`(var ,binder ,(second field)
|
||||
(dot-access (dot-access (dot-access ,scrutinee u) ,variant)
|
||||
,(first field)))))
|
||||
binders fields))
|
||||
|
||||
;;; (. v length) is member access while `length' is a field of v, and a
|
||||
;;; call to vec2ːlength once it is a method:
|
||||
;;;
|
||||
@@ -1220,6 +1312,84 @@
|
||||
(register-aggregate! form)
|
||||
(with-docstring doc form acc))))
|
||||
|
||||
;;; (sum shape ((circle ((r float))) (rect ((w float) (h float)))))
|
||||
;;; is the tag enum, a payload struct per variant that has fields, the
|
||||
;;; record holding both, and one constructor per variant:
|
||||
;;;
|
||||
;;; (var s shape (shape/circle 1.0))
|
||||
;;;
|
||||
;;; The payload structs are plain structs rather than records on
|
||||
;;; purpose: a record would typedef shapeːcircle into the ordinary
|
||||
;;; namespace, where the constructor of that name already lives. As a
|
||||
;;; tag it shares nothing.
|
||||
(define (process-sum form acc)
|
||||
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
|
||||
(name (and (pair? (cdr core)) (cadr core)))
|
||||
(declared (and (pair? (cddr core)) (caddr core))))
|
||||
(unless (and (symbol? name) (list? declared) (pair? declared))
|
||||
(sex-error form "sum needs a name and a list of variants" form))
|
||||
(let ((variants (map (lambda (v) (parse-variant form v)) declared)))
|
||||
(when (get-sum name)
|
||||
(sex-error form "sum is already defined" name))
|
||||
(add-sum! name variants)
|
||||
(fold match-sex-form acc
|
||||
(map (lambda (f) (copy-form-source! form f))
|
||||
(sum-forms name variants))))))
|
||||
|
||||
(define (parse-variant form v)
|
||||
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))
|
||||
(sex-error form "a variant is (name (fields ...))" v))
|
||||
(cons (car v) (normalize-fields (cadr v))))
|
||||
|
||||
(define (sum-forms name variants)
|
||||
(let ((carried (filter (lambda (v) (pair? (cdr v))) variants)))
|
||||
`(;; forward, so a variant may hold a pointer back to the sum
|
||||
(typedef ,name (struct ,name))
|
||||
(enum ,(sum-tag name)
|
||||
,(map (lambda (v) (sum-tag-constant name (car v))) variants))
|
||||
,@(map (lambda (v) `(struct ,(qualify-op name (car v)) ,(cdr v)))
|
||||
carried)
|
||||
(struct ,name
|
||||
((tag (enum ,(sum-tag name)))
|
||||
,@(if (null? carried)
|
||||
(list)
|
||||
`((u (union ,(map (lambda (v)
|
||||
`(,(car v)
|
||||
(struct ,(qualify-op name (car v)))))
|
||||
carried)))))))
|
||||
,@(map (lambda (v) (sum-constructor name v)) variants))))
|
||||
|
||||
;;; (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ːtagːcircle .u #(.circle #((struct shapeːcircle) : r)))
|
||||
(define (sum-literal name tag payload fields)
|
||||
(list->vector
|
||||
(append (list name ': (dotted 'tag) (sum-tag-constant name tag))
|
||||
(if (null? fields)
|
||||
(list)
|
||||
(list (dotted 'u)
|
||||
(vector (dotted tag)
|
||||
(list->vector
|
||||
(append (list (list 'struct payload) ':)
|
||||
(map car fields)))))))))
|
||||
|
||||
(define (dotted name)
|
||||
(string->symbol (string-append "." (symbol->string name))))
|
||||
|
||||
(define (sum-tag name) (qualify-op name 'tag))
|
||||
|
||||
(define (sum-tag-constant name variant)
|
||||
(qualify-op (sum-tag name) variant))
|
||||
|
||||
;;; (record point ((x int) (y int))) is a struct that also names
|
||||
;;; itself, so `point' alone stands where `(struct point)' had to:
|
||||
;;; (var p point ...), (* const point), (sizeof point).
|
||||
|
||||
Reference in New Issue
Block a user