aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-09-25 14:31:28 +0200
committerCamil Staps2016-09-25 14:31:28 +0200
commit99d46a3d6eb09860088c92fc67fd0487b262d50c (patch)
treec643eee228b18356a7b85c21143d96a8bee874f6
parentVarious minor cleanups (diff)
Added code: add, eq, ge, gt, le, lt, ne
-rw-r--r--doc/code.tex31
-rw-r--r--interpreter/code.c74
2 files changed, 104 insertions, 1 deletions
diff --git a/doc/code.tex b/doc/code.tex
index c25897a..09e8d4a 100644
--- a/doc/code.tex
+++ b/doc/code.tex
@@ -2,17 +2,46 @@
\label{sec:code}
+\begin{center}
{\renewcommand{\arraystretch}{1.3}
\begin{tabular}{l l l}
- \textbf{Name} & \textbf{Type} & \textbf{Description} \\\hline
+ \textbf{Name} & \textbf{Type} & \textbf{Description} \\
+
+ \hline\hline\multicolumn{3}{l}{\emph{Integer operations}} \\\hline
+ \fuspel{add a b}
+ & Int $\to$ Int $\to$ Int
+ & Adds $a$ and $b$ \\
\fuspel{mul a b}
& Int $\to$ Int $\to$ Int
& Multiplies $a$ and $b$ \\
\fuspel{sub a b}
& Int $\to$ Int $\to$ Int
& Subtracts $a$ from $b$ \\
+
+ \hline \multicolumn{3}{l}{\emph{Tests}} \\\hline
+ \fuspel{eq a b}
+ & Int $\to$ Int $\to \{0,1\}$
+ & 1 if $a=b$, 0 otherwise \\
+ \fuspel{ge a b}
+ & Int $\to$ Int $\to \{0,1\}$
+ & 1 if $a\ge b$, 0 otherwise \\
+ \fuspel{gt a b}
+ & Int $\to$ Int $\to \{0,1\}$
+ & 1 if $a>b$, 0 otherwise \\
+ \fuspel{le a b}
+ & Int $\to$ Int $\to \{0,1\}$
+ & 1 if $a\le b$, 0 otherwise \\
+ \fuspel{lt a b}
+ & Int $\to$ Int $\to \{0,1\}$
+ & 1 if $a<b$, 0 otherwise \\
+ \fuspel{ne a b}
+ & Int $\to$ Int $\to \{0,1\}$
+ & 1 if $a\neq b$, 0 otherwise \\
+
+ \hline\multicolumn{3}{l}{\emph{Miscellaneous}} \\\hline
\fuspel{time}
& Int
& The current time in seconds since the Unix Epoch. \\
\end{tabular}
}
+\end{center}
diff --git a/interpreter/code.c b/interpreter/code.c
index 595aac3..e623ff4 100644
--- a/interpreter/code.c
+++ b/interpreter/code.c
@@ -14,6 +14,10 @@ void fill_node_int(struct node** node, int i) {
use_node(*node, used_count - 1);
}
+void fill_node_bool(struct node** node, int i) {
+ fill_node_int(node, i ? 1 : 0);
+}
+
void fill_node_name(struct node** node, char* s) {
unsigned int used_count = (*node)->used_count;
free_node(*node, used_count, 0);
@@ -27,6 +31,13 @@ void code_time(struct node** result) {
fill_node_int(result, (int) time(NULL));
}
+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");
+ else
+ fill_node_int(result, *((int*) b->var1) + *((int*) a->var1));
+}
+
void code_mul(struct node** result, struct node* a, struct node* b) {
if (a->kind != NODE_INT || b->kind != NODE_INT)
fill_node_name(result, "mul on non-ints");
@@ -41,16 +52,79 @@ void code_sub(struct node** result, struct node* a, struct node* b) {
fill_node_int(result, *((int*) b->var1) - *((int*) a->var1));
}
+void code_eq(struct node** result, struct node* a, struct node* b) {
+ if (a->kind != NODE_INT || b->kind != NODE_INT)
+ fill_node_name(result, "eq on non-ints");
+ else
+ fill_node_bool(result, *((int*) a->var1) == *((int*) b->var1));
+}
+
+void code_gt(struct node** result, struct node* a, struct node* b) {
+ if (a->kind != NODE_INT || b->kind != NODE_INT)
+ fill_node_name(result, "gt on non-ints");
+ else
+ fill_node_bool(result, *((int*) a->var1) > *((int*) b->var1));
+}
+
+void code_ge(struct node** result, struct node* a, struct node* b) {
+ if (a->kind != NODE_INT || b->kind != NODE_INT)
+ fill_node_name(result, "ge on non-ints");
+ else
+ fill_node_bool(result, *((int*) a->var1) >= *((int*) b->var1));
+}
+
+void code_lt(struct node** result, struct node* a, struct node* b) {
+ if (a->kind != NODE_INT || b->kind != NODE_INT)
+ fill_node_name(result, "lt on non-ints");
+ else
+ fill_node_bool(result, *((int*) a->var1) < *((int*) b->var1));
+}
+
+void code_le(struct node** result, struct node* a, struct node* b) {
+ if (a->kind != NODE_INT || b->kind != NODE_INT)
+ fill_node_name(result, "le on non-ints");
+ else
+ fill_node_bool(result, *((int*) a->var1) <= *((int*) b->var1));
+}
+
+void code_ne(struct node** result, struct node* a, struct node* b) {
+ if (a->kind != NODE_INT || b->kind != NODE_INT)
+ fill_node_name(result, "ne on non-ints");
+ else
+ fill_node_bool(result, *((int*) a->var1) != *((int*) b->var1));
+}
+
unsigned char code_find(char* name, void** function) {
if (!strcmp(name, "time")) {
*function = (void(*)(void)) code_time;
return 0;
+ } else if (!strcmp(name, "add")) {
+ *function = (void(*)(void)) code_add;
+ return 2;
} else if (!strcmp(name, "mul")) {
*function = (void(*)(void)) code_mul;
return 2;
} else if (!strcmp(name, "sub")) {
*function = (void(*)(void)) code_sub;
return 2;
+ } else if (!strcmp(name, "eq")) {
+ *function = (void(*)(void)) code_eq;
+ return 2;
+ } else if (!strcmp(name, "ge")) {
+ *function = (void(*)(void)) code_ge;
+ return 2;
+ } else if (!strcmp(name, "gt")) {
+ *function = (void(*)(void)) code_gt;
+ return 2;
+ } else if (!strcmp(name, "le")) {
+ *function = (void(*)(void)) code_le;
+ return 2;
+ } else if (!strcmp(name, "lt")) {
+ *function = (void(*)(void)) code_lt;
+ return 2;
+ } else if (!strcmp(name, "ne")) {
+ *function = (void(*)(void)) code_ne;
+ return 2;
}
*function = NULL;