Browse Source

- Define the Mstate, Maatine, Coroutine and Work struct
- Update some docs

tcheukueppo 2 years ago
parent
commit
ffed4e1b1c
8 changed files with 165 additions and 63 deletions
  1. 0 0
      src/ma_array.h
  2. 0 0
      src/ma_expr.h
  3. 0 0
      src/ma_map.h
  4. 12 0
      src/ma_mvm.h
  5. 63 63
      src/ma_obj.h
  6. 0 0
      src/ma_parser.h
  7. 90 0
      src/ma_state.h
  8. 0 0
      src/ma_thread.h

+ 0 - 0
src/ma_array.h


+ 0 - 0
src/ma_expr.h


+ 0 - 0
src/ma_map.h


+ 12 - 0
src/ma_mvm.h

@@ -0,0 +1,12 @@
+#ifndef ma_mvm_h
+#define ma_mvm_h
+
+#include "ma_conf"
+#include "ma_mstate"
+
+typedef struct MVM {
+
+} MVM;
+
+
+#endif

+ 63 - 63
src/ma_obj.h

@@ -59,7 +59,7 @@
 typedef struct {
 typedef struct {
    Ubyte type;
    Ubyte type;
    union {
    union {
-      struct Header *obj;
+      struct Object *obj;
       Num n;
       Num n;
       CFunc f;
       CFunc f;
       void *p;
       void *p;
@@ -148,34 +148,51 @@ typedef uint64_t Value;
 #endif
 #endif
 
 
 /*
 /*
- * ##Header inherited by all the below objects
+ * ##Object inherited by all the below objects
  * #type: type of the object
  * #type: type of the object
  * #mark: flag to mark the object during collection
  * #mark: flag to mark the object during collection
  * #class: the object's class
  * #class: the object's class
  * #next: next obj,to keep track of all objects
  * #next: next obj,to keep track of all objects
  */
  */
-typedef struct Header {
+typedef struct Object {
    Ubyte type;
    Ubyte type;
    Ubyte mark;
    Ubyte mark;
    struct Class *class;
    struct Class *class;
-   struct Header *next;
-} Header;
+   struct Object *next;
+} Object;
 
 
 /* Test if the value 'v' is an object of type 't' */
 /* Test if the value 'v' is an object of type 't' */
 #define o_check_type(v, t)  is_obj(v) && check_type(v, t)
 #define o_check_type(v, t)  is_obj(v) && check_type(v, t)
 
 
 /* Defining all base objects, each object here may or not have variants */
 /* Defining all base objects, each object here may or not have variants */
 
 
-#define O_TYPE_CLASS     5   /* variant: O_TYPEV_CCLASS */
+/* O_TYPEV_CCLASS, O_TYPEV_ROLE */
+#define O_TYPE_CLASS     5
+
 #define O_TYPE_INSTANCE  6
 #define O_TYPE_INSTANCE  6
-#define O_TYPE_STR       7   /* variants: O_TYPEV_SHTSTR, O_TYPEV_LNGSTR */
+
+/* O_TYPEV_SHTSTR, O_TYPEV_LNGSTR */
+#define O_TYPE_STR       7
+
 #define O_TYPE_RANGE     8    
 #define O_TYPE_RANGE     8    
-#define O_TYPE_ARRAY     9   /* variants: O_TYPEV_SET, O_TYPEV_MSET, O_TYPEV_LIST */
-#define O_TYPE_MAP       10  /* variants: O_TYPEV_BAG, O_TYPEV_MBAG */
-#define O_TYPE_FUN       11  /* variant:  O_TYPEV_CLOSURE */
+
+/* O_TYPEV_SET, O_TYPEV_MSET, O_TYPEV_LIST */
+#define O_TYPE_ARRAY     9
+
+/* O_TYPEV_BAG, O_TYPEV_MBAG */
+#define O_TYPE_MAP       10
+
+/* O_TYPEV_CLOSURE */
+#define O_TYPE_FUN       11
+
 #define O_TYPE_UPVAL     12
 #define O_TYPE_UPVAL     12
-#define O_TYPE_MA        13  /* variants: O_TYPEV_CO, O_TYPEV_WORK */
-#define O_TYPE_RBQ       14  /* variants: O_TYPEV_CHAN, O_TYPEV_SCHEDQ */
+
+/* O_TYPEV_CO, O_TYPEV_GFUN, O_TYPEV_MA, O_TYPEV_WORK */
+#define O_TYPE_MSTATE    13
+
+/* O_TYPEV_CHAN, O_TYPEV_SCHEDQ */
+#define O_TYPE_RBQ       14
+
 #define O_TYPE_REGEX     15
 #define O_TYPE_REGEX     15
 #define O_TYPE_SOCKET    16
 #define O_TYPE_SOCKET    16
 #define O_TYPE_PIPE      17
 #define O_TYPE_PIPE      17
@@ -187,13 +204,6 @@ typedef struct Header {
 #define O_TYPE_NS        23
 #define O_TYPE_NS        23
 #define O_TYPE_TERM      24
 #define O_TYPE_TERM      24
 
 
-/*
- * A special kind of type to mark objects which act as keys to maps as
- * "dead". Dead objects will be resurrected by the program if they are
- * needed before the next GC cycle.
- */
-#define O_TYPE_DEADKEY      31
-
 /* A Check macro on 'v' for each object type */
 /* A Check macro on 'v' for each object type */
 #define is_ma(v)      o_check_type(v, O_TYPE_MA)
 #define is_ma(v)      o_check_type(v, O_TYPE_MA)
 #define is_rbq(v)     o_check_type(v, O_TYPE_RBQ)
 #define is_rbq(v)     o_check_type(v, O_TYPE_RBQ)
@@ -242,7 +252,7 @@ typedef struct Header {
 #define as_lngstr(v)  as_str(v)
 #define as_lngstr(v)  as_str(v)
 
 
 typedef struct Str {
 typedef struct Str {
-   Header obj;
+   Object obj;
    unsigned int hash;
    unsigned int hash;
    size_t rlen;
    size_t rlen;
    size_t len;
    size_t len;
@@ -259,7 +269,7 @@ typedef struct Str {
 #define as_range(v)  ((Range *)as_obj(v))
 #define as_range(v)  ((Range *)as_obj(v))
 
 
 typedef struct {
 typedef struct {
-   Header obj;
+   Object obj;
    Num x;
    Num x;
    Num y;
    Num y;
 } Range;
 } Range;
@@ -302,7 +312,7 @@ typedef struct Node {
 } Node;
 } Node;
 
 
 typedef struct Map {
 typedef struct Map {
-   Header obj;
+   Object obj;
    Value *array;
    Value *array;
    Uint asize;
    Uint asize;
    Ubyte lsize;
    Ubyte lsize;
@@ -316,7 +326,7 @@ typedef struct Map {
  * #capacity: The array's capacity.
  * #capacity: The array's capacity.
  */
  */
 
 
-/* Array Variants: comma-separated list of values, immutable and mutable sets */
+/* Variants: comma-separated list of values, immutable and mutable sets */
 #define O_TYPEV_LIST  vary(O_TYPE_ARRAY, 1)
 #define O_TYPEV_LIST  vary(O_TYPE_ARRAY, 1)
 #define O_TYPEV_SET   vary(O_TYPE_ARRAY, 2)
 #define O_TYPEV_SET   vary(O_TYPE_ARRAY, 2)
 #define O_TYPEV_MSET  vary(O_TYPE_ARRAY, 3)
 #define O_TYPEV_MSET  vary(O_TYPE_ARRAY, 3)
@@ -333,7 +343,7 @@ typedef struct Map {
 #define as_mset(v)   as_array(v)
 #define as_mset(v)   as_array(v)
 
 
 typedef struct Array {
 typedef struct Array {
-   Header obj;
+   Object obj;
    size_t size;
    size_t size;
    size_t capacity;
    size_t capacity;
    Value *array;
    Value *array;
@@ -372,11 +382,6 @@ typedef struct Array {
  * #supers exist mainly for class/object introspection since #c3 handles
  * #supers exist mainly for class/object introspection since #c3 handles
  * super calls.
  * super calls.
  */
  */
-
-/*
- * Cclass-A special way of extending Maat code with C/C++, here we store
- * references to C/C++ functions as methods and structs as attributes.
- */
 #define O_TYPEV_ROLE    vary(O_TYPE_CLASS, 1)
 #define O_TYPEV_ROLE    vary(O_TYPE_CLASS, 1)
 #define O_TYPEV_CCLASS  vary(O_TYPE_CLASS, 2)
 #define O_TYPEV_CCLASS  vary(O_TYPE_CLASS, 2)
 
 
@@ -390,7 +395,7 @@ typedef struct Array {
 #define as_cclass(v)  as_class(v)
 #define as_cclass(v)  as_class(v)
 
 
 typedef struct Class {
 typedef struct Class {
-   Header obj;
+   Object obj;
    Str *name;
    Str *name;
    Map *methods;
    Map *methods;
    union {
    union {
@@ -410,10 +415,10 @@ typedef struct Class {
 /*
 /*
  * ##Instance of a class.
  * ##Instance of a class.
  *
  *
- * #fields: A pointer to a to be allocated array of type "Value", each
- * field has a unique id which corresponds to an index in #fields. This
- * also holds inherited fields.
- * #call_level: Keeps track of the level of super calls for each method called
+ * #fields: A pointer to a to-be-allocated array of values, each field has a
+ * unique id which corresponds to an index in #fields. This also holds
+ * inherited fields.
+ * #c_level: Keeps track of the level of super calls for each method called
  * on the instance, it is 'NULL' if none of the methods of the instance's class
  * on the instance, it is 'NULL' if none of the methods of the instance's class
  * do super calls i.e self.SUPER::<method_name>(...). Where SUPER is a
  * do super calls i.e self.SUPER::<method_name>(...). Where SUPER is a
  * a pseudo-class that resolves to the next class in the instance's class c3
  * a pseudo-class that resolves to the next class in the instance's class c3
@@ -423,19 +428,18 @@ typedef struct Class {
 #define as_instance(v)  ((Instance *)as_obj(v))
 #define as_instance(v)  ((Instance *)as_obj(v))
 
 
 typedef struct Instance {
 typedef struct Instance {
-   Header obj;
+   Object obj;
    Value *fields;
    Value *fields;
-   Map *call_level;
+   Map *c_level;
 } Instance;
 } Instance;
 
 
 /*
 /*
- * ##Represents a namespace: 
- * variables which have to be fully qualified when accessed from outside
- * itself.
+ * ##Represents a namespace, e.g FOO::BAR, a namespace contains variables that
+ * can be access from out using the FQNs, for example "FOO::BAR::meth()" calls
+ * the method "meth" in the namespace "FOO::BAR".
  *
  *
  * #name: Namespace's name.
  * #name: Namespace's name.
  * #ns_val: The namespace value, either a class, role or package.
  * #ns_val: The namespace value, either a class, role or package.
- *
  * #ours: A map for the namespace's "our" variables. For the main::
  * #ours: A map for the namespace's "our" variables. For the main::
  * namespace, #ours takes care of the following type I & II special
  * namespace, #ours takes care of the following type I & II special
  * variables:
  * variables:
@@ -446,13 +450,13 @@ typedef struct Instance {
  * namespaces and as a result these special variables can be accessed
  * namespaces and as a result these special variables can be accessed
  * without using their FQNs.
  * without using their FQNs.
  *
  *
- * #exports: Values for keys of to be exported "our" variables.
+ * #exports: Names of to-be-exported symbols from the namespace when used.
  */
  */
 #define is_ns(v)  o_check_type(v, O_TYPE_NS)
 #define is_ns(v)  o_check_type(v, O_TYPE_NS)
 #define as_ns(v)  ((Namespace *)as_obj(v))
 #define as_ns(v)  ((Namespace *)as_obj(v))
 
 
 typedef struct Namespace {
 typedef struct Namespace {
-   Header obj;
+   Object obj;
    Str *name; 
    Str *name; 
    Map *ours;
    Map *ours;
    Value *exports;
    Value *exports;
@@ -472,7 +476,7 @@ typedef struct Namespace {
 #define as_fun(v)  ((Fun *)as_obj(v))
 #define as_fun(v)  ((Fun *)as_obj(v))
 
 
 typedef struct Fun {
 typedef struct Fun {
-   Header obj;
+   Object obj;
    int up_count;
    int up_count;
    int arity;
    int arity;
    CodeBuf code;
    CodeBuf code;
@@ -498,7 +502,7 @@ typedef struct Fun {
 /* #define next_upval(u)      (u->state.next) */
 /* #define next_upval(u)      (u->state.next) */
 
 
 typedef struct Upval {
 typedef struct Upval {
-   Header obj;
+   Object obj;
    Value *p;
    Value *p;
    union {
    union {
       struct Upval *next;
       struct Upval *next;
@@ -518,30 +522,26 @@ typedef struct Upval {
 #define as_closure(v)  ((Closure *)as_obj(v))
 #define as_closure(v)  ((Closure *)as_obj(v))
 
 
 typedef struct Closure {
 typedef struct Closure {
-   Header obj;
+   Object obj;
    Fun *fun;
    Fun *fun;
    Upvalue *upvals;
    Upvalue *upvals;
 } Closure;
 } Closure;
 
 
 /*
 /*
- * #ip:
- *
- * #temps: For temporarization(Maat's "temp" keyword) of package variables. It
- * is a map with keys corresponding to scope level counts since multiple scopes
- * can temporarize a same package variable, we are using a map because the
- * Array type has an initial capacity which is wastful in this scenario while the
- * Map uses its array part when necessary then optimizes into a real map when
- * scope indexes are sparsed. $temps takes care of the following special
- * variables:
- *   $<digit>(e.g $1, $2, $3, etc), $., $`, $&, and $'
- *
- * It is internally implemented c codes that have to appropiately set these
- * variables in $temps for usage e.g the Regex's method ".gmatch".
  */
  */
-
-typedef struct CallFrame {
-   uint8_t *ip;
-   Map *temps;
-} CallFrame;
+union Ounion {
+   Object obj;
+   struct Str str;
+   struct Array ar;
+   struct Map map;
+   struct Fun fun;
+   struct Closure clo;
+   struct Upval uv;
+   struct Class cls;
+   struct Instance ins;
+   struct MState ms;
+   struct Rbq rbq;
+   struct Namespace ns;
+} Ounion;
 
 
 #endif
 #endif

+ 0 - 0
src/ma_parser.h


+ 90 - 0
src/ma_state.h

@@ -0,0 +1,90 @@
+#ifndef ma_state_h
+#define ma_state_h
+
+#include "ma_conf.h"
+#include "ma_val.h"
+
+/*
+ * ##Representation of a Callframe.
+ *
+ * #ip: instruction pointer which points to the next-to-be-executed instruction.
+ * #closure: The closure associated to that callframe.
+ * #stack: The start of the frame, contains the function's first arg or local.
+ *
+ * #temps: For temporarization(Maat's "temp" keyword) of package variables. It
+ * is a map with keys corresponding to scope level counts since multiple scopes
+ * can temporarize a same package variable, we are using a map because it uses
+ * its array part when necessary then optimizes into a real map when scope
+ * indexes are sparsed. $temps lets some methods declare the following lexically
+ * scoped special variables:
+ *
+ *   $<digit>($1, $2, $3, etc), $., $`, $&, and $'
+ *
+ * It is internally implemented c codes that have to appropiately set these
+ * variables in $temps e.g ".gmatch" Regex method.
+ */
+
+typedef struct CallFrame {
+   uint8_t *ip;
+   Map *temps;
+   Value *start_stack;
+   Closure *closure;
+} CallFrame;
+
+/*
+ * ##An Mstate simple represent a thread of execution,
+ * Maatines(VM-level threads), Coroutines, and generator functions all inherit
+ * an MState.
+ *
+ * #
+ */
+typedef struct MState {
+   Header obj;
+   CallFrame *callstack;
+   size_t cs_cap;
+   size_t cs_size;
+   Upval *upvals;
+   Value *stack;
+   Value *stack_top;
+} MState;
+
+/*
+ * ##VM-level thread, kind-of a context executed by an OS-level thread
+ * #state: Current state of the Maatine
+ * #allobj: List of all allocated object in the current Maatine
+ * #is_work: Is maatine running as a Work?
+ * #num_temps: Number of temporary roots.
+ * #
+ */
+typedef struct Ma {
+   Object obj;
+   MState *mstate;
+   UByte state;
+   struct Work *work;
+   struct MVM *instance;
+   Object *gray;
+   Object *gcount;
+   Object *allobj;
+   Object *temproots;
+   Int num_temps;
+} Ma;
+
+/*
+ * ##A coroutine, it is a blocking VM-level thread that runs on top of a Maatine
+ */
+typedef struct Co {
+   Object obj;
+   UByte state;
+   MState *mstate;
+   Ma *ma;
+   struct Co *caller;
+} Co;
+
+typedef struct Work {
+   Object obj;
+   UByte state;
+   Closure *then;
+   Closure *catch;
+} Work;
+
+#endif

+ 0 - 0
src/ma_thread.h