grammar proto1
authorksg <ken@mihrtec.com>
Thu, 11 Aug 2016 00:32:04 +0000 (17:32 -0700)
committerksg <ken@mihrtec.com>
Thu, 11 Aug 2016 00:32:04 +0000 (17:32 -0700)
src/core/main.c
src/core/video.c [new file with mode: 0644]
src/lexer.c [new file with mode: 0644]
src/parser.y [new file with mode: 0644]

index 5e18ac5..a14ce6a 100644 (file)
@@ -90,12 +90,13 @@ main (int argc, char** argv)
 #undef main_loop
 #endif
 
-/** subsystem initializer
-    Calling main_init() boots the system, and may be called multiple
-    times to cause a system-wide reboot.
+/*@
+  subsystem initializer
+  Calling main_init() boots the system, and may be called multiple times to
+  cause a system-wide reboot.
   @return 0 if successful, -1 SDL, -2 IMG, -3 TTF, -4 STATE.
   SDL and logging is available after this is called
- ******************************************************************************/
+*/
 #define INIT(_cond,_errorstring,_quit)                           \
   do {                                                           \
     if (_cond)                                                   \
@@ -120,18 +121,17 @@ main_init()
   INIT(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG, IMG_GetError, IMG_Quit);
   INIT(TTF_Init() == -1, TTF_GetError, TTF_Quit);
   INIT(state_init(), state_get_error, state_quit);
-
-/*TODO:
-  INIT(io_init(), io_get_error, io_quit);
-  INIT(render_init(), io_get_error, io_quit);
-*/
+  INIT(fs_init(), fs_get_error, fs_quit);
 
   SDL_Log("Initialization Complete.");
   return 0;
 }
 
-/** main loop.
- *******************************************************************************/
+/*@
+  The main loop may be compiled in blocking or non-blocking mode, and
+  synchronizes time in ticks (milliseconds) as established by SDL anchored
+  on the state_tick() event.
+*/
 void
 main_loop()
 { static uint32_t state_last_ticks = 0;
diff --git a/src/core/video.c b/src/core/video.c
new file mode 100644 (file)
index 0000000..3691cc6
--- /dev/null
@@ -0,0 +1,38 @@
+/*!@file
+  \brief   core rendering support
+  \details initializes the SDL-based rendering target and context.
+  \author  Mihrtec
+  \date    2016
+----------------------------------------------------------------------------*/
+#include <glad/glad.h>
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+#include <SDL2/SDL_ttf.h>
+
+#define ERROR_BUFFER_SIZE
+
+int          video_init(void);
+const char * video_get_error(void);
+void         video_quit(void);
+
+static char *video_error = 0;
+
+int
+video_init()
+{ if (!gladLoadGLLoader((GLADloadproc) SDL_GL_GetProcAddress))
+  { renderer_error = "Failed to initialize OpenGL context";
+    return -1;
+  }
+  printf("OGL %d.%d\n", GLVersion.major, GLVersion.minor);
+  return 0;
+}
+
+const char *
+video_get_error()
+{ return renderer_error;
+}
+
+void
+video_quit()
+{
+}
diff --git a/src/lexer.c b/src/lexer.c
new file mode 100644 (file)
index 0000000..b230a4a
--- /dev/null
@@ -0,0 +1,143 @@
+/*!@file
+  \brief   lexical analyzer implementation for APC
+  \details lexer for tokenizing filenames from a directory root
+  \author  Jordan Lavatai
+  \date    Aug 2016
+  ----------------------------------------------------------------------------*/
+//stdc
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+//posix
+#include <unistd.h>
+#include <dirent.h>
+//bison
+#include "fileparser.tab.h"
+
+int lexer_init(void);
+int lexer();
+
+#define BUF_SIZE 256
+#define STACK_BUF_SIZE (BUF_SIZE >> 2)
+static DIR*  dp_stack_buf[STACK_BUF_SIZE];
+static char  path_buf[BUF_SIZE];
+static char  storage_buf[BUF_SIZE];
+
+/* Setup stack and first directory to read from */
+int
+lexer_init()
+{ char cwd_buf[MAX_TOK_LEN];
+
+  dsp = dp_stack_base;
+  getcwd(path_buf, MAX_TOK_LEN);
+  printf("|------cwd is %s------|\n", path_buf);
+  if(!(*dsp = opendir(path_buf)))
+    printf("opendir(cwd) failed in linit()\n");
+
+  *dsp = dp;
+  printf("dp_stack is %x, dsp is %x size is %d\n",*dp_stack, *dsp, sizeof dp);
+
+  return 0;
+}
+
+/* Returns token identifier and sets yylval */
+int
+lexer()
+{ static DIR** dsp = &dp_stack_buf;
+  int tok_t;
+  char buf[MAX_TOK_LEN];
+  char* file_name;
+
+  printf("|------in yylex(), calling tok_dir------|\n");
+  if((tok_t = tok_dir(*dsp, yylval.str)) == -1)
+    printf("tok_dir returned -1, something is broken\n");
+  printf("|------in yylex(), returning tok_t = %d | err = %s------|\n", tok_t, strerror(errno));
+  return tok_t;
+}
+
+#define DSP_PUSH(_val) *++dsp = (_val)
+
+int
+tok_dir(DIR* dp, char buf[])
+{
+  struct dirent* de; /* directory entry */
+  static DIR *tmp_dp;
+  char *tmp_path;
+  int path_len;
+
+
+tok_start:
+  if((*dsp == NULL) || (de = readdir(*dsp)) == NULL)
+    {
+      if(errno)
+        {  printf("Error:%s in tok_dir\n", strerror(errno));
+          return errno;
+        }
+      if( (dsp - dp_stack) >= 0)
+        {
+          printf("Current directory is null, pop one off ");
+#define DSP_POP() *dsp--
+
+          dp = DSP_POP();
+
+          /* Remove directory that was popped from path */
+#define SUB_PATH() tmp_path = strrchr(path, '/'); \
+          path_len = strlen(tmp_path);            \
+          memset(tmp_path, 0, path_len)
+
+          SUB_PATH();
+
+          goto tok_start;
+        }
+      return 0; /* Done */
+    }
+
+  else if(de->d_type == DT_REG)
+    { printf("|------dir_ent is a file, "\
+             "setting yylval to %s------|\n", de->d_name);
+      memmove(buf, de->d_name, MAX_TOK_LEN);
+      return FDAT;
+    }
+  else if (de->d_type == DT_DIR )
+    { if ((dsp - dp_stack) >= MAX_DIR_DEP) /* We've opened to many directories */
+        {
+          printf("Too many directories!\n");
+          return 1;
+        }
+      if(strcmp(de->d_name,".") == 0 || strcmp(de->d_name,"..") == 0)
+        {
+          printf("directory is %s \n", de->d_name);
+          goto tok_start;
+        }
+
+      printf("|------ dir_ent is directory %s, "\
+             "cwd = %s, path = %s ------|\n", de->d_name, get_current_dir_name(), path);
+
+
+      /* Add directory name to path */
+#define ADD_PATH(_name) strcat(path, "/");      \
+                        strcat(path, _name)
+
+      ADD_PATH(de->d_name);
+
+      tmp_dp = opendir(path);
+      if(tmp_dp == 0)
+        { printf("opening the directory failed,"\
+                 "errno = %s\n", strerror(errno));
+          return -1;
+        }
+
+      DSP_PUSH(tmp_dp);
+
+      goto tok_start;
+
+    }
+  else
+    {
+      printf("A file that is not a diretory or a regular file is unable to be tokenized: %s\n", de->d_name);
+      return -1;
+    }
+
+
+
+}
diff --git a/src/parser.y b/src/parser.y
new file mode 100644 (file)
index 0000000..8a1db87
--- /dev/null
@@ -0,0 +1,103 @@
+/* Asset Package Compiler */
+%{
+  #include <stdio.h>
+  #include <string.h>
+  #include <dirent.h>
+  #include "sprite.h"
+  #include "symbol.h"
+  #include <png.h>
+
+
+  extern int lexer_init();
+  extern int lexer();
+  #define yylex lexer
+
+
+  void yyerror();
+%}
+%define parse.error verbose
+%define lr.type ielr
+%define api.value.type union
+%token <int>   NUM
+%token <char*> WORD
+//operators
+%token         PROTOCLASS //+
+%token         SIBS       //|
+%token         FNAME      //!
+ //nonterminal types
+%type <int>    fd
+%type <int*>   fdlist
+
+/* %token <str> EXT */
+/* %token <str> FW */
+/* %token <str> SPR */
+/* %token CC */
+/*  /\* Rules *\/ */
+/* %% */
+/* SS: SPR '/' MD '.' EXT */
+/* MD: CC '_' FW */
+/* | FW */
+/* | CC */
+
+/* Rules */
+%%
+
+object_defs:
+  %empty
+| arch_id object_defs
+;
+
+arch_id:
+  archetype
+| arch_id_ref
+;
+
+arch_id_list:
+  arch_id
+| arch_id arch_id_list
+;
+
+archetype:
+  PROTOCLASS label arch_id_list SIBS arch_id_list
+| PROTOCLASS label arch_id_list
+| label arch_id_ref vdat
+;
+
+label:
+  WORD
+;
+
+arch_id_ref:
+  NUM
+;
+
+vdat:
+  spritesheet
+;
+
+spritesheet:
+  fdlist
+;
+
+fdlist:
+  fd
+| fd fdlist
+;
+
+fd:
+  FNAME WORD
+| NUM;
+%%
+
+int
+main(int argc, char** argv)
+{ /* Parse cmd line arguments */
+  lexer_init();
+  yyparse(); /* runs handle_fname for each filename */
+  return 0;
+}
+
+void
+yyerror (char const *s)
+{ fprintf(stderr, "%s\n", s);
+}