ir compiling!
[henge/apc.git] / src / lexer.rl
index 36a72ab..f6d25aa 100644 (file)
@@ -12,13 +12,23 @@ void lexer_pushtok(int, YYSTYPE);
 int lexer_setdirection(uint8_t*, int);
 int lexer_lexstring(uint8_t*, int);
 int lexer_setstr(uint8_t*, int);
+//apc.c
+extern
+yypstate* apc_pstate;
+extern
+yycstate* apc_cstate;
+
+/* Lexstring is the main lexer for APC and is generated by ragel. It lexes file names of files 
+   that have been scanned and pushes their types and values into the tok_stack, which yyparse 
+   eventually calls during parsing. */
 
 %%{    
   machine lexstring;
 
   # set up yylval and tok_t to be pushed to stack
-  action push_ref       { te = NULL; errno = 0;
-                          yylval.ref = strtoll((char*)ts,(char**)&te,16);
+  action push_ref       { long long ref;
+                          te = NULL; errno = 0;
+                          ref = strtoll((char*)ts,(char**)&te,16);
                          if (errno | (te != NULL))
                            { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
                              if (te != NULL)
@@ -28,65 +38,82 @@ int lexer_setstr(uint8_t*, int);
                                }
                              exit(1);
                            }
-                          lexer_pushtok(REF, yylval); ntok++;
+                         tok_val.ref = ref;
+                         yypush_parse(apc_pstate, REF, &tok_val, apc_cstate);
+                          ntok++;
                        }
-  action push_link      { lexer_pushtok(LINK,(YYSTYPE)0); ntok++; }
-  action push_val       { te = NULL; errno = 0;
-                          yylval.val = strtoll((char*)ts,(char**)&te,10);
+  action push_link      { tok_val.val = 0; yypush_parse(apc_pstate, LINK, &tok_val , apc_cstate);}
+  action push_val       { int val;
+                          te = NULL; errno = 0;
+                          val = strtoll((char*)ts,(char**)&te,10);
                          if (errno)
                            { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
                              exit(1);
                            }
-                         lexer_pushtok(NUM, yylval);
+                         tok_val.val = val;
+                         yypush_parse(apc_pstate, NUM, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_name      { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
-                          lexer_pushtok(NAME, yylval);
+                          *p = '\0';
+                         tok_val.str = ts;
+                          yypush_parse( apc_pstate, NAME, &tok_val, apc_cstate);
                          ntok++;
                         }
   action push_map       { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
-                          yylval.str = (uint8_t*) '~';
-                          lexer_pushtok(MOPEN, yylval);
-                         ntok++;
+                          tok_val.str = (uint8_t*) '~';
+                          yypush_parse(apc_pstate, MAP, &tok_val, apc_cstate);
+                          ntok++;
                        }
   action set_ts         { printf("Lexer_lexstring:: action:set_ts. ts = %s\n", p); ts = p; }
   action push_SS        { printf("Lexer_lexstring:: action:push_SS. p = %s\n",p);
-                          yylval.str = (uint8_t*) "SS";
-                          lexer_pushtok(SS, yylval);
-                         ntok++;
+                          tok_val.str = (uint8_t*) "SS";
+                          yypush_parse(apc_pstate, SS, &tok_val, apc_cstate);
+                          ntok++;
                         }
   action push_S         { printf("Lexer_lexstring:: action:push_S. p = %s\n", p);
-                          yylval.val = 0;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 0;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_SW        { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p);
-                          yylval.val = 1;
-                         lexer_pushtok(D, yylval);
-                        }
+                          tok_val.val = 1;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
+                       }
   action push_W         { printf("Lexer_lexstring:: action:push_W. p = %s\n", p);
-                          yylval.val = 2;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 2;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_NW        { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p);
-                          yylval.val = 3;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 3;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_N         { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
-                          yylval.val = 4;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 4;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_NE        { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p);
-                          yylval.val = 5;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 5;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_E         { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
-                          yylval.val = 6;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 6;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
                         }
   action push_SE        { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
-                          yylval.val = 7;
-                         lexer_pushtok(D, yylval);
+                          tok_val.val = 7;
+                          yypush_parse(apc_pstate, FACING, &tok_val, apc_cstate);
+                         ntok++;
+                        }
+  action ref_error      { printf("ref from %s to %s has an inappropriate amount of hex digits, it must have eight.\n", ts, p);
+                          exit(1);
                         }
-  #action lex_error      { printf("input error: character %c in filename %s is invalid\n p = %s\n", fc, str, p);}
   action p              { printf("Lexer_lexstring:: p = %s\n", p);}
     
   N = 'N' %push_N;
@@ -98,16 +125,13 @@ int lexer_setstr(uint8_t*, int);
   SW = 'SW' %push_SW;
   SE = 'SE' %push_SE;
 
-  #what goes in between tokens in a filename
   tok_delimiter = [_];
 
-  #types of tokes a filename can contain
   direction = (N | W | S | E | NW | NE | SW | SE) ;
-  #make sure 0x123123 doesnt get mistaken for a ref
   dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts  %push_val;
   link = '#'  %push_link;
   SS = ('+SS' %to(push_SS)) | ('+SS' %to(push_SS) link ) ;
-  ref = '0x' >set_ts alnum+ %push_ref;
+  ref = '0x' >set_ts alnum{8} $err(ref_error) %push_ref ;
   val = digit+ >set_ts %push_val ;
   name = (lower+ >set_ts) %push_name ;
   map = '+MAP' %to(push_map);
@@ -125,12 +149,14 @@ int lexer_lexstring
   int size
 )
 { uint8_t *p; 
-  uint8_t *ts, *pe, *te;
-  int cs, ntok;//, tok_t;
+  uint8_t *ts, *pe, *te, *eof;
+  int cs, ntok;
+  union YYSTYPE tok_val; 
 
   ntok = 0;
   p = ts = str;
-  pe = p + size + 1;
+  pe = eof =  p + size + 1;
+  
 
   printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);