summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c4
-rw-r--r--main.c2
-rw-r--r--std/io.toc4
-rw-r--r--test.toc13
-rw-r--r--types.c22
5 files changed, 28 insertions, 17 deletions
diff --git a/cgen.c b/cgen.c
index 607db80..1bd9e26 100644
--- a/cgen.c
+++ b/cgen.c
@@ -1803,7 +1803,7 @@ static void cgen_block(CGenerator *g, Block *b, const char *ret_name, U16 flags)
}
++g->indent_lvl;
-
+
arr_foreach(b->stmts, Statement, s)
cgen_stmt(g, s);
if (b->ret_expr && ret_name) {
@@ -1957,6 +1957,8 @@ static void cgen_decl(CGenerator *g, Declaration *d) {
if (has_expr) {
cgen_val_pre(g, val, type);
}
+ if (d->flags & DECL_IS_CONST)
+ cgen_write(g, "static ");
cgen_type_pre(g, type);
cgen_write(g, " ");
cgen_ident(g, i);
diff --git a/main.c b/main.c
index a584916..02f488b 100644
--- a/main.c
+++ b/main.c
@@ -8,8 +8,6 @@
/*
@TODO:
-does our use before declare thing work with #include?
-allow using constant before declaring it, and do DECL_FINDING_TYPE, for circular dependencies
#no_warn
start making a standard library... (printf; stringbuilder would be nice to have)
if we do #include "foo.toc", bar; and foo.toc fails, bar should be declared as TYPE_UNKNOWN (right now it's undeclared)
diff --git a/std/io.toc b/std/io.toc
index 4f05be5..4eaf113 100644
--- a/std/io.toc
+++ b/std/io.toc
@@ -13,7 +13,6 @@ unfortunately, we can't use fwrite because MSVC doesn't like it when you
use a file handle that's not from the DLL. (i.e. you can't pass your stdout
to the imported version of fwrite)
*/
-/*
writes ::= fn(x: []char) {
for c := x {
toc_putchar(c);
@@ -51,9 +50,7 @@ puti ::= fn(x: int) {
writei(x);
toc_putchar('\n');
}
-*/
-/*
writef ::= fn(x: float) {
fmt := "%f\0";
printf(&fmt[0], x);
@@ -62,4 +59,3 @@ putf ::= fn(x: float) {
writef(x);
toc_putchar('\n');
}
-*/
diff --git a/test.toc b/test.toc
index 4a91877..0f4d5a4 100644
--- a/test.toc
+++ b/test.toc
@@ -1 +1,14 @@
#include "std/io.toc", io;
+
+foo ::= fn() {
+ x := bar(12);
+ io.puti(x);
+}
+
+bar ::= fn(x: int) int {
+ y := x;
+ io.puti(y);
+ y
+}
+
+foo();
diff --git a/types.c b/types.c
index e657799..b6a3f5c 100644
--- a/types.c
+++ b/types.c
@@ -845,20 +845,22 @@ top:;
t->flags |= TYPE_IS_RESOLVED; /* for function templates */
return true;
} else {
- if (where.start <= d->where.end) {
+ if (d->flags & DECL_INFER) {
char *s = ident_to_str(i);
- err_print(where, "Use of identifier %s before its declaration.", s);
- info_print(d->where, "%s will be declared here.", s);
+ err_print(where, "Use of identifier %s before it has been inferred. You are trying to do stuff with inference which toc doesn't support.", s);
free(s);
return false;
- } else {
- if (d->flags & DECL_INFER) {
- err_print(where, "Use of identifier before it has been inferred. You are trying to do stuff with inference which toc doesn't support.");
- return false;
- }
- /* let's type the declaration, and redo this (for evaling future functions) */
+ }
+ if ((d->flags & DECL_IS_CONST) && (tr->block == NULL)) {
+ /* let's type the declaration, and redo this (for evaling future constants) */
if (!types_decl(tr, d)) return false;
goto top;
+ } else {
+ char *s = ident_to_str(i);
+ err_print(where, "Use of %s before its declaration.", s);
+ info_print(d->where, "%s will be declared here.", s);
+ free(s);
+ return false;
}
}
}
@@ -2074,7 +2076,7 @@ static Status types_expr(Typer *tr, Expression *e) {
}
if (undeclared) {
char *s = cstr(e->ident_str.str, e->ident_str.len);
- err_print(e->where, "Undeclared identifier \"%s\".", s);
+ err_print(e->where, "Undeclared identifier '%s'.", s);
free(s);
return false;
}