Skip to content
Browse files

use glib

  • Loading branch information...
1 parent bbb16a1 commit db160ef90a633e944c55a2b2660ffb225d7ac7d2 Kristoffer Grönlund committed Sep 2, 2009
Showing with 37 additions and 13 deletions.
  1. +5 −2 Makefile
  2. +25 −6 main.c
  3. +6 −4 stream.c
  4. +1 −1 stream.h
View
7 Makefile
@@ -3,11 +3,14 @@ CFLAGS=-Wall -Werror -g -std=c99
DEPS = stream.h
OBJ = main.o stream.o
+glib_cflags = `pkg-config --cflags glib-2.0`
+glib_libs = `pkg-config --libs glib-2.0`
+
%.o: %.c $(DEPS)
- $(CC) -c -o $@ $< $(CFLAGS)
+ $(CC) $(glib_cflags) -c -o $@ $< $(CFLAGS)
main: $(OBJ)
- $(CC) -o $@ $^ $(CFLAGS)
+ $(CC) -o $@ $^ $(glib_cflags) $(CFLAGS) $(glib_libs)
.PHONY: clean
View
31 main.c
@@ -1,9 +1,15 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-
+#include <glib.h>
#include "stream.h"
+/*
+ COND:
+ input: list of expressions and associated values
+ output: first value whose expression evaluates to true
+ */
+
struct cons_cell {
struct cons_cell* car;
struct cons_cell* cdr;
@@ -13,15 +19,28 @@ struct interpreter {
struct instream stream;
};
-int init_interpreter(struct interpreter* I) {
+gboolean init_interpreter(struct interpreter* I, const char* filename) {
memset(I, 0, sizeof(struct interpreter));
- return 0;
+ init_file_stream(&I->stream, filename);
+ return TRUE;
+}
+
+const char* intern(const char* str) {
+ return g_intern_string(str);
+}
+
+int get_char(struct interpreter* I) {
+ return I->stream.get_char(I->stream.hstream);
+}
+
+void put_char(struct interpreter* I, int ch) {
+ I->stream.put_char(ch, I->stream.hstream);
}
int main(int argc, char* argv[]) {
struct interpreter i;
- init_interpreter(&i);
- init_file_stream(&i.stream, argv[1]);
- printf("%c\n", i.stream.get_char(i.stream.hstream));
+ struct interpreter* I = &i;
+ init_interpreter(I, argv[1]);
+ printf("%c\n", get_char(I));
return 0;
}
View
10 stream.c
@@ -1,6 +1,8 @@
-#include "stream.h"
+#include <glib.h>
#include <stdio.h>
+#include "stream.h"
+
void file_close_stream(void* hstream) {
fclose((FILE*)hstream);
}
@@ -13,12 +15,12 @@ void file_put_char(int ch, void* hstream) {
ungetc(ch, (FILE*)hstream);
}
-int init_file_stream(struct instream* stream, const char* filename) {
+gboolean init_file_stream(struct instream* stream, const char* filename) {
stream->hstream = fopen(filename, "r");
if (!stream->hstream)
- return -1;
+ return FALSE;
stream->close_stream = &file_close_stream;
stream->get_char = &file_get_char;
stream->put_char = &file_put_char;
- return 0;
+ return TRUE;
}
View
2 stream.h
@@ -8,6 +8,6 @@ struct instream {
void (*put_char)(int, void*);
};
-int init_file_stream(struct instream* stream, const char* filename);
+gboolean init_file_stream(struct instream* stream, const char* filename);
#endif//_STREAM_H_

0 comments on commit db160ef

Please sign in to comment.
Something went wrong with that request. Please try again.