generators/iterators, raise/try/except, and slot-based object layout
- VM refactor: resumable CAT_VM_Frame (ip/sp/locals/handlers) - yield/next/generators: calling a yield-function returns a generator - for..in over any iterator, ends on explicit exhaustion status - raise/try/except: structured error values, nearest-handler unwinding - slots: C-declared member layout (offsetof), PyTypeObject/tp_members style - File now exposes path/mode/closed via slots - examples: generators.cat, errors.cat, slots.cat; 40/40 tests
This commit is contained in:
1 parent
52d118211f
commit
3c20496c78
19 files changed
+978
-99
No files matched your search
+130
-9
@@ -10,8 +10,15 @@
|
||||
*/
|
||||
#include "cat/value.h"
|
||||
#include "cat/error.h"
|
||||
#include "cat/types/nil.h"
|
||||
#include "cat/types/bool.h"
|
||||
#include "cat/types/int.h"
|
||||
#include "cat/types/float.h"
|
||||
#include "cat/types/text.h"
|
||||
#include "cat/types/ascii_text.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *cat_value_type_name(CAT_VALUE_Value *v)
|
||||
@@ -83,20 +90,44 @@ int cat_value_call(CAT_VALUE_Value *fn, CAT_VALUE_Value **args, int nargs,
|
||||
int cat_value_get_attribute(CAT_VALUE_Value *self, const char *name,
|
||||
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
if (vt(self) == NULL || vt(self)->get_attribute.fn == NULL)
|
||||
return (cat_error_raise(err,
|
||||
"type %s has no attributes", cat_value_type_name(self)), -1);
|
||||
return vt(self)->get_attribute.fn(self, name, out, err);
|
||||
const CAT_VALUE_Slot *s;
|
||||
|
||||
if (vt(self) != NULL && vt(self)->get_attribute.fn != NULL &&
|
||||
vt(self)->get_attribute.fn(self, name, out, err) == 0)
|
||||
return 0;
|
||||
/* The type's own handler declined; clear any error it left behind
|
||||
* before trying the C-known member layout (slots). */
|
||||
if (err != NULL)
|
||||
cat_error_clear(err);
|
||||
s = cat_value_slot_find(self, name);
|
||||
if (s != NULL && cat_value_slot_get(self, s, out, err) == 0) {
|
||||
if (err != NULL)
|
||||
cat_error_clear(err);
|
||||
return 0;
|
||||
}
|
||||
return (cat_error_raise(err,
|
||||
"type %s has no attribute '%s'", cat_value_type_name(self), name), -1);
|
||||
}
|
||||
|
||||
int cat_value_set_attribute(CAT_VALUE_Value *self, const char *name,
|
||||
CAT_VALUE_Value *value, CAT_ERROR_Error *err)
|
||||
{
|
||||
if (vt(self) == NULL || vt(self)->set_attribute.fn == NULL)
|
||||
return (cat_error_raise(err,
|
||||
"type %s has no settable attributes",
|
||||
cat_value_type_name(self)), -1);
|
||||
return vt(self)->set_attribute.fn(self, name, value, err);
|
||||
const CAT_VALUE_Slot *s;
|
||||
|
||||
if (vt(self) != NULL && vt(self)->set_attribute.fn != NULL &&
|
||||
vt(self)->set_attribute.fn(self, name, value, err) == 0)
|
||||
return 0;
|
||||
if (err != NULL)
|
||||
cat_error_clear(err);
|
||||
s = cat_value_slot_find(self, name);
|
||||
if (s != NULL && cat_value_slot_set(self, s, value, err) == 0) {
|
||||
if (err != NULL)
|
||||
cat_error_clear(err);
|
||||
return 0;
|
||||
}
|
||||
return (cat_error_raise(err,
|
||||
"type %s has no settable attribute '%s'",
|
||||
cat_value_type_name(self), name), -1);
|
||||
}
|
||||
|
||||
int cat_value_truthy(CAT_VALUE_Value *v, int *out, CAT_ERROR_Error *err)
|
||||
@@ -133,3 +164,93 @@ int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out,
|
||||
(void)r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cat_value_next(CAT_VALUE_Value *v, CAT_VALUE_Value **out,
|
||||
CAT_ERROR_Error *err)
|
||||
{
|
||||
if (vt(v) == NULL || vt(v)->next.fn == NULL)
|
||||
return (cat_error_raise(err,
|
||||
"type %s is not iterable", cat_value_type_name(v)), -1);
|
||||
return vt(v)->next.fn(v, out, err);
|
||||
}
|
||||
|
||||
const CAT_VALUE_Slot *cat_value_slot_find(CAT_VALUE_Value *v, const char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (vt(v) == NULL || vt(v)->slots == NULL)
|
||||
return NULL;
|
||||
for (i = 0; i < vt(v)->nslots; i++)
|
||||
if (strcmp(vt(v)->slots[i].name, name) == 0)
|
||||
return &vt(v)->slots[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int cat_value_slot_get(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s,
|
||||
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
char *base = (char *)v;
|
||||
|
||||
switch (s->kind) {
|
||||
case CAT_VALUE_SLOT_VALUE:
|
||||
*out = *(CAT_VALUE_Value **)(base + s->offset);
|
||||
break;
|
||||
case CAT_VALUE_SLOT_NUMBER:
|
||||
*out = cat_types_int_new(*(long long *)(base + s->offset));
|
||||
break;
|
||||
case CAT_VALUE_SLOT_DOUBLE:
|
||||
*out = cat_types_float_new(*(double *)(base + s->offset));
|
||||
break;
|
||||
case CAT_VALUE_SLOT_TEXT: {
|
||||
char *p = *(char **)(base + s->offset);
|
||||
*out = cat_types_ascii_text_new(p ? p : "");
|
||||
break;
|
||||
}
|
||||
case CAT_VALUE_SLOT_BOOL:
|
||||
*out = (*(int *)(base + s->offset)) ? cat_types_bool_true()
|
||||
: cat_types_bool_false();
|
||||
break;
|
||||
default:
|
||||
*out = cat_types_nil_get();
|
||||
break;
|
||||
}
|
||||
if (*out == NULL)
|
||||
return (cat_error_raise(err, "slot '%s' unreadable", s->name), -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cat_value_slot_set(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s,
|
||||
CAT_VALUE_Value *value, CAT_ERROR_Error *err)
|
||||
{
|
||||
char *base = (char *)v;
|
||||
|
||||
if (s->readonly)
|
||||
return (cat_error_raise(err, "slot '%s' is read-only", s->name), -1);
|
||||
switch (s->kind) {
|
||||
case CAT_VALUE_SLOT_VALUE:
|
||||
*(CAT_VALUE_Value **)(base + s->offset) = value;
|
||||
break;
|
||||
case CAT_VALUE_SLOT_NUMBER:
|
||||
*(long long *)(base + s->offset) = cat_types_number_as_int(value);
|
||||
break;
|
||||
case CAT_VALUE_SLOT_DOUBLE:
|
||||
*(double *)(base + s->offset) = cat_types_number_as_double(value);
|
||||
break;
|
||||
case CAT_VALUE_SLOT_TEXT: {
|
||||
const char *p = cat_types_text_bytes(value);
|
||||
char **dst = (char **)(base + s->offset);
|
||||
free(*dst);
|
||||
*dst = p ? strdup(p) : NULL;
|
||||
break;
|
||||
}
|
||||
case CAT_VALUE_SLOT_BOOL: {
|
||||
int t = 1;
|
||||
cat_value_truthy(value, &t, err);
|
||||
*(int *)(base + s->offset) = t;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user