type checker, phase 1: hints are checked where concrete

First slice of the static-typing foundation (traits/generics/Sized()
will build on it).  Typing stays structural and permissive: an unknown
or opaque name is never a rejection, so existing programs keep working.

- new module cat/types/type.{h,c}: compile-time-only descriptors
  (int/float/bool/text/empty/object/function/list/memory/any + interned
  named/opaque types), with equal/assignable/join and int->float widening.
- compiler: skip_type_hint -> parse_type_hint, which returns the type of
  the first identifier in a hint (the rest of a balanced hint is still
  consumed and ignored, so `cbyte[sentinel_sized<0>]` parses).
- `local NAME : T = <literal>` is checked: assigning a known concrete
  literal of the wrong kind to a known concrete type raises at compile
  time ("line N: cannot assign text to int").  Unknown/opaque names and
  non-literal expressions stay permissive.
- FuncCtx tracks a declared/inferred type per local slot.
- tests/types.cat + suite block: ok hints, int->float widening, opaque
  names accepted, and a wrong assignment rejected.  Suite 136 -> 140.
This commit is contained in:
Minus committed 2026-11-18 16:56:40 +00:00
1 parent b945182739
commit 48b1b84ea6
6 files changed
+309 -17

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
/*
* cat/types/type.h -- compile-time type descriptors.
*
* Naming rule: symbols here are cat_types_type_* / CAT_TYPES_TYPE_*.
*
* These are COMPILE-TIME only: they never reach the runtime value graph
* (no refcounting, no vtable). A type is either a primitive singleton or
* a named/opaque type interned by the compiler. Typing stays structural:
* an unknown or opaque type is `any`-compatible, so checking never turns
* a working program into a rejected one by guessing.
*/
#ifndef CAT_TYPES_TYPE_H
#define CAT_TYPES_TYPE_H
/* Type categories. NAMED is any user/interface/opaque name. */
typedef enum CAT_TYPES_TYPE_Kind {
CAT_TYPES_TYPE_ANY = 0,
CAT_TYPES_TYPE_EMPTY,
CAT_TYPES_TYPE_BOOL,
CAT_TYPES_TYPE_INT,
CAT_TYPES_TYPE_FLOAT,
CAT_TYPES_TYPE_TEXT,
CAT_TYPES_TYPE_OBJECT,
CAT_TYPES_TYPE_FUNCTION,
CAT_TYPES_TYPE_LIST,
CAT_TYPES_TYPE_MEMORY,
CAT_TYPES_TYPE_NAMED
} CAT_TYPES_TYPE_Kind;
typedef struct CAT_TYPES_TYPE_Type {
const char *name; /* lowercase name, or the interned user name */
int kind; /* a CAT_TYPES_TYPE_Kind */
} CAT_TYPES_TYPE_Type;
/* Primitive singletons (immortal, shared). */
CAT_TYPES_TYPE_Type *cat_types_type_any(void);
CAT_TYPES_TYPE_Type *cat_types_type_empty(void);
CAT_TYPES_TYPE_Type *cat_types_type_bool(void);
CAT_TYPES_TYPE_Type *cat_types_type_int(void);
CAT_TYPES_TYPE_Type *cat_types_type_float(void);
CAT_TYPES_TYPE_Type *cat_types_type_text(void);
CAT_TYPES_TYPE_Type *cat_types_type_object(void);
CAT_TYPES_TYPE_Type *cat_types_type_function(void);
CAT_TYPES_TYPE_Type *cat_types_type_list(void);
CAT_TYPES_TYPE_Type *cat_types_type_memory(void);
/* Intern a named/opaque type (`cint`, `Drawable`, `List<int>`, ...).
* Returns a shared descriptor; the name is copied. Never freed (a
* compiler process interns a bounded set of names). */
CAT_TYPES_TYPE_Type *cat_types_type_named(const char *name);
const char *cat_types_type_name(const CAT_TYPES_TYPE_Type *t);
/* Structural equality (same name). `any` equals only `any`. */
int cat_types_type_equal(const CAT_TYPES_TYPE_Type *a,
const CAT_TYPES_TYPE_Type *b);
/* Is a value of type `src` acceptable where `dst` is expected?
* Permissive by design: `any` is assignable both ways, and an unknown
* named type is assignable to/from a concrete one (structural, no
* guessing). Only two known, concrete, incompatible types fail.
* `int` is assignable to `float`. */
int cat_types_type_assignable(const CAT_TYPES_TYPE_Type *dst,
const CAT_TYPES_TYPE_Type *src);
/* Result type of combining two operand types (e.g. `int + float`). */
CAT_TYPES_TYPE_Type *cat_types_type_join(const CAT_TYPES_TYPE_Type *a,
const CAT_TYPES_TYPE_Type *b);
#endif /* CAT_TYPES_TYPE_H */