Browse files

readme

  • Loading branch information...
1 parent aac5e01 commit 6cccd85234d51610c02a708c87ac84ef009c81c6 Alan Dipert committed Jul 2, 2009
Showing with 50 additions and 12 deletions.
  1. +30 −0 README
  2. +12 −8 main.c
  3. +8 −4 parse.c
View
30 README
@@ -0,0 +1,30 @@
+shave
+
+This is the beginnings of a LISP interpreter. It's kind of full of garbage right now, because I got impatient and started copy-pasting. The lexer is handwritten, so yeah, it rules. The generated AST is a big linked list, which is evaluated in the eval step. The AST is made up of 'Token' structs, which have type and property enums used by the eval routines.
+
+compile:
+
+ $ make
+
+use:
+
+ $ ./shave
+ shave.
+ > (if (> 3 2) (/ 9 (+ 3 2)))
+ list:
+ if:
+
+ list:
+ >:
+ atom: 3
+ atom: 2
+
+ list:
+ /:
+ atom: 9
+
+ list:
+ +:
+ atom: 3
+ atom: 2
+ atom: 1.8
View
20 main.c
@@ -10,17 +10,21 @@
int main(int argc, char *argv[])
{
- int size;
- char buf[MAX_BUFFER_SIZE+1];
+ if(argc > 1) {
+ eval(argv[1]);
+ } else {
+ int size;
+ char buf[MAX_BUFFER_SIZE+1];
- printf("shave.");
- printf("\n> ");
- fflush(stdout);
- while ((size = read(fileno(stdin), buf, MAX_BUFFER_SIZE)) > 0) {
- buf[size]='\0';
- eval(buf);
+ printf("shave.");
printf("\n> ");
fflush(stdout);
+ while ((size = read(fileno(stdin), buf, MAX_BUFFER_SIZE)) > 0) {
+ buf[size]='\0';
+ eval(buf);
+ printf("\n> ");
+ fflush(stdout);
+ }
}
return 0;
View
12 parse.c
@@ -4,6 +4,8 @@
#include <ctype.h>
#include "parse.h"
+#define MAX_BUFFER_SIZE 1024
+
static Token* t_mk();
static Token* t_append(Token *t1, Token *t2);
static Token* t_rewind(Token *t);
@@ -14,8 +16,7 @@ static void t_pp(Token *t, int level);
static Token* tokenize(char *input);
static Token* process(Token *t);
-static PARSE_ERROR = 0;
-static EVAL_ERROR = 0;
+static int PARSE_ERROR = 0;
static Token* t_mk() {
return (Token*)malloc(sizeof(Token));
@@ -78,6 +79,7 @@ static void pp(Token *t, int level) {
printf("%s:\n", op_pp(t->o_type));
break;
case LIST:
+ printf("\n");
t_tab(level);
printf("list:\n");
t_pp(t->head, level+1);
@@ -105,7 +107,7 @@ static Token* tokenize(char *input) {
char *m_start;
m_count = 0;
Token *last_token = NULL;
- char buf[100];
+ char buf[MAX_BUFFER_SIZE];
for(i = 0; i < strlen(input); i++) {
if(input[i] == '(') {
int p_count = 1;
@@ -458,9 +460,11 @@ static Token* process(Token *t) {
void eval(char *input) {
Token *root = tokenize(input);
/*printf("parse tree:\n");*/
- /*t_pp(root, 0);*/
+ t_pp(root, 0);
/*printf("-----------------\nresult:\n");*/
if(!PARSE_ERROR) {
pp(process(root), 0);
+ } else {
+ PARSE_ERROR = 0;
}
}

0 comments on commit 6cccd85

Please sign in to comment.