summaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c53
1 files changed, 42 insertions, 11 deletions
diff --git a/parse.c b/parse.c
index ba1644f..d562b57 100644
--- a/parse.c
+++ b/parse.c
@@ -1,7 +1,8 @@
#include "parse.h"
+#include "Token.h"
#include <stdio.h>
-static void unexpected(const Token *tok);
+static void *unexpected(const Token *tok, int n);
static const Token *keyword_fn(const Token *tok);
static const Token *keyword_var(const Token *tok);
static const Token *keyword_const(const Token *tok);
@@ -11,43 +12,73 @@ int parse(const Token *tok)
while (tok != NULL && tok->type != TOK_NONE) {
switch (tok->type) {
case TOK_KW_FN:
- tok = keyword_fn(tok + 1);
+ tok = keyword_fn(tok);
break;
case TOK_KW_VAR:
- tok = keyword_var(tok + 1);
+ tok = keyword_var(tok);
break;
case TOK_KW_CONST:
- tok = keyword_const(tok + 1);
+ tok = keyword_const(tok);
break;
default:
- unexpected(tok);
+ unexpected(tok, -1);
return 1;
}
+ if (tok == NULL)
+ return 1;
}
return 0;
}
-static void unexpected(const Token *tok)
+static void *unexpected(const Token *tok, int n)
{
- fprintf(stderr, "unexpected %s %s%s%sat %u:%u\n",
- token_type_str(tok->type),
+ fprintf(stderr, "[%d] unexpected %s %s%s%sat %u:%u\n",
+ n, token_type_str(tok->type),
(tok->s != NULL) ? "(" : "",
(tok->s != NULL) ? tok->s : "",
(tok->s != NULL) ? ") " : "",
tok->line, tok->column);
+ return NULL;
}
static const Token *keyword_fn(const Token *tok)
{
- return tok;
+ tok += 1;
+ if (tok->type != TOK_WORD)
+ return unexpected(tok, 0);
+ const Token *name = tok;
+ tok += 1;
+ (void)name;
+ if (tok->type != TOK_PAREN_OPEN)
+ return unexpected(tok, 1);
+ tok += 1;
+ size_t args = 0;
+ while (tok->type == TOK_WORD) {
+ tok += 1;
+ args += 1;
+ if (tok->type == TOK_COMMA)
+ tok += 1;
+ }
+ if (tok->type != TOK_PAREN_CLOS)
+ return unexpected(tok, 2);
+ printf("%zu args\n", args);
+ return tok + 1;
}
static const Token *keyword_var(const Token *tok)
{
- return tok;
+ tok += 1;
+ if (tok->type != TOK_WORD)
+ return unexpected(tok, 10);
+ const Token *name = tok;
+ (void)name;
+ tok += 1;
+ if (tok->type != TOK_SEMICOLON)
+ return unexpected(tok, 11);
+ return tok + 1;
}
static const Token *keyword_const(const Token *tok)
{
- return tok;
+ return tok + 1;
}