prototype IR and parser
authorjordan@hack_attack <jordanlavatai@gmail.com>
Thu, 22 Sep 2016 01:10:44 +0000 (18:10 -0700)
committerjordan@hack_attack <jordanlavatai@gmail.com>
Thu, 22 Sep 2016 01:10:44 +0000 (18:10 -0700)
src/apc/ir.c
src/apc/ir.h
src/apc/parser.y

index c5c4125..4a8486f 100644 (file)
 #include <errno.h>
 #include <stdio.h>
-#include <string.h>
-#include <apc/ir.h>
+#include <stdint.h> //uint64_t
+#include <string.h> //memmove
+#include <stdlib.h> //malloc
+#include "ir.h"
+
+#define CURR_CDAT (*cdat_stackp)
+#define CURR_SET set_list[CURR_CDAT->num_sets]
+#define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
+#define PREV_REF (ref_buf[num_refs-1])
+#define CURR_REF (ref_buf[num_refs])
+#define PREV_ODAT (odat_buf[num_odats-1])
+#define CURR_ODAT (odat_buf[num_odats])
+#define CURR_VDAT (vdat_buf[num_vdats])
+#define PREV_VDAT (vdat_buf[num_vdats-1])
+#define CURR_MODEL model_list[CURR_VDAT->num_models]
+#define CURR_LINK (link_buf[num_links])
 
-#define CURR_ODAT (odat_buf[obi])
-#define CURR_VDAT (vdat_buf[vbi])
-#define CURR_CDAT (cdat_buf[cbi]) //more like the base cdat
 
-//TODO: label and vdat_id
 void
-insert_set()
+ir_init()
 {
-  CURR_CDAT.set_list[CURR_CDAT.set_index].odat_id = obi;
-  CURR_CDAT.set_list[CURR_CDAT.set_index].parent_id = 0;//from lexer
-  //TODO: add ele_stack is created in element_list
-  //TODO: add set to odat_buf
+  /* Init root cdat and stack */
+  char root[4] = "root";
+
+  cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof(struct cdat) );
+  cdat_buf[num_cdats]->idx = 0;
+  memmove(cdat_buf[num_cdats]->name, root, 4);
+
+  cdat_stackp = cdat_stack;
+  *cdat_stackp = cdat_buf[num_cdats++];
 
+  /* Init first odat */
+  if( (CURR_ODAT = (struct odat*) malloc(sizeof(struct odat))) == NULL)
+    perror("malloc first odat failed");
+
+  /* init first vdat*/
+  if( (CURR_VDAT = (struct vdat*) malloc(sizeof(struct vdat))) == NULL)
+    perror("malloc first vdat failed");
+
+  /* Init first ref */
+  if( (CURR_REF = (struct ref*)  malloc(sizeof(struct ref))) == NULL)
+    perror("malloc first ref failed");
+
+  /* Init first link */
+  if( (CURR_LINK = (struct link*) malloc(sizeof(struct link))) == NULL)
+    perror("malloc first link failed");
 }
 
-#define CURR_QUAD (CURR_ODAT.ref_list[CURR_ODAT.ref_index])
+//TODO: FREE MEMORY!
 void
-insert_ref(int x, int y, int z, int ref)
+malloc_cdat()
 {
-  CURR_QUAD.x = x;
-  CURR_QUAD.y = y;
-  CURR_QUAD.z = z;
-  CURR_QUAD.objref = ref;
+
+  if(curr_max_cdats <= num_cdats)
+    {
+      if( (realloc((void*) cdat_buf, PTRS_IN_PAGE * 4)) == NULL)
+        perror("realloc cdat_buf failed");
+      curr_max_cdats += PTRS_IN_PAGE;
+      if( (realloc( (void*) cdat_stack, PTRS_IN_PAGE * 4)) == NULL) //increase cdat_stack also
+        perror("realloc cdat_stack failed");
+    }
+  if( (cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof (struct cdat)) ) == NULL )
+    perror("malloc cdat failed");
+
+
 }
 
+/* Dynamically allocate memory for a class data structure,
+   or cdat, after a class has been identified in a grammar.
+   We also create a stack of class pointers so that
+   we can access the cdat during processing of that
+   cdats sets and elements, a requirement because the
+   nature of recursive classes prevents us from accessing
+   the cdat based on the previous index into cdat_buf,
+   which is a list of all allocated cdats*/
+void
+push_cdat(char* name)
+{
+
+  malloc_cdat();
+
+  memmove(cdat_buf[num_cdats]->name, name, 32);
+  cdat_buf[num_cdats]->idx = num_cdats;
+
+  /* Set the cdat as a class of the previous cdat */
+  (*cdat_stackp)->class_list[(*cdat_stackp)->num_classes++] = cdat_buf[num_cdats];
+
+  /* Push the cdat onto the cdat_stack */
+  *++cdat_stackp = cdat_buf[num_cdats++];
+
+}
+
+void
+pop_cdat()
+{
+  *cdat_stackp = NULL;
+  cdat_stackp--;
+}
+
+void
+inc_odat()
+{
+  num_odats++;
+  if(num_odats >= curr_max_odats)
+    {
+      if( (realloc((void*) odat_buf, PTRS_IN_PAGE * 4)) == NULL)
+        perror("realloc odat_buf failed");
+      curr_max_odats += PTRS_IN_PAGE;
+    }
+  if( (CURR_ODAT = (struct odat*) malloc(sizeof (struct odat))) == NULL)
+    {
+      perror("malloc odat failed");
+    }
+
+}
+
+void
+inc_vdat()
+{
+  num_vdats++;
+  if(num_vdats >= curr_max_vdats)
+    {
+      if( (realloc((void*) vdat_buf, PTRS_IN_PAGE * 4)) == NULL)
+        perror("realloc vdat_buf failed");
+      curr_max_vdats += PTRS_IN_PAGE;
+    }
+  if((CURR_VDAT = (struct vdat*) malloc(sizeof (struct vdat))) == NULL)
+    {
+      perror("malloc vdat failed");
+    }
+
+}
+
+void
+inc_link()
+{
+  num_links++;
+  if(num_links >= curr_max_links)
+    {
+      if( (realloc((void*) link_buf, PTRS_IN_PAGE * 4)) == NULL)
+        perror("realloc vdat_buf failed");
+      curr_max_links += PTRS_IN_PAGE;
+    }
+  if((CURR_LINK = (struct link*) malloc(sizeof (struct link))) == NULL)
+    {
+      perror("malloc link failed");
+    }
+}
+
+void
+inc_ref()
+{
+
+  if(num_refs % 16 == 0)
+    {
+      posts[num_posts++] = *CURR_REF;
+    }
+
+  num_refs++;
+  if(num_refs >= curr_max_refs)
+    {
+      if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
+        perror("realloc ref_buf failed");
+      curr_max_refs += PTRS_IN_PAGE;
+    }
+  if((CURR_REF = (struct ref*) malloc(sizeof (struct ref))) == NULL)
+    perror("malloc ref failed");
+}
 
-//Insert element into odat_buf and cdat_buf
 void
-insert_ele(char* label, int vdat_id)
+insert_set_label(char* name, uint64_t ref_id)
 {
 
 
-  memmove(CURR_ODAT.label,label,32);
-  CURR_ODAT.vdat_id = vdat_id;
-  //TODO: check set_obi to see if set_map_data exists
-  //comes from e
-  //CURR_ODAT.num_ref = //comes from its set
-  //CURR_ODAT.ref_list = //comes from its set
-  //CURR_ODAT.class_id = //comes from lexer?
+  memmove(CURR_CDAT->CURR_SET.name,name,32);
+  memmove(&CURR_CDAT->CURR_SET.ref_id,&ref_id,64);
 
-  obi++;
 }
+void
+insert_set_olink(uint64_t ref_id)
+{
+  CURR_CDAT->CURR_SET.cdat_idx = CURR_CDAT->idx;
+  CURR_CDAT->CURR_SET.ref_id = ref_id; /* Will be resolved to offset
+                                          when link is processed */
+  CURR_LINK->type = 1;
+  CURR_LINK->link_t.olink.ref_id = ref_id;
+  CURR_LINK->cdat_idx = CURR_CDAT->idx;
+  CURR_LINK->set_idx = CURR_CDAT->num_sets++;
+  CURR_LINK->ele_idx = -1;
 
+  inc_link();
+}
 
-/* fd could be a directory entry */
 void
-insert_fdat(char* label, char direction, int fd)
+insert_set_vlink(uint64_t ref_id, char* anim_name)
 {
-  memmove(CURR_VDAT.model_list[CURR_VDAT.num_models].label,label,32);
-  CURR_VDAT.model_list[CURR_VDAT.num_models].fdat_id[(int)direction] = fd;
+  /* Insert vlink into link_stack so that it gets processed at
+     output time */
+  CURR_LINK->cdat_idx = CURR_CDAT->idx;
+  CURR_LINK->set_idx = CURR_CDAT->num_sets;
+  CURR_LINK->type = 2;
+  CURR_LINK->link_t.vlink.ref_id = ref_id;
+  memmove(CURR_LINK->link_t.vlink.anim_name, anim_name, 32);
+
+
 }
 
 void
-condense()
+insert_set_svlink(uint64_t ref_id)
 {
-  FILE *vp, *op, *cp;
-  int v, m;
-  int num_models;
 
-  vp = fopen("vdat_output", "w+");
-  if(!vp)
-    perror("vdat_output failed to open\n");
+  /* Insert vlink into link_stack so that it gets processed at
+     output time */
+  CURR_LINK->cdat_idx = CURR_CDAT->idx;
+  CURR_LINK->set_idx = CURR_CDAT->num_sets;
+  CURR_LINK->type = 3;
+  CURR_LINK->link_t.svlink.ref_id = ref_id;
 
-  op = fopen("odat_output", "w+");
-  if(!op)
-    perror("odat_output failed to open\n");
+}
 
-  cp = fopen("cdat_output", "w+");
-  if(!cp)
-    perror("cdat_output failed to open\n");
+/* At the point of reducing to a set, most of the
+   sets odat information has already been populated
+   during the reduction of its right hand side
+   non terminals (hitbox, root, quad_list). */
+void
+insert_set()
+{
+  uint64_t ref_id;
 
+  ref_id = CURR_CDAT->CURR_SET.ref_id;
 
+  CURR_CDAT->CURR_SET.cdat_idx = CURR_CDAT->idx;
+  memmove(CURR_ODAT->name, CURR_CDAT->CURR_SET.name, 32);
+  CURR_CDAT->num_sets++;
 
-  /* fwrite vdat */
-  for(v = 0; v <= vbi; v++)
-    {
-      num_models = vdat_buf[v].num_models; //data duplication for caching
-      for(m = 0; m <= num_models; m++)
-        {
-            
-        }
+  CURR_ODAT->cdat_idx = CURR_CDAT->idx;
+  CURR_ODAT->refp = CURR_REF;
+
+
+  CURR_REF->lastref = PREV_REF;
+  PREV_REF->nextref = CURR_REF;
+  CURR_REF->odatp = CURR_ODAT;
+
+
+  if(ref_id == -1) /* user did not define a ref_id so */
+    {              /* use a ref_id from system_space  */
+      ref_id = ss_ref_id;
+      ss_ref_id++;
     }
 
-  /* fwrite odat */
-  /* Convert ref_list to actual offset */
+  CURR_REF->ref_id = ref_id;
 
-  /* fwrite cdat */
 
 
+  inc_ref();
+  inc_odat();
 }
+
 void
-inc_cbi()
+insert_vdat()
 {
-  cbi++;
+  PREV_ODAT->vdat_id = num_vdats; //NULL for vlink, svlink
+  inc_vdat();
 }
 
+/* Populates both the odat name and ref_id
+   for element. */
 void
-set_class_label(char* label)
+insert_ele_label(char* name, uint64_t ref_id)
 {
-  memmove(CURR_CDAT.label,label,32);
+  memmove(CURR_CDAT->CURR_SET.CURR_ELE.name, name, 32);
+  memmove(&CURR_CDAT->CURR_SET.ele_list[CURR_CDAT->CURR_SET.ref_id].ref_id, &ref_id, 64);
 }
 
 void
-inc_subclass_idx()
+insert_ele_olink(uint64_t ref_id)
 {
-  CURR_CDAT.subclass_index++;
+  CURR_CDAT->CURR_SET.CURR_ELE.cdat_idx = CURR_CDAT->idx;
+  CURR_CDAT->CURR_SET.CURR_ELE.ref_id = ref_id; /* Will be resolved to offset
+                                                   when link is processed */
+  CURR_LINK->type = 1;
+  CURR_LINK->link_t.olink.ref_id = ref_id;
+  CURR_LINK->cdat_idx = CURR_CDAT->idx;
+  CURR_LINK->set_idx = CURR_CDAT->num_sets++;
+  CURR_LINK->ele_idx = CURR_CDAT->CURR_SET.num_ele++;
+
 }
+void
+insert_ele_vlink(uint64_t ref_id, char* anim_name)
+{
+
+  /* Insert vlink into link_stack so that it gets processed at
+     output time */
+  CURR_LINK->cdat_idx = CURR_CDAT->idx;
+  CURR_LINK->type = 2;
+  CURR_LINK->set_idx = CURR_CDAT->num_sets;
+  CURR_LINK->ele_idx = CURR_CDAT->CURR_SET.num_ele;
+  CURR_LINK->link_t.vlink.ref_id = ref_id;
+  memmove(CURR_LINK->link_t.vlink.anim_name, anim_name, 32);
 
+
+}
 void
-inc_set_index()
+insert_ele_svlink(uint64_t ref_id)
 {
-  cdat_buf[cbi].set_index++;
+
+  CURR_LINK->cdat_idx = CURR_CDAT->idx;
+  CURR_LINK->type = 3;
+  CURR_LINK->set_idx = CURR_CDAT->num_sets;
+  CURR_LINK->ele_idx = CURR_CDAT->CURR_SET.num_ele;
+  CURR_LINK->link_t.svlink.ref_id = ref_id;
+
+
+}
+//Insert element into odat_buf and cdatpages
+void
+insert_ele()
+{
+
+  uint64_t ref_id;
+
+  ref_id = CURR_CDAT->CURR_SET.CURR_ELE.ref_id;
+
+  CURR_CDAT->CURR_SET.CURR_ELE.cdat_idx = CURR_CDAT->idx;
+  memmove(CURR_ODAT->name,CURR_CDAT->CURR_SET.CURR_ELE.name, 32);
+  CURR_CDAT->CURR_SET.num_ele++;
+
+  CURR_ODAT->cdat_idx = CURR_CDAT->idx;
+  CURR_ODAT->refp = CURR_REF;
+
+  if(ref_id == -1) /* user did not define a ref_id so */
+    {              /* use a ref_id from system_space  */
+      ref_id = ss_ref_id;
+      ss_ref_id++;
+    }
+
+  CURR_REF->ref_id = ref_id;
+
+  inc_odat();
+  inc_ref();
+
 }
 
 void
-inc_ref()
+insert_framesheets(char direction, char* name, uint64_t ref_id, int height , int width, int num_frames)
+{
+  CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].height = height;
+  CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].width = width;
+  CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].num_frames = num_frames;
+  CURR_VDAT->num_models++;
+}
+
+#define CURR_QUAD (CURR_ODAT->quad_list[CURR_ODAT->num_quads])
+void
+insert_quad(int x, int y, int z, uint64_t ref_id)
+{
+  CURR_QUAD.x = x;
+  CURR_QUAD.y = y;
+  CURR_QUAD.z = z;
+  CURR_QUAD.ref_id = ref_id;
+  CURR_ODAT->num_quads++;
+}
+
+/* Inserting the hitbox into the set
+   odat. Elements that don't have
+   a hitbox will use the sets root. */
+void
+insert_hitbox(int hitbox)
 {
-  CURR_ODAT.ref_index++;
-  CURR_ODAT.num_ref++;
+  CURR_ODAT->hitbox = hitbox;
 }
 
-//TODO: This needs to be changed to account for
-//      when the set is of a subclass.
+/* Inserting the root into the set
+   odat. Elements that don't have
+   a root will use the sets root. */
 void
-inc_ele()
+insert_root(int x, int y, int z)
 {
-  CURR_CDAT.set_list[CURR_CDAT.set_index].num_ele++;
-  vbi++;
+
+  CURR_ODAT->root.x = x;
+  CURR_ODAT->root.y = y;
+  CURR_ODAT->root.z = z;
 }
 
 void
-inc_models()
+insert_frame_pointer(char direction, void* frame)
 {
-  CURR_VDAT.num_models++;
+  CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].frames[CURR_VDAT->CURR_MODEL.spritesheet[(int)direction].num_frames++] = frame;
 }
+
index ef54476..d8a7ec3 100644 (file)
-/* Structures allocated for and updated during parse time that
-   are the IR before writing to the output file */
+/*!@file
+  \brief   Intermediate Representation (IR) between Directory Structure and Engine Grammar
+  \details The IR serves as a storage structure that is populated during the
+           parsing of the input directory structure. After parsing is complete,
+           the IR will be condensed (removed of excess allocated space) and then
+           output as the Engine Grammar. In this file we describe the semantic actions
+           that are called at each step, and the memory buffers that they populate.
+           See parser.y for the description on how the input grammar is constructed,
+           and where/when semantic actions are called.
+           TODO: or just write it here.
+  \author  Jordan Lavatai
+  \date    Aug 2016
+  ----------------------------------------------------------------------------*/
+
+
+#include <stdint.h>
+//#include <apc/mem.h>TODO:
 
 #define BUF_SIZE 256
-#define MAX_SUBCLASSES 16
 #define MAX_SETS 256
 #define MAX_ELES 256
-#define MAX_REFS 256
+#define MAX_QUADS 256
 #define MAX_MODELS 256
+#define MAX_POSTS 256
+#define MAX_CLASS_DEPTH 256
+#define MAX_CLASSES 256
+#define MAX_FRAMES 256
+/*  All bufs are of pointers to their respective structs. When a buf is full */
+/*  (number of data structs pointers >= max number of data struct pointers),        */
+/*  we need to allocate a more pointers for that buf. Allocate these       */
+/*  pointers a page at a time (1024 = Page bytes (4096)/bytes per pointer(4))   */
+/*  TODO: Account for different page sizes in different system                  */
+#define PTRS_IN_PAGE 1024
+
+/*  General: All information from the directory structure is stored in        */
+/*  five buffers that comprise the IR: cdat_buf, odat_buf, vdat_buf, ref_buf  */
+/*  and link_buf. Each buf corresponds to the data structure that it stores.  */
+/*  The storage techique for all bufs (except cdat) is the same. Each bufs member first */
+/*  populates its struct and then allocates the space for the next member     */
+/*  and increments the buf index. This means that we have to allocate the     */
+/*  very first member of each buf at ir_init(), so that we don't segfault     */
+/*  as the first member attempts to access memory that its previous member    */
+/*  didn't allocate (because it doesnt exist). We access the buf members      */
+/*  through standard array indexing but conceal the tediousness of array      */
+/*  indexing with macros. E.g. without macros, acessing an elements name      */
+/*  member would look like (split up to not go over line char limit):         */
+/*  (*cdat_stackp)->set_list[(*cdat_stackp)->num_sets] */
+/*     .ele_list[(*cdat_stackp)->set_list[(*cdat_stackp->num_sets)].num_ele].name */
+
+/* For cdats in cdat_buf, we allocate the memory for a cdat once a cdat
+   is recognized in the grammar. Cdat_buf is different from the other bufs
+   because cdats have a root cdat that all cdats are a subclass of. This root
+   cdat can have a set_list like other cdats.                                */
 
 
 
-struct ref {
-  int x, y, z, objref;
-};
+
+/*  Elements: Ele stands for element and has two representations in the IR.   */
+/*  In the cdat_buf eles store their name, cdat_idx (their classes index in   */
+/*  the cdat_buf) and the ref_id (refer to ref ). In the odat_buf, eles store */
+/*  their object data (odat). At output time, the ref_id is dereferenced to   */
+/*  determine the elements odat which is the data that the engine expects     */
+/*  from an element.                                                          */
 
 struct ele {
-  int odat_id;
-  int parent_id;//offset into class set_stack
+  char name[32];
+  uint64_t ref_id;
+  int cdat_idx;
 };
 
+/*  Sets: The set is similar to the ele, but it contains a list of its    */
+/*  elements. The set is populated at parse time AFTER the elements are   */
+/*  populated, due to the nature of bottom up parsing.                    */
+
 struct set {
-  int odat_id;
-  int parent_id;//offset into CB
+  char name[32];
+  uint64_t ref_id;
+  int cdat_idx;
   int num_ele;
-  int ele_index; //same as num_ele?
   struct ele ele_list[MAX_ELES];
 };
 
+/*  Cdats: A cdat is a class data structure. Cdats serve as the central       */
+/*  data types of the IR. At output, the cdat_buf is iterated through and     */
+/*  each is written to the output file. For each cdat, sets and element       */
+/*  ref_ids must be dereferenced to determine the odat information. Cdats     */
+/*  contain pointers to their subclasses so that the relationship between     */
+/*  classes can be determined, but the subclasses are not represented inside  */
+/*  of the cdat itself but rather in the subsequent cdats in cdat_buf. We     */
+/*  can determine the number of subclasses (the last index into cdat_buf      */
+/*  that represents a subclass of some arbitrary cdat) each cdat has by       */
+/*  incrementing num_classes during parse time.                               */
+/*  TODO: Should classes point to their parent class?                         */
 
-//8 ids for each direction
-//fdat_id ordered by alphabetical direction
-struct model {
-  char label[32];
-  int fdat_id[8];
+struct cdat {
+  char name[32];
+  int idx;
+  int num_classes;
+  int num_sets;
+  struct cdat* class_list[MAX_CLASSES];
+  struct set set_list[MAX_SETS];
 };
 
-struct vdat {
-  char label[32];
-  int num_models;
-  int msi; //model_stack_index
-  struct model model_list[MAX_MODELS];
+/* There are an unknown amount of cdats at compile time, so we maintain    */
+/*   a cdat_buf of cdat pointers that can be expanded as needed.           */
+struct cdat* cdat_buf[PTRS_IN_PAGE];
+int num_cdats = 0;
+int curr_max_cdats = PTRS_IN_PAGE;
+
+/* The cdat_stack is a stack pointers to cdat pointers, the top of which is
+   the cdat that is currently being parsed. Whenever a new cdat is recognized
+   by the grammar (CLOPEN), a cdat is pushed onto the cdat_stack, and we refer
+   to this cdat through the macro CURR_CDAT. By keeping a cdat_stack, we have
+   access to the current cdat so that the elements and sets can populate themselves
+   in the cdat accordingly. */
+
+struct cdat* cdat_stack[PTRS_IN_PAGE];
+struct cdat** cdat_stackp;
+
+/* Refs: Each set/ele has a reference to its object data (odat) through a ref_id.
+   Ref_ids are unsigned 64 byte integers that map to the hex values RGBA. During
+   the construction of the directory structure, users can choose a RGBA value for
+   each object that any other object can refer to via links (see link). If a user
+   does not choose an RGBA value, then the object is given one from the system space.
+   We maintain a doubly linked list of refs in the ref_buf at parse time so that
+   links can be resolved after the parsing of the directory structure is complete.
+   For every 16th ref, we create a post so that we can reduce on the search time for
+   a random access. */
+
+struct ref {
+  int type;
+  struct ref* nextref;
+  struct ref* lastref;
+  struct odat* odatp;
+  uint64_t ref_id; //0xFFFFFF->digit
 };
 
-struct cdat {
-  char label[32];
-  int num_subclasses;
-  int num_sets;
-  int subclass_index;
-  int set_index;
-  struct cdat* subclass_list[MAX_SUBCLASSES];
-  struct set set_list[MAX_SETS];
+/* Like the cdat_buf, ref_buf stores pointers to refs and can
+   increase in size */
+struct ref* ref_buf[PTRS_IN_PAGE];
+int num_refs = 0;
+int curr_max_refs = PTRS_IN_PAGE;
+uint64_t ss_ref_id = 0x00FFFFFF; /* system space for ref_ids */
+
+
+/* posts for ref_buf */
+struct ref posts[MAX_POSTS];
+int num_posts;
+
+/* Links: At parse time, a set/ele can include a link in their
+   grammar representation instead of the actual data and this signifies
+   to the APC that that set/ele wishes to use the data of another
+   set/ele, either its video data (vdat) or object data (odat). The link
+   itself contains the type of link it is, the ref_id OR name, and
+   which set/ele created the link. During parse time, links can be made
+   to o/vdats that have yet to be parsed. In order to accomodate for this,
+   we resolve all links AFTER parse time by iterating through the link_buf,
+   finding the ref_id that was stored for some object (if the ref_id exists),
+   and creating a relative pointer from the original object to the data that
+   was linked */
+
+/* Svlinks stand for short vlink, which is a link to a vdat
+   TODO: diff btwn vlink*/
+
+struct svlink {
+  uint64_t ref_id;
+};
+
+/* A vlink is what it sounds like, a link to a vdat
+ TODO: model link? */
+struct vlink {
+  uint64_t ref_id;
+  char anim_name[32];
+};
+
+/* Olinks are links to odats */
+struct olink {
+  uint64_t ref_id;
+};
+
+union link_t {
+  struct olink olink;
+  struct vlink vlink;
+  struct svlink svlink;
+};
+
+struct link {
+  int type; //1 = olink, 2 = vlink, 3 = svlink
+  union link_t link_t;
+  int cdat_idx;
+  int set_idx;
+  int ele_idx;
+};
+/* link_buf contains all the links that
+   we encountered during parse time that need
+   to be resolved to an offset at output time.
+   This does not include quad refs, because
+   those are already known to need to be resolved */
+struct link* link_buf[PTRS_IN_PAGE];
+int num_links = 0;
+int curr_max_links = PTRS_IN_PAGE;
+
+
+/* Odats: Odats consist of the object data necessary for
+   each object. Odats are sometimes referred to as archetypes
+   at compile-time, in order to distinguish the difference from
+   a runtime object and a compile-time object.
+   TODO: Need more info about objects at runtime, to described
+         the reasoning behind odat structure at compile-time*/
+
+/* Each set has a quad_list or a list of quads. The quad_list
+   is the ? */
+struct quad {
+  int x, y, z;
+  uint64_t ref_id; //rgba
+};
+
+struct root {
+  int x, y, z;
 };
 
-//Element or a set
 struct odat {
-  char label[32];
+  char name[32];
   int vdat_id;
-  int class_id;
-  int num_ref;
-  int ref_index;
-  struct ref ref_list[MAX_REFS];
+  int cdat_idx;
+  int hitbox;
+  struct root root;
+  struct ref* refp; /* pointer to it's ref on ref_list */
+  int num_quads;
+  struct quad quad_list[MAX_QUADS];
+};
+
+/* Populated and allocated same way as other bufs */
+struct odat* odat_buf[PTRS_IN_PAGE];
+int curr_max_odats = PTRS_IN_PAGE;
+int num_odats = 0;
+
+/* A framesheet is a grouping of animation frames in
+   a single direction (N,W,S,E) */
+struct framesheet {
+  int width;
+  int height;
+  int num_frames;
+  void* frames[MAX_FRAMES];
 };
 
+/* A model is a collection of framesheets for every
+   direction (N,W,S,E,NW,NE,SW,SE)*/
+/* NAMED spritesheet */
+struct model {
+  char name[32];
+  struct framesheet spritesheet[8]; //one for each
+};
+
+/* Vdat: Vdats are the video data of each object. They can not be
+   created as a stand alone object (because they consist solely
+   of animation information and not the skeleton on which the
+   animation manipulates). Vdats have a list of models for every
+   animation that the vdats odat can do for that vdat*/
+struct vdat {
+  struct odat* creator; //pointer to odat that made this vdat
+  int num_models;
+  struct model model_list[MAX_MODELS];
+};
 
-struct cdat cdat_buf[BUF_SIZE];
-struct odat odat_buf[BUF_SIZE];
-struct vdat vdat_buf[BUF_SIZE];
 
-//indexes for buffers
-int cbi = 0;
-int vbi = 0;
-int obi = 0;
+struct vdat* vdat_buf[PTRS_IN_PAGE];
+int curr_max_vdats = PTRS_IN_PAGE;
+int num_vdats = 0;
 
+/* The initalization function of the IR. Mallocs the
+   first c/v/odat and the first links and refs and
+   inits the cdat_stack */
 void
-insert_set(void);
+ir_init(void);
+
+/* mallocs memory for a new cdat. If the cdat_buf
+   is full, mallocs another 1024 cdat pointers. */
+void
+malloc_cdat(void);
 
+/* Called after the cdat open operator has been recognized in grammar. Allocates
+   the space for a cdat on the cdat_buf, pushes that pointer onto
+   the cdat_stack */
 void
-insert_ref(int, int, int, int);
+push_cdat(char*);
 
+/* Called after a cdat end operator has been recognized in grammar. Sets
+   top stack cdat ** to null and decrements stack pointer */
 void
-inc_cbi(void);
+pop_cdat(void);
 
+/* Called after an odat has been populated. Allocates memory for
+   the next odat. */
 void
-set_class_label(char*);
+inc_odat(void);
 
+/* Called after an vdat has been populated. Allocates memory for
+   the next vdat. */
 void
-inc_subclass_index(void);
+inc_vdat(void);
 
 void
-inc_subclass_index(void);
+inc_link(void);
 
 void
 inc_ref(void);
 
+/* Called in the reduction of a set. While both odats (eles and sets)
+   have identical label terminals, we are unable to give a single grammatical rule
+   for both due to how we allocate odats in the odat buf. Due to the
+   nature of bottom up parsing, all the elements will be inserted into the
+   odat_buf first, and then the set that contains these element is inserted. Since
+   the sets label comes before the element list in the grammar, we would be giving an element
+   a set label in its respective odat, which would then be replaced by the
+   elements label. Instead, we store the label in the sets representation inside
+   CURR_CDAT and after we are done parsing the element_list and know that the CURR_ODAT
+   is the set, we populate the sets label members in CURR_ODAT with the values we stored
+   previously in CURR_CDAT. */
+void
+insert_set_label(char*, uint64_t);
+
+/* Populate the sets representation in CURR_CDAT with a ref_id and insert a link
+   into the link_buf that will resolve the ref_id to an actual odat after parse time. */
 void
-inc_models(void);
+insert_set_olink(uint64_t);
+
+/* Put the vlink in the link_buf to be processed after parsetime */
+void
+insert_set_vlink(uint64_t, char*);
+
+/* Put svlink in the link_buf to be processed after parsetime */
+void
+insert_set_svlink(uint64_t);
+
+/* Called for every set reduction except for sets with olinks. Populates the
+   set data structures in the CDAT and in the ODAT. Uses the name and ref_id
+   from insert_set_label. Also inserts a ref into the ref_buf with the CURR_ODAT
+   pointer so that we can also resolve the odat from its ref_id. */
+void
+insert_set(void);
+
+/* Insertion of eles is practically equivalent to how sets are inserted because both
+   share the same data type (ODAT). Like sets, eles have links, labels
+   and odats. Eles have the added notion of a parent set, and so must be inserted
+   into said parent set, but this is the only place they truly differ from sets. */
+
+void
+insert_ele_label(char*, uint64_t);
+
+void
+insert_ele_olink(uint64_t);
+
+void
+insert_ele_vlink(uint64_t, char*);
+
+void
+insert_ele_svlink(uint64_t);
+
+void
+insert_ele(void);
+
+/* Created as a seperate function, instead of setting the ODATS vdat_id and
+   calling inc_vdat() inside of insert_set(), to account for the set reduction
+   where a vdat is not created (o/v/svlinks). Because insert_set/ele is always
+   called before insert_vdat, and thus increments the CURR_ODAT to be the next
+   ODAT to be populated, insert_vdat() targets the last ODAT that was populated,
+   via PREV_ODAT. */
+void
+insert_vdat(void);
+
+/* Inserts the hitbox into the CURR_ODAT */
+void
+insert_hitbox(int);
+
+/* Inserts the root into the CURR_ODAT */
+void
+insert_root(int, int, int);
+
+/* Inserts a quad into the CURR_ODAT */
+void
+insert_quad(int, int, int, uint64_t);
+
+void
+insert_model(void);
+
+void
+insert_framesheet(char, char*, uint64_t, int, int, int);
+
+void
+insert_frame_pointer(char, void*);
+
index fdbb56f..ffc672d 100644 (file)
@@ -27,8 +27,8 @@
 %token         VOPEN
 %token         VCLOSE
 
-%token         RLS     //!
-%token         RLE    //#
+%token         QOPEN     //!
+%token         QCLOSE    //#
 %token         RT     //*
 %token         HB
 %token         MOD
@@ -51,9 +51,9 @@
 %token <int> HEIGHT
 %token <int> NUM_PTRS
 //precedence
-%precedence LOW
-%precedence MED
-%precedence HIGH
+%precedence LP
+%precedence MP
+%precedence HP
 
  /* Syntax Directed Translation Scheme of the APC grammar */
 
@@ -74,7 +74,7 @@ NAME CLOPEN {push_cdat($1);} class_block CLCLOSE              {pop_cdat();};
 
 class_block:
 class_list
-| class_list set_list 
+| class_list set_list
 | set_list
 ;
 
@@ -86,17 +86,14 @@ set_list set
 root:
 RT NUM NUM NUM                                                {insert_root($2, $3, $4);};
 ;
-quad_list:
-RLS quads RLE
-;
 
-quads:
-quads quad
+quad_list:
+quad_list quad
 | quad
 ;
 
 quad:
-NUM NUM NUM REF                                               {insert_quad($1, $2, $3, $4);};
+QOPEN NUM NUM NUM REF QCLOSE                                  {insert_quad($2, $3, $4, $5);};
 
 hitbox:
 HB NUM                                                        {insert_hitbox($2);}
@@ -123,8 +120,8 @@ SOPEN set_label element_list set_map_data vdat SCLOSE
 
 
 set_label:
-HIGH NAME REF                                                 {insert_set_label($2,$3);};
-| LOW NAME                                                    {insert_set_label($2, -1);};
+HP NAME REF                                                 {insert_set_label($2,$3);};
+| LP NAME                                                    {insert_set_label($2, -1);};
 ;
 
 set_svlink:
@@ -141,13 +138,13 @@ REF
 
 //parent_id is the set_index of the subclass_index.
 element_list:
-element_list element MED
-| element LOW
+element_list element MP
+| element LP
 ;
 
 ele_label:
-HIGH NAME REF                                                                  {insert_ele_label($2, $3);};
-| LOW NAME                                                                     {insert_ele_label($2, -1);};
+HP NAME REF                                                                  {insert_ele_label($2, $3);};
+| LP NAME                                                                     {insert_ele_label($2, -1);};
 ;
 
 ele_vlink:
@@ -178,20 +175,20 @@ model_list model
 ;
 
 model:
-spritesheet LOW
+spritesheet LP
 ;
 
 spritesheet:
-spritesheet HIGH framesheet
+spritesheet HP framesheet
 | framesheet
 ;
 
 framesheet:
-SSD NAME REF HEIGHT WIDTH NUM_PTRS frame_pointers LOW                         {insert_framesheet($1, $2, $3, $4, $5, $6);};
+SSD NAME REF HEIGHT WIDTH NUM_PTRS frame_pointers LP                         {insert_framesheet($1, $2, $3, $4, $5, $6);};
 ;
 
 frame_pointers:
-frame_pointers SSD HIGH FPTR                                           {insert_frame_pointer($2, $4);};
+frame_pointers SSD HP FPTR                                           {insert_frame_pointer($2, $4);};
 | SSD FPTR                                                        {insert_frame_pointer($1, $2);};
 ;