summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-09-30 23:34:09 -0400
committerIan Moffett <ian@osmora.org>2025-09-30 23:34:09 -0400
commitde12f97c8d41898255c7d9eed2c24b3c567b5747 (patch)
tree65225dd673f3750d8c67b039012a82976442b10f /src
parent9745e110b69bcf7e32896c444ddc4acd9ed0f8bf (diff)
np: lex: Break up big token switch-case
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r--src/sys/np/core/np_lex.c81
1 files changed, 63 insertions, 18 deletions
diff --git a/src/sys/np/core/np_lex.c b/src/sys/np/core/np_lex.c
index 6e252a3..e6dd07c 100644
--- a/src/sys/np/core/np_lex.c
+++ b/src/sys/np/core/np_lex.c
@@ -157,6 +157,64 @@ lex_matchstr(struct np_work *work, char c, struct lex_token *res)
}
/*
+ * Scan arithmetic operators and return the token type
+ * (tt_t) on success
+ *
+ * @work: Input work
+ * @c: Character to check
+ *
+ * Returns a less than zero value on failure
+ */
+static int
+lex_arithop(struct np_work *work, char c, struct lex_token *res)
+{
+ switch (c) {
+ case '*':
+ res->token = TT_STAR;
+ break;
+ case '-':
+ res->token = TT_MINUS;
+ break;
+ case '+':
+ res->token = TT_PLUS;
+ break;
+ case '/':
+ res->token = TT_SLASH;
+ break;
+ default:
+ return -1;
+ }
+
+ return res->token;
+}
+
+/*
+ * Scan compare operators and return the token type
+ * (tt_t) on success
+ *
+ * @work: Input work
+ * @c: Character to check
+ *
+ * Returns a less than zero value on failure
+ */
+static int
+lex_cmpop(struct np_work *work, char c, struct lex_token *res)
+{
+ switch (c) {
+ case '>':
+ res->token = TT_GT;
+ break;
+ case '<':
+ res->token = TT_LT;
+ break;
+ default:
+ return -1;
+ }
+
+ return res->token;
+}
+
+/*
* Nom a token
*/
int
@@ -194,28 +252,15 @@ lex_nom(struct np_work *work, struct lex_token *res)
case ',':
res->token = TT_COMMA;
break;
- case '*':
- res->token = TT_STAR;
- break;
- case '-':
- res->token = TT_MINUS;
- break;
- case '+':
- res->token = TT_PLUS;
- break;
- case '/':
- res->token = TT_SLASH;
- break;
case '=':
res->token = TT_EQUALS;
break;
- case '>':
- res->token = TT_GT;
- break;
- case '<':
- res->token = TT_LT;
- break;
default:
+ if (lex_arithop(work, c, res) >= 0)
+ break;
+ if (lex_cmpop(work, c, res) >= 0)
+ break;
+
/* Stuff like '1var_name' is invalid */
if (!is_alpha(c)) {
pr_error("unexpected token '%c'\n", c);