summaryrefslogtreecommitdiff
path: root/attocc.c
diff options
context:
space:
mode:
Diffstat (limited to 'attocc.c')
-rw-r--r--attocc.c44
1 files changed, 38 insertions, 6 deletions
diff --git a/attocc.c b/attocc.c
index 467a4e5..b233703 100644
--- a/attocc.c
+++ b/attocc.c
@@ -623,6 +623,7 @@ void token_print(struct token tok) {
int token_step(struct token_iter *iter, struct token *a) {
token_free(*a);
*a = token_iter_next(iter);
+ token_print(*a);
return a->kind == TOK_END || a->kind == TOK_ERROR;
}
@@ -680,15 +681,19 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
return 1;
if (a->kind == TOK_CONSTANT && a->data.constant_kind == CONST_INT) {
value = a->data.constant_int_value;
+ if (token_step(iter, a))
+ return 1;
} else {
fprintf(stderr, "expected int const after enum assign\n");
return 1;
}
- } else if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_COMMA) {
+ }
+
+ if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_COMMA) {
if (token_step(iter, a))
return 1;
} else {
- fprintf(stderr, "expected assign or comma after enum ident\n");
+ fprintf(stderr, "expected comma after variant\n");
return 1;
}
@@ -715,6 +720,23 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
}
}
+int function_decl(struct global_context *gcx, struct token_iter *iter,
+ struct token *a, char *fun_name) {
+ if (token_step(iter, a)) // lparen
+ return 1;
+ while (1) {
+ if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_RPAREN)
+ break;
+ }
+ if (token_step(iter, a)) // rparen
+ return 1;
+ if (token_step(iter, a)) // lcurly
+ return 1;
+
+ token_step(iter, a); // rcurly
+ return 0;
+}
+
#ifdef DEBUG
void gcx_print(struct global_context *gcx) {
printf("Constants:\n");
@@ -739,7 +761,7 @@ int toplevel(struct token_iter *iter) {
struct token a = token_iter_next(iter);
struct global_context gcx;
- char *type_name = NULL;
+ char *name = NULL;
gcx.num_constants = 0;
gcx.constants = NULL;
@@ -749,20 +771,22 @@ int toplevel(struct token_iter *iter) {
struct type type;
while (1) {
- if (a.kind == TOK_KEYWORD) {
+ if (a.kind == TOK_END) {
+ break;
+ } else if (a.kind == TOK_KEYWORD) {
switch (a.data.keyword) {
case KW_ENUM:
if (token_step(iter, &a))
return 1;
if (a.kind == TOK_IDENTIFIER) {
- type_name = strdup_failsafe(a.data.identifier);
+ name = strdup_failsafe(a.data.identifier);
if (token_step(iter, &a))
return 1;
}
if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LCURLY) {
if (token_step(iter, &a))
return 1;
- if (enum_decl(&gcx, iter, &a, type_name))
+ if (enum_decl(&gcx, iter, &a, name))
return 1;
if (a.kind != TOK_SEPERATOR && a.data.seperator != SEP_SEMICOLON) {
fprintf(stderr, "expected semicolon after enum decl\n");
@@ -784,8 +808,16 @@ int toplevel(struct token_iter *iter) {
return 1;
}
} else if (a.kind == TOK_IDENTIFIER) {
+ name = strdup_failsafe(a.data.identifier);
if (token_step(iter, &a))
return 1;
+ if (a.kind == TOK_OPERATOR && a.data.operator== OP_ASSIGN) {
+ fprintf(stderr, "todo: handle global var\n");
+ return 1;
+ } else if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LPAREN) {
+ if (function_decl(&gcx, iter, &a, name))
+ return 1;
+ }
} else {
fprintf(stderr, "undhandled toplevel token: ");
token_print(a);