Browse files

foreach with test

  • Loading branch information...
1 parent 847cbbe commit d2e458c54ab14e314c31ed7c12eb4abe8a75058b @ajlopez committed Mar 25, 2012
Showing with 13 additions and 1 deletion.
  1. +7 −0 lib/ajlogo.js
  2. +6 −1 test/test.js
View
7 lib/ajlogo.js
@@ -777,6 +777,13 @@
return proc.apply(this, [arg]);
});
+ topcontext.setProcedure('foreach', function(name, list) {
+ var proc = this.getProcedure(name);
+
+ for (var k = 0; k < list.length; k++)
+ proc.apply(this, [list[k]]);
+ });
+
topcontext.setProcedure('output', function(value) {
return new ReturnValue(value);
});
View
7 test/test.js
@@ -203,6 +203,7 @@ assert.ok(ctx.getProcedure('while'));
assert.ok(ctx.getProcedure('until'));
assert.ok(ctx.getProcedure('apply'));
assert.ok(ctx.getProcedure('invoke'));
+assert.ok(ctx.getProcedure('foreach'));
result = ajlogo.compileText('make "three 3');
(new ajlogo.CompositeExpression(result)).evaluate(ctx);
@@ -514,7 +515,7 @@ output = ''
ajlogo.evaluateText('make "a 0 to dox until :a [ type :a stop] type "end end dox');
assert.equal('0', output);
-// apply, invoke
+// apply, invoke, foreach
assert.equal(3, ajlogo.evaluateText('apply "sum [1 2]'));
assert.equal(-1, ajlogo.evaluateText('apply "difference [1 2]'));
@@ -523,3 +524,7 @@ output = '';
ajlogo.evaluateText('invoke "type 1');
assert.equal('1', output);
+output = '';
+ajlogo.evaluateText('foreach "type [1 2 3 4]');
+assert.equal('1234', output);
+

0 comments on commit d2e458c

Please sign in to comment.