summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c4
-rw-r--r--err.c47
-rw-r--r--test.toc8
-rw-r--r--test2.toc22
-rw-r--r--tokenizer.c15
5 files changed, 85 insertions, 11 deletions
diff --git a/cgen.c b/cgen.c
index 22b9fa7..da259d3 100644
--- a/cgen.c
+++ b/cgen.c
@@ -1550,9 +1550,9 @@ static bool cgen_fn(CGenerator *g, FnExpr *f, Location where, U64 instance, Valu
cgen_write(g, " = ");
if (!cgen_val(g, arg, type, where))
return false;
+ cgen_write(g, ";");
+ cgen_nl(g);
}
- cgen_write(g, ";");
- cgen_nl(g);
carg_idx++;
}
}
diff --git a/err.c b/err.c
index b8b1d4b..66ffd8f 100644
--- a/err.c
+++ b/err.c
@@ -3,7 +3,15 @@
This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/
+/* #define ERR_EMACS 1 */
+
+#ifndef USE_COLORED_TEXT
+#if ERR_EMACS
+#define USE_COLORED_TEXT 0
+#else
#define USE_COLORED_TEXT 1
+#endif
+#endif
#if USE_COLORED_TEXT
#define TEXT_ERROR(x) "\x1b[91m" x "\x1b[0m"
@@ -17,6 +25,10 @@
#define TEXT_IMPORTANT(x) x
#endif
+#if defined(TOC_DEBUG) && __STDC_VERSION__ >= 199901
+#define ERR_SHOW_SOURCE_LOCATION 1
+#endif
+
static inline const char *ordinals(size_t x) {
switch (x % 10) {
case 1: return "st";
@@ -43,15 +55,27 @@ static void err_vfprint(const char *fmt, va_list args) {
}
static void err_print_header_(Location where) {
- err_fprint(TEXT_ERROR("error:") " at line %lu of %s:\n", (unsigned long)where.line, where.ctx->filename);
+#if ERR_EMACS
+ err_fprint("%s:%lu: " TEXT_ERROR("error") ":\n", where.ctx->filename, (unsigned long)where.line);
+#else
+ err_fprint(TEXT_ERROR("error") " at line %lu of %s:\n", (unsigned long)where.line, where.ctx->filename);
+#endif
}
static void info_print_header_(Location where) {
- err_fprint(TEXT_INFO("info:") " at line %lu of %s:\n", (unsigned long)where.line, where.ctx->filename);
+#if ERR_EMACS
+ err_fprint("%s:%lu: " TEXT_INFO("info") ":\n", where.ctx->filename, (unsigned long)where.line);
+#else
+ err_fprint(TEXT_ERROR("info") " at line %lu of %s:\n", (unsigned long)where.line, where.ctx->filename);
+#endif
}
static void warn_print_header_(Location where) {
- err_fprint(TEXT_WARN("warning:") " at line %lu of %s:\n", (unsigned long)where.line, where.ctx->filename);
+#if ERR_EMACS
+ err_fprint("%s:%lu: " TEXT_WARN("warning") ":\n", where.ctx->filename, (unsigned long)where.line);
+#else
+ err_fprint(TEXT_WARN("warning") " at line %lu of %s:\n", (unsigned long)where.line, where.ctx->filename);
+#endif
}
static void err_print_location_text(Location where) {
@@ -90,18 +114,29 @@ static void err_vprint(Location where, const char *fmt, va_list args) {
err_print_footer_(where);
}
-static void err_print_(int line, const char *file, Location where, const char *fmt, ...) {
+
+static void err_print_(
+#if ERR_SHOW_SOURCE_LOCATION
+ int line, const char *file,
+#endif
+ Location where, const char *fmt, ...) {
va_list args;
if (!where.ctx->enabled) return;
- if (file)
+#if ERR_SHOW_SOURCE_LOCATION
+ if (file)
err_fprint("Generated by line %d of %s:\n", line, file);
+#endif
va_start(args, fmt);
err_vprint(where, fmt, args);
va_end(args);
}
+#if ERR_SHOW_SOURCE_LOCATION
#define err_print(...) err_print_(__LINE__, __FILE__, __VA_ARGS__)
-
+#else
+#define err_print err_print_
+#endif
+
static void info_print(Location where, const char *fmt, ...) {
va_list args;
if (!where.ctx->enabled) return;
diff --git a/test.toc b/test.toc
index e17d3a2..44bde7e 100644
--- a/test.toc
+++ b/test.toc
@@ -1 +1,7 @@
-x : [1]int; \ No newline at end of file
+f ::= fn(x := 3, y:int) {
+
+};
+
+main ::= fn() {
+ f(5);
+};
diff --git a/test2.toc b/test2.toc
new file mode 100644
index 0000000..b32d203
--- /dev/null
+++ b/test2.toc
@@ -0,0 +1,22 @@
+puti ::= fn(x: int) {
+ #C("printf(\"%ld\\n\", (long)x);
+");
+};
+putf ::= fn(x: float) {
+ #C("printf(\"%f\\n\", (double)x);
+");
+};
+
+// f ::= fn(t ::= int, x : t) t {
+// x + 1
+// };
+
+f ::= fn(x : t, t ::= float) t {
+ x+1
+};
+
+main ::= fn() {
+ putf(f(3.321));
+ puti(f(3, int));
+ putf(f(3.3421, float));
+}; \ No newline at end of file
diff --git a/tokenizer.c b/tokenizer.c
index 4a57fd1..3992a75 100644
--- a/tokenizer.c
+++ b/tokenizer.c
@@ -158,15 +158,26 @@ static void tokenization_err(Tokenizer *t, const char *fmt, ...) {
}
/* to be used after tokenization */
-static void tokr_err_(const char *src_file, int src_line, Tokenizer *t, const char *fmt, ...) {
+static void tokr_err_(
+#if ERR_SHOW_SOURCE_LOCATION
+ const char *src_file, int src_line,
+#endif
+ Tokenizer *t, const char *fmt, ...) {
if (!t->token->where.ctx->enabled) return;
- err_fprint("At line %d of %s:\n", src_line, src_file); /* RELEASE: Remove this */
+#if ERR_SHOW_SOURCE_LOCATION
+ err_fprint("At line %d of %s:\n", src_line, src_file);
+#endif
va_list args;
va_start(args, fmt);
err_vprint(t->token->where, fmt, args);
va_end(args);
}
+
+#if ERR_SHOW_SOURCE_LOCATION
#define tokr_err(...) tokr_err_(__FILE__, __LINE__, __VA_ARGS__)
+#else
+#define tokr_err tokr_err_
+#endif
static void tokr_put_location(Tokenizer *tokr, Token *t) {
t->where.line = tokr->line;