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:
1 parent
b945182739
commit
48b1b84ea6
6 files changed
+309
-17
No files matched your search
+64
-17
@@ -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 <stdarg.h>
|
||||
#include <stdio.h>
|
||||
@@ -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 <body> end -- iterate any iterator
|
||||
* (generator). Each round calls NEXT; empty ends the loop. */
|
||||
|
||||
Reference in new issue
Block a user