diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/as_parser.c | 15 | ||||
| -rw-r--r-- | src/utils.c | 5 |
2 files changed, 16 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); diff --git a/src/utils.c b/src/utils.c index 354159b..1baf283 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,5 +1,8 @@ #include "utils.h" +#include <stdio.h> +#include <assert.h> + struct allocator { void** bufs; size_t cap; @@ -19,10 +22,12 @@ void deleteAllocator(Allocator alct) { for (size_t i = 0; i < alct->len; i++) { free(alct->bufs[i]); } + free(alct->bufs); free(alct); } void * allocate(Allocator alct, size_t size) { + assert(size > 0); if (alct->len >= alct->cap) { alct->cap = alct->cap * 2; // Doubling the capacity alct->bufs = realloc(alct->bufs, sizeof(void*) * alct->cap); |
