harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 3c38ec45c2a972357866ef107b118538c3e0d23a
parent 5bab90a0b147434119ec607cff42664cee3c15e1
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed,  7 Oct 2020 20:00:55 -0400

lex: add lex2

Diffstat:
Msrc/lex.c | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/main.c | 3++-
2 files changed, 114 insertions(+), 3 deletions(-)

diff --git a/src/lex.c b/src/lex.c @@ -98,7 +98,7 @@ static const char *tokens[] = { [T_SLICE] = "..", [T_TIMES] = "*", [T_TIMESEQ] = "*=", - [T_XOR] = "^=", + [T_XOR] = "^", [T_XOREQ] = "^=", }; @@ -212,6 +212,115 @@ lex_literal(struct lexer *lexer, struct token *out) assert(0); // TODO } +static int +lex2(struct lexer *lexer, struct token *out) +{ + int c = next(lexer, false), d = next(lexer, false); + assert(c != EOF); + + switch (c) { + case '^': + switch (d) { + case '=': + out->token = T_XOREQ; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_XOR; + break; + } + return d; + case '*': + switch (d) { + case '=': + out->token = T_TIMESEQ; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_TIMES; + break; + } + return d; + case '%': + switch (d) { + case '=': + out->token = T_MODEQ; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_MODULO; + break; + } + return d; + case '/': + switch (d) { + case '=': + out->token = T_DIVEQ; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_DIV; + break; + } + return d; + case '+': + switch (d) { + case '=': + out->token = T_PLUSEQ; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_PLUS; + break; + } + return d; + case '-': + switch (d) { + case '=': + out->token = T_MINUSEQ; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_MINUS; + break; + } + return d; + case ':': + switch (d) { + case ':': + out->token = T_DOUBLE_COLON; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_COLON; + break; + } + return d; + case '!': + switch (d) { + case '=': + out->token = T_NEQUAL; + break; + case EOF: + default: + push(lexer, d, false); + out->token = T_LNOT; + break; + } + return d; + } + + out->token = T_ERROR; // Right? + return d; +} + int lex(struct lexer *lexer, struct token *out) { @@ -251,7 +360,8 @@ lex(struct lexer *lexer, struct token *out) case '-': // - -= case ':': // : :: case '!': // ! != - assert(0); // TODO + push(lexer, c, false); + return lex2(lexer, out); case '~': out->token = T_BNOT; break; diff --git a/src/main.c b/src/main.c @@ -8,7 +8,8 @@ main(int argc, char *argv[]) lex_init(&lexer, stdin); struct token tok; - while (lex(&lexer, &tok) != EOF) { + while (tok.token != T_EOF) { + lex(&lexer, &tok); switch (tok.token) { case T_NAME: fprintf(stderr, "'%s'\n", tok.name);