Skip to content

Commit

Permalink
Added for loop, break statement not implemented yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
ellamental committed Sep 10, 2010
1 parent 7cc4bce commit 94d42d0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lispy.c
Expand Up @@ -129,6 +129,8 @@ object *h_vector(object *exp, object *env);
object *h_length(object *obj);
object *h_list(object *exp, object *env);
object *h_string(object *exp, object *env);
object *h_for(object *exp, object *env);

object *h_emptyp(object *obj);


Expand Down Expand Up @@ -1307,6 +1309,11 @@ object *eval(object *exp, object *env) {
return h_vector(cdr(exp), env);
}

/** for **/
else if (is_primitive_syntax(exp, for_symbol)) {
return h_for(cdr(exp), env);
}

/** Application **/
else if (is_application(exp)) {
procedure = eval(car(exp), env);
Expand Down Expand Up @@ -2540,6 +2547,23 @@ object *make_expression(object *exp, object *var) {
}
}


// for

object *h_for(object *exp, object *env) {
object *var = car(exp);
object *seq = eval(caddr(exp), env);
object *expression = make_expression(cadddr(exp), var);
object *result;

while (h_emptyp(seq) != True) {
result = eval(make_application(expression, h_first(seq)), env);
seq = h_rest(seq);
}
return result;
}


// list

object *h_list_for(object *exp, object *env) {
Expand Down
18 changes: 18 additions & 0 deletions unit_test.lispy
Expand Up @@ -758,6 +758,24 @@
)



;; Iterators
;;_______________________________________________________;;

;; for
;;_________________________;;

(test
(for ii in '(1 2 3) ii)
>>> 3
(for ii in #(1 2 3) ii)
>>> 3
(for ii in "hello" ii)
>>> #\o
)



;; Sequence Procedures
;;_______________________________________________________;;

Expand Down

0 comments on commit 94d42d0

Please sign in to comment.