diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2025-03-14 17:37:27 +0000 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2025-03-14 17:37:27 +0000 |
commit | af780dc32cbc9d9a40bec1e2ea538e71001c36aa (patch) | |
tree | 6e1386faa63125ba5537e923b933868699df3156 /lex.c |
Diffstat (limited to 'lex.c')
-rw-r--r-- | lex.c | 102 |
1 files changed, 102 insertions, 0 deletions
@@ -0,0 +1,102 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#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); +} |