diff --git a/Makefile b/Makefile index ab27f57..1168ea1 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ SRCS := \ src/cat/vm.c \ src/cat/module.c \ src/cat/types/empty.c \ + src/cat/types/type.c \ src/cat/types/bool.c \ src/cat/types/number.c \ src/cat/types/int.c \ diff --git a/include/cat/types/type.h b/include/cat/types/type.h new file mode 100644 index 0000000..5bcfae4 --- /dev/null +++ b/include/cat/types/type.h @@ -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`, ...). + * 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 */ diff --git a/src/cat/compiler.c b/src/cat/compiler.c index 39000a8..0481d33 100644 --- a/src/cat/compiler.c +++ b/src/cat/compiler.c @@ -28,6 +28,7 @@ #include "cat/types/int.h" #include "cat/types/float.h" #include "cat/types/ascii_text.h" +#include "cat/types/type.h" #include #include @@ -40,6 +41,7 @@ typedef struct { CAT_BYTECODE_Function *fn; char locals[MAXLOCALS][64]; + CAT_TYPES_TYPE_Type *local_types[MAXLOCALS]; /* declared/inferred */ int nlocals; int is_generator; /* body used `yield` */ } FuncCtx; @@ -152,6 +154,7 @@ static int add_local(Compiler *c, const char *name) return -1; slot = f->nlocals++; snprintf(f->locals[slot], sizeof(f->locals[slot]), "%s", name); + f->local_types[slot] = NULL; if (f->nlocals > f->fn->nlocals) f->fn->nlocals = f->nlocals; return slot; @@ -200,17 +203,21 @@ static int bp_of(int kind) } } -/* Skip an advisory type expression: e.g. `: int`, `: cbyte[]`, - * `: DynSized()`, `: sentinel_sized<'\0'>`. Types are advisory only - * (catscript is structurally typed), so we parse and discard a full - * balanced type expression, ignoring `()`, `<>`, `[]` nesting. */ -static void skip_type_hint(Compiler *c) +/* Parse an optional type hint: e.g. `: int`, `: cbyte[]`, + * `: DynSized()`, `: sentinel_sized<0>`. We consume a full balanced + * expression (ignoring `()`, `<>`, `[]` nesting) and return the + * descriptor of the FIRST identifier -- structural typing: the first + * name IS the type. With no `:` it returns `any`. */ +static CAT_TYPES_TYPE_Type *parse_type_core(Compiler *c, int colon) { + CAT_TYPES_TYPE_Type *t = NULL; int depth = 0; - if (!check(c, CAT_LEXER_TOK_COLON)) - return; - next(c); + if (colon) { + if (!check(c, CAT_LEXER_TOK_COLON)) + return cat_types_type_any(); + next(c); + } for (;;) { int k = c->tok.kind; if (k == CAT_LEXER_TOK_LPAREN || k == CAT_LEXER_TOK_LT || @@ -227,14 +234,43 @@ static void skip_type_hint(Compiler *c) next(c); continue; } - if ((k == CAT_LEXER_TOK_NAME || k == CAT_LEXER_TOK_INT || - k == CAT_LEXER_TOK_STRING) || - (k == CAT_LEXER_TOK_COMMA && depth > 0)) { + if (k == CAT_LEXER_TOK_NAME) { + if (t == NULL) + t = cat_types_type_named(c->tok.text); + next(c); + continue; + } + if (k == CAT_LEXER_TOK_INT || k == CAT_LEXER_TOK_STRING) { + next(c); + continue; + } + if (k == CAT_LEXER_TOK_COMMA && depth > 0) { next(c); continue; } break; } + return (t != NULL) ? t : cat_types_type_any(); +} + +static CAT_TYPES_TYPE_Type *parse_type_hint(Compiler *c) +{ + return parse_type_core(c, 1); +} + +/* The type of the value the cursor is on, when it is a literal; NULL when + * the expression is not a simple literal (then we say nothing). */ +static CAT_TYPES_TYPE_Type *literal_type(Compiler *c) +{ + switch (c->tok.kind) { + case CAT_LEXER_TOK_INT: return cat_types_type_int(); + case CAT_LEXER_TOK_FLOAT: return cat_types_type_float(); + case CAT_LEXER_TOK_STRING: return cat_types_type_text(); + case CAT_LEXER_TOK_TRUE: + case CAT_LEXER_TOK_FALSE: return cat_types_type_bool(); + case CAT_LEXER_TOK_NIL: return cat_types_type_empty(); + default: return NULL; + } } /* Compile `function (params) body end` starting at `function`. */ @@ -255,7 +291,7 @@ static int compile_function_literal(Compiler *c, const char *name) char pn[64]; snprintf(pn, sizeof(pn), "%s", c->tok.text); next(c); - skip_type_hint(c); + nc.local_types[nc.nlocals] = parse_type_hint(c); /* add_local into nc directly */ snprintf(nc.locals[nc.nlocals], sizeof(nc.locals[0]), "%s", pn); nc.nlocals++; @@ -618,16 +654,27 @@ static void stat(Compiler *c) if (check(c, CAT_LEXER_TOK_NAME)) { char name[256]; int slot; + CAT_TYPES_TYPE_Type *decl; snprintf(name, sizeof(name), "%s", c->tok.text); next(c); - skip_type_hint(c); - if (accept(c, CAT_LEXER_TOK_ASSIGN)) + decl = parse_type_hint(c); + if (accept(c, CAT_LEXER_TOK_ASSIGN)) { + CAT_TYPES_TYPE_Type *lit = literal_type(c); + if (lit != NULL && + !cat_types_type_assignable(decl, lit)) + err_at(c, line, + "cannot assign %s to %s", + cat_types_type_name(lit), + cat_types_type_name(decl)); expr(c); - else + } else { emit(c, CAT_BYTECODE_OP_EMPTY, 0, 0, line); + } slot = add_local(c, name); - if (slot >= 0) + if (slot >= 0) { + ctx(c)->local_types[slot] = decl; emit(c, CAT_BYTECODE_OP_SETLOCAL, slot, 0, line); + } } break; @@ -711,7 +758,7 @@ static void stat(Compiler *c) snprintf(var, sizeof(var), "%s", c->tok.text); next(c); } - skip_type_hint(c); + (void)parse_type_hint(c); if (check(c, CAT_LEXER_TOK_IN)) { /* for NAME in expr do end -- iterate any iterator * (generator). Each round calls NEXT; empty ends the loop. */ diff --git a/src/cat/types/type.c b/src/cat/types/type.c new file mode 100644 index 0000000..760e00a --- /dev/null +++ b/src/cat/types/type.c @@ -0,0 +1,139 @@ +/* + * cat/types/type.c -- compile-time type descriptors. + * + * Naming rule: symbols here are cat_types_type_* / CAT_TYPES_TYPE_*. + * + * All descriptors are immortal singletons (no teardown): a compiler + * process interns a bounded, small set of names. + */ +#include "cat/types/type.h" + +#include +#include + +/* One descriptor per primitive. Names are lowercase per the language + * rule. */ +static CAT_TYPES_TYPE_Type g_any = { "any", CAT_TYPES_TYPE_ANY }; +static CAT_TYPES_TYPE_Type g_empty = { "empty", CAT_TYPES_TYPE_EMPTY }; +static CAT_TYPES_TYPE_Type g_bool = { "bool", CAT_TYPES_TYPE_BOOL }; +static CAT_TYPES_TYPE_Type g_int = { "int", CAT_TYPES_TYPE_INT }; +static CAT_TYPES_TYPE_Type g_float = { "float", CAT_TYPES_TYPE_FLOAT }; +static CAT_TYPES_TYPE_Type g_text = { "text", CAT_TYPES_TYPE_TEXT }; +static CAT_TYPES_TYPE_Type g_object = { "object", CAT_TYPES_TYPE_OBJECT }; +static CAT_TYPES_TYPE_Type g_function = { "function", CAT_TYPES_TYPE_FUNCTION }; +static CAT_TYPES_TYPE_Type g_list = { "list", CAT_TYPES_TYPE_LIST }; +static CAT_TYPES_TYPE_Type g_memory = { "memory", CAT_TYPES_TYPE_MEMORY }; + +/* Interned named types. A small open-addressed table of string -> Type. */ +#define NAMED_CAP 512 + +static CAT_TYPES_TYPE_Type *g_named[NAMED_CAP]; + +CAT_TYPES_TYPE_Type *cat_types_type_any(void) { return &g_any; } +CAT_TYPES_TYPE_Type *cat_types_type_empty(void) { return &g_empty; } +CAT_TYPES_TYPE_Type *cat_types_type_bool(void) { return &g_bool; } +CAT_TYPES_TYPE_Type *cat_types_type_int(void) { return &g_int; } +CAT_TYPES_TYPE_Type *cat_types_type_float(void) { return &g_float; } +CAT_TYPES_TYPE_Type *cat_types_type_text(void) { return &g_text; } +CAT_TYPES_TYPE_Type *cat_types_type_object(void) { return &g_object; } +CAT_TYPES_TYPE_Type *cat_types_type_function(void) { return &g_function; } +CAT_TYPES_TYPE_Type *cat_types_type_list(void) { return &g_list; } +CAT_TYPES_TYPE_Type *cat_types_type_memory(void) { return &g_memory; } + +CAT_TYPES_TYPE_Type *cat_types_type_named(const char *name) +{ + unsigned long h = 5381; + const char *p; + size_t slot; + + if (name == NULL || name[0] == '\0') + return &g_any; + /* Known primitives are their singletons, not new named types. */ + if (strcmp(name, "any") == 0) return &g_any; + if (strcmp(name, "empty") == 0) return &g_empty; + if (strcmp(name, "bool") == 0) return &g_bool; + if (strcmp(name, "int") == 0) return &g_int; + if (strcmp(name, "float") == 0) return &g_float; + if (strcmp(name, "text") == 0) return &g_text; + if (strcmp(name, "object") == 0) return &g_object; + if (strcmp(name, "function") == 0 || + strcmp(name, "closure") == 0) return &g_function; + if (strcmp(name, "list") == 0) return &g_list; + if (strcmp(name, "memory") == 0) return &g_memory; + + for (p = name; *p != '\0'; p++) + h = ((h << 5) + h) + (unsigned char)*p; + slot = h % NAMED_CAP; + for (;;) { + if (g_named[slot] == NULL) { + CAT_TYPES_TYPE_Type *t = calloc(1, sizeof(*t)); + if (t == NULL) + return &g_any; + t->name = strdup(name); + t->kind = CAT_TYPES_TYPE_NAMED; + g_named[slot] = t; + return t; + } + if (g_named[slot]->kind == CAT_TYPES_TYPE_NAMED && + strcmp(g_named[slot]->name, name) == 0) + return g_named[slot]; + slot = (slot + 1) % NAMED_CAP; + } +} + +const char *cat_types_type_name(const CAT_TYPES_TYPE_Type *t) +{ + return (t != NULL) ? t->name : "any"; +} + +int cat_types_type_equal(const CAT_TYPES_TYPE_Type *a, + const CAT_TYPES_TYPE_Type *b) +{ + if (a == b) + return 1; + if (a == NULL || b == NULL) + return 0; + if (a->kind != b->kind) + return 0; + if (a->kind == CAT_TYPES_TYPE_NAMED) + return strcmp(a->name, b->name) == 0; + return 1; +} + +int cat_types_type_assignable(const CAT_TYPES_TYPE_Type *dst, + const CAT_TYPES_TYPE_Type *src) +{ + if (dst == NULL || src == NULL) + return 1; + if (dst->kind == CAT_TYPES_TYPE_ANY || src->kind == CAT_TYPES_TYPE_ANY) + return 1; + if (cat_types_type_equal(dst, src)) + return 1; + /* int widens to float */ + if (dst->kind == CAT_TYPES_TYPE_FLOAT && src->kind == CAT_TYPES_TYPE_INT) + return 1; + /* A named/opaque type is structural: we do not know its members, so + * we never reject a concrete value against it (or vice versa). Only + * two KNOWN, CONCRETE, distinct primitives fail. */ + if (dst->kind == CAT_TYPES_TYPE_NAMED || src->kind == CAT_TYPES_TYPE_NAMED) + return 1; + return 0; +} + +CAT_TYPES_TYPE_Type *cat_types_type_join(const CAT_TYPES_TYPE_Type *a, + const CAT_TYPES_TYPE_Type *b) +{ + if (a == NULL || b == NULL) + return &g_any; + if (a->kind == CAT_TYPES_TYPE_ANY || b->kind == CAT_TYPES_TYPE_ANY) + return &g_any; + if (a->kind == CAT_TYPES_TYPE_FLOAT || b->kind == CAT_TYPES_TYPE_FLOAT) { + /* float dominates numeric combinations */ + if (a->kind == CAT_TYPES_TYPE_FLOAT || + a->kind == CAT_TYPES_TYPE_INT) + return &g_float; + } + if (cat_types_type_equal(a, b)) + return (CAT_TYPES_TYPE_Type *)a; + return &g_any; +} diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 9a10b00..7e2da56 100644 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -254,6 +254,21 @@ esac # ---- self-hosted front-end (written in catscript) ------------------- +out=`$BIN tests/types.cat` +check "types-ok" "ok: 42 3.5 hi true" "`echo "$out" | sed -n 1p`" +check "types-widen" "widen: 7" "`echo "$out" | sed -n 2p`" +check "types-opaque" "opaque: 42 empty" "`echo "$out" | sed -n 3p`" + +# type hints are CHECKED where both sides are concrete +printf 'local x : int = "bad"\n' > /tmp/catscript_badtype.cat +if $BIN /tmp/catscript_badtype.cat >/dev/null 2>&1 +then + check "types-reject" "reject" "accepted" +else + check "types-reject" "reject" "reject" +fi +rm -f /tmp/catscript_badtype.cat + out=`$BIN tests/interface.cat` check "iface-is-sub" "circle is Drawable: true" "`echo "$out" | sed -n 1p`" check "iface-is-inherited" "circle is Shape: true" "`echo "$out" | sed -n 2p`" diff --git a/tests/types.cat b/tests/types.cat new file mode 100644 index 0000000..1341e90 --- /dev/null +++ b/tests/types.cat @@ -0,0 +1,20 @@ +-- tests/types.cat -- compile-time type checking of hints. +-- +-- Hints are now CHECKED where both sides are concrete and known; opaque +-- or unknown names remain permissive (structural typing never rejects a +-- program by guessing). + +local x : int = 42 +local y : float = 3.5 +local z : text = "hi" +local b : bool = true +print("ok:", x, y, z, b) + +-- int widens to float +local f : float = 7 +print("widen:", f) + +-- opaque type names stay permissive (no false rejection) +local n : cint = 42 +local buf : cbyte[] = empty +print("opaque:", n, buf)