From 9e9bd1823942e0b5e93cb3866799e7d336f2bb0d Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 7 Sep 2019 19:09:57 -0400 Subject: a few more things --- a.out | Bin 0 -> 16800 bytes eval.c | 5 +++++ out.c | 17 +++++++++-------- out.h | 2 +- test.toc | 12 +++++++----- toc.c | 1 + util/where.c | 3 +++ 7 files changed, 26 insertions(+), 14 deletions(-) create mode 100755 a.out create mode 100644 util/where.c diff --git a/a.out b/a.out new file mode 100755 index 0000000..c8b4185 Binary files /dev/null and b/a.out differ diff --git a/eval.c b/eval.c index 48534d6..bbefde5 100644 --- a/eval.c +++ b/eval.c @@ -74,6 +74,11 @@ static bool eval_expr_as_int(Expression *e, Integer *i) { return false; } Declaration *d = id_decl->decl; + if (is_after(d->where, e->where)) { + err_print(e->where, "Use of constant before its declaration."); + info_print(d->where, "Declaration will be here."); + return false; + } if (!(d->flags & DECL_FLAG_CONST)) { err_print(e->where, "Use of non-constant identifier in a constant expression."); info_print(d->where, "Declaration was here."); diff --git a/out.c b/out.c index cd8f28b..de7dad8 100644 --- a/out.c +++ b/out.c @@ -1,19 +1,20 @@ #include "out.h" /* toc */ +int64_t N = 10; #include -void foo(int64_t (*out__)[3][3]) { - int64_t av___0[3][3] = {{0}}; - int64_t (*x)[3] = av___0; - memcpy(*out__, x, 9 * sizeof(int64_t )); return; +void foo(int64_t (*out__)[10][10]) { + int64_t av___0[10][10] = {{0}}; + int64_t (*x)[10] = av___0; + memcpy(*out__, x, 100 * sizeof(int64_t )); return; } void main__(void) { - int64_t av___1[3][3]; + int64_t av___1[10][10]; foo(&av___1); - int64_t (*x)[3] = av___1; + int64_t (*x)[10] = av___1; -for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) +for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) printf("%ld", x[i][j]); puts(""); ; diff --git a/out.h b/out.h index a8d55c8..7b1844e 100644 --- a/out.h +++ b/out.h @@ -1,5 +1,5 @@ #include #include #include -void foo(int64_t (*out__)[3][3]); +void foo(int64_t (*out__)[10][10]); void main__(void); diff --git a/test.toc b/test.toc index 5c5b9fc..f3ebac3 100644 --- a/test.toc +++ b/test.toc @@ -1,13 +1,15 @@ #C("#include \n"); -foo @= fn() [3][3]int { - x : [3][3]int; +N @= 10; + +foo @= fn() [N][N]int { + x : [N][N]int; x }; main @= fn() { - x : [3][3]int = foo(); + x : [N][N]int = foo(); #C(" -for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) +for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) printf(\"%ld\", x[i][j]); puts(\"\"); "); diff --git a/toc.c b/toc.c index fb9864c..b36c5fa 100644 --- a/toc.c +++ b/toc.c @@ -22,6 +22,7 @@ typedef long double Floating; /* OPTIM: Switch to double */ #include "util/str.c" #include "identifiers.c" #include "tokenizer.c" +#include "util/where.c" #include "parse.c" #include "eval.c" #include "types.c" diff --git a/util/where.c b/util/where.c new file mode 100644 index 0000000..4ac786d --- /dev/null +++ b/util/where.c @@ -0,0 +1,3 @@ +bool is_after(Location a, Location b) { /* a is after b? */ + return a.code > b.code; +} -- cgit v1.2.3