Skip to content

Commit

Permalink
change read_tokens()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmitou committed Nov 14, 2010
1 parent 249383b commit 4b8af1f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 53 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ HDRS = lispobj.h
SRCS = test_lispobj.c lispobj.c


all: test
all: test
test: $(SRCS)
gcc -Wall -g $(HDRS) $(SRCS) -o test

tag:
gtags -v

lint:
Expand Down
98 changes: 57 additions & 41 deletions lispobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,13 @@ lispobj *eval(lispobj *exp, environment *env)
else
{
fprintf(stderr, "eval error: not applicable\n");
abort();
}
}
else
{
fprintf(stderr, "eval error: can not evaluate\n");
abort();
}

return result;
Expand Down Expand Up @@ -770,6 +772,32 @@ int delete_tokens(list *tokens)
return 1;
}

bool get_current_exp(list *tokens, cell **tail)
{
bool result;
char *s;

if(tokens == NULL)
{
*tail = NULL;
result = false;
}
else
{
s = car(tokens);
if(s[0] == '(')
{
*tail = close_token(tokens, 0);
}
else
{
*tail = tokens;
}
result = true;
}
return result;
}

list *close_token(list *tokens, int count)
{
list *result = NULL;
Expand Down Expand Up @@ -846,73 +874,61 @@ lispobj *new_lispobj(char *exp)
return result;
}

list *read_tokens(list *tokens)
lispobj* read_tokens(list *tokens)
{
lispobj *obj;
char *s;

if(tokens == NULL)
{
obj = NULL;
return NULL;
}

s = car(tokens);

if(s[0] == '(')
{
cell *closer;
get_current_exp(tokens, &closer);
obj = read_listtokens(cdr(tokens), closer);
}
else
{
s = car(tokens);

if(s[0] == '(')
{
cell *closer = close_token(tokens, 0);
obj = read_listtokens(tokens, closer);
}
else
{
obj = new_lispobj(s);
}
obj = new_lispobj(car(tokens));
}
return obj;
}

/*@null@*/
list *read_listtokens(cell *tokens, cell *tail)
list *read_listtokens(list *tokens, cell *tail)
{
list *result = NULL;
lispobj *obj;
cell *closer;

if(tokens == NULL)
{
return NULL;
return NULL;
}

if(tokens == tail)
else if(tokens == tail)
{
return NULL;
}

char *s = car(tokens);
if(s[0] == '(')

get_current_exp(tokens, &closer);

if(tokens == closer)
{
cell *next = cdr(tokens);
s = car(next);
if(s[0] == '(')
{
cell *closer = close_token(next, 0);
result = cons(
read_listtokens(next, closer),
read_listtokens(cdr(closer), tail));
}
else
{
result = cons(
new_lispobj(s),
read_listtokens(cdr(next), tail));
}
obj = cons(
new_lispobj(car(tokens)),
read_listtokens(cdr(tokens), tail));
}
else
{
result = cons(
new_lispobj(s),
read_listtokens(cdr(tokens), tail));
obj = cons(
read_listtokens(cdr(tokens), closer),
read_listtokens(cdr(closer), tail));
}
return result;

return obj;
}

bool print_lispobj(lispobj *obj)
Expand Down
1 change: 1 addition & 0 deletions lispobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ list *tokenize(char *exp);
int print_token(list *tokens);
int delete_tokens(list *tokens);
list *read_tokens(list *tokens);
list *get_closer(list *tokens, int count);
list *close_token(list *tokens, int count);
list *read_listtokens(cell *head, cell *tail);
bool print_lispobj(lispobj* obj);
Expand Down
21 changes: 10 additions & 11 deletions test_lispobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,30 +282,29 @@ int test_read_tokens()

l = tokenize("10");
i = read_tokens(l);
print_lispobj(i);
assert(integer_to_int(i) == 10);


l = tokenize("(+ 5 5)");
r = read_tokens(l);
i = eval(r, env);
assert(integer_to_int(i) == 10);
printf("----\n");
printf("----\n");


l = tokenize("(+ (+ 1 4) (+ 2 3))");
r = read_tokens(l);
print_lispobj(car(r));
printf("-\n");
print_lispobj(cdr(r));
printf("-\n");
print_lispobj(car(cdr(r)));
printf("-\n");

i = eval(r, env);
assert(integer_to_int(i) == 10);
printf("----\n");

l = tokenize("(+ (+ 1 2) (+ 2 3) (+ 3 4))");
r = read_tokens(l);
i = eval(r, env);
assert(integer_to_int(i) == 15);

l = tokenize("(+ (+ (+ 1 2) (+ 2 3)) (+ 3 4))");
r = read_tokens(l);
i = eval(r, env);
assert(integer_to_int(i) == 15);

return 1;
}
Expand Down

0 comments on commit 4b8af1f

Please sign in to comment.