ma_obj.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Abstract: Type definitions for Maat objects
  3. * Lisenced under AGL, see LICENSE file for copyright and license details.
  4. */
  5. #ifndef ma_obj_h
  6. #define ma_obj_h
  7. #include "ma_conf.h"
  8. /*
  9. * The struct `Class` is also an object which
  10. * is collectable.
  11. */
  12. #define Class
  13. #define Bool
  14. #define Num
  15. #define Str
  16. #define Range
  17. #define Array
  18. #define Map
  19. #define Set
  20. #define MSet
  21. #define Bag
  22. #define MBag
  23. #define Fun
  24. #define GFun
  25. #define Ma
  26. #define Work
  27. #define Chan
  28. #define Regex
  29. #define Socket
  30. #define Pipe
  31. #define File
  32. #define Dir
  33. #define Proc
  34. #define Sys
  35. #define Date
  36. #define Term
  37. /*
  38. * Representing a value
  39. */
  40. #if defined(MA_NAN_TAGGING)
  41. #else
  42. /* Different possible types of values */
  43. #define TYPE_NIL 0
  44. #define TYPE_BOOL 1
  45. #define TYPE_NUM 2
  46. #define TYPE_CFUNC 3
  47. /*
  48. * $val: the value itself.
  49. * $n: Maat's representation of a value(configured at 'ma_conf.h').
  50. * $obj: pointer to an object, value is an object.
  51. *
  52. * The below attributes are used to extend Maat code with C/C++:
  53. *
  54. * $p: pointer to C-class attributes('struct's).
  55. * $f: pointer to a C functions, either represent a method to a
  56. * C-class or just a standalone function.
  57. *
  58. * $type: determines val's type, this variable already gather all what is
  59. * necessary to represent booleans and nils values. It is segmented into
  60. * three parts. Bits distribution:
  61. *
  62. * - bits 0-4
  63. * to represent the different types of values except as objects. 'val'
  64. * is an object when bits 5-6 are set to 1, if 'val' is an object then
  65. * bits 0-4 determine its object's type. This gives us a maximum of 32
  66. * objects which is sufficient enough.
  67. *
  68. * - bits 5-6
  69. * to represent variants of certain TYPE_* types. You can have at most
  70. * 2 variants since bits 5-6 all set to one tell 'val' is an object.
  71. *
  72. * - bit 7
  73. * Whether 'val' is collectable(1) or not(0).
  74. */
  75. typedef struct {
  76. Ubyte type;
  77. union {
  78. struct Header *obj;
  79. Num n;
  80. void *p;
  81. CFunc f;
  82. } val;
  83. } Value;
  84. #define OBJ_BITS (0b11 << 5)
  85. /* Vary type 't' with the variant bits 'v' */
  86. #define vary(t,v) ((t) | (v << 5))
  87. #define with_variant(v) (v.type)
  88. #define without_variant(v) (v.type & (~OBJ_BITS))
  89. #define check_type(v,t) (without_variant(v) == t)
  90. #define check_vartype(v,t) (with_variant(v) == t)
  91. #define is_obj(v) ((with_variant(v) & OBJ_BITS) == OBJ_BITS)
  92. #define is_num(v) check_type(v,TYPE_NUM)
  93. #define is_nil(v) check_type(v,TYPE_NIL)
  94. #define is_bool(v) check_type(v,TYPE_BOOL)
  95. #define is_cfunc(v) check_type(v,TYPE_CFUNC)
  96. /* Define the 'nil' value/type with its variants */
  97. #define NIL vary(TYPE_NIL, 0)
  98. #define KEYNIL vary(TYPE_NIL, 1)
  99. #define VALNIL vary(TYPE_NIL, 2)
  100. #define is_strict_nil(v) check_vartype(v,NIL)
  101. /* Define the boolean value/type with its Variants */
  102. #define BOOL vary(TYPE_BOOL, 0)
  103. #define TRUE vary(TYPE_BOOL, 1)
  104. #define FALSE vary(TYPE_BOOL, 2)
  105. #define is_bool(v) check_type(v,TYPE_BOOL)
  106. #define is_false(v) check_vartype(to_bool(v),FALSE)
  107. #define is_true(v) check_vartype(to_bool(v),TRUE)
  108. #define to_bool (is_bool(v) ? v : coerce_to_bool(v))
  109. #define as_bool(v) (is_true(v))
  110. #define as_num(v) (v.val.n)
  111. #define as_obj(v) (v.val.obj)
  112. #define as_cfunc(v) (v.val.f)
  113. #define COLLECTABLE_BIT (0b1 << 7)
  114. #define is_collectable(v) (with_variant(v) & COLLECTABLE_BIT)
  115. #endif
  116. /*
  117. * struct Header inherited by all objects
  118. * $type: type of the object
  119. * $Class: the object's class
  120. * $next: next obj, to keep track of all objects
  121. */
  122. typedef struct Header {
  123. Ubyte type;
  124. struct Class *class;
  125. struct Header *next;
  126. } Header;
  127. /*
  128. * $obj: header
  129. * $hash: hash value of $str
  130. * $length: length of $str
  131. * $str: the string itself
  132. */
  133. typedef struct Str {
  134. Header obj;
  135. unsigned int hash;
  136. long double length;
  137. char str[1];
  138. } Str;
  139. /*
  140. * $obj: header
  141. * $from: from (inclusive)
  142. * $to: to (inclusive)
  143. */
  144. typedef struct Range {
  145. Header obj;
  146. double from;
  147. double to;
  148. } Range;
  149. typedef struct Map {
  150. Header obj;
  151. Value *
  152. } Map;
  153. /*
  154. * $obj: header
  155. * $sup: class' superclasses (C3 mro)
  156. * $name: class name
  157. * $nfields: number of fields
  158. * $attributes: attributes
  159. * $methods: a map for methods
  160. */
  161. typedef struct Class {
  162. Header obj;
  163. struct Class **sup;
  164. Str *name;
  165. int nfields;
  166. Map **methods;
  167. Value *attributes;
  168. } Class;
  169. #endif