summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--igen/lexer.c57
-rw-r--r--igen/lexer.h4
-rw-r--r--igen/preprocessor.c41
-rw-r--r--igen/preprocessor.h2
4 files changed, 58 insertions, 46 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);
diff --git a/igen/lexer.h b/igen/lexer.h
index 0c5fea4..f4da3cd 100644
--- a/igen/lexer.h
+++ b/igen/lexer.h
@@ -10,6 +10,7 @@ typedef struct source source;
typedef struct alt_stmt alt_stmt;
typedef struct cond_stmt cond_stmt;
typedef struct stmt stmt;
+typedef void *(lex_func)(source *src, int dbg);
enum stmt_type {
STMT_NONE,
@@ -32,7 +33,7 @@ enum cond_type {
struct alt_stmt {
int type;
size_t offset;
- void *(*lex)(source *src, int dbg);
+ lex_func *lex;
};
struct cond_stmt {
@@ -49,6 +50,7 @@ struct stmt {
cond_stmt *cond_stmt;
stmt *down;
};
+ whitespace wsp;
stmt *next;
};
diff --git a/igen/preprocessor.c b/igen/preprocessor.c
index e6a895c..5ee9aa4 100644
--- a/igen/preprocessor.c
+++ b/igen/preprocessor.c
@@ -5,15 +5,18 @@
#include "misc.h"
#include "preprocessor.h"
+
+char *pp_include(source *src, int dbg);
+
static const keyword *preproc_keywords[] = {
- &(const keyword *) {"include", DIR_INCLUDE, pp_include},
+ &(const keyword) {"include", DIR_INCLUDE, (keyword_cb *)pp_include},
NULL
};
char *skip_line(const char *str, int dbg) {
- size_t span = strcspn(str, "\r\n\f");
+ const size_t span = strcspn(str, "\r\n\f");
/*span += strspn(&str[span], "\r\n\f");*/
- return &str[span+strspn(&str[span], "\r\n\f")];
+ return (char *)&str[span+strspn(&str[span], "\r\n\f")];
}
size_t line_span(const char *str, int dbg) {
@@ -74,16 +77,16 @@ char *skip_whitespace(source *src, whitespace *wspace, int count_lines, int coun
size_t span = get_whitespace_span(text, count_lines, count_columns, dbg);
whitespace wsp = {0};
- count_whitespace(&wsp, str, span, count_lines, count_columns, dbg);
+ count_whitespace(&wsp, text, span, count_lines, count_columns, dbg);
if (wsp.tabs) {
const int tab_stop = src->tab_width;
const int extra_tabs = wsp.spaces/tab_stop;
- src->cur.x += ((wsp.tabs+extra_tabs)*tab_stop);
+ src->cur.column += ((wsp.tabs+extra_tabs)*tab_stop);
} else {
- src->cur.x += wsp.spaces;
+ src->cur.column += wsp.spaces;
}
- src->cur.y += wsp.lines;
+ src->cur.line += wsp.lines;
if (wspace != NULL) {
*wspace = wsp;
}
@@ -107,7 +110,7 @@ char *skip_comment(source *src, whitespace *wspace, comment_type *type, int dbg)
count_whitespace(&wsp, &text[span], strspn(&text[span], "\r\n\f"), 0, 1, dbg);
} else {
*type = COMM_SINGLE;
- ++wsp.lines
+ ++wsp.lines;
text -= 2;
}
@@ -118,11 +121,11 @@ char *skip_comment(source *src, whitespace *wspace, comment_type *type, int dbg)
if (wsp.tabs) {
const int tab_stop = src->tab_width;
const int extra_tabs = wsp.spaces/tab_stop;
- src->cur.x += ((wsp.tabs+extra_tabs)*tab_stop);
+ src->cur.column += ((wsp.tabs+extra_tabs)*tab_stop);
} else {
- src->cur.x += wsp.spaces;
+ src->cur.column += wsp.spaces;
}
- src->cur.y += wsp.lines;
+ src->cur.line += wsp.lines;
} else {
--text;
*type = COMM_NONE;
@@ -169,11 +172,11 @@ char *find_comment(source *src, whitespace *wspace, comment_type *type, int dbg)
if (wsp.tabs) {
const int tab_stop = src->tab_width;
const int extra_tabs = wsp.spaces/tab_stop;
- src->cur.x += ((wsp.tabs+extra_tabs)*tab_stop);
+ src->cur.column += ((wsp.tabs+extra_tabs)*tab_stop);
} else {
- src->cur.x += wsp.spaces;
+ src->cur.column += wsp.spaces;
}
- src->cur.y += wsp.lines;
+ src->cur.line += wsp.lines;
return &text[span];
} else {
@@ -240,7 +243,7 @@ keyword **copy_keyword_table(keyword **keywords, size_t *key_count, int dbg) {
*key_count = 0;
return NULL;
} else {
- for (*key_count = 0; keywords[count] != NULL; ++(*key_count));
+ for (*key_count = 0; keywords[*key_count] != NULL; ++(*key_count));
if (!*key_count) {
return NULL;
} else {
@@ -348,7 +351,7 @@ int is_included(source *parent, const char *filename, int dbg) {
}
void free_str_list(struct str_list *list) {
- for (struct str_list *l = list, l2 = l; l != NULL; l = l->next, l2 = l) {
+ for (struct str_list *l = list, *l2 = l; l != NULL; l = l->next, l2 = l) {
free(l2->str);
l2->str = NULL;
free(l2);
@@ -356,7 +359,7 @@ void free_str_list(struct str_list *list) {
}
void free_comment_list(struct comment_list *list) {
- for (struct comment_list *l = list, l2 = l; l != NULL; l = l->next, l2 = l) {
+ for (struct comment_list *l = list, *l2 = l; l != NULL; l = l->next, l2 = l) {
free(l2->com);
l2->com = NULL;
free(l2);
@@ -423,7 +426,7 @@ source *preprocess(source *parent, const char *filename, int dbg) {
src->included = (parent != NULL);
for (char *text = src->text; *text != '\0'; ++text) {
- text = skip_whitespace(src, NULL, 1, 1, dbg)
+ text = skip_whitespace(src, NULL, 1, 1, dbg);
text = skip_comment(src, NULL, NULL, dbg);
if (is_directive) {
@@ -434,7 +437,7 @@ source *preprocess(source *parent, const char *filename, int dbg) {
dir_key = calloc(key_len+1, sizeof(char));
memcpy(dir_key, text, key_len);
- dir_type = find_keyword(dir_key, preproc_keywords, src, &key_data, dbg);
+ dir_type = find_keyword(dir_key, (keyword **)preproc_keywords, src, &key_data, dbg);
free(dir_key);
if (key_data != NULL) {
diff --git a/igen/preprocessor.h b/igen/preprocessor.h
index 200f5b2..951f36c 100644
--- a/igen/preprocessor.h
+++ b/igen/preprocessor.h
@@ -74,6 +74,8 @@ struct source {
};
/*extern keyword *find_keyword(const char *key, keyword **keywords, int dbg);*/
+extern char *skip_whitespace(source *src, whitespace *wspace, int count_lines, int count_columns, int dbg);
+extern char *skip_comment(source *src, whitespace *wspace, comment_type *type, int dbg);
extern int find_keyword(const char *key, keyword **keywords, void *ctx, void **callback_ret, int dbg);
extern source *preprocess(source *parent, const char *filename, int dbg);
#endif