aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/code.tex3
-rw-r--r--examples/trace.fusp7
-rw-r--r--interpreter/code.c17
-rw-r--r--interpreter/eval.c2
4 files changed, 25 insertions, 4 deletions
diff --git a/doc/code.tex b/doc/code.tex
index 09e8d4a..deeade9 100644
--- a/doc/code.tex
+++ b/doc/code.tex
@@ -42,6 +42,9 @@
\fuspel{time}
& Int
& The current time in seconds since the Unix Epoch. \\
+ \fuspel{trace}
+ & $a\to b\to b$
+ & \fuspel{trace x y} prints \fuspel{x} to stdout and returns \fuspel{y}. \\
\end{tabular}
}
\end{center}
diff --git a/examples/trace.fusp b/examples/trace.fusp
new file mode 100644
index 0000000..2f78059
--- /dev/null
+++ b/examples/trace.fusp
@@ -0,0 +1,7 @@
+import list;
+
+trace p r = code trace p r;
+
+dup f x = f x x;
+
+main = map (dup trace) [1:[2:[3:[4:[5:[]]]]]];
diff --git a/interpreter/code.c b/interpreter/code.c
index e623ff4..be9ece0 100644
--- a/interpreter/code.c
+++ b/interpreter/code.c
@@ -3,7 +3,9 @@
#include <string.h>
#include <time.h>
+#include "graphs.h"
#include "mem.h"
+#include "print.h"
void fill_node_int(struct node** node, int i) {
unsigned int used_count = (*node)->used_count;
@@ -11,7 +13,7 @@ void fill_node_int(struct node** node, int i) {
(*node)->kind = NODE_INT;
(*node)->var1 = my_calloc(1, sizeof(int));
*((int*) (*node)->var1) = i;
- use_node(*node, used_count - 1);
+ use_node(*node, used_count);
}
void fill_node_bool(struct node** node, int i) {
@@ -24,13 +26,21 @@ void fill_node_name(struct node** node, char* s) {
(*node)->kind = NODE_NAME;
(*node)->var1 = my_calloc(1, strlen(s) + 1);
strcpy((*node)->var1, s);
- use_node(*node, used_count - 1);
+ use_node(*node, used_count);
}
void code_time(struct node** result) {
fill_node_int(result, (int) time(NULL));
}
+void code_trace(struct node** result, struct node *p, struct node *r) {
+ print_node(p);
+ printf("\n");
+ use_node(r, (*result)->used_count);
+ free_node(*result, (*result)->used_count, 1);
+ *result = r;
+}
+
void code_add(struct node** result, struct node* a, struct node* b) {
if (a->kind != NODE_INT || b->kind != NODE_INT)
fill_node_name(result, "add on non-ints");
@@ -98,6 +108,9 @@ unsigned char code_find(char* name, void** function) {
if (!strcmp(name, "time")) {
*function = (void(*)(void)) code_time;
return 0;
+ } else if (!strcmp(name, "trace")) {
+ *function = (void(*)(void)) code_trace;
+ return 2;
} else if (!strcmp(name, "add")) {
*function = (void(*)(void)) code_add;
return 2;
diff --git a/interpreter/eval.c b/interpreter/eval.c
index 6933c0d..cd72f40 100644
--- a/interpreter/eval.c
+++ b/interpreter/eval.c
@@ -232,8 +232,6 @@ void eval_code_app(fuspel* rules, struct node** node) {
f2(node, *args[1], *args[2]);
}
- use_node(*node, 1);
-
my_free(args);
}