forked from alex-eg/sex
type an indexed step of a member path
(. q [pts 2] y) looked the step up as a field whole, so its type was lost: the next step guessed a value, and through an array of pointers wrote . for ->.
This commit is contained in:
27
semen.scm
27
semen.scm
@@ -1180,8 +1180,10 @@
|
|||||||
(let ((walked (drop-right (cddr form) (length fields))))
|
(let ((walked (drop-right (cddr form) (length fields))))
|
||||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||||
(if (null? walked) base `(|.| ,base ,@walked)))))
|
(if (null? walked) base `(|.| ,base ,@walked)))))
|
||||||
(step (list (if target '%-> 'dot-access) expr (car fields))
|
(step (list (if target '%-> 'dot-access)
|
||||||
(field-type (or target type) (car fields))
|
expr
|
||||||
|
(nested-step (car fields)))
|
||||||
|
(step-type (or target type) (car fields))
|
||||||
(cdr fields)
|
(cdr fields)
|
||||||
(or told? (not type))))))))
|
(or told? (not type))))))))
|
||||||
|
|
||||||
@@ -1456,10 +1458,27 @@
|
|||||||
(define (member-path-type type fields)
|
(define (member-path-type type fields)
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
type
|
type
|
||||||
(member-path-type (field-type (or (pointer-target type) type)
|
(member-path-type (step-type (or (pointer-target type) type)
|
||||||
(car fields))
|
(car fields))
|
||||||
(cdr fields))))
|
(cdr fields))))
|
||||||
|
|
||||||
|
;;; What a step of a path gives: its field, or for [xs i] an element of
|
||||||
|
;;; the field xs, one for each index
|
||||||
|
(define (step-type type field)
|
||||||
|
(let ((t (field-type type (field-name field))))
|
||||||
|
(if (and t (pair? field) (eq? '¤ (car field)))
|
||||||
|
(fold (lambda (index t)
|
||||||
|
(and t (or (array-element-type t) (pointer-target t))))
|
||||||
|
t
|
||||||
|
(cddr field))
|
||||||
|
t)))
|
||||||
|
|
||||||
|
;;; [grid 1 2] as a step is [[grid 1] 2], as it is anywhere else
|
||||||
|
(define (nested-step field)
|
||||||
|
(if (and (pair? field) (eq? '¤ (car field)) (> (length field) 3))
|
||||||
|
(list '¤ (nested-step (drop-right field 1)) (last field))
|
||||||
|
field))
|
||||||
|
|
||||||
(define (field-type type field)
|
(define (field-type type field)
|
||||||
(let* ((fields (type-fields type))
|
(let* ((fields (type-fields type))
|
||||||
(entry (and fields (assq field fields))))
|
(entry (and fields (assq field fields))))
|
||||||
|
|||||||
@@ -4,6 +4,14 @@
|
|||||||
|
|
||||||
(import srfi-13)
|
(import srfi-13)
|
||||||
|
|
||||||
|
;;; Paths through arrays of a struct and of pointers to one
|
||||||
|
(define poly-src
|
||||||
|
(string-append
|
||||||
|
"(struct pt ((x int) (y int)))"
|
||||||
|
"(struct poly ((pts [(struct pt) 4]) (pp [* struct pt 4]) (grid [(struct pt) 2 3])))"
|
||||||
|
"(fn f ((q (* struct poly))) int"
|
||||||
|
" (return (+ (. q [pts 2] y) (. q [pp 0] y) (. q [grid 1 2] x))))"))
|
||||||
|
|
||||||
(define (in-fn body)
|
(define (in-fn body)
|
||||||
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
||||||
|
|
||||||
@@ -971,6 +979,16 @@
|
|||||||
(test-group "member access"
|
(test-group "member access"
|
||||||
(test-assert "a value reads with a dot"
|
(test-assert "a value reads with a dot"
|
||||||
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
||||||
|
;; an indexed step was looked up as a field whole, so its type was
|
||||||
|
;; lost and the step after it guessed
|
||||||
|
(test-assert "an indexed step is its field's element"
|
||||||
|
(emits? poly-src "q->pts[2].y"))
|
||||||
|
(test-assert "and says so without a warning"
|
||||||
|
(not (warns? poly-src "cannot type")))
|
||||||
|
(test-assert "so a pointer element is read through"
|
||||||
|
(emits? poly-src "q->pp[0]->y"))
|
||||||
|
(test-assert "and every index of one is written"
|
||||||
|
(emits? poly-src "q->grid[1][2].x"))
|
||||||
(test-assert "a pointer reads with an arrow"
|
(test-assert "a pointer reads with an arrow"
|
||||||
(emits? (in-line "(= (. pp x) 1)") "pp->x = 1"))
|
(emits? (in-line "(= (. pp x) 1)") "pp->x = 1"))
|
||||||
;; each step asks the type in hand, so one path may be both
|
;; each step asks the type in hand, so one path may be both
|
||||||
|
|||||||
Reference in New Issue
Block a user