summaryrefslogtreecommitdiff
path: root/igen/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'igen/lexer.c')
-rw-r--r--igen/lexer.c57
1 files changed, 31 insertions, 26 deletions
diff --git a/igen/lexer.c b/igen/lexer.c
index 15226b8..a8eb039 100644
--- a/igen/lexer.c
+++ b/igen/lexer.c
@@ -1,3 +1,4 @@
+#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -6,6 +7,8 @@
#include "misc.h"
#include "preprocessor.h"
+stmt *lex_stmt(source *src, int dbg);
+
char *skip_seperators(source *src, whitespace *wspace, int preserve_src, int dbg) {
char *text = NULL;
char *tmp = src->text;
@@ -13,7 +16,7 @@ char *skip_seperators(source *src, whitespace *wspace, int preserve_src, int dbg
src->text = max(src->text, text);
src->text = skip_whitespace(src, wspace, 1, 1, dbg);
- src->text = skip_comment(src, wspace, dbg);
+ src->text = skip_comment(src, wspace, NULL, dbg);
text = src->text;
if (preserve_src) {
@@ -24,12 +27,12 @@ char *skip_seperators(source *src, whitespace *wspace, int preserve_src, int dbg
cond_stmt *lex_cond_stmt(source *src, int dbg) {
const keyword *cond_keywords[] = {
- {"do", COND_DO, NULL},
- {"for", COND_FOR, NULL},
- {"if", COND_IF, NULL},
- {"while", COND_WHILE, NULL},
+ &(const keyword) {"do", COND_DO, NULL},
+ &(const keyword) {"for", COND_FOR, NULL},
+ &(const keyword) {"if", COND_IF, NULL},
+ &(const keyword) {"while", COND_WHILE, NULL},
};
- const char *text = src->text;
+ char *text = src->text;
char *key = NULL;
size_t key_len = 0;
cond_type type = COND_NONE;
@@ -40,7 +43,7 @@ cond_stmt *lex_cond_stmt(source *src, int dbg) {
key = calloc(key_len+1, sizeof(char));
memcpy(key, text, key_len);
- type = find_keyword(key, cond_keywords, NULL, NULL, dbg);
+ type = find_keyword(key, (keyword **)cond_keywords, NULL, NULL, dbg);
free(key);
text += key_len+1;
@@ -50,7 +53,7 @@ cond_stmt *lex_cond_stmt(source *src, int dbg) {
cond->type = type;
src->text = text;
- src->cur.x += key_len+1;
+ src->cur.column += key_len+1;
if (type != COND_DO) {
cond->expr = lex_expr(src, dbg);
@@ -69,7 +72,7 @@ cond_stmt *lex_cond_stmt(source *src, int dbg) {
text += key_len+1;
src->text = text;
- src->cur.x += key_len+1;
+ src->cur.column += key_len+1;
cond->expr = lex_expr(src, dbg);
@@ -80,14 +83,14 @@ cond_stmt *lex_cond_stmt(source *src, int dbg) {
throw_error(src, 1, "Missing \';\' after do while statement.");
} else {
src->text = text;
- ++src->cur.x;
+ ++src->cur.column;
}
} else {
throw_error(src, 1, "Missing \'while\' after do while statement.");
}
free(key);
}
- return s;
+ return cond;
}
}
@@ -96,21 +99,24 @@ stmt *lex_comp_stmt(source *src, int dbg) {
char *text = NULL;
whitespace wsp = {0};
- text = skip_seperators(src, &wsp, dbg);
+ text = skip_seperators(src, &wsp, 0, dbg);
if (*text++ == '{') {
stmt *s;
src->text = text;
- ++src->cur.x;
+ ++src->cur.column;
- s = lex_stmt(src, dbg);
- s->wsp = wsp;
+ for (s = lex_stmt(src, dbg); *src->text != '}' && *src->text != '\0'; s = s->next) {
+ s->wsp = wsp;
+ s->next = lex_stmt(src, dbg);
+ text = skip_seperators(src, &wsp, 1, dbg);
+ }
text = src->text;
if (*text++ == '}') {
- ++src->cur.x;
+ ++src->cur.column;
} else {
--text;
throw_error(src, 1, "Missing terminating \'}\' in compound statement.");
@@ -122,12 +128,12 @@ stmt *lex_comp_stmt(source *src, int dbg) {
}
stmt *lex_stmt(source *src, int dbg) {
- const char *text = src->text;
+ char *text = src->text;
const alt_stmt alts[] = {
- {STMT_FUNC, offsetof(stmt, func), lex_func},
- {STMT_EXPR, offsetof(stmt, expr), lex_exprs},
- {STMT_COND, offsetof(stmt, cond_stmt), lex_cond_stmt},
- {STMT_COMP, offsetof(stmt, down), lex_comp_stmt},
+ {STMT_FUNC, offsetof(stmt, func), (lex_func *)lex_func},
+ {STMT_EXPR, offsetof(stmt, expr), (lex_func *)lex_exprs},
+ {STMT_COND, offsetof(stmt, cond_stmt), (lex_func *)lex_cond_stmt},
+ {STMT_COMP, offsetof(stmt, down), (lex_func *)lex_comp_stmt},
};
for (int i = 0; i < NUM_STMTS; ++i) {
@@ -135,8 +141,8 @@ stmt *lex_stmt(source *src, int dbg) {
void *data = alts[i].lex(src, dbg);
if (data != NULL) {
stmt *s = calloc(1, sizeof(stmt));
- void **member = (char **)s+alts[i].offset;
- *member = data;
+ char *stmt = (char *)s;
+ *(void **)(stmt+alts[i].offset) = data;
return s;
}
}
@@ -148,17 +154,16 @@ stmt *lex_stmt(source *src, int dbg) {
void lex_library(source *src, int dbg) {
src->root = lex_stmt(src, dbg);
src->last = (src->last != NULL) ? src->last : src->root;
- for (stmt *s = src->root; s != NULL; s = lex_stmt(str, dbg)) {
+ for (stmt *s = src->root; s != NULL; s = lex_stmt(src, dbg)) {
src->last->next = s;
src->last = s;
}
- return start;
};
int lex(source *src, int dbg) {
char *text = src->text;
- library(src, dbg);
+ lex_library(src, dbg);
src->text = text;
return (src->root != NULL && src->last != NULL);