Permalink
Browse files

run code from files

  • Loading branch information...
1 parent f0b944e commit 07bb47885578b2212484b9c62ad4d12488b46e52 @jaz303 committed Jan 4, 2012
Showing with 107 additions and 38 deletions.
  1. +2 −1 Makefile
  2. +8 −0 include/lispy/io.h
  3. +0 −1 include/lispy/lispy.h
  4. +38 −0 src/io.c
  5. +49 −1 src/lispy.c
  6. +1 −35 src/repl.c
  7. +9 −0 test.lisp
View
@@ -16,7 +16,8 @@ OBJS = ext/jazlib/src/common.o \
src/parser.o \
src/binding.o \
src/eval.o \
- src/native.o
+ src/native.o \
+ src/io.o
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
View
@@ -0,0 +1,8 @@
+#ifndef IO_H
+#define IO_H
+
+#include "lispy/lispy.h"
+
+void pretty_print(env_t *env, VALUE v, int i);
+
+#endif
View
@@ -71,7 +71,6 @@ typedef struct obj {
typedef struct {
obj_t obj; /* object header */
size_t length; /* number of values in list */
-// VALUE (*form)(state_t*, VALUE); /* evaluation function for this list */
VALUE values[0]; /* array of list values */
} list_t;
View
@@ -0,0 +1,38 @@
+#include "lispy/io.h"
+#include "lispy/intern.h"
+
+#include <stdio.h>
+
+static void indent(int indent) {
+ int i;
+ for (i = 0; i < indent; i++) fputs(" ", stdout);
+}
+
+void pretty_print(env_t *env, VALUE v, int i) {
+ if (VALUE_IS_ERROR(v)) {
+ printf("<error>\n");
+ } else if (IS_LIST(v)) {
+ printf("(\n");
+ int j;
+ for (j = 0; j < list_len(v); j++) {
+ indent(i + 1); pretty_print(env, list_get(v, j), i + 1);
+ }
+ indent(i); printf(")\n");
+ } else {
+ if (VALUE_IS_INT(v)) {
+ printf("%lld\n", INTVAL(v));
+ } else if (VALUE_IS_BOOL(v)) {
+ printf("#%c\n", BOOLVAL(v) ? 't' : 'f');
+ } else if (VALUE_IS_NIL(v)) {
+ printf("#nil\n");
+ } else if (VALUE_IS_IDENT(v)) {
+ printf("%s\n", intern_table_get_str(&env->intern, IDENT(v)));
+ } else if (VALUE_IS_ATOM(v)) {
+ printf(":%s\n", intern_table_get_str(&env->intern, ATOM(v)));
+ } else if (IS_STRING(v)) {
+ printf("\"%s\"\n", string_chars(v));
+ } else {
+ printf("<unknown %p>\n", v);
+ }
+ }
+}
View
@@ -2,13 +2,61 @@
#include "lispy/env.h"
#include "lispy/repl.h"
+#include "lispy/io.h"
+#include "lispy/lexer.h"
+#include "lispy/parser.h"
+#include "lispy/eval.h"
+
+#include <stdio.h>
+#include <stdlib.h>
int main(int argc, char *argv[]) {
env_t env;
env_init(&env);
- repl_run(&env);
+ if (argc > 1) {
+ VALUE out;
+
+ int i;
+ for (i = 1; i < argc; i++) {
+ FILE *file = fopen(argv[1], "r");
+ if (file) {
+
+ fseek(file, 0, SEEK_END);
+ long sz = ftell(file);
+ char *source = malloc(sz + 1);
+ fseek(file, 0, SEEK_SET);
+ fread(source, 1, sz, file);
+ source[sz] = 0;
+ fclose(file);
+
+ lexer_t lexer;
+ parser_t parser;
+
+ lexer_init(&lexer);
+ lexer_reset(&lexer, source);
+
+ parser_init(&parser, &lexer, &env);
+
+ list_t *list = parser_parse(&parser);
+ if (list) {
+ out = eval(&env, &env.binding, list);
+ } else {
+ printf("parse error: %s\n", parser.error);
+ exit(1);
+ }
+
+ free(source);
+
+ }
+ }
+
+ pretty_print(&env, out, 0);
+
+ } else {
+ repl_run(&env);
+ }
return 0;
View
@@ -7,41 +7,7 @@
#include "lispy/intern.h"
#include "lispy/parser.h"
#include "lispy/eval.h"
-
-static void indent(int indent) {
- int i;
- for (i = 0; i < indent; i++) fputs(" ", stdout);
-}
-
-static void pretty_print(env_t *env, VALUE v, int i);
-static void pretty_print(env_t *env, VALUE v, int i) {
- if (VALUE_IS_ERROR(v)) {
- printf("<error>\n");
- } else if (IS_LIST(v)) {
- printf("(\n");
- int j;
- for (j = 0; j < list_len(v); j++) {
- indent(i + 1); pretty_print(env, list_get(v, j), i + 1);
- }
- indent(i); printf(")\n");
- } else {
- if (VALUE_IS_INT(v)) {
- printf("%lld\n", INTVAL(v));
- } else if (VALUE_IS_BOOL(v)) {
- printf("#%c\n", BOOLVAL(v) ? 't' : 'f');
- } else if (VALUE_IS_NIL(v)) {
- printf("#nil\n");
- } else if (VALUE_IS_IDENT(v)) {
- printf("%s\n", intern_table_get_str(&env->intern, IDENT(v)));
- } else if (VALUE_IS_ATOM(v)) {
- printf(":%s\n", intern_table_get_str(&env->intern, ATOM(v)));
- } else if (IS_STRING(v)) {
- printf("\"%s\"\n", string_chars(v));
- } else {
- printf("<unknown %p>\n", v);
- }
- }
-}
+#include "lispy/io.h"
void repl_run(env_t *env) {
View
@@ -0,0 +1,9 @@
+(begin
+ (define add (lambda (v) (+ v 10)))
+ (define foo 100)
+ (add foo)
+
+ (if #f
+ (add 200)
+ (add 100))
+)

0 comments on commit 07bb478

Please sign in to comment.