]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
CP: Token reader is working started to work on token parser
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 30 Apr 2014 21:00:59 +0000 (17:00 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 30 Apr 2014 21:00:59 +0000 (17:00 -0400)
source/slvm/main.c
source/slvm/parser.c

index 1af77d9d8c93233208d24199be3b6139d4503eb7..fe5a629bf8ecac14f3e27e4f06b0be493d1d1166 100644 (file)
@@ -156,6 +156,7 @@ defcode("fetch", _fetch, 0, &_fpeekc){
 defcode("parse", _parse, 0, &_fetch){
     ArgStackPtr++;
     *(ArgStackPtr-1) = (val_t)parse( (char*)*(ArgStackPtr-1), ArgStackPtr );
+    //EXEC(swap);
 }
 // defcode("interp", , 0, &){}
 
index e98577ce579158645079b45a1cd6a79df643a109..46f5924b57a1829392aa3e4a75d533f63abe63af 100644 (file)
 #include <stdio.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 
 /* Fetching Tokens
  *****************************************************************************/
 static void skip_whitespace(FILE* input);
 static void skip_comment(FILE* input);
-static char* parse_string(FILE* input);
-static char* parse_token(FILE* input);
-static void grow_token(size_t* p_size, size_t* p_index, char** p_p_str, char ch);
+static char* read_string(FILE* input);
+static char* read_token(FILE* input);
+//static void grow_token(size_t* p_size, size_t* p_index, char** p_p_str, char ch);
+static char* grow_token(size_t* p_size, size_t* p_index, char* p_str, char ch);
 static bool is_whitespace(FILE* input);
 static char fpeekc(FILE* input);
 
 char* fetch_token(FILE* input)
 {
     char* result = NULL;
+    puts("skipping whitespace");
     skip_whitespace(input);
+    puts("reading token");
     switch(fpeekc(input))
     {
-        case '#':  skip_comment(input);
-                   result = fetch_token(input);
-                   break;
-        case '"':  result = parse_string(input);
-                   break;
-        case EOF:  break;
-        default:   result = parse_token(input);
-                   break;
+        case '#':
+            skip_comment(input);
+            result = fetch_token(input);
+            break;
+
+        case '"':
+            result = read_string(input);
+            break;
+
+        case EOF:
+            break;
+
+        default:
+            puts("reading word num or char");
+            result = read_token(input);
+            break;
 
     }
+    puts("returning token");
     return result;
 }
 
-static char* parse_string(FILE* input)
+static char* read_string(FILE* input)
 {
     size_t strsize  = 8;
     size_t strindex = 0;
     char*  string   = (char*)malloc(strsize);
-    grow_token(&strsize, &strindex, &string, fgetc(input));
+
+    string = grow_token(&strsize, &strindex, string, fgetc(input));
     while('"' != fpeekc(input))
     {
         if(feof(input)) {
             free(string);
             return NULL;
         }
-        grow_token(&strsize, &strindex, &string, fgetc(input));
+        string = grow_token(&strsize, &strindex, string, fgetc(input));
     }
-    grow_token(&strsize, &strindex, &string, fgetc(input));
+    string = grow_token(&strsize, &strindex, string, fgetc(input));
+
     return string;
 }
 
-static char* parse_token(FILE* input)
+static char* read_token(FILE* input)
 {
     size_t strsize  = 8;
     size_t strindex = 0;
     char*  string   = (char*)malloc(strsize);
+
     while(!is_whitespace(input))
     {
         if(feof(input)) {
             free(string);
             return NULL;
         }
-        grow_token(&strsize, &strindex, &string, fgetc(input));
+        string = grow_token(&strsize, &strindex, string, fgetc(input));
     }
+
     return string;
 }
 
-static void grow_token(size_t* p_size, size_t* p_index, char** p_p_str, char ch)
+static char* grow_token(size_t* p_size, size_t* p_index, char* p_str, char ch)
 {
-    /* If we don't have enough room for the new char */
-    if( (*(p_index)+1) >= *p_size )
+    /* If we're about to be too big for the string */
+    if( (*(p_index) + 1) >= *(p_size) )
     {
         /* Double the string size */
         *(p_size) *= 2;
-        *(p_p_str) = (char*)realloc( *(p_p_str), *(p_size) );
+        p_str = (char*)realloc( p_str, *(p_size) );
     }
-    /* Add the new char and null terminate the string */
-    *(p_p_str)[*(p_index)++] = ch;
-    *(p_p_str)[*(p_index)]   = '\0';
+    /* Set the current char to the provided value and null terminate the str */
+    p_str[ *(p_index) ]  = ch;
+    p_str[ *(p_index)+1] = '\0';
+    *(p_index) += 1;
+    /* Return the (possibly moved) string */
+    return p_str;
 }
 
 static void skip_whitespace(FILE* input)
@@ -117,47 +137,61 @@ static char fpeekc(FILE* input)
 
 /* Parsing Tokens
  *****************************************************************************/
-static bool is_float(val_t* p_val);
+//static bool is_float(val_t* p_val);
 static bool is_integer(val_t* p_val);
-static bool is_string(val_t* p_val);
-static bool is_char(val_t* p_val);
+//static bool is_string(val_t* p_val);
+//static bool is_char(val_t* p_val);
 
 TokenType_T parse(char* str, val_t* p_val)
 {
     TokenType_T type = ERROR;
     if(str != NULL)
     {
-        if(!is_float(p_val) &&
-           !is_integer(p_val) &&
-           !is_string(p_val) &&
-           !is_char(p_val))
+        puts(str);
+        if(is_integer(p_val))
+        {
+            type = INTEGER;
+        }
+        else
         {
             type = WORD;
             *(p_val) = (val_t)str;
         }
+        //if(!is_float(p_val) &&
+        //   !is_integer(p_val) &&
+        //   !is_string(p_val) &&
+        //   !is_char(p_val))
+        //{
+        //    type = WORD;
+        //    *(p_val) = (val_t)str;
+        //}
     }
     return type;
 }
 
 
-static bool is_float(val_t* p_val)
-{
-    return false;
-}
+//static bool is_float(val_t* p_val)
+//{
+//    return false;
+//}
 
 static bool is_integer(val_t* p_val)
 {
-    return false;
+    char* str = (char*)(*p_val);
+    char* end;
+    *(p_val) = (val_t)strtol(str,&end,0);
+    printf("%#x == %#x\n", end, &(str[strlen(str)-1]));
+    return (end == &(str[strlen(str)-1]));
 }
 
-static bool is_string(val_t* p_val)
-{
-    return false;
-}
+//static bool is_string(val_t* p_val)
+//{
+//    return false;
+//}
 
-static bool is_char(val_t* p_val)
-{
-    return false;
-}
+//static bool is_char(val_t* p_val)
+//{
+//    return false;
+//}