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.
71 lines
2.4 KiB
C
71 lines
2.4 KiB
C
/*
|
|
* cat/lexer.h -- tokenizer for catscript source.
|
|
*
|
|
* Naming rule: symbols here are cat_lexer_* / CAT_LEXER_*.
|
|
*
|
|
* The lexer turns Lua-like source into a stream of CAT_LEXER_Token.
|
|
* Keywords follow Lua; type hints use `:` in a few documented spots.
|
|
*/
|
|
#ifndef CAT_LEXER_H
|
|
#define CAT_LEXER_H
|
|
|
|
#include <stdio.h>
|
|
|
|
/* Token kinds. */
|
|
enum CAT_LEXER_Kind {
|
|
CAT_LEXER_TOK_EOF = 0,
|
|
CAT_LEXER_TOK_NAME,
|
|
CAT_LEXER_TOK_INT,
|
|
CAT_LEXER_TOK_FLOAT,
|
|
CAT_LEXER_TOK_STRING,
|
|
/* keywords */
|
|
CAT_LEXER_TOK_AND, CAT_LEXER_TOK_BREAK, CAT_LEXER_TOK_DO,
|
|
CAT_LEXER_TOK_ELSE, CAT_LEXER_TOK_ELSEIF, CAT_LEXER_TOK_END,
|
|
CAT_LEXER_TOK_FALSE, CAT_LEXER_TOK_FOR, CAT_LEXER_TOK_FUNCTION,
|
|
CAT_LEXER_TOK_IF, CAT_LEXER_TOK_IN, CAT_LEXER_TOK_LOCAL,
|
|
CAT_LEXER_TOK_NIL, CAT_LEXER_TOK_NOT, CAT_LEXER_TOK_OR,
|
|
CAT_LEXER_TOK_REPEAT, CAT_LEXER_TOK_RETURN, CAT_LEXER_TOK_THEN,
|
|
CAT_LEXER_TOK_TRUE, CAT_LEXER_TOK_UNTIL, CAT_LEXER_TOK_WHILE,
|
|
CAT_LEXER_TOK_YIELD, CAT_LEXER_TOK_TRY, CAT_LEXER_TOK_EXCEPT,
|
|
CAT_LEXER_TOK_RAISE, CAT_LEXER_TOK_USE, CAT_LEXER_TOK_CLASS,
|
|
CAT_LEXER_TOK_EXTENDS, CAT_LEXER_TOK_INTERFACE, CAT_LEXER_TOK_IMPLEMENTS,
|
|
CAT_LEXER_TOK_IS,
|
|
/* symbols */
|
|
CAT_LEXER_TOK_PLUS, CAT_LEXER_TOK_MINUS, CAT_LEXER_TOK_STAR,
|
|
CAT_LEXER_TOK_SLASH, CAT_LEXER_TOK_PERCENT, CAT_LEXER_TOK_CARET,
|
|
CAT_LEXER_TOK_HASH, CAT_LEXER_TOK_EQ, CAT_LEXER_TOK_NE,
|
|
CAT_LEXER_TOK_LE, CAT_LEXER_TOK_GE, CAT_LEXER_TOK_LT,
|
|
CAT_LEXER_TOK_GT, CAT_LEXER_TOK_ASSIGN, CAT_LEXER_TOK_LPAREN,
|
|
CAT_LEXER_TOK_RPAREN, CAT_LEXER_TOK_LBRACE, CAT_LEXER_TOK_RBRACE,
|
|
CAT_LEXER_TOK_LBRACKET, CAT_LEXER_TOK_RBRACKET,
|
|
CAT_LEXER_TOK_SEMI, CAT_LEXER_TOK_COMMA, CAT_LEXER_TOK_DOT,
|
|
CAT_LEXER_TOK_COLON, CAT_LEXER_TOK_DCOLON, CAT_LEXER_TOK_CONCAT,
|
|
CAT_LEXER_TOK_ARROW
|
|
};
|
|
|
|
/* A lexed token: kind + payload + source line. */
|
|
typedef struct CAT_LEXER_Token {
|
|
int kind;
|
|
char text[256]; /* NAME / STRING payload */
|
|
long long ival; /* INT payload */
|
|
double fval; /* FLOAT payload */
|
|
int line;
|
|
} CAT_LEXER_Token;
|
|
|
|
/* CAT_LEXER_Lexer -- cursor over a source buffer. */
|
|
typedef struct CAT_LEXER_Lexer {
|
|
const char *src;
|
|
size_t pos;
|
|
int line;
|
|
} CAT_LEXER_Lexer;
|
|
|
|
void cat_lexer_init(CAT_LEXER_Lexer *lx, const char *src);
|
|
|
|
/* Produce the next token into *out; returns 0 on success. */
|
|
int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out);
|
|
|
|
/* Peek-friendly variant used by the parser: one-token lookahead. */
|
|
const char *cat_lexer_kind_name(int kind);
|
|
|
|
#endif /* CAT_LEXER_H */
|