commit 42474ce54d2b7b4ff125fdb734a0b9235d9b2109
parent 60c19a42c5847a3de673891f23c005db7957500a
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Sun, 5 Dec 2021 14:02:55 -0600
use 'let' for declaration
This simplifies determining what is a declaration
Diffstat:
4 files changed, 18 insertions(+), 13 deletions(-)
diff --git a/main.c b/main.c
@@ -36,6 +36,9 @@ lex(struct slice start)
if (slice_cmplit(&start, "if") == 0) {
cur->type = TOK_IF;
ADVANCE(2);
+ } else if (slice_cmplit(&start, "let") == 0) {
+ cur->type = TOK_LET;
+ ADVANCE(3);
} else if (slice_cmplit(&start, "else") == 0) {
cur->type = TOK_ELSE;
ADVANCE(4);
@@ -370,7 +373,6 @@ parse(struct token **tok)
{
struct block items = { 0 };
struct item item;
- struct token *name;
bool curlies = false;
if ((*tok)->type == TOK_LCURLY) {
@@ -380,15 +382,16 @@ parse(struct token **tok)
while ((*tok)->type != TOK_NONE && (*tok)->type != TOK_RCURLY) {
item = (struct item){ 0 };
- if ((*tok)->type != TOK_IF && (*tok)->type != TOK_LOOP) {
- expect((*tok), TOK_NAME);
- name = (*tok);
- }
- if ((*tok)->next && (*tok)->next->type == TOK_NAME && (*tok)->next->next && (*tok)->next->next->type == TOK_EQUAL) {
+ if ((*tok)->type == TOK_LET) {
struct decl decl;
item.kind = ITEM_DECL;
- (*tok) = (*tok)->next;
+ *tok = (*tok)->next;
+
+ expect(*tok, TOK_NAME);
+ decl.s = (*tok)->slice;
+ *tok = (*tok)->next;
+ expect(*tok, TOK_NAME);
if (strncmp((*tok)->slice.data, "i64", 3) == 0) {
decl.type = TYPE_I64;
} else if (strncmp((*tok)->slice.data, "str", 3) == 0) {
@@ -397,10 +400,11 @@ parse(struct token **tok)
error("unknown type");
}
- (*tok) = (*tok)->next->next;
+ *tok = (*tok)->next;
+ expect(*tok, TOK_EQUAL);
+ *tok = (*tok)->next;
decl.val = parseexpr(tok);
- decl.s = name->slice;
array_add((&decls), decl);
item.idx = decls.len - 1;
diff --git a/nooc.h b/nooc.h
@@ -20,6 +20,7 @@ enum tokentype {
TOK_NUM,
TOK_STRING,
+ TOK_LET,
TOK_IF,
TOK_ELSE,
TOK_LOOP
diff --git a/prog.nc b/prog.nc
@@ -1,5 +1,5 @@
-exit i64 = 60
-write i64 = 1
+let exit i64 = 60
+let write i64 = 1
loop {
syscall(write, 0, "hello\n", 6)
}
diff --git a/test/yes.nooc b/test/yes.nooc
@@ -1,5 +1,5 @@
-write i64 = 1
-stdout i64 = 0
+let write i64 = 1
+let stdout i64 = 0
loop {
syscall(write, stdout, "y\n", 2)
}
\ No newline at end of file