Files
catscript/src/cat/lexer.c
T
Minus b945182739 interface + structural is: runtime-checked interfaces
Add nominal interface declarations with structural, runtime checking
(the owner asked for the operator AND the runtime check).

- lexer: `interface` and `is` are keywords; `implements` is a soft
  keyword (matched by text in the class clause) so the `implements()`
  stdlib function is not shadowed.
- `interface NAME [extends BASE] <function m() end> end` builds a table
  tagged `__interface`; members name the required methods (with an
  optional default body, shared via `__index`). Interface inheritance
  follows `__index`.
- class: `class C [extends B] [implements I, ...]` -- after the class
  global is set, `__implements_check` runs and raises if the class does
  not satisfy each declared interface.
- new opcode `IS` (`obj is iface`): structural membership test, and it
  degrades to an exact type (vtable) match when the right side is not an
  interface, so `x is SomeClass` reads naturally. Backed by
  `cat_value_is()`.
- stdlib: `implements(obj, iface)` (the same test as a function).
- tests/interface.cat + suite block: interface satisfaction, interface
  inheritance, duck typing (no `implements` needed), and a missing
  method failing. Suite 130 -> 136, all green.
2026-11-18 15:56:10 +00:00

302 lines
7.7 KiB
C

/*
* cat/lexer.c -- tokenizer implementation.
*
* Naming rule: symbols here are cat_lexer_* / CAT_LEXER_*.
*/
#include "cat/lexer.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
/* Keyword table: name -> token kind. */
struct kw {
const char *name;
int kind;
};
static const struct kw g_keywords[] = {
{ "and", CAT_LEXER_TOK_AND },
{ "break", CAT_LEXER_TOK_BREAK },
{ "class", CAT_LEXER_TOK_CLASS },
{ "do", CAT_LEXER_TOK_DO },
{ "else", CAT_LEXER_TOK_ELSE },
{ "elseif", CAT_LEXER_TOK_ELSEIF },
{ "except", CAT_LEXER_TOK_EXCEPT },
{ "end", CAT_LEXER_TOK_END },
{ "extends", CAT_LEXER_TOK_EXTENDS },
{ "interface", CAT_LEXER_TOK_INTERFACE },
{ "is", CAT_LEXER_TOK_IS },
{ "false", CAT_LEXER_TOK_FALSE },
{ "for", CAT_LEXER_TOK_FOR },
{ "function", CAT_LEXER_TOK_FUNCTION },
{ "if", CAT_LEXER_TOK_IF },
{ "in", CAT_LEXER_TOK_IN },
{ "local", CAT_LEXER_TOK_LOCAL },
{ "not", CAT_LEXER_TOK_NOT },
{ "or", CAT_LEXER_TOK_OR },
{ "repeat", CAT_LEXER_TOK_REPEAT },
{ "raise", CAT_LEXER_TOK_RAISE },
{ "return", CAT_LEXER_TOK_RETURN },
{ "then", CAT_LEXER_TOK_THEN },
{ "true", CAT_LEXER_TOK_TRUE },
{ "try", CAT_LEXER_TOK_TRY },
{ "until", CAT_LEXER_TOK_UNTIL },
{ "use", CAT_LEXER_TOK_USE },
{ "while", CAT_LEXER_TOK_WHILE },
{ "yield", CAT_LEXER_TOK_YIELD },
};
void cat_lexer_init(CAT_LEXER_Lexer *lx, const char *src)
{
lx->src = src ? src : "";
lx->pos = 0;
lx->line = 1;
}
static int peek(CAT_LEXER_Lexer *lx)
{
return (unsigned char)lx->src[lx->pos];
}
static int peek2(CAT_LEXER_Lexer *lx)
{
return (unsigned char)lx->src[lx->pos + 1];
}
static int advance(CAT_LEXER_Lexer *lx)
{
int c = lx->src[lx->pos];
if (c == '\0')
return -1;
lx->pos++;
if (c == '\n')
lx->line++;
return c;
}
/* Skip whitespace and `--` line comments. */
static void skip_ws(CAT_LEXER_Lexer *lx)
{
for (;;) {
int c = peek(lx);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
advance(lx);
} else if (c == '-' && peek2(lx) == '-') {
advance(lx);
advance(lx);
while (peek(lx) != '\n' && peek(lx) != '\0')
advance(lx);
} else {
break;
}
}
}
static int lookup_keyword(const char *s)
{
size_t i;
for (i = 0; i < sizeof(g_keywords) / sizeof(g_keywords[0]); i++)
if (strcmp(g_keywords[i].name, s) == 0)
return g_keywords[i].kind;
return CAT_LEXER_TOK_NAME;
}
int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out)
{
int c;
memset(out, 0, sizeof(*out));
skip_ws(lx);
out->line = lx->line;
c = peek(lx);
if (c == '\0') {
out->kind = CAT_LEXER_TOK_EOF;
return 0;
}
/* identifiers / keywords */
if (isalpha(c) || c == '_') {
int n = 0;
while (isalnum(peek(lx)) || peek(lx) == '_') {
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)advance(lx);
else
advance(lx);
}
out->text[n] = '\0';
out->kind = lookup_keyword(out->text);
return 0;
}
/* numbers: int or float, with 0x/0b/0o integer bases */
if (c == '0' && (peek2(lx) == 'x' || peek2(lx) == 'X' ||
peek2(lx) == 'b' || peek2(lx) == 'B' ||
peek2(lx) == 'o' || peek2(lx) == 'O')) {
int base = 16;
int n = 0;
int marker = peek2(lx);
out->text[n++] = (char)advance(lx); /* '0' */
out->text[n++] = (char)advance(lx); /* x/b/o */
if (marker == 'b' || marker == 'B')
base = 2;
else if (marker == 'o' || marker == 'O')
base = 8;
while (isxdigit(peek(lx)) || peek(lx) == '_') {
int d = peek(lx);
advance(lx);
if (d == '_')
continue;
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)d;
}
out->text[n] = '\0';
/* skip the 0x/0b/0o prefix for strtoll */
out->kind = CAT_LEXER_TOK_INT;
out->ival = strtoll(out->text + 2, NULL, base);
return 0;
}
if (isdigit(c)) {
int isfloat = 0;
int n = 0;
while (isdigit(peek(lx))) {
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)advance(lx);
else
advance(lx);
}
if (peek(lx) == '.' && isdigit(peek2(lx))) {
isfloat = 1;
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)advance(lx);
while (isdigit(peek(lx))) {
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)advance(lx);
else
advance(lx);
}
}
out->text[n] = '\0';
if (isfloat) {
out->kind = CAT_LEXER_TOK_FLOAT;
out->fval = strtod(out->text, NULL);
} else {
out->kind = CAT_LEXER_TOK_INT;
out->ival = strtoll(out->text, NULL, 10);
}
return 0;
}
/* strings: '...' or "..." with \n \t \\ \" escapes */
if (c == '"' || c == '\'') {
int quote = advance(lx);
int n = 0;
while (peek(lx) != quote && peek(lx) != '\0') {
int ch = advance(lx);
if (ch == '\\') {
int e = advance(lx);
switch (e) {
case 'n': ch = '\n'; break;
case 't': ch = '\t'; break;
case '\\': ch = '\\'; break;
case '"': ch = '"'; break;
case '\'': ch = '\''; break;
default: ch = e; break;
}
}
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)ch;
}
if (peek(lx) == quote)
advance(lx);
out->text[n] = '\0';
out->kind = CAT_LEXER_TOK_STRING;
return 0;
}
/* symbols */
switch (c) {
case '+': advance(lx); out->kind = CAT_LEXER_TOK_PLUS; return 0;
case '-':
advance(lx);
if (peek(lx) == '>') {
advance(lx);
out->kind = CAT_LEXER_TOK_ARROW;
return 0;
}
out->kind = CAT_LEXER_TOK_MINUS;
return 0;
case '*': advance(lx); out->kind = CAT_LEXER_TOK_STAR; return 0;
case '/': advance(lx); out->kind = CAT_LEXER_TOK_SLASH; return 0;
case '%': advance(lx); out->kind = CAT_LEXER_TOK_PERCENT; return 0;
case '^': advance(lx); out->kind = CAT_LEXER_TOK_CARET; return 0;
case '#': advance(lx); out->kind = CAT_LEXER_TOK_HASH; return 0;
case ';': advance(lx); out->kind = CAT_LEXER_TOK_SEMI; return 0;
case ',': advance(lx); out->kind = CAT_LEXER_TOK_COMMA; return 0;
case '(': advance(lx); out->kind = CAT_LEXER_TOK_LPAREN; return 0;
case ')': advance(lx); out->kind = CAT_LEXER_TOK_RPAREN; return 0;
case '{': advance(lx); out->kind = CAT_LEXER_TOK_LBRACE; return 0;
case '}': advance(lx); out->kind = CAT_LEXER_TOK_RBRACE; return 0;
case '[': advance(lx); out->kind = CAT_LEXER_TOK_LBRACKET; return 0;
case ']': advance(lx); out->kind = CAT_LEXER_TOK_RBRACKET; return 0;
case '=':
advance(lx);
if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_EQ; }
else out->kind = CAT_LEXER_TOK_ASSIGN;
return 0;
case '~':
advance(lx);
if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_NE; return 0; }
out->kind = CAT_LEXER_TOK_EOF;
return 0;
case '<':
advance(lx);
if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_LE; }
else out->kind = CAT_LEXER_TOK_LT;
return 0;
case '>':
advance(lx);
if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_GE; }
else out->kind = CAT_LEXER_TOK_GT;
return 0;
case '.':
advance(lx);
if (peek(lx) == '.') {
advance(lx);
out->kind = CAT_LEXER_TOK_CONCAT;
} else {
out->kind = CAT_LEXER_TOK_DOT;
}
return 0;
case ':':
advance(lx);
if (peek(lx) == ':') { advance(lx); out->kind = CAT_LEXER_TOK_DCOLON; }
else out->kind = CAT_LEXER_TOK_COLON;
return 0;
default:
advance(lx);
out->kind = CAT_LEXER_TOK_EOF;
return 0;
}
}
const char *cat_lexer_kind_name(int kind)
{
/* Must match enum CAT_LEXER_Kind exactly (same order). */
static const char *names[] = {
"EOF", "NAME", "INT", "FLOAT", "STRING",
"and", "break", "do", "else", "elseif", "end", "false",
"for", "function", "if", "in", "local", "nil", "not",
"or", "repeat", "return", "then", "true", "until", "while",
"yield", "try", "except", "raise", "use", "class", "extends",
"interface", "implements", "is",
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=",
"<", ">", "=", "(", ")", "{", "}", "[", "]", ";", ",",
".", ":", "::", "..", "->"
};
if (kind >= 0 && kind < (int)(sizeof(names) / sizeof(names[0])))
return names[kind];
return "?";
}