Permalink
Browse files

repeat, forever with tests

  • Loading branch information...
1 parent ddf415e commit 1fe5cb2a08938c779860435dd691f73f20af21fe @ajlopez committed Mar 25, 2012
Showing with 38 additions and 1 deletion.
  1. +21 −1 lib/ajlogo.js
  2. +17 −0 test/test.js
View
@@ -230,7 +230,7 @@
var result = (new CompositeExpression(this.body)).evaluate(newctx);
- return result ? result.returnValue : null;
+ return (result && result instanceof ReturnValue) ? result.returnValue : null;
}
// to processor
@@ -796,6 +796,26 @@
return [result.returnValue];
});
+ topcontext.setProcedure('repeat', function(count, block) {
+ for (var k = 0; k < count; k++)
+ {
+ var result = evaluateBlock(block, this);
+
+ if (result && result instanceof ReturnValue)
+ return result.returnValue;
+ }
+ });
+
+ topcontext.setProcedure('forever', function(block) {
+ while (true)
+ {
+ var result = evaluateBlock(block, this);
+
+ if (result && result instanceof ReturnValue)
+ return result.returnValue;
+ }
+ });
+
topcontext.setProcedure('local', function(name) {
this.defineVariable(name);
});
View
@@ -194,6 +194,8 @@ assert.ok(ctx.getProcedure('radarcsin'));
assert.ok(ctx.getProcedure('radarccos'));
assert.ok(ctx.getProcedure('iseq'));
assert.ok(ctx.getProcedure('rseq'));
+assert.ok(ctx.getProcedure('repeat'));
+assert.ok(ctx.getProcedure('forever'));
result = ajlogo.compileText('make "three 3');
(new ajlogo.CompositeExpression(result)).evaluate(ctx);
@@ -448,3 +450,18 @@ assert.equal(3, result.length);
assert.equal(1, result[0]);
assert.equal(0, result[1]);
assert.equal(-1, result[2]);
+
+// repeat, forever
+
+output = ''
+ajlogo.evaluateText('repeat 4 [type 1]');
+assert.equal('1111', output);
+
+output = ''
+ajlogo.evaluateText('forever [type 1 stop]');
+assert.equal('1', output);
+
+output = ''
+ajlogo.evaluateText('make "a 10 forever [make "a difference :a 1 test :a iffalse [stop]]');
+assert.equal(0, ctx.getVariable('a'));
+

0 comments on commit 1fe5cb2

Please sign in to comment.