Browse Source

1. define the representation of values when nan-tagging is disabled
2. define data types `Byte`, `UByte` and `Num`

tcheukueppo 2 years ago
parent
commit
b483d5b78a
2 changed files with 140 additions and 63 deletions
  1. 24 13
      src/ma_conf.h
  2. 116 50
      src/ma_obj.h

+ 24 - 13
src/ma_conf.h

@@ -19,11 +19,11 @@
 #define <limits.h>
 #define <stddef.h>
 
-/* $$Executable interpreter name */
+/*$$ Executable interpreter name */
 #define MAAT  maat MA_VERSION
 
-/*
- * $$Macros used to enable some platform specific features. Either
+/*$$
+ * Macros used to enable some platform specific features. Either
  * MA_IN_LINUX, MA_IN_MACOSX or MA_IN_IOS is defined in the Makefile
  * during the build process.
  *
@@ -51,8 +51,8 @@
 #define MA_USE_DLOPEN
 #endif
 
-/*
- * $$Configuring dir separator and default paths for Maat and external libs
+/*$$
+ * Configuring dir separator and default paths for Maat and external libs
  */
 
 /* On windows? */
@@ -94,7 +94,7 @@
 
 #endif
 
-/* $$Some macros which acts as utility functions */
+/*$$ Some macros which acts as utility functions */
 
 /* Get local radix character (decimal point) */
 #if !defined(mt_getradixchar)
@@ -118,7 +118,12 @@
 
 #endif
 
-/* $$Define attributes to mark symbols during their declarations/definitions */
+#if !defined(ma_assert)
+#define ma_assert
+#define ma_longassert
+#endif
+
+/*$$ Define attributes to mark symbols during their declarations/definitions */
 
 #if defined(_WIN32) || defined(__CYGWIN__)
 
@@ -150,14 +155,13 @@
  * Without this attribute, check with `nm libmaat.so | grep ' T ' | wc -l` to see
  * how the number of dynamic symbols increased.
  */
-
 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 302) && defined(__ELF__)
 #define MA_IFUNC  __attribute__ ((visibility("internal")))
 #elif
 #define MA_IFUNC  /* extern by default */
 #endif
 
-/* $$Configs for inline functions */
+/*$$ Configs for inline functions */
 
 #if defined(__GNUC__)
 #define ma_inline  __inline__
@@ -167,10 +171,17 @@
 
 #define ma_sinline  static ma_inline
 
-/* $$NAN_BOXING to optimize value representation */
+/*$$ Data type configuration */
 
-#if !defined(MA_NAN_TAGGING)
-#define MA_NAN_TAGGING
-#endif
+#define Ubyte          unsigned char
+#define Byte           signed char
+#define Num            double
+#define ma_number_fmt  "%.14g"
+
+/*$$
+ * MA_NAN_TAGGING is an optimization technique for value representation.
+ * You either define it here or with the '-D' option during compilation.
+ */
+/*#define MA_NAN_TAGGING */
 
 #endif

+ 116 - 50
src/ma_obj.h

@@ -6,55 +6,122 @@
 #ifndef ma_obj_h
 #define ma_obj_h
 
-#define unsigned char u_byte
-
-typedef enum {
-   Bool, Num, Str,
-   Range, Array, Map,
-   Set, MSet, Bag,
-   MBag, Fun, GFun,
-   Regex, Ma, Chan,
-   Pipe, File, Dir,
-   Date, Sys, Socket,
-   Proc, Term,
-} type;
-
-#define isBool
-#define isNum
-#define isStr
-#define isRange
-#define isArray
-#define isMap
-#define isSet
-#define isMSet
-#define isBag
-#define isMBag
-#define isFun
-#define isGFun
-#define isRegex
-#define isMa
-#define isChan
-#define isPipe
-#define isFile
-#define isDir
-#define isDate
-#define isSys
-#define isSocket
-#define isProc
-#define isTerm
+#include "ma_conf.h"
 
+/*
+ * The struct `Class` is also an object which
+ * is collectable.
+ */
+#define Class
+#define Bool
+#define Num
+#define Str
+#define Range
+#define Array
+#define Map
+#define Set
+#define MSet
+#define Bag
+#define MBag
+#define Fun
+#define GFun
+#define Ma
+#define Work
+#define Chan
+#define Regex
+#define Socket
+#define Pipe
+#define File
+#define Dir
+#define Proc
+#define Sys
+#define Date
+#define Term
+
+/*
+ * Representing a value
+ */
+#if defined(MA_NAN_TAGGING)
+
+#else 
 
+/* Different possible types of values */
+#define VAL_NIL   0
+#define VAL_BOOL  1
+#define VAL_NUM   2
+#define VAL_CFUNC 3
 
 /*
- * Header of all objects
- * $type: type of the object
- * $obj_class: class to which the object belongs
- * $next: next obj to keep track of all objects
- * $class: the object's class
- * $marked: object marked for collection?
+ * $val: the value itself.
+ *
+ * $type: determines val's type, this variable already gather all what is
+ * necessary to represent booleans and nils values. It is segmented into
+ * three parts. Bits distribution:
+ *
+ * - bits 0-4
+ *   to represent the different types of values except as objects. 'val'
+ *   is an object when bits 5-6 are set to 1, if 'val' is an object then
+ *   bits 0-4 determine its object's type. This gives us a maximum of 32
+ *   objects which is sufficient enough.
+ *
+ * - bits 5-6
+ *   to represent variants of certain VAL_* types. You can have at most
+ *   2 variants since bits 5-6 all set to one tell 'val' is an object.
+ *
+ * - bit 7
+ *   Whether 'val' is collectable(1) or not(0).
  */
 typedef struct {
-   u_byte type;
+   Ubyte type;
+   union {
+      struct Header *obj;
+      Num n;
+   } val;
+} Value;
+
+
+#define OBJ_BITS  (0b11 << 5)
+
+/* 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 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 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)  ()
+
+
+/* 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 isFalse
+#define isTrue
+
+#define IS_COLLECTABLE(v)
+
+#endif
+
+/*
+ * Header to all objects
+ * $type: type of the object
+ * $Class: the object's class
+ * $next: next obj, to keep track of all objects
+ */
+typedef struct Header {
+   Ubyte type;
    struct Class *class;
    struct Header *next;
 } Header;
@@ -65,10 +132,10 @@ typedef struct {
  * $length: length of $str
  * $str: the string itself
  */
-typedef struct {
+typedef struct Str {
    Header obj;
    unsigned int hash;
-   size_t length;
+   long double length;
    char str[1];
 } Str;
 
@@ -77,15 +144,15 @@ typedef struct {
  * $from: from (inclusive)
  * $to: to (inclusive)
  */
-typedef struct {
+typedef struct Range {
    Header obj;
    double from;
    double to;
 } Range;
 
-typedef struct {
+typedef struct Map {
    Header obj;
-   
+   Value *
 } Map;
 
 /*
@@ -96,7 +163,7 @@ typedef struct {
  * $attributes: attributes
  * $methods: a map for methods
  */
-typedef struct {
+typedef struct Class {
    Header obj;
    struct Class **sup;
    Str *name;
@@ -107,6 +174,5 @@ typedef struct {
 
 
 
-
 #endif