test complete
authorken <ken@mihrtec.com>
Mon, 16 Jan 2017 01:19:57 +0000 (17:19 -0800)
committerken <ken@mihrtec.com>
Mon, 16 Jan 2017 01:19:57 +0000 (17:19 -0800)
src/apc.c
src/ir.c
src/ir.h
src/lexer.rl
src/parser.y
src/scanner.c

index e49d03b..5bd6d73 100644 (file)
--- a/src/apc.c
+++ b/src/apc.c
@@ -24,7 +24,7 @@
 #define DEFAULT_PAGESIZE 4096
 const char* cargs['Z'] = {0};
 long  sys_pagesize;
-
+  
 int main(int, char*[]);
 
 extern //lexer.c
@@ -84,8 +84,7 @@ int main
   }
   if ((sys_pagesize = sysconf(_SC_PAGESIZE)) == 0)
     sys_pagesize = DEFAULT_PAGESIZE;
-
-  if (scanner_init() || ir_init())
+  if (ir_init())
     { perror("init");
       exit(EXIT_FAILURE);
     }
@@ -93,9 +92,7 @@ int main
     { perror("scanner");
       exit(EXIT_FAILURE);
     }
-  scanner_quit();
   ir_linker();
   ir_condenser();
   exit(EXIT_SUCCESS);
 }
-
index f39f85b..ebb2545 100644 (file)
--- a/src/ir.c
+++ b/src/ir.c
@@ -201,6 +201,11 @@ int ir_condenser
 ( void )\r
 { return 0; }\r
 \r
+/* Return the class's name string */\r
+uint8_t* ir_class_name\r
+( struct ir_class_t* class )\r
+{ return class->name; }\r
+\r
 /* Return a pointer to the root class */\r
 struct ir_class_t* ir_class_root\r
 ( void )\r
index 339a4e7..aec70db 100644 (file)
--- a/src/ir.h
+++ b/src/ir.h
@@ -27,7 +27,6 @@ typedef struct ir_set_t*     ir_set;
 typedef struct ir_class_t*   ir_class;\r
 typedef struct ir_setld_t*   ir_setld;\r
 typedef struct ir_classld_t* ir_classld;\r
-\r
 /* Classes and Sets\r
    Classes are rooted at a special root class, representing the current working\r
    directory at scan-time, named ".".  The root class can always be identified\r
@@ -47,6 +46,7 @@ typedef struct ir_classld_t* ir_classld;
 */\r
 ir_class   ir_class_root(void);\r
 ir_class   ir_class_addchild(ir_class,const uint8_t*);\r
+uint8_t*   ir_class_name(ir_class);\r
 ir_set     ir_class_addset(ir_class,const uint8_t*);\r
 ir_set     ir_set_addchild(ir_set,const uint8_t*);\r
 /* Set Data\r
index 5e2fb77..6bb1d1b 100644 (file)
@@ -6,26 +6,28 @@
 #include "apc.h"
 #include <unistdio.h>
 #include <unistr.h>
-extern //lexer.c
-void lexer_pushtok(int, YYSTYPE);
 /* Public */
-int lexer_setdirection(uint8_t*, int);
-int lexer_lexfile(uint8_t*);
-int lexer_lexdir(uint8_t*);
-int lexer_lexstring(uint8_t*, int);
-int lexer_setstr(uint8_t*, int);
+int  lexer_init(void);
+void lexer_quit(void);
+int  lexer_lexfile(uint8_t*);
+int  lexer_lexdir(uint8_t*);
+int  lexer_lexstring(uint8_t*, int);
 //apc.c
-extern
-yypstate* apc_pstate;
-extern
-yycstate* apc_cstate;
 static
-YYSTYPE   lval_stack[1024], * lvalsp;
-#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (L), apc_cstate)
+yypstate* pstate;
+static
+yycstate* cstate;
+/* Ring buffer for keeping lexical tokens valid for up to 255 tokens */
+static
+YYSTYPE   lval_stack[0xFF + 1];
+static
+uint8_t   lval_offs;
+#define PUSHTOK(T,L) yypush_parse(pstate, T, (L), cstate)
 #define LEXTOK(T,Y,L) do {                     \
-    lvalsp->Y = L;                             \
-    PUSHTOK(T,lvalsp);                         \
-    lvalsp++;                                  \
+    printf("lval[%i]\n",lval_offs);\
+    lval_stack[lval_offs].Y = L;                               \
+    PUSHTOK(T,lval_stack + lval_offs);                         \
+    lval_offs = (lval_offs + 1);                               \
     ntok++;                                    \
   } while (0);
 #define PUSHFACE(F) LEXTOK(FACING, face, F)
@@ -128,6 +130,20 @@ YYSTYPE   lval_stack[1024], * lvalsp;
   
 }%%
 
+int lexer_init
+( void )
+{ pstate = yypstate_new();
+  cstate = yycstate_new();
+  lval_offs = 0;
+  return !pstate || !cstate;
+}
+
+void lexer_quit
+( void )
+{ if (pstate) yypstate_delete(pstate);
+  if (cstate) yycstate_delete(cstate);
+}
+
 int lexer_lexstring
 ( uint8_t* str, 
   int size
@@ -137,7 +153,6 @@ int lexer_lexstring
   int cs, ntok;
   YYSTYPE lval;
 
-  lvalsp = lval_stack;
   ntok = 0;
   p = ts = str;
   pe = eof =  p + size + 1;
@@ -147,7 +162,7 @@ int lexer_lexstring
   %%write init;
   %%write exec;
 
-   printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
+  printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
 
   return ntok;
 }
@@ -160,7 +175,7 @@ int lexer_lexstring
 int lexer_lexfile
 ( uint8_t* filename )
 { uint8_t* last_period,* iter,* filename_end;
-  int            ntok;
+  int      ntok;
   last_period = NULL;
   for (iter = filename; *iter; iter++)
     switch (*iter)
@@ -203,235 +218,3 @@ int lexer_closedir
   PUSHOP(CLCLOSE);
   return ntok;
 }
-
-/**************************/
-/****Abandon All Hope******/
-/**************************/
-/***                    ***/
-/***                    ***/       
-/***                    ***/
-/***                    ***/
-
-
-#if 0 
-
-%%{
-  machine setdirection;
-
-  action ret_north {printf("Lexer_setdirection:: direction is north, returning 4\n"); return 4;; }
-  action ret_west { printf("Lexer_setdirection:: direction is west, returning 2\n");return 2;}
-  action ret_east { printf("Lexer_setdirection:: direction is east, returning 6\n");return 6;}
-  action ret_south { printf("Lexer_setdirection:: direction is south, returning 0\n");return 0;}
-  action ret_northeast { printf("Lexer_setdirection:: direction is northeast, returning 5\n");return 5 ;}
-  action ret_northwest { printf("Lexer_setdirection:: direction is northwest, returning 3\n");return 3;}
-  action ret_southeast { printf("Lexer_setdirection:: direction is southeast, returning 7\n");return 7;}
-  action ret_southwest { printf("Lexer_setdirection:: direction is southwest, returning 1\n");return 1;}
-
-  def = [_\0] %to(ret_south);
-  N = 'N'[_\0] %to(ret_north);
-  W = 'W' [_\0] %to(ret_west);
-  S = 'S' [_\0] %to(ret_south);
-  E = 'E' [_\0] %to(ret_east);
-  NW = 'NW' [_\0] %to(ret_northwest);
-  NE = 'NE' [_\0] %to(ret_northeast);
-  SW = 'SW' [_\0] %to(ret_southwest);
-  SE = 'SE' [_\0] %to(ret_southeast);
-
-  direction = (N | W | S | E | NW | NE | SW | SE | def);
-
-  main := direction; 
-
-  write data nofinal noprefix noerror;
-
-}%%
-
-
-int
-lexer_setdirection
-(uint8_t* str, int size)
-{ uint8_t *p, *pe, *eof;
-  int cs;
-
-
-  p = str;
-  pe = str + size + 1; 
-  
-  printf("|--- Begin lexer_setdirection str = %s, p = %s, pe = %s ---|\n", str,p,  pe);
-  
-  %%write init;
-  %%write exec noend;
-
-  printf("|--- Error in: lexer_setdirection ---|\n");
-  
-  return -1;
-}
-
-
-
-%%{
-  machine setstr;
-
-  
-  action lex_setvlink {printf("Lexer_setstr:: Returning setvlink filetype for %s\n", str); type =  5; newstrt = lexer_lexsetvlink(str); fbreak;}
-  action lex_elevlink {printf("Lexer_setstr:: Returning elevlink filetype for %s\n", str); type =  6; newstrt = lexer_lexelevlink(str); fbreak;}
-  action lex_setmodel {printf("Lexer_setstr:: Returning setmodel filetype\n"); newstrt = lexer_lexsetmodel(str); type =  1; fbreak;}
-  action lex_setmap {printf("Lexer_setstr:: Returning setmap filetype\n"); newstrt = lexer_lexsetmap(str); type =  2; fbreak;}
-  action lex_elemodel {printf("Lexer_setstr:: Returning elemodel filetype for %s\n", str); newstrt = lexer_lexelemodel(str); type =  3; fbreak;}
-  action lex_elemap {printf("Lexer_setstr:: Returning elemap filetype for %s\n", str); newstrt = lexer_lexelemap(str); type = 4; fbreak;}
-  action lex_setolink  { printf("Lexer_setstr:: Returning setolink filetype\n"); type = 8; newstrt = lexer_lexsetolink(str); fbreak;}
-  action lex_eleolink  { printf("Lexer_setstr:: Returning eleolink filetype\n"); type = 7; newstrt = lexer_lexeleolink(str); fbreak;}
-  action p {printf("p = %s \n",p);}
-  action name_error {printf("In %s, there is a syntactic error. Make sure your set/element names dont conflict with the reserved keywords.\n", str);}
-
-  
-  N = 'N';
-  W = 'W';
-  S = 'S';
-  E = 'E';
-  NW = 'NW';
-  NE = 'NE';
-  SW = 'SW';
-  SE = 'SE';
-
-  SS = 'SS';
-  direction = (N | W | S | E | NW | NE | SW | SE) $p;
-  
-  SSD = SS direction;
-
-  
-
-  name = alpha+ $p - SSD $p;
-  num = digit+ $p;
-  ref = '0x' $p alnum+ $p;
-
-  
-  set_label = name | (name '_' ref);
-  ele_label = name | (name '_' ref);
-
-  model_types = (name) | (name '_' num '_' num) | (name '_' num);
-  
-
-  set_model =  set_label '_' SS  %to(lex_setmodel);
-  set_map = set_label '_' '~'  %to(lex_setmap);
-  ele_model = set_label '_' ele_label '_' SS %to(lex_elemodel);
-  ele_map = set_label '_' ele_label '_' '~' %to(lex_elemap);
-  set_olink = ref %to(lex_setolink) [\0] ;
-  ele_olink = set_label '_' '~' '_' ref [\0] %to(lex_eleolink);
-  set_vlink = set_label '_' '#' '_' (ref | ref '_' name) [\0]  %to(lex_setvlink);
-  ele_vlink = set_label '_' ele_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_elevlink);
-  
-   main := (ele_map | set_model | set_map |ele_model | ele_vlink | set_vlink | set_olink | ele_olink);
-
-  write data;
-
-
-}%%
-
-int
-lexer_setstr
-(uint8_t* str, int size)
-{ uint8_t *p, *pe, *eof;
-  int cs, type, newstrt;
-
-  type = newstrt = 0;
-
-  p = str;
-  pe = str + size + 1;
-  
-  printf("|--- Begin lexer_setstr with str = %s, p = %s, pe = %s ---|\n", str,p,  pe);
-  
-  %%write init;
-  %%write exec noend;
-
-  printf("|--- End lexer_setstr. Incrementing str by %d, type is %d ---|\n", newstrt, type);
-  
-  return newstrt;
-}
-
-#endif
-
-
-/* %%{ */
-/*   machine file_matcher; */
-  
-/*   action call_ml { ts = p; fgoto set_hw ;} */
-/*   action call_tl { return 0;} */
-/*   action set_height {height = ttov(p, p-ts+1); ts = p;} */
-/*   action set_width { width = ttov(p, p-ts+1);} */
-/*   action call_lmf {lexer_lexmapfile(height, width); } */
-/*   action lex_error {printf("input error: character %c in filename %s is invalid\n  = %s\n", fc, str, p);} */
-  
-/*   #This machine determines the type of file we are lexing */
-/*   #and calls the appropriate machine to handle it. */
-
-/*   #TODO add mapping name */
-/*   width = digit+ %set_width; */
-/*   height = digit+ %set_height; */
-
-/*   set_hw :=  height . '_' . width [\0] %to(call_lmf); */
-
-/*   tok_segment = alnum; */
-/*   map_end = 'm' . '_' %to(call_ml); */
-/*   tok_end = alnum+  . [\0] %to(call_tl); */
-
-/*   file_matcher := (tok_segment+ . '_' )+ ( map_end | tok_end ); */
-
-/*   write data; */
-/* }%% */
-
-/* int */
-/* lexer_matchfile */
-/* (char* str, int size) */
-/* {  *p, *pe; */
-/*   char* ts; */
-/*   int  cs, ntok, height, width; */
-
-/*   p =  str; */
-/*   pe = p + size; */
-/*   height = width = 0; */
-
-/*   printf("Checking if filename is a map file:: filename = %s, p = %c, pe = %c\n", str, *p, *pe); */
-
-/*   %%write init; */
-/*   %%write exec noend; */
-
-/*   printf("Ending lexer_ismapfile on %s\n", str); */
-
-/*   return ntok; */
-/* } */
-
-/* %%{ */
-/*   machine vartype; */
-
-/*   action isele {return 0;} */
-/*   action ismodel {return 1;} */
-
-/*   set_name = alpha+; */
-/*   ele_name = alpha+; */
-/*   model_name = alpha+; */
-
-/*   ele = set_name  '_' model_name '_' ele_name %isele; */
-/*   model = set_name '_'  model_name [\0] %ismodel; */
-  
-
-/*   ismodel := (ele | model); */
-
-/*   write data; */
-
-/* }%% */
-
-/* int */
-/* lexer_ismodel */
-/* (uint8_t* str, int size) */
-/* { uint8_t *p, *pe, *eof; */
-/*   int cs; */
-
-/*   p = str; */
-/*   pe = p + size + 1; */
-
-/*   %%write init; */
-/*   %%write exec; */
-  
-
-/* } */
index 07cf5b8..db8e30b 100644 (file)
@@ -80,7 +80,13 @@ class_list:
 ;
 
 class:
-  NAME CLOPEN   { yyclass_push(cs,ir_class_addchild(yyclass(cs), $1)); }
+NAME CLOPEN   { printf("new class\n");
+                printf("class name: %s\n", $1);
+               printf("current class head: %s\n", ir_class_name(yyclass(cs)));
+               yyclass_push(cs,ir_class_addchild(yyclass(cs), $1));
+               printf("Pushed class\n");
+               printf("current class head: %s\n", ir_class_name(yyclass(cs)));
+}
   progn
   CLCLOSE       { yyclass_pop(cs); }
 ;
@@ -147,24 +153,22 @@ yycstate* yycstate_new
     { yyerror(class_state, "Memory allocation error.");
       return NULL;
     }
-  class_state->csp = &class_state->class_stack[0];
-  yyclass_push(class_state, ir_class_root());
+  class_state->csp = class_state->class_stack;
+  *(class_state->csp) = ir_class_root();
   return class_state;
 }
 
 static inline
 ir_class yyclass_pop
 ( yycstate* cs )
-{ return *(cs->csp)--; }
+{ return *((cs->csp)--); }
 
 static inline
 ir_class yyclass_push
 ( yycstate* cs,
   ir_class class
 )
-{ *++cs->csp = class;
-  return class;
-}
+{ return *++(cs->csp) = class; }
 
 void yycstate_delete
 ( yycstate* class_state )
index d9aa426..d01be15 100644 (file)
 /* Internal */
 #include "parser.tab.h"
 /* Public */
-int   scanner_init(void);
 void  scanner_quit(void);
 int   scanner_scanpath(char const*);
 int   scanner_scandir(DIR*);
-yypstate* apc_pstate;
-yycstate* apc_cstate;
 /* Private */
+static
+int   scanner_scandir_r(DIR*);
+extern //lexer.rl
+int   lexer_init(void);
+extern //lexer.rl
+void  lexer_quit(void);
 extern //lexer.rl
 int   lexer_lexfile(uint8_t*);
 extern //lexer.rl
 int   lexer_lexdir(uint8_t*);
 extern //lexer.rl
 void  lexer_closedir(void);
-
-/* Init
-   Establishes yy states
-*/
-int scanner_init
-( void )
-{ if (apc_pstate != NULL || apc_cstate != NULL)
-    scanner_quit();
-  apc_pstate = yypstate_new();
-  apc_cstate = yycstate_new();
-  return (apc_pstate == NULL || apc_cstate == NULL);
-}
-
-/* Quit
-   Free initialized memory
-*/
-void scanner_quit
-( void )
-{ yypstate_delete(apc_pstate);
-  yycstate_delete(apc_cstate);
-  apc_pstate = NULL;
-  apc_cstate = NULL;
-}
-
 /* Scan the provided path
    Changes working directory to the provided pathname and, if successful, sends
    a directory stream of the provided path to scanner_scandir
@@ -84,6 +63,17 @@ int scanner_scanpath
 */
 int scanner_scandir
 ( DIR* dirp )
+{ int scandir_status;
+  if (lexer_init())
+    return -1;
+  scandir_status = scanner_scandir_r(dirp);
+  lexer_quit();
+  return scandir_status;
+}
+
+static
+int scanner_scandir_r
+( DIR* dirp )
 { DIR* cdirp;
   struct dirent* direntp;
  scan_next_dirent:
@@ -105,7 +95,7 @@ int scanner_scandir
            errno = 0;
            if ((cdirp = opendir(".")) == NULL || errno) //open it
              goto libfail;
-           if(scanner_scandir(cdirp))    //scan the directory
+           if(scanner_scandir_r(cdirp))  //scan the directory
              goto libfail;
            if (chdir(".."))              //return to the parent dir
              goto libfail;