From 4b7c7ef890804be98a3429ffa706dac519ed2027 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Fri, 8 Mar 2013 23:48:01 -0500 Subject: [PATCH] Implemented proper hexadecimal syntax --- premake4.lua | 12 ++++++-- source/lexer/classes.c | 1 - source/lexer/lex.c | 62 ++++++++++++++++++++++++++++++++---------- source/lexer/lex.h | 8 +++++- 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/premake4.lua b/premake4.lua index 34289b2..7ab8603 100644 --- a/premake4.lua +++ b/premake4.lua @@ -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" diff --git a/source/lexer/classes.c b/source/lexer/classes.c index 4e088df..f2fc3c4 100644 --- a/source/lexer/classes.c +++ b/source/lexer/classes.c @@ -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')); } diff --git a/source/lexer/lex.c b/source/lexer/lex.c index 4ff5e96..cc697af 100644 --- a/source/lexer/lex.c +++ b/source/lexer/lex.c @@ -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]); diff --git a/source/lexer/lex.h b/source/lexer/lex.h index ef4a08d..62d8977 100644 --- a/source/lexer/lex.h +++ b/source/lexer/lex.h @@ -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 */ -- 2.52.0