From e7b381e8b0d1f060a16d8702d20a14a4cb726f6d Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Fri, 26 Aug 2016 14:27:36 +0200
Subject: Added code_print

---
 interpreter/code.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

(limited to 'interpreter/code.c')

diff --git a/interpreter/code.c b/interpreter/code.c
index 8845227..a306e02 100644
--- a/interpreter/code.c
+++ b/interpreter/code.c
@@ -1,9 +1,11 @@
 #include "code.h"
 
+#include <stdio.h>
 #include <string.h>
 #include <time.h>
 
 #include "mem.h"
+#include "print.h"
 
 expression* make_int_expression(int i) {
 	expression* result = my_calloc(1, sizeof(expression));
@@ -25,6 +27,14 @@ expression* code_time(void) {
 	return make_int_expression((int) time(NULL));
 }
 
+expression* code_print(expression* expr) {
+	expression* result = my_calloc(1, sizeof(expression));
+	cpy_expression(result, expr);
+	print_expression(expr);
+	printf("\n");
+	return result;
+}
+
 expression* code_mul(expression* a, expression* b) {
 	return (a->kind != EXPR_INT || b->kind != EXPR_INT)
 		? make_name_expression("mul on non-ints")
@@ -38,15 +48,18 @@ expression* code_sub(expression* a, expression* b) {
 }
 
 unsigned char code_find(char* name, void** function) {
-	if (!strcmp(name, "mul")) {
+	if (!strcmp(name, "time")) {
+		*function = (void(*)(void)) code_time;
+		return 0;
+	} else if (!strcmp(name, "print")) {
+		*function = (void(*)(void)) code_print;
+		return 1;
+	} 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, "time")) {
-		*function = (void(*)(void)) code_time;
-		return 0;
 	}
 
 	*function = NULL;
-- 
cgit v1.2.3