From af780dc32cbc9d9a40bec1e2ea538e71001c36aa Mon Sep 17 00:00:00 2001 From: thing1 Date: Fri, 14 Mar 2025 17:37:27 +0000 Subject: init commit --- lex.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 lex.c (limited to 'lex.c') diff --git a/lex.c b/lex.c new file mode 100644 index 0000000..db16db0 --- /dev/null +++ b/lex.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +#include "types.h" + +#define size(arr) sizeof(arr) / sizeof(arr[0]) + +char *keywords[] = { + "int", + "char", + "return", + "for", + "while", + "if", + "else", + "elif" +}; + + +char *saveptr = NULL; + +int lexchar(lexobj *l, char *line, char c) { + if (line[0] == c) { + l->data = NULL; + l->t = c; + saveptr = line + 1; + return 1; + } + return 0; +} + +int lexchars(lexobj *l, char *line, char *cs) { + for (int i = 0; i < strlen(cs); i++) { + if (lexchar(l, line, cs[i])) return 1; + } + return 0; +} + +lexobj lex(char *line) { +removepadding: + if (line[0] == ' ') line++; + if (line[0] == ' ') goto removepadding; + + static lexobj l; + + if (isdigit(line[0])) { + static char num[256]; + int ptr = 0; + while (isdigit(line[0])) { + num[ptr] = line[0]; + ptr++; + line++; + } + num[ptr] = 0; + saveptr = line; + l.data = num; + l.t = INTLIT; + return l; + } + + if (lexchars(&l, line, "(){};+-*/")) return l; + + for (int i = 0; i < size(keywords); i++) { + if (strstr(line, keywords[i]) == line) { + line += strlen(keywords[i]); + saveptr = line; + if (line[0] != ' ') goto retry; + l.data = keywords[i]; + l.t = KEYWORD; + return l; + } +retry: + continue; + } + + char *data = strchr(line, '('); + *data = 0; + l.data = strdup(line); + line += data - line; + *data = '('; + saveptr = line; + + return l; +} + +void stripwhitespace(char *line) { + char *out = malloc(256); + int ptr = 0; + for (int i = 0; i < strlen(line); i++) { + if (line[i] == '\t' || line[i] == '\n' + || line[i] == '\v') continue; + else { + out[ptr] = line[i]; + ptr++; + } + } + out[ptr] = 0; + + memcpy(line, out, 256); +} -- cgit v1.2.3