Skip to content

Commit

Permalink
add evaluate_file
Browse files Browse the repository at this point in the history
  • Loading branch information
mmitou committed Nov 29, 2010
1 parent 6cdb31b commit 360c49a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
以下の事だけできます。

syntax:
define lambda begin
define defmacro lambda begin cond

primitive procedure:
+ car cdr
Expand Down
120 changes: 54 additions & 66 deletions lispobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

typedef enum type_id
{
SYMBOL, CELL, INTEGER, CHARACTER, BOOLEAN,
SYMBOL, CELL, INTEGER, CHARACTER, BOOLEAN, STRING,
SYNTAX, MACRO, PRIM_PROC, LAMBDA, NUM_OF_TYPES
} type_id;

Expand Down Expand Up @@ -281,80 +281,24 @@ int list_length(list *l)
/*@null@*/
string *new_string(char *s)
{
char *i = s;
character *c = NULL;
character *d = NULL;
string *result = NULL;

if(*i != '\0')
{
result = new_character(*i);
c = result;
i++;
while(*i != '\0')
{
d = new_character(*i);
set_cdr(c, d);
c = d;
++i;
}
}

return result;
string *ns = (string*)malloc(sizeof(string));
ns->tid = STRING;
set_car(ns, s);
set_cdr(ns, NULL);
return ns;
}

bool is_string(lispobj *s)
{
bool result = false;
if(s == NULL)
{
result = true;
}
else if(is_character(s))
{
result = is_string(cdr(s));
}
else
{
result = false;
}
return result;
return s->tid == STRING;
}

bool equal_string(string *l, string *r)
{
bool result = false;
if(l == NULL && r == NULL)
{
result = true;
}
else if( l != NULL && r != NULL)
{
if(equal_character(l, r))
{
result = equal_string(cdr(l), cdr(r));
}
}
return result;
return strcmp(car(l), car(r)) == 0;
}

/*@null@*/
char *string_to_chars(string *l)
{
int len = list_length(l);
char *result = (char*)malloc(sizeof(char)*len + 1);
char *i = result;
character *c = l;

while(c != NULL)
{
*i = *(char*)(c->value[0]);
++i;
c = c->value[1];
}
*i = '\0';
return result;
}

bool generic_equal(lispobj *l, lispobj *r)
{
Expand Down Expand Up @@ -1488,8 +1432,7 @@ bool equal_boolean(boolean *lhs, boolean *rhs)
}
}

#ifdef __MAIN__
int main()
bool repl()
{
char buf[256];
environment *env = new_env();
Expand All @@ -1509,6 +1452,51 @@ int main()
}
printf("\n");
}
return true;
}

lispobj* evaluate_file(char *filepath, environment *env)
{
FILE *fp = fopen(filepath, "r");
char buf[BUFSIZ] = {0};
list *tokens = cons("(", cons("begin", NULL));
list *objs;

if(fp == NULL)
{
fprintf(stderr, "fopen error");
abort();
}

while(fgets(buf, BUFSIZ, fp))
{
append(tokens,tokenize(buf));
}

append(tokens, tokenize(")"));
tokens = expand_readmacro(tokens);
objs = read_tokens(tokens);
objs = eval(objs, env);
print_result(objs);
printf("\n");

fclose(fp);

return objs;
}


#ifdef __MAIN__
int main(int argc, char **argv)
{
if(argc == 2)
{
evaluate_file(argv[1], new_env());
}
else
{
repl();
}
return 0;
}
#endif
2 changes: 1 addition & 1 deletion lispobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ typedef list string;
string *new_string(char *s);
bool is_string(lispobj *s);
bool equal_string(string *l, string *r);
char *string_to_chars(string *l);


/* generic func */
bool generic_equal(lispobj *l, lispobj *r);
Expand Down
9 changes: 2 additions & 7 deletions test_lispobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ int test_quote()
int test_string()
{
string *s = new_string("hello!");
char *c = string_to_chars(cdr(s));

printf("%s\n", c);
assert(is_string(s));
assert(strcmp(car(s), "hello!") == 0);
return 1;
}

Expand Down Expand Up @@ -570,18 +569,14 @@ int main()
test_cell();
test_list();
test_environment();

test_eval();

test_plus_integer();
test_begin();

test_string();
test_tokenize();
test_newlispobj();
test_expandreadmacro();
test_read_tokens();

test_macro();
test_equal();
test_cond();
Expand Down

0 comments on commit 360c49a

Please sign in to comment.