]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Implemented proper hexadecimal syntax
authorMike D. Lowis <mike@mdlowis.com>
Sat, 9 Mar 2013 04:48:01 +0000 (23:48 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Sat, 9 Mar 2013 04:48:01 +0000 (23:48 -0500)
premake4.lua
source/lexer/classes.c
source/lexer/lex.c
source/lexer/lex.h

index 34289b21c4f7f51c1dcfcfdae346492aada5d31c..7ab860356c70a386a351b96ac687a04138f39c5b 100644 (file)
@@ -49,8 +49,16 @@ project "sclpl-parse"
     kind "ConsoleApp"
     language "C"
     location "build"
-    includedirs { "source/lexer/**", "source/runtime/**" }
-    files { "source/parser/**.*", "source/runtime/collector/**.*"}
+    includedirs {
+        "source/lexer/**",
+        "source/runtime/**",
+        "source/common/**"
+    }
+    files {
+        "source/parser/**.*",
+        "source/common/**.*",
+        "source/runtime/collector/**.*"
+    }
 
 project "sclpl-parse-tests"
     kind "ConsoleApp"
index 4e088df1f0353e1bd62b847f40b13898b7842f78..f2fc3c4006a45eef382a5f89dcab5a9c94718fb2 100644 (file)
@@ -24,7 +24,6 @@ bool hex_digit(void)
 {
     char ch = file_peek();
     return (('0' <= ch) && (ch <= '9')) ||
-           (('a' <= ch) && (ch <= 'f')) ||
            (('A' <= ch) && (ch <= 'F'));
 }
 
index 4ff5e96bda2c047e497858c924b4f7b3bb97582e..cc697af9095a961383260362de040fc9b291d6cf 100644 (file)
@@ -20,11 +20,13 @@ static void abort(void);
 static void reset(void);
 static void match_and_consume(char ch);
 static bool one_or_more(predicate_t pfn);
+static void keyword(void);
 static void comment(void);
 static void punctuation(void);
 static void number(void);
 static void hexadecimal(void);
 static void floating_point(void);
+static void exponent(void);
 static void identifier(void);
 
 /* Global Variables
@@ -42,6 +44,14 @@ const char* Types[TOK_MAX] = {
     "LBRACE", /* TOK_LBRACE */
     "RBRACE", /* TOK_RBRACE */
     "TERM",   /* TOK_TERM */
+    "BOOL",   /* TOK_BOOL */
+};
+
+const lex_keyword_t Keywords[] = {
+    { "end",   TOK_TERM },
+    { "true",  TOK_BOOL },
+    { "false", TOK_BOOL },
+    { NULL,    TOK_MAX}
 };
 
 /* Control Functions
@@ -101,7 +111,7 @@ void next_token(tok_t* p_token)
 
             if (matches_any("()[]{};"))
                 punctuation();
-            else if (matches('-') || digit() || matches('h'))
+            else if (matches('-') || digit())
                 number();
             //else if (matches('\''))
             //    character();
@@ -114,15 +124,26 @@ void next_token(tok_t* p_token)
         {
             identifier();
         }
-
-        /* the keyword "end" is actually a TOK_TERM */
-        if (0 == strcmp(tok_string(),"end"))
-            tok_set_type(Types[TOK_TERM]);
-
+        keyword();
     }
     tok_copy( p_token );
 }
 
+static void keyword(void)
+{
+    const char* p_text = tok_string();
+    int i = 0;
+    while ( Keywords[i].p_text != NULL)
+    {
+        if (0 == strcmp( p_text, Keywords[i].p_text ))
+        {
+            tok_set_type( Types[ Keywords[i].type ] );
+            break;
+        }
+        i++;
+    }
+}
+
 static void comment(void)
 {
     while (!matches('\n'))
@@ -149,25 +170,32 @@ static void punctuation(void)
 static void number(void)
 {
     tok_set_type(Types[TOK_NUM]);
-    if (matches('h'))
+    if (matches('0'))
     {
-        hexadecimal();
-    }
-    else
-    {
-        floating_point();
-        if (matches_any("eE"))
+        tok_consume();
+        if (matches('x'))
         {
             tok_consume();
+            hexadecimal();
+        }
+        else if (matches('-'))
+            abort();
+        else if (!token_end())
+        {
             floating_point();
+            if (!token_end()) exponent();
         }
     }
+    else
+    {
+        floating_point();
+        if (!token_end()) exponent();
+    }
     accept();
 }
 
 static void hexadecimal(void)
 {
-    match_and_consume('h');
     one_or_more( hex_digit );
 }
 
@@ -182,6 +210,12 @@ static void floating_point(void)
     }
 }
 
+static void exponent(void)
+{
+    match_and_consume('e');
+    floating_point();
+}
+
 static void identifier(void)
 {
     tok_set_type(Types[TOK_ID]);
index ef4a08d32eb4163a6074b603b675c79be0685f10..62d89774cb0c3707dc567733b474859da2dd6057 100644 (file)
@@ -21,9 +21,15 @@ typedef enum {
     TOK_LBRACE = 7,
     TOK_RBRACE = 8,
     TOK_TERM   = 9,
-    TOK_MAX    = 10,
+    TOK_BOOL   = 10,
+    TOK_MAX    = 11,
 } lex_tok_t;
 
+typedef struct {
+    const char* p_text;
+    tok_type_t type;
+} lex_keyword_t;
+
 void next_token(tok_t* p_token);
 
 #endif /* LEX_H */