Permalink
Browse files

local, localmake with tests

  • Loading branch information...
1 parent 0bf0dca commit b4f1ba18a6fdfd75c7073042b48893289222fc1e @ajlopez committed Mar 25, 2012
Showing with 53 additions and 17 deletions.
  1. +25 −17 lib/ajlogo.js
  2. +28 −0 test/test.js
View
@@ -26,12 +26,20 @@
Context.prototype.setVariable = function(name, value)
{
- this.variables[name] = value;
+ if (this.parent != null && !this.variables.hasOwnProperty(name))
+ this.parent.setVariable(name, value);
+ else
+ this.variables[name] = value;
}
+
+ Context.prototype.defineVariable = function(name)
+ {
+ this.variables[name] = null;
+ }
Context.prototype.getVariable = function(name)
{
- if (!this.variables.hasOwnProperty(name) && this.parent != null)
+ if (this.parent != null && !this.variables.hasOwnProperty(name))
return this.parent.getVariable(name);
return this.variables[name];
@@ -502,13 +510,13 @@
if (!block.compiled)
block.compiled = compileList(block);
- return (new CompositeExpression(block.compiled)).evaluate(context);
+ return (new CompositeExpression(block.compiled)).evaluate(new Context(context));
}
// Primitives
topcontext.setProcedure('make', function(name, value) {
- topcontext.setVariable(name, value);
+ this.setVariable(name, value);
});
topcontext.setProcedure('add', function(x, y) {
@@ -579,30 +587,21 @@
if (!this.lasttest)
return;
- if (!block.compiled)
- block.compiled = compileList(block);
-
- return (new CompositeExpression(block.compiled)).evaluate(this);
+ return evaluateBlock(block, this);
});
topcontext.setProcedure('iffalse', function(block) {
if (this.lasttest)
return;
- if (!block.compiled)
- block.compiled = compileList(block);
-
- return (new CompositeExpression(block.compiled)).evaluate(this);
+ return evaluateBlock(block, this);
});
topcontext.setProcedure('run', function(block) {
if (this.lasttest)
return;
- if (!block.compiled)
- block.compiled = compileList(block);
-
- (new CompositeExpression(block.compiled)).evaluate(this);
+ return evaluateBlock(block, this);
});
topcontext.setProcedure('runresult', function(block) {
@@ -612,13 +611,22 @@
if (!block.compiled)
block.compiled = compileList(block);
- var result = (new CompositeExpression(block.compiled)).evaluate(this);
+ var result = evaluateBlock(block, this);
if (result == null || result.returnValue == null)
return [];
return [result.returnValue];
});
+
+ topcontext.setProcedure('local', function(name) {
+ this.defineVariable(name);
+ });
+
+ topcontext.setProcedure('localmake', function(name, value) {
+ this.defineVariable(name);
+ this.setVariable(name, value);
+ });
// Exports
View
@@ -21,6 +21,24 @@ var newctx = new ajlogo.Context(ctx);
assert.ok(ctx.getProcedure('make'));
assert.ok(newctx.getProcedure('make'));
+ctx.setVariable('a', 1);
+newctx.setVariable('a', 2);
+
+assert.equal(2, ctx.getVariable('a'));
+assert.equal(2, newctx.getVariable('a'));
+
+ctx.setVariable('b', 1);
+newctx.defineVariable('b');
+newctx.setVariable('b', 2);
+
+assert.equal(1, ctx.getVariable('b'));
+assert.equal(2, newctx.getVariable('b'));
+
+newctx.setVariable('c', 3);
+
+assert.equal(3, ctx.getVariable('c'));
+assert.equal(3, newctx.getVariable('c'));
+
// Evaluate Composite Expression
var expression = new ajlogo.CompositeExpression([1, 2, 3]);
@@ -140,6 +158,8 @@ assert.ok(ctx.getProcedure('iftrue'));
assert.ok(ctx.getProcedure('iffalse'));
assert.ok(ctx.getProcedure('run'));
assert.ok(ctx.getProcedure('runresult'));
+assert.ok(ctx.getProcedure('local'));
+assert.ok(ctx.getProcedure('localmake'));
result = ajlogo.compileText('make "three 3');
(new ajlogo.CompositeExpression(result)).evaluate(ctx);
@@ -252,5 +272,13 @@ assert.ok(result instanceof Array);
assert.equal(1, result.length);
assert.equal(2, result[0]);
+// local, make, localmake
+
+ajlogo.evaluateText('make "a 1 run [local "a make "a 2 make "b :a]');
+assert.equal(1, ctx.getVariable('a'));
+assert.equal(2, ctx.getVariable('b'));
+ajlogo.evaluateText('make "a 3 run [localmake "a 4 make "b :a]');
+assert.equal(3, ctx.getVariable('a'));
+assert.equal(4, ctx.getVariable('b'));

0 comments on commit b4f1ba1

Please sign in to comment.