if-else chain now is an iteration over a table
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
unicode('');
|
|
||||||
+24
-52
@@ -218,59 +218,31 @@ static Token *tokenize(Source *src, BPAlloc *alloc, Error *error)
|
|||||||
|
|
||||||
tok->length = i - tok->offset;
|
tok->length = i - tok->offset;
|
||||||
|
|
||||||
#define matchstr(str, len, const_str) \
|
static const struct {
|
||||||
(len == sizeof(const_str)-1 && !strncmp(str, const_str, sizeof(const_str)-1))
|
TokenKind kind;
|
||||||
|
int size;
|
||||||
if(matchstr(str + tok->offset, tok->length, "if"))
|
const char *text;
|
||||||
{
|
} kwords[] = {
|
||||||
tok->kind = TKWIF;
|
{ TKWIF, 2, "if" },
|
||||||
}
|
{ TKWFUN, 3, "fun" },
|
||||||
else if(matchstr(str + tok->offset, tok->length, "fun"))
|
{ TKWNOT, 3, "not" },
|
||||||
{
|
{ TKWAND, 3, "and" },
|
||||||
tok->kind = TKWFUN;
|
{ TKWOR, 2, "or" },
|
||||||
}
|
{ TKWELSE, 4, "else" },
|
||||||
else if(matchstr(str + tok->offset, tok->length, "not"))
|
{ TKWNONE, 4, "none" },
|
||||||
{
|
{ TKWTRUE, 4, "true" },
|
||||||
tok->kind = TKWNOT;
|
{ TKWFALSE, 5, "false" },
|
||||||
}
|
{ TKWRETURN, 6, "return" },
|
||||||
else if(matchstr(str + tok->offset, tok->length, "and"))
|
{ TKWWHILE, 5, "while" },
|
||||||
{
|
{ TKWDO, 2, "do" },
|
||||||
tok->kind = TKWAND;
|
};
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "or"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWOR;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "else"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWELSE;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "none"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWNONE;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "true"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWTRUE;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "false"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWFALSE;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "return"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWRETURN;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "while"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWWHILE;
|
|
||||||
}
|
|
||||||
else if(matchstr(str + tok->offset, tok->length, "do"))
|
|
||||||
{
|
|
||||||
tok->kind = TKWDO;
|
|
||||||
}
|
|
||||||
|
|
||||||
#undef matchstr
|
for(unsigned int i = 0; i < sizeof(kwords)/sizeof(*kwords); i += 1)
|
||||||
|
if(kwords[i].size == tok->length && !strncmp(kwords[i].text, str + tok->offset, tok->length))
|
||||||
|
{
|
||||||
|
tok->kind = kwords[i].kind;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(isdigit(str[i]))
|
else if(isdigit(str[i]))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user