summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--err.c1
-rw-r--r--main.c1
-rw-r--r--parse.c6
-rw-r--r--test.toc34
-rw-r--r--tests/io.toc16
-rwxr-xr-xtests/test.sh1
-rw-r--r--tests/use.toc45
-rw-r--r--tests/use_expected6
8 files changed, 71 insertions, 39 deletions
diff --git a/err.c b/err.c
index 9fada7b..01e3de2 100644
--- a/err.c
+++ b/err.c
@@ -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 */
diff --git a/main.c b/main.c
index 7dc73ef..b3508da 100644
--- a/main.c
+++ b/main.c
@@ -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?
diff --git a/parse.c b/parse.c
index 5f3e6a8..9a4d57f 100644
--- a/parse.c
+++ b/parse.c
@@ -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;
}
diff --git a/test.toc b/test.toc
index 9b805fc..843b3ad 100644
--- a/test.toc
+++ b/test.toc
@@ -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