summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c9
-rwxr-xr-xtests/arr2/test.sh2
-rw-r--r--tests/nms/expected4
-rw-r--r--tests/nms/got4
-rw-r--r--tests/nms/io.toc53
-rw-r--r--tests/nms/nms.toc20
-rwxr-xr-xtests/nms/test.sh3
-rwxr-xr-xtests/test.sh7
-rw-r--r--toc.c7
-rw-r--r--types.c4
10 files changed, 99 insertions, 14 deletions
diff --git a/main.c b/main.c
index 5500512..09d6750 100644
--- a/main.c
+++ b/main.c
@@ -72,13 +72,16 @@ int main(int argc, char **argv) {
ErrCtx err_ctx = {0};
err_ctx.enabled = true;
err_ctx.color_enabled = true;
- char *contents = read_entire_file(&main_allocr, &err_ctx, in_filename);
+
+ File file = {0};
+ file.filename = in_filename;
+ Location file_where = {0};
+ file_where.file = &file;
+ char *contents = read_entire_file(&main_allocr, in_filename, file_where);
Identifiers idents;
idents_create(&idents);
Tokenizer t;
- File file = {0};
- file.filename = in_filename;
file.contents = contents;
file.ctx = &err_ctx;
tokr_create(&t, &idents, &err_ctx, &main_allocr);
diff --git a/tests/arr2/test.sh b/tests/arr2/test.sh
index c6ffb01..0c1a5b7 100755
--- a/tests/arr2/test.sh
+++ b/tests/arr2/test.sh
@@ -1,3 +1,3 @@
#!/bin/sh
./arr2.bin > got || exit 1
-diff got expected > /dev/null || exit 1
+diff got expected || exit 1
diff --git a/tests/nms/expected b/tests/nms/expected
new file mode 100644
index 0000000..d26c123
--- /dev/null
+++ b/tests/nms/expected
@@ -0,0 +1,4 @@
+Hello!
+2
+3
+5
diff --git a/tests/nms/got b/tests/nms/got
new file mode 100644
index 0000000..d26c123
--- /dev/null
+++ b/tests/nms/got
@@ -0,0 +1,4 @@
+Hello!
+2
+3
+5
diff --git a/tests/nms/io.toc b/tests/nms/io.toc
new file mode 100644
index 0000000..51cfd76
--- /dev/null
+++ b/tests/nms/io.toc
@@ -0,0 +1,53 @@
+get_type_with_size ::= fn(size :: i64) Type {
+ if size == 1 { i8 }
+ elif size == 2 { i16 }
+ elif size == 4 { i32 }
+ elif size == 8 { i64 }
+ else { f32 }
+};
+
+get_utype_with_size ::= fn(size :: i64) Type {
+ if size == 1 { u8 }
+ elif size == 2 { u16 }
+ elif size == 4 { u32 }
+ elif size == 8 { u64 }
+ else { f32 }
+};
+
+c_int ::= get_type_with_size(#builtin("sizeof int"));
+c_size_t ::= get_utype_with_size(#builtin("sizeof size_t"));
+
+c_putchar :: fn(c_int) c_int = #foreign "putchar", "libc.so.6";
+toc_putchar ::= fn(x: char) {
+ c_putchar(x as c_int);
+};
+
+c_fwrite :: fn(&u8, c_size_t, c_size_t, &u8) = #foreign "fwrite", "libc.so.6";
+
+stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) {
+ c_fwrite(data, size as c_size_t, nmemb as c_size_t, #builtin("stdout"));
+};
+
+puts ::= fn(x: []char) {
+ stdout_fwrite(&x[0] as &u8, 1, x.len as u64);
+ toc_putchar('\n');
+};
+
+puti ::= fn(x: int) {
+ if x < 0 {
+ toc_putchar('-');
+ // NOTE: don't do x = -x; here to make sure I64_MIN works
+ }
+ abs ::= fn(x: int) int { if x < 0 { -x } else { x } };
+ scan_digit := 1000000000000000000;
+ started := false;
+ while scan_digit > 0 {
+ digit := abs((x / scan_digit) % 10);
+ if digit > 0 { started = true; }
+ if started {
+ toc_putchar((('0' as int) + digit) as char);
+ }
+ scan_digit /= 10;
+ }
+ toc_putchar('\n');
+}; \ No newline at end of file
diff --git a/tests/nms/nms.toc b/tests/nms/nms.toc
new file mode 100644
index 0000000..413da8d
--- /dev/null
+++ b/tests/nms/nms.toc
@@ -0,0 +1,20 @@
+io ::= nms {
+ #include "io.toc";
+};
+
+n ::= nms {
+ x := 1;
+ counter ::= fn() int { x += 1; x };
+};
+
+
+main ::= fn() {
+ a := n.counter();
+ b := n.counter();
+ n.counter();
+ c := n.counter();
+ io.puts("Hello!");
+ io.puti(a);
+ io.puti(b);
+ io.puti(c);
+};
diff --git a/tests/nms/test.sh b/tests/nms/test.sh
new file mode 100755
index 0000000..8f1a481
--- /dev/null
+++ b/tests/nms/test.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+./nms.bin > got || exit 1
+diff got expected || exit 1
diff --git a/tests/test.sh b/tests/test.sh
index ceb6cbe..9224ee3 100755
--- a/tests/test.sh
+++ b/tests/test.sh
@@ -1,5 +1,5 @@
#!/bin/bash
-DIR=$(dirname $0)
+DIR=$(pwd)/$(dirname $0)
TOC=$DIR/../toc
CFLAGS="-g -Wno-parentheses-equality"
echo $$
@@ -15,15 +15,15 @@ compile() {
}
do_tests() {
+ cd "$DIR/$1"
valgrind -q --exit-on-first-error=yes --error-exitcode=1 $TOC "$DIR/$1/$1.toc" -o "$DIR/$1/$1.c" >/dev/null || exit 1
for CC in "gcc -O0 -g" "tcc" "clang -O3 -s"; do
printf "Running test $1 with C compiler $CC... "
compile "$1"
- cd "$DIR/$1"
./test.sh || { printf "\x1b[91mfailed!\x1b[0m\n"; exit 1; }
printf '\x1b[92mpassed!\x1b[0m\n'
- cd $STARTPWD
done
+ cd $STARTPWD
}
@@ -34,3 +34,4 @@ do_tests arr
do_tests arr2
do_tests foreign
do_tests params
+do_tests nms
diff --git a/toc.c b/toc.c
index d4ac0d7..bd94586 100644
--- a/toc.c
+++ b/toc.c
@@ -84,15 +84,10 @@ static size_t compiler_sizeof(Type *t);
#include "copy.c"
/* returns NULL on error */
-static char *read_entire_file(Allocator *a, ErrCtx *ectx, const char *filename) {
+static char *read_entire_file(Allocator *a, const char *filename, Location where) {
FILE *in = fopen(filename, "r");
if (!in) {
- Location where = {0};
- File file = {0};
- file.ctx = ectx;
- file.filename = filename;
- where.file = &file;
err_print(where, "Could not open file: %s.", filename);
return NULL;
}
diff --git a/types.c b/types.c
index c2b5437..fc88d82 100644
--- a/types.c
+++ b/types.c
@@ -2446,7 +2446,9 @@ static bool types_stmt(Typer *tr, Statement *s) {
char *filename = eval_expr_as_cstr(tr, &s->inc.filename, "import filename");
if (!filename)
return false;
- char *contents = read_entire_file(tr->allocr, tr->err_ctx, filename);
+ char *contents = read_entire_file(tr->allocr, filename, s->where);
+ if (!contents)
+ return false;
Tokenizer tokr;
tokr_create(&tokr, tr->idents, tr->err_ctx, tr->allocr);
File *file = typer_calloc(tr, 1, sizeof *file);