refuse the matches and sums cc would refuse
Two else arms, a variant declared twice, and a match standing where a value is wanted all reached cc as broken C; an arm that is not a list crashed sexc. A variant called tag collided with the tag enum, now spelled shapeǂ so no variant name can reach it.
This commit is contained in:
81
semen.scm
81
semen.scm
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
(import
|
(import
|
||||||
scheme
|
scheme
|
||||||
|
(only (scheme base) make-parameter)
|
||||||
(chicken base)
|
(chicken base)
|
||||||
(chicken keyword)
|
(chicken keyword)
|
||||||
(chicken string)
|
(chicken string)
|
||||||
@@ -276,7 +277,8 @@
|
|||||||
(let* ((env (make-fn-env expanded))
|
(let* ((env (make-fn-env expanded))
|
||||||
;; the parameters are the body's outermost scope
|
;; the parameters are the body's outermost scope
|
||||||
(header (map resolve-closure-types (take expanded boundary)))
|
(header (map resolve-closure-types (take expanded boundary)))
|
||||||
(body (walk-body (drop expanded boundary) env))
|
(body (parameterize ((operand-position? #f))
|
||||||
|
(walk-body (drop expanded boundary) env)))
|
||||||
;; A function is header and body stitched together, so the
|
;; A function is header and body stitched together, so the
|
||||||
;; result is fresh structure: without this the definition
|
;; result is fresh structure: without this the definition
|
||||||
;; has no location, and nothing downstream can say where
|
;; has no location, and nothing downstream can say where
|
||||||
@@ -538,6 +540,20 @@
|
|||||||
(list walked))))
|
(list walked))))
|
||||||
forms))
|
forms))
|
||||||
|
|
||||||
|
;;; #t while the walk is inside a value -- an initializer, an argument,
|
||||||
|
;;; a returned expression -- where a statement-only form has no C
|
||||||
|
;;; spelling:
|
||||||
|
;;;
|
||||||
|
;;; (var n int (match s ...)) -> int n = switch (s.tag) { ...
|
||||||
|
;;;
|
||||||
|
;;; A condition is not tracked: `walk-parts' takes every slot of an `if'
|
||||||
|
;;; alike.
|
||||||
|
(define operand-position? (make-parameter #f))
|
||||||
|
|
||||||
|
(define (walk-operand form env)
|
||||||
|
(parameterize ((operand-position? #t))
|
||||||
|
(walk-statement form env)))
|
||||||
|
|
||||||
(define (walk-statement form env)
|
(define (walk-statement form env)
|
||||||
(cond
|
(cond
|
||||||
((not (list? form)) form)
|
((not (list? form)) form)
|
||||||
@@ -557,7 +573,12 @@
|
|||||||
(else
|
(else
|
||||||
(case (car form)
|
(case (car form)
|
||||||
((do for while if switch)
|
((do for while if switch)
|
||||||
(with-scope env (lambda () (walk-parts form env))))
|
(parameterize ((operand-position? #f))
|
||||||
|
(with-scope env (lambda () (walk-parts form env)))))
|
||||||
|
|
||||||
|
((case case/fallthrough default)
|
||||||
|
(parameterize ((operand-position? #f))
|
||||||
|
(walk-parts form env)))
|
||||||
|
|
||||||
((lambda)
|
((lambda)
|
||||||
(let ((name (aux-name! env make-lambda-name)))
|
(let ((name (aux-name! env make-lambda-name)))
|
||||||
@@ -574,8 +595,8 @@
|
|||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
form
|
form
|
||||||
`(,construct ,@(map (lambda (capture)
|
`(,construct ,@(map (lambda (capture)
|
||||||
(walk-statement (capture-argument capture)
|
(walk-operand (capture-argument capture)
|
||||||
env))
|
env))
|
||||||
(fourth form))))))))
|
(fourth form))))))))
|
||||||
|
|
||||||
((var)
|
((var)
|
||||||
@@ -595,7 +616,8 @@
|
|||||||
form
|
form
|
||||||
(append prefix
|
(append prefix
|
||||||
(if (> (length form) 3)
|
(if (> (length form) 3)
|
||||||
(walk-body (drop form 3) env)
|
(parameterize ((operand-position? #t))
|
||||||
|
(walk-body (drop form 3) env))
|
||||||
(list))))
|
(list))))
|
||||||
env))
|
env))
|
||||||
(bound (if (>= (length walked) 4)
|
(bound (if (>= (length walked) 4)
|
||||||
@@ -612,7 +634,8 @@
|
|||||||
bound))
|
bound))
|
||||||
|
|
||||||
((return)
|
((return)
|
||||||
(let ((walked (walk-parts form env)))
|
(let ((walked (parameterize ((operand-position? #t))
|
||||||
|
(walk-parts form env))))
|
||||||
(if (>= (length walked) 2)
|
(if (>= (length walked) 2)
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
walked
|
walked
|
||||||
@@ -622,7 +645,10 @@
|
|||||||
(cddr walked))))
|
(cddr walked))))
|
||||||
walked)))
|
walked)))
|
||||||
|
|
||||||
((match) (walk-match form env))
|
((match)
|
||||||
|
(when (operand-position?)
|
||||||
|
(sex-error form "match is a statement, and has no value"))
|
||||||
|
(walk-match form env))
|
||||||
|
|
||||||
((dot-access) (walk-member-path form env))
|
((dot-access) (walk-member-path form env))
|
||||||
|
|
||||||
@@ -634,14 +660,16 @@
|
|||||||
;; the receiver is walked first: `((closure ((x int))
|
;; the receiver is walked first: `((closure ((x int))
|
||||||
;; int () ...) 5)' registers `struct ƛint_int' on the
|
;; int () ...) 5)' registers `struct ƛint_int' on the
|
||||||
;; way, and the call helper's signature names it
|
;; way, and the call helper's signature names it
|
||||||
(let ((receiver (walk-statement (car form) env)))
|
(let ((receiver (walk-operand (car form) env)))
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
form
|
form
|
||||||
`(,(register-closure-call! closure form)
|
`(,(register-closure-call! closure form)
|
||||||
,receiver
|
,receiver
|
||||||
,@(map (lambda (a) (walk-statement a env))
|
,@(map (lambda (a) (walk-operand a env))
|
||||||
(cdr form)))))
|
(cdr form)))))
|
||||||
(convert-arguments (walk-parts form env) env)))))))))
|
(convert-arguments (parameterize ((operand-position? #t))
|
||||||
|
(walk-parts form env))
|
||||||
|
env)))))))))
|
||||||
|
|
||||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||||
(define (resolve-wildcard form env)
|
(define (resolve-wildcard form env)
|
||||||
@@ -797,15 +825,23 @@
|
|||||||
(define (match-arm-variant arm) (and (pair? (car arm)) (car (car arm))))
|
(define (match-arm-variant arm) (and (pair? (car arm)) (car (car arm))))
|
||||||
|
|
||||||
(define (check-match form name variants arms)
|
(define (check-match form name variants arms)
|
||||||
|
(for-each
|
||||||
|
(lambda (arm)
|
||||||
|
(unless (and (pair? arm)
|
||||||
|
(or (eq? (car arm) 'else)
|
||||||
|
(and (list? (car arm)) (= 2 (length (car arm)))
|
||||||
|
(list? (second (car arm))))))
|
||||||
|
(sex-error form "an arm is ((variant (binders ...)) body ...)" arm)))
|
||||||
|
arms)
|
||||||
(let* ((else? (lambda (arm) (eq? (car arm) 'else)))
|
(let* ((else? (lambda (arm) (eq? (car arm) 'else)))
|
||||||
(named (remove else? arms))
|
(named (remove else? arms))
|
||||||
(covered (map match-arm-variant named)))
|
(covered (map match-arm-variant named)))
|
||||||
|
(when (> (count else? arms) 1)
|
||||||
|
(sex-error form "match has more than one else"))
|
||||||
(for-each
|
(for-each
|
||||||
(lambda (arm)
|
(lambda (arm)
|
||||||
(let* ((variant (match-arm-variant arm))
|
(let* ((variant (match-arm-variant arm))
|
||||||
(fields (variant-fields variants variant)))
|
(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
|
(unless fields
|
||||||
(sex-error form "not a variant of" name variant))
|
(sex-error form "not a variant of" name variant))
|
||||||
(unless (= (length fields) (length (second (car arm))))
|
(unless (= (length fields) (length (second (car arm))))
|
||||||
@@ -880,8 +916,8 @@
|
|||||||
form
|
form
|
||||||
`(,method
|
`(,method
|
||||||
,(method-receiver method (or target written) (and target #t)
|
,(method-receiver method (or target written) (and target #t)
|
||||||
(walk-statement receiver env) form)
|
(walk-operand receiver env) form)
|
||||||
,@(map (lambda (argument) (walk-statement argument env))
|
,@(map (lambda (argument) (walk-operand argument env))
|
||||||
(cddr form))))))))
|
(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
|
||||||
@@ -918,7 +954,7 @@
|
|||||||
;;; path would have meant before any of this.
|
;;; path would have meant before any of this.
|
||||||
(define (walk-member-path form env)
|
(define (walk-member-path form env)
|
||||||
(let ((base (second form)))
|
(let ((base (second form)))
|
||||||
(let step ((expr (walk-statement base env))
|
(let step ((expr (walk-operand base env))
|
||||||
(type (expression-type base env))
|
(type (expression-type base env))
|
||||||
(fields (cddr form))
|
(fields (cddr form))
|
||||||
(told? #f))
|
(told? #f))
|
||||||
@@ -1556,6 +1592,10 @@
|
|||||||
(unless (and (symbol? name) (list? declared) (pair? declared))
|
(unless (and (symbol? name) (list? declared) (pair? declared))
|
||||||
(sex-error form "sum needs a name and a list of variants" form))
|
(sex-error form "sum needs a name and a list of variants" form))
|
||||||
(let ((variants (map (lambda (v) (parse-variant form v)) declared)))
|
(let ((variants (map (lambda (v) (parse-variant form v)) declared)))
|
||||||
|
(let* ((names (map car variants))
|
||||||
|
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names)))
|
||||||
|
(when twice
|
||||||
|
(sex-error form "variant is declared twice" twice)))
|
||||||
(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)
|
||||||
@@ -1597,7 +1637,7 @@
|
|||||||
`(fn ,payload ,fields ,name
|
`(fn ,payload ,fields ,name
|
||||||
(return ,(sum-literal name tag payload fields)))))
|
(return ,(sum-literal name tag payload fields)))))
|
||||||
|
|
||||||
;;; #(shape : .tag shapeːtagːcircle .u #(.circle #((struct shapeːcircle) : r)))
|
;;; #(shape : .tag shapeǂcircle .u #(.circle #((struct shapeːcircle) : r)))
|
||||||
(define (sum-literal name tag payload fields)
|
(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))
|
||||||
@@ -1612,10 +1652,15 @@
|
|||||||
(define (dotted name)
|
(define (dotted name)
|
||||||
(string->symbol (string-append "." (symbol->string name))))
|
(string->symbol (string-append "." (symbol->string name))))
|
||||||
|
|
||||||
(define (sum-tag name) (qualify-op name 'tag))
|
;;; shape -> enum shapeǂ, circle -> shapeǂcircle. Not shapeːtag: that
|
||||||
|
;;; is where a variant named `tag' keeps its payload struct, and an enum
|
||||||
|
;;; and a struct share C's tag namespace.
|
||||||
|
(define (sum-tag name)
|
||||||
|
(string->symbol (string-append (symbol->string name) "ǂ")))
|
||||||
|
|
||||||
(define (sum-tag-constant name variant)
|
(define (sum-tag-constant name variant)
|
||||||
(qualify-op (sum-tag name) variant))
|
(string->symbol (string-append (symbol->string (sum-tag name))
|
||||||
|
(symbol->string variant))))
|
||||||
|
|
||||||
;;; (record point ((x int) (y int))) is a struct that also names
|
;;; (record point ((x int) (y int))) is a struct that also names
|
||||||
;;; itself, so `point' alone stands where `(struct point)' had to:
|
;;; itself, so `point' alone stands where `(struct point)' had to:
|
||||||
|
|||||||
@@ -140,7 +140,11 @@
|
|||||||
;; fields, the record holding both, and a constructor each.
|
;; fields, the record holding both, and a constructor each.
|
||||||
(test-group "sum"
|
(test-group "sum"
|
||||||
(test-assert "the tag enum is named after the sum"
|
(test-assert "the tag enum is named after the sum"
|
||||||
(emits? (sum-src "") "enum shapeːtag"))
|
(emits? (sum-src "") "enum shapeǂ {"))
|
||||||
|
(test-assert "and its constants after the sum and the variant"
|
||||||
|
(emits? (sum-src "") "shapeǂcircle"))
|
||||||
|
(test-assert "a variant may be called tag"
|
||||||
|
(emits? "(sum t ((tag ((x int))) (none ())))" "struct tːtag {"))
|
||||||
(test-assert "a variant with fields gets a payload struct"
|
(test-assert "a variant with fields gets a payload struct"
|
||||||
(emits? (sum-src "") "struct shapeːcircle {"))
|
(emits? (sum-src "") "struct shapeːcircle {"))
|
||||||
(test-assert "the sum is typedef'd"
|
(test-assert "the sum is typedef'd"
|
||||||
@@ -160,7 +164,9 @@
|
|||||||
(test-assert "and needs variants"
|
(test-assert "and needs variants"
|
||||||
(reports? "(sum t)" "sum needs a name and a list of variants"))
|
(reports? "(sum t)" "sum needs a name and a list of variants"))
|
||||||
(test-assert "a variant is a name and a field list"
|
(test-assert "a variant is a name and a field list"
|
||||||
(reports? "(sum t (a))" "a variant is")))
|
(reports? "(sum t (a))" "a variant is"))
|
||||||
|
(test-assert "and is declared once"
|
||||||
|
(reports? "(sum t ((a ((x int))) (a ())))" "variant is declared twice")))
|
||||||
|
|
||||||
;; A spec is a contract that emits nothing; a realization is its
|
;; A spec is a contract that emits nothing; a realization is its
|
||||||
;; methods for one type. The refusals are what make it a contract.
|
;; methods for one type. The refusals are what make it a contract.
|
||||||
@@ -305,6 +311,26 @@
|
|||||||
(reports? (sum-src "(match s ((circle (r)) (= out r)) ((circle (q)) (= out q))"
|
(reports? (sum-src "(match s ((circle (r)) (= out r)) ((circle (q)) (= out q))"
|
||||||
" (else (= out 0.0)))")
|
" (else (= out 0.0)))")
|
||||||
"arm is written twice"))
|
"arm is written twice"))
|
||||||
|
(test-assert "so is a second else"
|
||||||
|
(reports? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0))"
|
||||||
|
" (else (= out 1.0)))")
|
||||||
|
"more than one else"))
|
||||||
|
(test-assert "so is an arm that is not one"
|
||||||
|
(reports? (sum-src "(match s foo)") "an arm is"))
|
||||||
|
(test-assert "match has no value"
|
||||||
|
(reports? (sum-src "(var n float (match s ((circle (r)) r) (else 0.0)))")
|
||||||
|
"match is a statement"))
|
||||||
|
(test-assert "not even as an argument"
|
||||||
|
(reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))")
|
||||||
|
"match is a statement"))
|
||||||
|
(test-assert "but a case body is a statement"
|
||||||
|
(emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))"
|
||||||
|
" (else (= out 0.0)))))")
|
||||||
|
"switch (s.tag)"))
|
||||||
|
(test-assert "and so is a closure's, wherever the closure stands"
|
||||||
|
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
|
||||||
|
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
|
||||||
|
"switch (t.tag)"))
|
||||||
(test-assert "and a scrutinee that is not a sum"
|
(test-assert "and a scrutinee that is not a sum"
|
||||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||||
"match wants a sum"))))
|
"match wants a sum"))))
|
||||||
|
|||||||
Reference in New Issue
Block a user