Start C version. Create tape module
authorMichael Welch <michaelgwelch@gmail.com>
Sat, 25 Feb 2012 17:48:37 +0000 (11:48 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Sat, 25 Feb 2012 17:48:37 +0000 (11:48 -0600)
csrc/Makefile [new file with mode: 0644]
csrc/prog.c [new file with mode: 0644]
csrc/tape.c [new file with mode: 0644]
csrc/tape.h [new file with mode: 0644]

diff --git a/csrc/Makefile b/csrc/Makefile
new file mode 100644 (file)
index 0000000..53970a4
--- /dev/null
@@ -0,0 +1,29 @@
+#IDIR =../include
+INC=/usr/local/include/glib-2.0 /usr/local/lib/glib-2.0/include
+IDIR=$(foreach d, $(INC), -I$d)
+CC=gcc
+CFLAGS=$(IDIR)
+
+ODIR=obj
+LDIR =/usr/local/lib
+
+LIBS=-lglib-2.0
+
+#_DEPS = tape.h
+#DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
+DEPS = tape.h
+
+_OBJ = prog.o tape.o
+OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
+
+
+$(ODIR)/%.o: %.c $(DEPS)
+       $(CC) -c -o $@ $< $(CFLAGS)
+
+brainmess: $(OBJ)
+       gcc -o $@ $^ $(CFLAGS) $(LIBS)
+
+.PHONY: clean
+
+clean:
+       rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 
diff --git a/csrc/prog.c b/csrc/prog.c
new file mode 100644 (file)
index 0000000..f409d1f
--- /dev/null
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include "tape.h"
+int main() {
+    Tape* tape = tape_new();
+    tape_increment(tape);
+    tape_increment(tape);
+    tape_increment(tape);
+    tape_move_forward(tape);
+    tape_increment(tape);
+    tape_increment(tape);
+    tape_move_backward(tape);
+    tape_move_backward(tape);
+    tape_increment(tape);
+    printf("%s\n", tape_to_string(tape)); 
+}
diff --git a/csrc/tape.c b/csrc/tape.c
new file mode 100644 (file)
index 0000000..501c473
--- /dev/null
@@ -0,0 +1,79 @@
+#include <stdlib.h>
+#include "tape.h"
+
+const gpointer GPTR_ZERO = GINT_TO_POINTER(0);
+
+Tape* tape_new()
+{
+    Tape* newTape;
+    newTape = malloc(sizeof(*newTape));
+    newTape->currentCell = g_list_append(NULL, 0);
+    return newTape;
+}
+
+GList* nextCell(GList *list)
+{
+    GList* next = g_list_next(list);
+    if (NULL == next)
+    {
+        GList* new_list = g_list_append(list, GPTR_ZERO); 
+        next = g_list_next(new_list);
+    }
+    return next;
+}
+
+GList* prevCell(GList *list)
+{
+    GList* prev = list->prev;
+    if (NULL == prev)
+    {
+        prev = g_list_prepend(list, GPTR_ZERO);
+    }
+    return prev;
+}
+
+void tape_set(Tape *tape, int value)
+{
+    tape->currentCell->data = GINT_TO_POINTER(value);
+}
+
+int tape_get(Tape *tape)
+{
+    return GPOINTER_TO_INT(tape->currentCell->data);
+}
+
+void tape_increment(Tape *tape)
+{
+    tape_set(tape, tape_get(tape) + 1);
+}
+
+void tape_decrement(Tape *tape)
+{
+    tape_set(tape, tape_get(tape) - 1);
+}
+
+void tape_move_forward(Tape *tape)
+{
+    tape->currentCell = nextCell(tape->currentCell);
+}
+
+void tape_move_backward(Tape *tape)
+{
+    tape->currentCell = prevCell(tape->currentCell);
+}
+
+void append_next(gpointer data, gpointer string)
+{
+    g_string_append_printf(string, ", %d", GPOINTER_TO_INT(data));
+}
+
+const gboolean RETURN_BUFFER = FALSE;
+gchar* tape_to_string(Tape* tape)
+{
+    GString *result = g_string_new("Tape [");
+    GList *current = g_list_first(tape->currentCell);
+    g_string_append_printf(result, "%d", GPOINTER_TO_INT(current->data));
+    g_list_foreach(current->next, (GFunc) append_next, result);
+    g_string_append(result, "]");
+    return g_string_free(result, RETURN_BUFFER);
+}
diff --git a/csrc/tape.h b/csrc/tape.h
new file mode 100644 (file)
index 0000000..6348905
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef _Tape_H_
+#define _Tape_H_
+
+#include <glib.h>
+
+typedef struct _Tape Tape;
+
+struct _Tape {
+   GList *currentCell;
+};
+
+Tape* tape_new();
+gchar* tape_to_string(Tape *tape);
+void tape_increment(Tape *tape);
+void tape_decrement(Tape *tape);
+void tape_move_forward(Tape *tape);
+void tape_move_backward(Tape *tape);
+void tape_set(Tape *tape, int value);
+int tape_get(Tape *tape);
+
+
+#endif