Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move Env to types and make it a subclass of Type

  • Loading branch information...
commit 125cbf2cf886a90ea251d86e7ad90b867a916123 1 parent f07ae97
@marianoguerra authored
View
1  js/squim.demo.js
@@ -3,7 +3,6 @@ require.config({
baseUrl: "js/",
paths: {
"squim": "../src/squim",
- "squim.env": "../src/squim.env",
"squim.types": "../src/squim.types",
"squim.parser": "../src/squim.parser",
"squim.error": "../src/squim.error",
View
85 src/squim.env.js
@@ -1,85 +0,0 @@
-/*global define SquimError*/
-(function (root, factory) {
- "use strict";
-
- if (typeof define === 'function' && define.amd) {
- // AMD. Register as an anonymous module.
- define(['squim.error'], function (Error) {
- // Also create a global in case some scripts
- // that are loaded still are looking for
- // a global even when an AMD loader is in use.
- return (root.SquimEnv = factory(Error));
- });
- } else {
- // Browser globals
- root.SquimEnv = factory(SquimError);
- }
-
-}(this, function (Error) {
- "use strict";
-
- function Env(bindings, parents, inmutable) {
-
- if (bindings === undefined) {
- bindings = {};
- }
-
- if (parents === undefined) {
- parents = [];
- }
-
- this.parents = parents;
- this.bindings = bindings;
- this.inmutable = (inmutable === true);
- }
-
- Env.prototype.define = function (name, value) {
- if (this.inmutable) {
- return Error.MutationError("can't mutate inmutable environment", {name: name, value: value});
- } else {
- this.bindings[name] = value;
- }
- };
-
- Env.prototype.get = function (name) {
- var i, value = this.bindings[name];
-
- if (value === undefined) {
- for (i = 0; i < this.parents.length; i += 1) {
- value = this.parents[i].get(name);
-
- if (value !== undefined) {
- return value;
- }
- }
-
- return undefined;
- } else {
- return value;
- }
- };
-
- Env.prototype.eq_p = function (obj) {
- return this === obj;
- };
-
- Env.prototype.equal_p = Env.prototype.eq_p;
-
- // @internal
- Env.prototype.toJs = function () {
- var i, parents = [];
-
- for (i = 0; i < this.parents.length; i += 1) {
- parents.push(this.parents[i].toJs());
- }
-
- return {
- "bindings": this.bindings,
- "parents": parents
- };
- };
-
- // NOTE: functions are added to Env in squim.types to avoid
- // circular dependency
- return Env;
-}));
View
74 src/squim.types.js
@@ -1,20 +1,20 @@
-/*global define SquimError SquimEnv SquimUtil*/
+/*global define SquimError SquimUtil*/
(function (root, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
- define(['json', 'squim.env', 'squim.error', 'squim.util'], function (JSON, Env, Error, Util) {
+ define(['json', 'squim.error', 'squim.util'], function (JSON, Error, Util) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
- return (root.SquimTypes = factory(JSON, Env, Error, Util));
+ return (root.SquimTypes = factory(JSON, Error, Util));
});
} else {
// Browser globals
- root.SquimTypes = factory(JSON, SquimEnv, SquimError, SquimUtil);
+ root.SquimTypes = factory(JSON, SquimError, SquimUtil);
}
-}(this, function (JSON, Env, Error, Util) {
+}(this, function (JSON, Error, Util) {
"use strict";
var obj = {};
@@ -63,7 +63,7 @@
}
};
- Symbol.prototype.expand = Type.prototype.eval_;
+ Symbol.prototype.expand = Symbol.prototype.eval_;
Symbol.prototype.eq_p = function (obj) {
// because a string may contain the same value
@@ -236,6 +236,68 @@
Pair.nil = new Pair.Nil();
+ function Env(bindings, parents, inmutable) {
+
+ if (bindings === undefined) {
+ bindings = {};
+ }
+
+ if (parents === undefined) {
+ parents = [];
+ }
+
+ this.parents = parents;
+ this.bindings = bindings;
+ this.inmutable = (inmutable === true);
+ }
+
+ Env.prototype = new Type(null);
+
+ Env.prototype.define = function (name, value) {
+ if (this.inmutable) {
+ return Error.MutationError("can't mutate inmutable environment", {name: name, value: value});
+ } else {
+ this.bindings[name] = value;
+ }
+ };
+
+ Env.prototype.get = function (name) {
+ var i, value = this.bindings[name];
+
+ if (value === undefined) {
+ for (i = 0; i < this.parents.length; i += 1) {
+ value = this.parents[i].get(name);
+
+ if (value !== undefined) {
+ return value;
+ }
+ }
+
+ return undefined;
+ } else {
+ return value;
+ }
+ };
+
+ Env.prototype.eq_p = function (obj) {
+ return this === obj;
+ };
+
+ Env.prototype.equal_p = Env.prototype.eq_p;
+
+ Env.prototype.toJs = function () {
+ var i, parents = [];
+
+ for (i = 0; i < this.parents.length; i += 1) {
+ parents.push(this.parents[i].toJs());
+ }
+
+ return {
+ "bindings": this.bindings,
+ "parents": parents
+ };
+ };
+
function Applicative(operative) {
this.operative = operative;
}
View
12 test/squim.env.test.js
@@ -1,22 +1,22 @@
-/*global define QUnit SquimEnv SquimTypes SquimError*/
+/*global define QUnit SquimTypes SquimError*/
(function (root, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
- define(['qunit', 'squim.env', 'squim.types', 'squim.error'], function (Q, Env, Types, Error) {
+ define(['qunit', 'squim.types', 'squim.error'], function (Q, Types, Error) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
- return (root.SquimEnvTest = factory(Q, Env, Types, Error));
+ return (root.SquimEnvTest = factory(Q, Types, Error));
});
} else {
// Browser globals
- root.SquimEnvTest = factory(QUnit, SquimEnv, SquimTypes, SquimError);
+ root.SquimEnvTest = factory(QUnit, SquimTypes, SquimError);
}
-}(this, function (Q, Env, Types, Error) {
+}(this, function (Q, Types, Error) {
"use strict";
- var obj = {};
+ var obj = {}, Env = Types.Env;
function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
View
1  test/squim.test.js
@@ -5,7 +5,6 @@ require.config({
paths: {
qunit: "http://code.jquery.com/qunit/qunit-git",
"squim": "../src/squim",
- "squim.env": "../src/squim.env",
"squim.types": "../src/squim.types",
"squim.parser": "../src/squim.parser",
"squim.error": "../src/squim.error",
View
1  test/squim.types.test.js
@@ -37,6 +37,7 @@
check(new Types.Inert());
check(new Types.Ignore());
check(new Types.Str("asd"));
+ check(new Types.Env());
});
Q.module("Squim.Symbol");
Please sign in to comment.
Something went wrong with that request. Please try again.