forked from alex-eg/sex
implement type inference
Two things out of one mechanism. `_' as a type means "work it out from
the initializer", so (var n _ (strlen s)) stops needing size-t spelled
out; `type-of' hands a macro the type of an expression, so a macro can
dispatch on what it was handed rather than on what was declared. Both
read the same answers from two sides.
Algorithm W's core, intra-procedural, with the extensions C forces:
- an unknown type, since (include stdio.h) brings in names we never
parsed. Unification is consistency rather than equality, so
anything touching an unparsed declaration stops constraining
instead of rejecting a program that compiled yesterday;
- the usual arithmetic conversions, since `+' is not a function of
one type;
- checking mode for initializers, since #(0 0) has no type of its own
and takes one from its context. #(T : ...) is the way out of that.
What it wanted on the way:
- what type a *name* has, which neither the typedef nor the tag
database recorded. One table serves functions and variables, since
a function type already has a surface spelling;
- a scope chain, so a (var c int 9) inside a do ends with the block;
- form-type, keyed by cons cell, so one form has one type;
- macros expanded during the walk rather than before it, so type-of
is answered in the scope the macro was written in.
Closures take the same machinery: a receiver whose type comes from a
call, captures written (name expr) and typed from the expression, and
conversion from a bare function wherever a closure is expected.
type-match grew `_' on the pattern side, since (closure ((int)) int)
and (closure ((float)) int) were separate clauses for one case.
This commit is contained in:
@@ -166,9 +166,55 @@
|
||||
#f
|
||||
(type-match 'float (int 'yes)))
|
||||
|
||||
;; `_' in a pattern matches anything in that position; written last it
|
||||
;; takes the rest, since a type's words are spread and not nested
|
||||
(test "a wildcard matches an atom"
|
||||
'yes (type-match 'int (_ 'yes) (else 'no)))
|
||||
(test "a pointer to anything"
|
||||
'yes (type-match '(* int) ((* _) 'yes) (else 'no)))
|
||||
(test "including one spelled with qualifiers"
|
||||
'yes (type-match '(* const char) ((* _) 'yes) (else 'no)))
|
||||
(test "an array of anything, any length"
|
||||
'yes (type-match '(¤ int 4) ((¤ _ _) 'yes) (else 'no)))
|
||||
(test "but a sized pattern does not match an unsized array"
|
||||
'no (type-match '(¤ int) ((¤ _ _) 'yes) (else 'no)))
|
||||
(test "an aggregate of any tag"
|
||||
'yes (type-match '(struct point) ((struct _) 'yes) (else 'no)))
|
||||
(test "and the keyword still has to agree"
|
||||
'no (type-match '(union point) ((struct _) 'yes) (else 'no)))
|
||||
(test "a closure of any signature"
|
||||
'yes (type-match '(closure ((float)) int) ((closure _ _) 'yes) (else 'no)))
|
||||
(test "an exact pattern is still exact"
|
||||
'no (type-match '(* int) ((* const char) 'yes) (else 'no)))
|
||||
|
||||
(test "an undeclared name has no entry"
|
||||
#f
|
||||
(get-type-info 't-never-declared))
|
||||
(test "and no fields"
|
||||
#f
|
||||
(get-fields 't-never-declared)))
|
||||
(get-fields 't-never-declared))
|
||||
|
||||
;; What type a *name* has -- the third table, which functions and
|
||||
;; variables share because a function type has a surface spelling
|
||||
(add-name-type! 't-sum '(fn ((int) (int)) int))
|
||||
(add-name-type! 't-origin '(struct t-point))
|
||||
(test "a function's signature comes back whole"
|
||||
'(fn ((int) (int)) int)
|
||||
(get-name-type 't-sum))
|
||||
(test "and a variable's type"
|
||||
'(struct t-point)
|
||||
(get-name-type 't-origin))
|
||||
(test "the return type is what a call site wants"
|
||||
'int
|
||||
(get-return-type 't-sum))
|
||||
(test "a variable has no return type"
|
||||
#f
|
||||
(get-return-type 't-origin))
|
||||
;; not an error: this is how a name from an included C header looks,
|
||||
;; and the caller decides what to make of it
|
||||
(test "an undeclared name has no type"
|
||||
#f
|
||||
(get-name-type 't-never-declared))
|
||||
(test "nor a return type"
|
||||
#f
|
||||
(get-return-type 't-never-declared)))
|
||||
|
||||
Reference in New Issue
Block a user