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) &&
|
||||
|
||||
+5
-1
@@ -18,11 +18,13 @@ struct kw {
|
||||
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 },
|
||||
{ "false", CAT_LEXER_TOK_FALSE },
|
||||
{ "for", CAT_LEXER_TOK_FOR },
|
||||
{ "function", CAT_LEXER_TOK_FUNCTION },
|
||||
@@ -39,6 +41,7 @@ static const struct kw g_keywords[] = {
|
||||
{ "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 },
|
||||
};
|
||||
@@ -261,7 +264,8 @@ const char *cat_lexer_kind_name(int kind)
|
||||
"yield", "try", "except", "raise",
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<",
|
||||
">", "=", "(", ")", "{", "}", "[", "]", ";", ",", ".", ":",
|
||||
"::", "..", "->"
|
||||
"::", "..", "->",
|
||||
"class", "extends", "use"
|
||||
};
|
||||
if (kind >= 0 && kind < (int)(sizeof(names) / sizeof(names[0])))
|
||||
return names[kind];
|
||||
|
||||
@@ -327,6 +327,54 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* `use expr` -- import another catscript source file (alias of dofile),
|
||||
* so `use "std/io/file.cat"` pulls its definitions into this unit. */
|
||||
static int lf_use(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
return lf_dofile(vm, args, nargs, out);
|
||||
}
|
||||
|
||||
/* `__class_new(cls, ...)` -- default constructor installed on every
|
||||
* class as `new`. Copies the class into a fresh instance and runs the
|
||||
* class's `init` method (if any) with the given arguments. */
|
||||
static int lf_class_new(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_VALUE_Value *cls;
|
||||
CAT_VALUE_Value *inst;
|
||||
CAT_VALUE_Value *init = NULL;
|
||||
CAT_ERROR_Error e;
|
||||
int i;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
cls = args[0];
|
||||
inst = cat_types_table_new();
|
||||
/* share the class's methods/layout with the instance */
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(inst, "__index", cls, &e);
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(inst, "__class", cls, &e);
|
||||
/* run init(args...) if the class defines it */
|
||||
cat_error_clear(&e);
|
||||
if (cat_value_get_attribute(cls, "init", &init, &e) == 0 &&
|
||||
init != NULL) {
|
||||
CAT_VALUE_Value *r = NULL;
|
||||
CAT_VALUE_Value *callargs[258];
|
||||
callargs[0] = inst;
|
||||
for (i = 1; i < nargs && i < 257; i++)
|
||||
callargs[i] = args[i];
|
||||
cat_error_clear(&e);
|
||||
cat_value_call(init, callargs, nargs, &r, &e);
|
||||
}
|
||||
*out = inst;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_abstractfile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -375,6 +423,8 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
|
||||
cat_vm_register_native(vm, "chr", lf_chr);
|
||||
cat_vm_register_native(vm, "substr", lf_substr);
|
||||
cat_vm_register_native(vm, "dofile", lf_dofile);
|
||||
cat_vm_register_native(vm, "use", lf_use);
|
||||
cat_vm_register_native(vm, "__class_new", lf_class_new);
|
||||
/* the file family: AbstractFile (abstract base), File, StringIO, BytesIO */
|
||||
cat_vm_set_global(vm, "AbstractFile",
|
||||
cat_types_native_new("AbstractFile", lf_abstractfile));
|
||||
|
||||
@@ -36,6 +36,27 @@ static int table_set_attribute(CAT_VALUE_Value *self, const char *name,
|
||||
return cat_types_table_set(self, name, value, err);
|
||||
}
|
||||
|
||||
/* Calling a table invokes its `new` method with the table as the class
|
||||
* (self). A class built by the `class` syntax installs `new` =
|
||||
* __class_new, so `Animal("cat")` constructs an instance. */
|
||||
static int table_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
CAT_VALUE_Value *ctor = NULL;
|
||||
CAT_VALUE_Value *callargs[258];
|
||||
CAT_ERROR_Error e;
|
||||
int i;
|
||||
|
||||
cat_error_clear(&e);
|
||||
if (cat_value_get_attribute(self, "new", &ctor, &e) != 0 || ctor == NULL)
|
||||
return (cat_error_raise(err,
|
||||
"table is not callable (no 'new')"), -1);
|
||||
callargs[0] = self;
|
||||
for (i = 0; i < nargs && i < 257; i++)
|
||||
callargs[i + 1] = args[i];
|
||||
return cat_value_call(ctor, callargs, nargs + 1, out, err);
|
||||
}
|
||||
|
||||
static void table_destroy(CAT_VALUE_Value *self)
|
||||
{
|
||||
CAT_TYPES_TABLE_Table *t = (CAT_TYPES_TABLE_Table *)self;
|
||||
@@ -53,6 +74,7 @@ static void table_destroy(CAT_VALUE_Value *self)
|
||||
static CAT_VALUE_Vtable g_table_vtable = {
|
||||
.type_name = "table",
|
||||
.kind = CAT_VALUE_KIND_TABLE,
|
||||
.call = { table_call },
|
||||
.get_attribute = { table_get_attribute },
|
||||
.set_attribute = { table_set_attribute },
|
||||
.destroy = table_destroy,
|
||||
|
||||
Reference in new issue
Block a user