summaryrefslogtreecommitdiff
path: root/attocc.c
diff options
context:
space:
mode:
Diffstat (limited to 'attocc.c')
-rw-r--r--attocc.c36
1 files changed, 31 insertions, 5 deletions
diff --git a/attocc.c b/attocc.c
index 5a575fa..82ea8d8 100644
--- a/attocc.c
+++ b/attocc.c
@@ -624,7 +624,9 @@ void token_print(struct token tok) {
int token_step(struct token_iter *iter, struct token *a) {
token_free(*a);
*a = token_iter_next(iter);
+#ifdef DEBUG
token_print(*a);
+#endif
return a->kind == TOK_END || a->kind == TOK_ERROR;
}
@@ -774,6 +776,12 @@ int read_type(struct global_context *gcx, struct token_iter *iter,
if (token_step(iter, a))
return 1;
return 0;
+ } else if (a->data.keyword == KW_CHAR) {
+ ty->kind = TY_PRIMITIVE;
+ ty->index = 1;
+ if (token_step(iter, a))
+ return 1;
+ return 0;
} else {
return 1;
}
@@ -786,13 +794,30 @@ int function_decl(struct global_context *gcx, struct token_iter *iter,
while (1) {
if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_RPAREN)
break;
+ struct type type;
+ if (read_type(gcx, iter, a, &type))
+ return 1;
+ if (a->kind != TOK_IDENTIFIER) {
+ fprintf(stderr, "expected ident after arg type\n");
+ return 1;
+ }
+ char *name = strdup_failsafe(a->data.identifier);
+ if (token_step(iter, a))
+ return 1;
}
if (token_step(iter, a)) // rparen
return 1;
+ if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_SEMICOLON) {
+ fprintf(stderr, "forward decl not supported yet\n");
+ return 1;
+ }
+ if (a->kind != TOK_SEPERATOR || a->data.seperator != SEP_LCURLY) {
+ fprintf(stderr, "expected lcurly after function head\n");
+ return 1;
+ }
if (token_step(iter, a)) // lcurly
return 1;
- token_step(iter, a); // rcurly
return 0;
}
@@ -839,19 +864,20 @@ int toplevel(struct token_iter *iter) {
if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_SEMICOLON) {
// was a type decl, continue
if (token_step(iter, &a))
- return 1;
+ break;
} else if (a.kind == TOK_IDENTIFIER) {
// either var or func
char *name = strdup_failsafe(a.data.identifier);
if (token_step(iter, &a))
return 1;
-
if (a.kind == TOK_OPERATOR && a.data.operator == OP_ASSIGN) {
// variable / constant
TRAP
} else if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LPAREN) {
- // function decl
- TRAP
+ if (function_decl(&gcx, iter, &a, name))
+ return 1;
+ if (token_step(iter, &a))
+ break;
} else {
fprintf(stderr, "unknown type-ident started toplevel statement\n");
return 1;