defined all operators + build appropriate maps

This commit is contained in:
mrexodia 2016-06-05 11:23:58 +02:00
parent d63737fd2d
commit 7d2384b265
No known key found for this signature in database
GPG Key ID: D72F9A4FAA0073B4
4 changed files with 110 additions and 7 deletions

View File

@ -20,6 +20,7 @@
<ClInclude Include="filehelper.h" /> <ClInclude Include="filehelper.h" />
<ClInclude Include="handle.h" /> <ClInclude Include="handle.h" />
<ClInclude Include="keywords.h" /> <ClInclude Include="keywords.h" />
<ClInclude Include="operators.h" />
<ClInclude Include="stringutils.h" /> <ClInclude Include="stringutils.h" />
<ClInclude Include="testfiles.h" /> <ClInclude Include="testfiles.h" />
</ItemGroup> </ItemGroup>

View File

@ -44,5 +44,8 @@
<ClInclude Include="keywords.h"> <ClInclude Include="keywords.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="operators.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -10,13 +10,17 @@
#include "stringutils.h" #include "stringutils.h"
#include "testfiles.h" #include "testfiles.h"
#define MAKE_OP_TRIPLE(ch1, ch2, ch3) (ch3 << 16 | ch2 << 8 | ch1)
#define MAKE_OP_DOUBLE(ch1, ch2) (ch2 << 8 | ch1)
#define MAKE_OP_SINGLE(ch1) (ch1)
using namespace std; using namespace std;
struct Lexer struct Lexer
{ {
explicit Lexer() explicit Lexer()
{ {
SetupKeywordMap(); SetupTokenMaps();
} }
enum Token enum Token
@ -33,7 +37,16 @@ struct Lexer
//others //others
tok_identifier, //[a-zA-Z_][a-zA-Z0-9_] tok_identifier, //[a-zA-Z_][a-zA-Z0-9_]
tok_number, //(0x[0-9a-fA-F]+)|([0-9]+) tok_number, //(0x[0-9a-fA-F]+)|([0-9]+)
tok_stringlit //"([^\\"\r\n]|\\[\\"abfnrtv])*" tok_stringlit, //"([^\\"\r\n]|\\[\\"abfnrtv])*"
//operators
#define DEF_OP_TRIPLE(enumval, ch1, ch2, ch3) enumval,
#define DEF_OP_DOUBLE(enumval, ch1, ch2) enumval,
#define DEF_OP_SINGLE(enumval, ch1) enumval,
#include "operators.h"
#undef DEF_OP_TRIPLE
#undef DEF_OP_DOUBLE
#undef DEF_OP_SINGLE
}; };
vector<uint8_t> Input; vector<uint8_t> Input;
@ -62,16 +75,39 @@ struct Lexer
} }
unordered_map<string, Token> KeywordMap; unordered_map<string, Token> KeywordMap;
unordered_map<Token, string> ReverseKeywordMap; unordered_map<Token, string> ReverseTokenMap;
unordered_map<int, Token> OpTripleMap;
unordered_map<int, Token> OpDoubleMap;
unordered_map<int, Token> OpSingleMap;
void SetupKeywordMap() void SetupTokenMaps()
{ {
//setup keyword map
#define DEF_KEYWORD(keyword) KeywordMap[#keyword] = tok_##keyword; #define DEF_KEYWORD(keyword) KeywordMap[#keyword] = tok_##keyword;
#include "keywords.h" #include "keywords.h"
#undef DEF_KEYWORD #undef DEF_KEYWORD
#define DEF_KEYWORD(keyword) ReverseKeywordMap[tok_##keyword] = "tok_" #keyword;
//setup token maps
#define DEF_OP_TRIPLE(enumval, ch1, ch2, ch3) OpTripleMap[MAKE_OP_TRIPLE(ch1, ch2, ch3)] = enumval;
#define DEF_OP_DOUBLE(enumval, ch1, ch2) OpDoubleMap[MAKE_OP_DOUBLE(ch1, ch2)] = enumval;
#define DEF_OP_SINGLE(enumval, ch1) OpSingleMap[MAKE_OP_SINGLE(ch1)] = enumval;
#include "operators.h"
#undef DEF_OP_TRIPLE
#undef DEF_OP_DOUBLE
#undef DEF_OP_SINGLE
//setup reverse token maps
#define DEF_KEYWORD(keyword) ReverseTokenMap[tok_##keyword] = "tok_" #keyword;
#include "keywords.h" #include "keywords.h"
#undef DEF_KEYWORD #undef DEF_KEYWORD
#define DEF_OP_TRIPLE(enumval, ch1, ch2, ch3) ReverseTokenMap[enumval] = #enumval;
#define DEF_OP_DOUBLE(enumval, ch1, ch2) ReverseTokenMap[enumval] = #enumval;
#define DEF_OP_SINGLE(enumval, ch1) ReverseTokenMap[enumval] = #enumval;
#include "operators.h"
#undef DEF_OP_TRIPLE
#undef DEF_OP_DOUBLE
#undef DEF_OP_SINGLE
} }
Token ReportError(const String & error) Token ReportError(const String & error)
@ -96,8 +132,8 @@ struct Lexer
case tok_stringlit: return StringUtils::sprintf("tok_stringlit \"%s\"", StringUtils::Escape(StringLit).c_str()); case tok_stringlit: return StringUtils::sprintf("tok_stringlit \"%s\"", StringUtils::Escape(StringLit).c_str());
default: default:
{ {
auto found = ReverseKeywordMap.find(Token(tok)); auto found = ReverseTokenMap.find(Token(tok));
if (found != ReverseKeywordMap.end()) if (found != ReverseTokenMap.end())
return found->second; return found->second;
if (tok > 0 && tok < 265) if (tok > 0 && tok < 265)
{ {

63
cparser/operators.h Normal file
View File

@ -0,0 +1,63 @@
#ifndef DEF_OP_TRIPLE
#define DEF_OP_TRIPLE(enumval, ch1, ch2, ch3)
#endif //DEF_OP_TRIPLE
#ifndef DEF_OP_DOUBLE
#define DEF_OP_DOUBLE(enumval, ch1, ch2)
#endif //DEF_OP_DOUBLE
#ifndef DEF_OP_SINGLE
#define DEF_OP_SINGLE(enumval, ch1)
#endif //DEF_OP_SINGLE
DEF_OP_TRIPLE(tok_assign_shl, '<', '<', '=')
DEF_OP_TRIPLE(tok_assign_shr, '>', '>', '=')
DEF_OP_DOUBLE(tok_op_inc, '+', '+')
DEF_OP_DOUBLE(tok_op_dec, '-', '-')
DEF_OP_DOUBLE(tok_op_shl, '<', '<')
DEF_OP_DOUBLE(tok_op_shr, '>', '>')
DEF_OP_DOUBLE(tok_lop_less_equal, '<', '=')
DEF_OP_DOUBLE(tok_lop_greater_equal, '>', '=')
DEF_OP_DOUBLE(tok_lop_equal, '=', '=')
DEF_OP_DOUBLE(tok_lop_not_equal, '!', '=')
DEF_OP_DOUBLE(tok_lop_and, '&', '&')
DEF_OP_DOUBLE(tok_lop_or, '|', '|')
DEF_OP_DOUBLE(tok_assign_plus, '+', '=')
DEF_OP_DOUBLE(tok_assign_min, '-', '=')
DEF_OP_DOUBLE(tok_assign_mul, '*', '=')
DEF_OP_DOUBLE(tok_assign_div, '/', '=')
DEF_OP_DOUBLE(tok_assign_mod, '%', '=')
DEF_OP_DOUBLE(tok_assign_and, '&', '=')
DEF_OP_DOUBLE(tok_assign_xor, '^', '=')
DEF_OP_DOUBLE(tok_assign_or, '|', '=')
DEF_OP_SINGLE(tok_parenopen, '(')
DEF_OP_SINGLE(tok_parenclose, ')')
DEF_OP_SINGLE(tok_blockopen, '{')
DEF_OP_SINGLE(tok_blockclose, '}')
DEF_OP_SINGLE(tok_annotopen, '<')
DEF_OP_SINGLE(tok_annotclose, '>')
DEF_OP_SINGLE(tok_subopen, '[')
DEF_OP_SINGLE(tok_subclose, ']')
DEF_OP_SINGLE(tok_memberaccess, '.')
DEF_OP_SINGLE(tok_comma, ',')
DEF_OP_SINGLE(tok_tenary1, '?')
DEF_OP_SINGLE(tok_tenary2, ':')
DEF_OP_SINGLE(tok_assign, '=')
DEF_OP_SINGLE(tok_op_mul, '*')
DEF_OP_SINGLE(tok_op_div, '/')
DEF_OP_SINGLE(tok_op_mod, '%')
DEF_OP_SINGLE(tok_op_plus, '+')
DEF_OP_SINGLE(tok_op_min, '-')
DEF_OP_SINGLE(tok_op_neg, '~')
DEF_OP_SINGLE(tok_op_xor, '^')
DEF_OP_SINGLE(tok_op_and, '&')
DEF_OP_SINGLE(tok_op_or, '|')
DEF_OP_SINGLE(tok_lop_less, '<')
DEF_OP_SINGLE(tok_lop_greater, '>')
DEF_OP_SINGLE(tok_lop_not, '!')