commit 9f14011d13c1e9c2b20af43e06e9ece090bd3222
parent 0d5b94030b9645b15012f93669b05f07a110ccad
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Thu, 9 Oct 2025 12:35:07 +0100
added support for EOF
Diffstat:
4 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
CC=c89
-CFLAGS=-ggdb
+CFLAGS=-ggdb -pedantic -fsanitize=address
SRC = lex.c main.c
OBJ = ${SRC:.c=.o}
diff --git a/lex.c b/lex.c
@@ -16,7 +16,10 @@ lex_val *(*nextfn)(void);
int
getchr(FILE *in) {
int c;
- while (isblank((c = getc(in)))) continue;
+ while ((c = getc(in)) != EOF && (isblank(c) || c == '\n'))
+ continue;
+ if (c == EOF)
+ lex_error.type = EOI;
return c;
}
@@ -82,6 +85,7 @@ lex_semi() {
lex_val *
lex_assign() {
+ nextfn = &lex_value;
return lex_char('=', ASSIGN);
}
@@ -112,15 +116,20 @@ lex_sym() {
}
}
+/* TODO make this read ints and other litterals */
+lex_val *
+lex_value() {
+ return lex_name();
+}
-
-/* TODO: make this execpt pointer derefs at the start of a name */
lex_val *
lex_name(void) {
-
- static char name[32] = { 0 };
+ static char name[32];
char c, len = 0;
+ memset(name, 0, 32); /* note this resets the previous value,
+ when converting to an ast, use a dup */
+
if (peekc(input) == '*') {
getchr(input);
lv.type = DEREF;
diff --git a/lex.h b/lex.h
@@ -29,7 +29,8 @@ enum lex_type {
SHORT,
LONG,
OCBRACE,
- CCBRACE
+ CCBRACE,
+ EOI
};
typedef struct lex_val {
@@ -38,6 +39,10 @@ typedef struct lex_val {
} lex_val;
lex_val *lex_name(void);
+lex_val *lex_value(void);
+lex_val *lex_sym(void);
+lex_val *lex_comma(void);
+lex_val *lex_semi(void);
lex_val *lex_type(void);
lex_val *get_next(void);
diff --git a/main.c b/main.c
@@ -8,9 +8,12 @@ main() {
FILE *f = fopen("test.hlc", "r");
init_lexer(f);
- while (val = get_next())
- if (val->type == UNKNOWN)
+ while (val = get_next()) {
+ if (val->type == EOI)
break;
+ if (val->type == UNKNOWN)
+ fprintf(stderr, "unknown syntax\n");
+ }
fclose(f);
}