Author | Jakob Wakeling <[email protected]> |
Date | 2020-07-02 00:27:43 |
Commit | 1d938fdb1f0f41cbdbccaa53de636f05d8ee46c4 |
Parent | 7d3dca29ad94068f748ee06a23d27973b26cccae |
Format lexer switch
Diffstat
M | src/parse.c | | | 32 | +++++++++++++++----------------- |
1 files changed, 15 insertions, 17 deletions
diff --git a/src/parse.c b/src/parse.c index 154453a..4a0b3d0 100644 --- a/src/parse.c +++ b/src/parse.c @@ -46,22 +46,20 @@ static inline tok_t lex(char *str) { static char *p; static size_t line, col; tok_t tok; if (str) { p = str; line = 0; col = 0; } tok.num = 1; - for (; *p; ++p) { - switch (*p) { - case '>': { TOKSET(RGT); goto count; } - case '<': { TOKSET(LFT); goto count; } - case '+': { TOKSET(ADD); goto count; } - case '-': { TOKSET(SUB); goto count; } - case '.': { TOKSET(PUT); ++p; ++col; return tok; } - case ',': { TOKSET(GET); ++p; ++col; return tok; } - case '[': { TOKSET(OPN); ++p; ++col; return tok; } - case ']': { TOKSET(CLS); ++p; ++col; return tok; } - case '\n': { ++line; col = 0; continue; } - count: { - for (register char c = *p++; *p == c; ++p) { ++tok.num; } - col += tok.num; return tok; - } - } + for (; *p; ++p) switch (*p) { + case '>': { TOKSET(RGT); goto count; } + case '<': { TOKSET(LFT); goto count; } + case '+': { TOKSET(ADD); goto count; } + case '-': { TOKSET(SUB); goto count; } + case '.': { TOKSET(PUT); ++p; ++col; return tok; } + case ',': { TOKSET(GET); ++p; ++col; return tok; } + case '[': { TOKSET(OPN); ++p; ++col; return tok; } + case ']': { TOKSET(CLS); ++p; ++col; return tok; } + case '\n': { ++line; col = 0; continue; } + count: { + for (register char c = *p++; *p == c; ++p) { ++tok.num; } + col += tok.num; return tok; + } } tok.typ = END; return tok; @@ -72,7 +70,7 @@ tok_t *parse(char *str, size_t len) { if (!toks) { return NULL; } register tok_t *p = toks; - *p = lex(str); while (p->typ != EOF) { *++p = lex(NULL); } + *p = lex(str); while (p->typ != END) { *++p = lex(NULL); } return toks; }