aboutsummaryrefslogtreecommitdiff
path: root/src/as_parser.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-03-18 21:32:58 +0800
committerMistivia <i@mistivia.com>2025-03-18 21:32:58 +0800
commitafb5ae7905c666eb259288ba0d3b47f71a13958f (patch)
tree9d754a68179a4b7736f3d02f9a5dd5a910c65841 /src/as_parser.c
parentc684a4a302e95d860210a9ddaa891adf9775eadc (diff)
add parser unit test
Diffstat (limited to 'src/as_parser.c')
-rw-r--r--src/as_parser.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/as_parser.c b/src/as_parser.c
index f848730..990bc1c 100644
--- a/src/as_parser.c
+++ b/src/as_parser.c
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
// BNF
// ===
@@ -32,8 +33,11 @@ Stmts parseStmts(Allocator alct, TokenStream ts) {
Stmt s = parseStmt(alct, ts);
if (s == NULL) continue;
if (len == capacity) {
- capacity = capacity * 2 + 1;
- ss->stmts = realloc(ss->stmts, sizeof(Stmt*) * capacity);
+ size_t newCapacity = capacity * 2 + 1;
+ void* newStmts = allocate(alct, sizeof(Stmt*) * newCapacity);
+ memcpy(newStmts, ss->stmts, sizeof(Stmt*) * capacity);
+ ss->stmts = newStmts;
+ capacity = newCapacity;
}
// expect newline
if (peekToken(alct, ts)->type == NEWLINE) {
@@ -74,6 +78,7 @@ Stmt parseStmt(Allocator alct, TokenStream ts) {
if (peekToken(alct, ts)->type == NEWLINE) {
return stmt;
}
+ t = peekToken(alct, ts);
}
if (t->type == OP) {
stmt->instr = parseInstr(alct, ts);
@@ -84,7 +89,7 @@ Stmt parseStmt(Allocator alct, TokenStream ts) {
if (t->type == NEWLINE) {
return NULL;
}
- fprintf(stderr, "%d:%d Expect lable + instruction, lable, or instruction.", t->line, t->col);
+ fprintf(stderr, "%d:%d Expect lable + instruction, lable, or instruction.\n", t->line, t->col);
exit(-1);
}
@@ -105,9 +110,11 @@ enum op parseOp(Allocator alct, TokenStream ts) {
}
Instr parseInstr(Allocator alct, TokenStream ts) {
- Token t = nextToken(alct, ts);
+ Token t = peekToken(alct, ts);
Instr i = allocate(alct, sizeof(struct instr));
i->tagName = NULL;
+ i->arg = NULL;
+ i->op = OPEND;
if (t->type == OP) {
i->op = parseOp(alct, ts);
t = peekToken(alct, ts);