std/ tree, use (import) and class syntax
- std/ layout: std/lang/{lexer,parser,checker,compiler}.cat moved from src/catscript
- std/io/io.cat, std/fs/fs.cat, std/math/math.cat modules
- use "path.cat": import another unit (compile+run in the same VM)
- class NAME [extends BASE] ... end: methods, inheritance via __index,
default new constructor (runs optional init), tables are callable
- examples/classes.cat, tests/std.cat; 48/48 tests
This commit is contained in:
1 parent
3c20496c78
commit
ef305e7824
19 files changed
+370
-9
No files matched your search
@@ -742,6 +742,79 @@ static void stat(Compiler *c)
|
||||
break;
|
||||
}
|
||||
|
||||
case CAT_LEXER_TOK_USE:
|
||||
/* `use expr` imports another catscript source file: it reads,
|
||||
* compiles and runs that file in the current VM (like dofile),
|
||||
* so its definitions become available here. */
|
||||
next(c);
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, "use"), 0, line);
|
||||
expr(c);
|
||||
emit(c, CAT_BYTECODE_OP_CALL, 1, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_POP, 0, 0, line);
|
||||
accept(c, CAT_LEXER_TOK_SEMI);
|
||||
break;
|
||||
|
||||
case CAT_LEXER_TOK_CLASS: {
|
||||
/* class NAME [extends BASE] <methods> end
|
||||
*
|
||||
* Builds a class table: each `function m(...) end` becomes a method
|
||||
* on the table, `__index` points at the base (so instances inherit),
|
||||
* and a default `new` constructor fills an instance and runs the
|
||||
* class's `init` if present. The table is bound to a global NAME. */
|
||||
char name[256];
|
||||
char base[256];
|
||||
next(c);
|
||||
name[0] = '\0';
|
||||
base[0] = '\0';
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
snprintf(name, sizeof(name), "%s", c->tok.text);
|
||||
next(c);
|
||||
}
|
||||
if (accept(c, CAT_LEXER_TOK_EXTENDS)) {
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
snprintf(base, sizeof(base), "%s", c->tok.text);
|
||||
next(c);
|
||||
}
|
||||
}
|
||||
emit(c, CAT_BYTECODE_OP_NEWTABLE, 0, 0, line);
|
||||
/* __index: base (inheritance) or the class itself */
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
if (base[0] != '\0')
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, base), 0, line);
|
||||
else
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR, add_string(c, "__index"), 0, line);
|
||||
/* default constructor `new` (a user-defined `new` overrides it) */
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, "__class_new"), 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR, add_string(c, "new"), 0, line);
|
||||
/* methods */
|
||||
while (!check(c, CAT_LEXER_TOK_END) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
if (!check(c, CAT_LEXER_TOK_FUNCTION)) {
|
||||
err_at(c, c->tok.line, "expected 'function' or 'end' in class");
|
||||
break;
|
||||
}
|
||||
next(c);
|
||||
{
|
||||
char mname[256];
|
||||
int proto;
|
||||
mname[0] = '\0';
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
snprintf(mname, sizeof(mname), "%s", c->tok.text);
|
||||
next(c);
|
||||
}
|
||||
proto = compile_function_literal(c, mname);
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_CLOSURE, proto, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR, add_string(c, mname), 0, line);
|
||||
}
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_END, "'end'");
|
||||
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
||||
break;
|
||||
}
|
||||
|
||||
case CAT_LEXER_TOK_RETURN:
|
||||
next(c);
|
||||
if (!check(c, CAT_LEXER_TOK_END) && !check(c, CAT_LEXER_TOK_EOF) &&
|
||||
|
||||
Reference in new issue
Block a user