diff --git a/semen.scm b/semen.scm index adb4dd5..f961d21 100644 --- a/semen.scm +++ b/semen.scm @@ -433,9 +433,17 @@ (define (make-fn-env fn-form) (let ((env (make-hash-table)) (parameters (make-hash-table))) + ;; `(p (* struct point))' writes the type in one piece and + ;; `(p * struct point)' spreads it over the rest, so the type is + ;; the whole tail whenever there is more of it than one element (for-each (lambda (param) - (when (and (pair? param) (pair? (cdr param))) - (hash-table-set! parameters (first param) (second param)))) + (when (and (pair? param) (pair? (cdr param)) + (named-arg? param)) + (hash-table-set! parameters + (first param) + (if (null? (cddr param)) + (second param) + (cdr param))))) (sex-fn-arglist fn-form)) (set! (hash-table-ref env :fn-name) (sex-fn-name fn-form)) (set! (hash-table-ref env :lambda-counter) 0) @@ -603,6 +611,8 @@ ((match) (walk-match form env)) + ((dot-access) (walk-member-path form env)) + (else (if (method-head? (car form)) (walk-method-call form env) @@ -876,6 +886,32 @@ method)) (else `(& ,receiver))))) +;;; `(. p x)' is `p->x' when p is a pointer and `p.x' when it is not: +;;; the receiver's type says which, so a path reads the same however +;;; what it starts from is held. One step at a time, since only the +;;; type in hand can answer for the next field: +;;; +;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x +;;; +;;; A type we cannot work out is read as a value, which is what the +;;; 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)) + (type (expression-type base env)) + (fields (cddr form)) + (told? #f)) + (if (null? fields) + (copy-form-source! form expr) + (let ((target (pointer-target type))) + (unless (or type told?) + (sex-warning form "cannot type this; `.' reads it as a value" + base)) + (step (list (if target '-> 'dot-access) expr (car fields)) + (field-type (or target type) (car fields)) + (cdr fields) + (or told? (not type)))))))) + (define (lvalue? expr) (cond ((symbol? expr) #t) ((vector? expr) #t) ; a compound literal is one @@ -1081,22 +1117,36 @@ (eq? '* (car type)) (unwrap-type (cdr type)))) +;;; A path looks through a pointer wherever it meets one, the way the +;;; access itself does: `l.a->x' and `l.b.x' are both (. l a x) and +;;; both answer with x's type (define (member-path-type type fields) (if (null? fields) type - (member-path-type (field-type type (car fields)) (cdr fields)))) + (member-path-type (field-type (or (pointer-target type) type) + (car fields)) + (cdr fields)))) (define (field-type type field) - (let ((name (cond ((symbol? type) type) - ((and (list? type) - (= 2 (length type)) - (memq (car type) '(struct union))) - (second type)) - (else #f)))) - (and name - (let* ((fields (get-fields name)) - (entry (and fields (assq field fields)))) - (and entry (second entry)))))) + (let* ((fields (type-fields type)) + (entry (and fields (assq field fields)))) + (and entry (second entry)))) + +;;; `(struct point)' names its fields in the type database. A sum's +;;; payload writes its union out in place -- `(union ((circle (struct +;;; shapeːcircle)) ...))' -- and carries them with it, so there is +;;; nothing to look up +(define (type-fields type) + (cond ((and (list? type) + (= 2 (length type)) + (memq (car type) '(struct union)) + (list? (second type))) + (second type)) + ;; `ops-key' is already the "name of the aggregate" question, + ;; const and all: (* const expr) derefs to (const expr), which + ;; has the fields `expr' has + ((ops-key type) => get-fields) + (else #f))) (define (make-lambda-name enclosing-fn-name counter) (string->symbol diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index fd05daf..a29fd26 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -110,10 +110,19 @@ (define (c-paren x) (cat "(" (c-expr x) ")")) + ;; `.' and `->' associate left, and their right operand is always a + ;; name, so a member access written inside another is the left one + ;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r' + (define (c-member-op? x) + (let ((str (display-to-string x))) + (or (string=? str ".") (string=? str "->")))) + (define (c-maybe-paren op x) (lambda (st) ((fmt-let 'op op (if (and (c-op<= (fmt-op st) op) + (not (and (c-member-op? (fmt-op st)) + (c-member-op? op))) (not (vector? x))) (c-paren x) x)) diff --git a/tests/codegen.scm b/tests/codegen.scm index 8538ec0..43215e3 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -26,8 +26,16 @@ (parameterize ((sex-line-directives 'none)) (emit-c (semen-process forms))))))) +;;; Warnings are what `warns?' is for; a test that happens to trip one +;;; should not print it +(define (silently thunk) + "THUNK's value, with whatever it wrote to stderr dropped." + (let ((value #f)) + (with-error-output-to-string (lambda () (set! value (thunk)))) + value)) + (define (emits? source fragment) - (and (string-contains (sex->c source) fragment) #t)) + (and (string-contains (silently (lambda () (sex->c source))) fragment) #t)) (define (error-message source) "Compile SOURCE and return the error text as the user sees it -- @@ -48,6 +56,23 @@ compiles." (define (in-fn body) (string-append "(fn f ((a int) (b int)) void " body ")")) +;;; A struct, a struct holding a pointer to one, and locals of every +;;; shape a member path can start from +(define (in-line body) + (string-append + "(struct point ((x int) (y int)))" + "(struct line ((a (* struct point)) (b (struct point))))" + "(fn f ((lp (* struct line))) void" + " (var p (struct point)) (var pp (* struct point) (& p))" + " (var l (struct line)) " body ")")) + +(define (warnings source) + "What compiling SOURCE writes to stderr." + (with-error-output-to-string (lambda () (sex->c source)))) + +(define (warns? source fragment) + (and (string-contains (warnings source) fragment) #t)) + (test-group "codegen" ;; c-switch handed its scrutinee straight to `cat', which only works @@ -635,6 +660,32 @@ compiles." (test-assert "nor one behind a pointer" (emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]"))) + ;; `.' is C's `.' or its `->' depending on what is being read from, + ;; so a path reads the same however the thing it starts from is held + (test-group "member access" + (test-assert "a value reads with a dot" + (emits? (in-line "(= (. p x) 1)") "p.x = 1")) + (test-assert "a pointer reads with an arrow" + (emits? (in-line "(= (. pp x) 1)") "pp->x = 1")) + ;; each step asks the type in hand, so one path may be both + (test-assert "a pointer field inside a value" + (emits? (in-line "(= (. l a x) 1)") "l.a->x = 1")) + (test-assert "a value field inside a pointer" + (emits? (in-line "(= (. lp b y) 1)") "lp->b.y = 1")) + (test-assert "and two pointers in a row" + (emits? (in-line "(= (. lp a x) 1)") "lp->a->x = 1")) + ;; a path is left-associative and its right operand is always a + ;; name, so none of it needs parenthesising + (test-assert "a path carries no parens" + (not (emits? (in-line "(= (. lp a x) 1)") "(lp->a)"))) + ;; ...which is not to say nothing does + (test-assert "though a dereference still does" + (emits? (in-line "(= (. (* pp) x) 1)") "(*pp).x = 1")) + (test-assert "a type we cannot work out is read as a value" + (emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1")) + (test-assert "and said so" + (warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))) + ;; Toplevel forms used to be separated by a blank line unconditionally, ;; which spread a block of adjacent typedefs over twice the lines its ;; author wrote diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index c855546..8bf0285 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -228,7 +228,7 @@ (test-assert "an arm binds its payload" (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") - "float r = ((s.u).circle).r")) + "float r = s.u.circle.r")) (test-assert "_ binds nothing" (not (emits? (sum-src "(match s ((circle (r)) (= out r))" " ((rect (w _)) (= out w))"