forked from alex-eg/sex
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
|
||||
scheme
|
||||
(only (scheme base) make-parameter)
|
||||
(chicken base)
|
||||
(chicken keyword)
|
||||
(chicken string)
|
||||
@@ -276,7 +277,8 @@
|
||||
(let* ((env (make-fn-env expanded))
|
||||
;; the parameters are the body's outermost scope
|
||||
(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
|
||||
;; result is fresh structure: without this the definition
|
||||
;; has no location, and nothing downstream can say where
|
||||
@@ -538,6 +540,20 @@
|
||||
(list walked))))
|
||||
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)
|
||||
(cond
|
||||
((not (list? form)) form)
|
||||
@@ -557,7 +573,12 @@
|
||||
(else
|
||||
(case (car form)
|
||||
((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)
|
||||
(let ((name (aux-name! env make-lambda-name)))
|
||||
@@ -574,8 +595,8 @@
|
||||
(copy-form-source!
|
||||
form
|
||||
`(,construct ,@(map (lambda (capture)
|
||||
(walk-statement (capture-argument capture)
|
||||
env))
|
||||
(walk-operand (capture-argument capture)
|
||||
env))
|
||||
(fourth form))))))))
|
||||
|
||||
((var)
|
||||
@@ -595,7 +616,8 @@
|
||||
form
|
||||
(append prefix
|
||||
(if (> (length form) 3)
|
||||
(walk-body (drop form 3) env)
|
||||
(parameterize ((operand-position? #t))
|
||||
(walk-body (drop form 3) env))
|
||||
(list))))
|
||||
env))
|
||||
(bound (if (>= (length walked) 4)
|
||||
@@ -612,7 +634,8 @@
|
||||
bound))
|
||||
|
||||
((return)
|
||||
(let ((walked (walk-parts form env)))
|
||||
(let ((walked (parameterize ((operand-position? #t))
|
||||
(walk-parts form env))))
|
||||
(if (>= (length walked) 2)
|
||||
(copy-form-source!
|
||||
walked
|
||||
@@ -622,7 +645,10 @@
|
||||
(cddr 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))
|
||||
|
||||
@@ -634,14 +660,16 @@
|
||||
;; 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-statement (car form) env)))
|
||||
(let ((receiver (walk-operand (car form) env)))
|
||||
(copy-form-source!
|
||||
form
|
||||
`(,(register-closure-call! closure form)
|
||||
,receiver
|
||||
,@(map (lambda (a) (walk-statement a env))
|
||||
,@(map (lambda (a) (walk-operand a env))
|
||||
(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))'.
|
||||
(define (resolve-wildcard form env)
|
||||
@@ -797,15 +825,23 @@
|
||||
(define (match-arm-variant arm) (and (pair? (car arm)) (car (car arm))))
|
||||
|
||||
(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)))
|
||||
(named (remove else? arms))
|
||||
(covered (map match-arm-variant named)))
|
||||
(when (> (count else? arms) 1)
|
||||
(sex-error form "match has more than one else"))
|
||||
(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))))
|
||||
@@ -880,8 +916,8 @@
|
||||
form
|
||||
`(,method
|
||||
,(method-receiver method (or target written) (and target #t)
|
||||
(walk-statement receiver env) form)
|
||||
,@(map (lambda (argument) (walk-statement argument env))
|
||||
(walk-operand receiver env) form)
|
||||
,@(map (lambda (argument) (walk-operand argument env))
|
||||
(cddr form))))))))
|
||||
|
||||
;;; A method taking (* Self) wants an address and one taking Self wants
|
||||
@@ -918,7 +954,7 @@
|
||||
;;; path would have meant before any of this.
|
||||
(define (walk-member-path form env)
|
||||
(let ((base (second form)))
|
||||
(let step ((expr (walk-statement base env))
|
||||
(let step ((expr (walk-operand base env))
|
||||
(type (expression-type base env))
|
||||
(fields (cddr form))
|
||||
(told? #f))
|
||||
@@ -1556,6 +1592,10 @@
|
||||
(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)))
|
||||
(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)
|
||||
(sex-error form "sum is already defined" name))
|
||||
(add-sum! name variants)
|
||||
@@ -1597,7 +1637,7 @@
|
||||
`(fn ,payload ,fields ,name
|
||||
(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)
|
||||
(list->vector
|
||||
(append (list name ': (dotted 'tag) (sum-tag-constant name tag))
|
||||
@@ -1612,10 +1652,15 @@
|
||||
(define (dotted 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)
|
||||
(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
|
||||
;;; itself, so `point' alone stands where `(struct point)' had to:
|
||||
|
||||
@@ -140,7 +140,11 @@
|
||||
;; fields, the record holding both, and a constructor each.
|
||||
(test-group "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"
|
||||
(emits? (sum-src "") "struct shapeːcircle {"))
|
||||
(test-assert "the sum is typedef'd"
|
||||
@@ -160,7 +164,9 @@
|
||||
(test-assert "and needs variants"
|
||||
(reports? "(sum t)" "sum needs a name and a list of variants"))
|
||||
(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
|
||||
;; 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))"
|
||||
" (else (= out 0.0)))")
|
||||
"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"
|
||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||
"match wants a sum"))))
|
||||
|
||||
Reference in New Issue
Block a user