diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-04-23 14:01:20 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-04-23 14:01:20 -0400 |
commit | f7bfb9492ab546e886fd40bda1a0ab25fcb05247 (patch) | |
tree | 1ec694390050cc3eb21009963d40dc6655c0a5fa | |
parent | 4062e5c965bc04ec9654d82526e5e785f7f45d03 (diff) |
fixed error printing problem, added new test: use
-rw-r--r-- | err.c | 1 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parse.c | 6 | ||||
-rw-r--r-- | test.toc | 34 | ||||
-rw-r--r-- | tests/io.toc | 16 | ||||
-rwxr-xr-x | tests/test.sh | 1 | ||||
-rw-r--r-- | tests/use.toc | 45 | ||||
-rw-r--r-- | tests/use_expected | 6 |
8 files changed, 71 insertions, 39 deletions
@@ -83,6 +83,7 @@ static void err_fprint(ErrCtx *ctx, const char *fmt, ...) { } static void print_location_highlight(FILE *out, Location where) { + assert(where.end >= where.start); File *f = where.file; ErrCtx *ctx = f->ctx; if (where.start == 0 && where.end == 0) { err_fprint(ctx, "\n"); return; } /* null location */ @@ -8,7 +8,6 @@ /* @TODO: -test used ret decls consider: don't do inference for function calls; get rid of was_expr -- now that we have struct params EXPR_IDENT should be a string before typing, also struct member accesses do we need the possibility that IdentSlot.decl is NULL? @@ -2450,12 +2450,14 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { Block *body = &e->nms->body; body->kind = BLOCK_NMS; body->where = s->where; + parser_put_end(p, &body->where); + ++body->where.end; /* past semicolon */ body->parent = p->block; idents_create(&body->idents, p->allocr, body); Statement *inc_stmt = parser_arr_add_ptr(p, body->stmts); inc_stmt->kind = STMT_INCLUDE; inc_stmt->flags = STMT_INC_TO_NMS; - inc_stmt->where = s->where; + inc_stmt->where = body->where; inc_stmt->inc = parser_calloc(p, 1, sizeof *inc_stmt->inc); inc_stmt->inc->filename = filename; } else if (i->flags & INC_FORCED) { @@ -2471,7 +2473,6 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { return false; } ++t->token; - break; } break; case DIRECT_ERROR: case DIRECT_WARN: @@ -2523,6 +2524,7 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { if (!valid) return false; } + parser_put_end(p, &s->where); return true; } @@ -1,34 +1,4 @@ -#include "std/io.toc", io; -#include "std/mem.toc", mem; -use mem; +#include "io.toc", io; -main ::= fn() { - Point ::= struct { - x: int; - y: int; - a ::= 3; - } - - use io; - - { - use p: Point; - use io; - x = 5; - puti(x); - } - - ps := news(Point, 5); - for use p, i := &ps { - x = i; - y = 2*i; - } - for use p := ps { - writei(x); - writes(" "); - writei(y); - puts(""); - } - dels(ps); -} +#include "mem.toc", mem; diff --git a/tests/io.toc b/tests/io.toc index d3ce467..2c82dc9 100644 --- a/tests/io.toc +++ b/tests/io.toc @@ -2,16 +2,19 @@ putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int; toc_putchar ::= fn(x: char) { putchar(x as #C int); } +printf ::= #foreign("printf", "libc.so.6") fn(#C &"char const", #C ..) #C int; +writes ::= fn(x: []char) { + printf_strfmt := "%s\0"; + printf(&printf_strfmt[0], &x[0]); +} puts ::= fn(x: []char) { - for c := x { - toc_putchar(c); - } + writes(x); toc_putchar('\n'); } -puti ::= fn(x: int) { +writei ::= fn(x: int) { if x < 0 { toc_putchar('-'); // NOTE: don't do x = -x; here to make sure I64_MIN works @@ -31,5 +34,10 @@ puti ::= fn(x: int) { scan_digit /= 10; } } +} + +puti ::= fn(x: int) { + writei(x); toc_putchar('\n'); } + diff --git a/tests/test.sh b/tests/test.sh index a603767..e6e8fe5 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -13,6 +13,7 @@ params nms varargs printf +use misc' STARTPWD=$(pwd) diff --git a/tests/use.toc b/tests/use.toc new file mode 100644 index 0000000..f1228f6 --- /dev/null +++ b/tests/use.toc @@ -0,0 +1,45 @@ +#include "io.toc", io; +#include "mem.toc", mem; + +use mem; + +Point ::= struct { + x: int; + y: int; + a ::= 3; +} + +make_point ::= fn (x_: int, y_: int) use p: Point { + x = x_+a; + y = y_+a; +} + +main ::= fn() { + + use io; + + { + use p: Point; + use io; + x = 5; + puti(x); + } + + + + ps := news(Point, 5); + for p := &ps { + *p = make_point(3, 5); + } + for use p, i := &ps { + x += i; + y += 2*i; + } + for use p := ps { + writei(x); + writes(" "); + writei(y); + puts(""); + } + dels(ps); +} diff --git a/tests/use_expected b/tests/use_expected new file mode 100644 index 0000000..d47a933 --- /dev/null +++ b/tests/use_expected @@ -0,0 +1,6 @@ +5 +6 8 +7 10 +8 12 +9 14 +10 16 |