Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a generic stack data structure.

It'll be needed in the parser, and possibly other areas.

The frequency of commits has dropped off, and will remain low for at
least the next few months because I'm back in school. The project
hasn't been abandoned, but it does have a lower priority now.
  • Loading branch information...
commit 3bdf680e40825afff0ab807e4dc6f2ac9ebb949f 1 parent a73ef47
@ingramj authored
Showing with 85 additions and 3 deletions.
  1. +2 −2 Makefile
  2. +22 −0 rescheme.c
  3. +15 −1 rescheme.h
  4. +7 −0 rescheme_p.h
  5. +39 −0 stack.c
View
4 Makefile
@@ -3,8 +3,8 @@ CC = clang
#CFLAGS = -std=c99 -pedantic -Wall -Wextra -Werror -DNDEBUG -Os
CFLAGS = -std=c99 -pedantic -Wall -Wextra -Werror -g -DDEBUG -O0
-OBJECTS = object.o read.o eval.o write.o gc.o symtab.o buffer.o error.o \
- rescheme.o
+OBJECTS = object.o read.o eval.o write.o gc.o symtab.o buffer.o stack.o \
+ error.o rescheme.o
.PHONY: clean cleaner
View
22 rescheme.c
@@ -1,9 +1,15 @@
#include "rescheme.h"
+static void stack_test(void);
+
int main(void)
{
printf("ReScheme v0.3\n");
+#ifdef DEBUG
+ stack_test();
+#endif
+
rs_gc_init();
rs_object obj;
@@ -19,3 +25,19 @@ int main(void)
rs_gc_shutdown();
return 0;
}
+
+
+static void stack_test(void)
+{
+ struct rs_stack *stack = NULL;
+ stack = rs_stack_push(stack, (void *) 1);
+ stack = rs_stack_push(stack, (void *) 2);
+ stack = rs_stack_push(stack, (void *) 3);
+ for (int i = 3; i > 0; i--) {
+ int data = (int) rs_stack_pop(&stack);
+ if (data != i) {
+ rs_fatal("expected %d, got %d", i, data);
+ }
+ }
+ TRACE("passed.");
+}
View
16 rescheme.h
@@ -181,10 +181,24 @@ const char *rs_buf_cstr(struct rs_buf *buf);
+/**** stack.c - generic stack data structure. ****/
+
+/* A stack frame. Stack functions take pointers to these, and a NULL pointer
+ means an empty stack. */
+struct rs_stack;
+
+/* Push data onto stack, and return the new top of the stack. */
+struct rs_stack *rs_stack_push(struct rs_stack *stack, void *data);
+
+/* Pop the top value of stack, and set stack to point to the new top frame. */
+void *rs_stack_pop(struct rs_stack **stack);
+
+
+
/**** error.c - error-reporting. ****/
/* The error macros have a printf-like interface. If the format string ends with
- a ':', then the result of strerror(3) is appened. The "_s" varieties take an
+ a ':', then the result of strerror(3) is appended. The "_s" varieties take an
integer status code to pass to strerror(3), and the normal varieties use
errno.
*/
View
7 rescheme_p.h
@@ -134,6 +134,13 @@ struct rs_buf {
};
+/**** stack.c ****/
+struct rs_stack {
+ struct rs_stack *next;
+ void *data;
+};
+
+
/**** error.c ****/
void eprintf(int status, char const * const func, char const * const fmt, ...);
View
39 stack.c
@@ -0,0 +1,39 @@
+#include "rescheme.h"
+
+#include <assert.h>
+
+static struct rs_stack *rs_stack_alloc_frame(void *data);
+
+struct rs_stack *rs_stack_push(struct rs_stack *stack, void *data)
+{
+ struct rs_stack *new_top = rs_stack_alloc_frame(data);
+ if (stack != NULL) {
+ new_top->next = stack;
+ }
+ return new_top;
+}
+
+void *rs_stack_pop(struct rs_stack **stack)
+{
+ assert(stack != NULL);
+ assert(*stack != NULL);
+
+ struct rs_stack *old_top = *stack;
+ *stack = old_top->next;
+ void *data = old_top->data;
+
+ free(old_top);
+ return data;
+}
+
+
+static struct rs_stack *rs_stack_alloc_frame(void *data)
+{
+ struct rs_stack *frame = malloc(sizeof(struct rs_stack));
+ if (frame == NULL) {
+ rs_fatal("could not allocate stack frame:");
+ }
+ frame->data = data;
+ frame->next = NULL;
+ return frame;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.