summaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/parse.c b/parse.c
index d562b57..922ce4d 100644
--- a/parse.c
+++ b/parse.c
@@ -3,6 +3,7 @@
#include <stdio.h>
static void *unexpected(const Token *tok, int n);
+static const Token *expression(const Token *tok);
static const Token *keyword_fn(const Token *tok);
static const Token *keyword_var(const Token *tok);
static const Token *keyword_const(const Token *tok);
@@ -41,6 +42,17 @@ static void *unexpected(const Token *tok, int n)
return NULL;
}
+static const Token *expression(const Token *tok)
+{
+ if (tok->type == TOK_INTEGER)
+ return tok + 1;
+ if (tok->type == TOK_STRING)
+ return tok + 1;
+ if (tok->type == TOK_WORD)
+ return tok + 1;
+ return unexpected(tok, 20);
+}
+
static const Token *keyword_fn(const Token *tok)
{
tok += 1;
@@ -73,8 +85,18 @@ static const Token *keyword_var(const Token *tok)
const Token *name = tok;
(void)name;
tok += 1;
- if (tok->type != TOK_SEMICOLON)
+ /* no initialization */
+ if (tok->type == TOK_SEMICOLON)
+ return tok + 1;
+ if (tok->type != TOK_ASSIGN)
return unexpected(tok, 11);
+ tok += 1;
+ /* expression */
+ tok = expression(tok);
+ if (tok == NULL)
+ return NULL;
+ if (tok->type != TOK_SEMICOLON)
+ return unexpected(tok, 12);
return tok + 1;
}