Переглянути джерело

- Add more macro functions for operations on value.
- Update `Value` struct to store a function pointer and void pointer used to represent C classes and functions/methods respectively.

tcheukueppo 2 роки тому
батько
коміт
11c1d2601e
1 змінених файлів з 46 додано та 25 видалено
  1. 46 25
      src/ma_obj.h

+ 46 - 25
src/ma_obj.h

@@ -46,13 +46,21 @@
 #else 
 
 /* Different possible types of values */
-#define VAL_NIL   0
-#define VAL_BOOL  1
-#define VAL_NUM   2
-#define VAL_CFUNC 3
+#define TYPE_NIL   0
+#define TYPE_BOOL  1
+#define TYPE_NUM   2
+#define TYPE_CFUNC 3
 
 /*
  * $val: the value itself.
+ *    $n: Maat's representation of a value(configured at 'ma_conf.h').
+ *    $obj: pointer to an object, value is an object.
+ *
+ *    The below attributes are used to extend Maat code with C/C++:
+ *
+ *    $p: pointer to C-class attributes('struct's).
+ *    $f: pointer to a C functions, either represent a method to a
+ *    C-class or just a standalone function.
  *
  * $type: determines val's type, this variable already gather all what is
  * necessary to represent booleans and nils values. It is segmented into
@@ -65,7 +73,7 @@
  *   objects which is sufficient enough.
  *
  * - bits 5-6
- *   to represent variants of certain VAL_* types. You can have at most
+ *   to represent variants of certain TYPE_* types. You can have at most
  *   2 variants since bits 5-6 all set to one tell 'val' is an object.
  *
  * - bit 7
@@ -76,6 +84,8 @@ typedef struct {
    union {
       struct Header *obj;
       Num n;
+      void *p;
+      CFunc f;
    } val;
 } Value;
 
@@ -85,37 +95,48 @@ typedef struct {
 /* Vary type 't' with the variant bits 'v' */
 #define vary(t,v)  ((t) | (v << 5))
 
-#define withvariant(t)     (t.type)
-#define withoutvariant(t)  (t.type & (~OBJ_BITS))
+#define with_variant(v)     (v.type)
+#define without_variant(v)  (v.type & (~OBJ_BITS))
+#define check_type(v,t)     (without_variant(v) == t)
+#define check_vartype(v,t)  (with_variant(v) == t)
+
+#define is_obj(v)    ((with_variant(v) & OBJ_BITS) == OBJ_BITS)
+#define is_num(v)    check_type(v,TYPE_NUM)
+#define is_nil(v)    check_type(v,TYPE_NIL)
+#define is_bool(v)   check_type(v,TYPE_BOOL)
+#define is_cfunc(v)  check_type(v,TYPE_CFUNC)
+
+/* Define the 'nil' value/type with its variants */
+#define NIL     vary(TYPE_NIL, 0)
+#define KEYNIL  vary(TYPE_NIL, 1)
+#define VALNIL  vary(TYPE_NIL, 2)
 
-#define isObj(v)    ((withvariant(v) & OBJ_BITS) == OBJ_BITS)
-#define isNum(v)    (withoutvariant(v) == VAL_NUM)
-#define isNil(v)    (withoutvariant(v) == VAL_NIL)
-#define isBool(v)   (withoutvariant(v) == VAL_BOOL)
-#define isCFunc(v)  (withoutvariant(v) == VAL_CFUNC)
+#define is_strict_nil(v)  check_vartype(v,NIL)
 
-/* Define the 'nil' value with its variants */
-#define NIL     vary(VAL_NIL, 0)
-#define KEYNIL  vary(VAL_NIL, 1)
-#define VALNIL  vary(VAL_NIL, 2)
 
-#define isStrictNil(v)  ()
+/* Define the boolean value/type with its Variants */
+#define BOOL   vary(TYPE_BOOL, 0)
+#define TRUE   vary(TYPE_BOOL, 1)
+#define FALSE  vary(TYPE_BOOL, 2)
 
+#define is_bool(v)   check_type(v,TYPE_BOOL)
+#define is_false(v)  check_vartype(to_bool(v),FALSE)
+#define is_true(v)   check_vartype(to_bool(v),TRUE)
 
-/* Variants of a boolean value */
-#define BOOL     vary(VAL_BOOL, 0) /* This will be our bool object */
-#define TRUE     vary(VAL_BOOL, 1)
-#define FALSE    vary(VAL_BOOL, 2)
+#define to_bool  (is_bool(v) ? v : coerce_to_bool(v))
 
-#define isFalse
-#define isTrue
+#define as_bool(v)   (is_true(v))
+#define as_num(v)    (v.val.n)
+#define as_obj(v)    (v.val.obj)
+#define as_cfunc(v)  (v.val.f)
 
-#define IS_COLLECTABLE(v)
+#define COLLECTABLE_BIT    (0b1 << 7)
+#define is_collectable(v)  (with_variant(v) & COLLECTABLE_BIT)
 
 #endif
 
 /*
- * Header to all objects
+ * struct Header inherited by all objects
  * $type: type of the object
  * $Class: the object's class
  * $next: next obj, to keep track of all objects