Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: marianoguerra/squim
base: 6f9f50b328
...
head fork: marianoguerra/squim
compare: f6fa96b428
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
6 src/squim.js
@@ -40,7 +40,11 @@
env = new obj.types.Env({}, [obj.types.Env.makeGround()]);
}
- exp = obj.parse(code);
+ if (typeof code === "string") {
+ exp = obj.parse(code);
+ } else {
+ exp = code;
+ }
function onResult(value) {
result = value;
View
118 src/squim.types.js
@@ -96,6 +96,10 @@
return this.value;
};
+ Symbol.prototype.toJs = function () {
+ return "/" + this.value + "/";
+ };
+
Symbol.prototype.eval_ = function (cc) {
var result = cc.env.get(this.value);
@@ -138,7 +142,7 @@
};
Str.prototype.toJs = function () {
- return JSON.stringify(this.value);
+ return this.value;
};
Str.prototype.eq_p = function (obj) {
@@ -357,6 +361,69 @@
Pair.nil = new Pair.Nil();
+ function Obj(attrs) {
+ this.attrs = attrs;
+ }
+
+ Obj.prototype = new Type(null);
+
+ Obj.prototype.eq_p = function (obj) {
+ return this === obj;
+ };
+
+ Obj.prototype.equal_p = Obj.prototype.eq_p;
+
+ Obj.prototype.toJs = function () {
+ var attr, val, key, attrs = {};
+
+ for (key in this.attrs) {
+ attr = this.attrs[key];
+
+ if (attr instanceof Pair) {
+ val = attr.toJs();
+ } else if (attr instanceof Pair.Nil) {
+ val = [];
+ } else if (attr instanceof Obj) {
+ val = attr.toJs();
+ } else {
+ val = attr.value;
+ }
+
+ attrs[key] = val;
+ }
+
+ return attrs;
+ };
+
+ Obj.prototype.toString = function () {
+ var key, attr, attrs = {};
+
+ for (key in this.attrs) {
+ attr = this.attrs[key];
+ if (attr instanceof Str) {
+ attrs[key] = attr.value;
+ } else {
+ attrs[key] = attr.toJs();
+ }
+ }
+
+ return JSON.stringify(attrs);
+ };
+
+ // convert js object *obj* to a Obj
+ // return a new Obj with *attrs* as own attrs
+ Obj.fromJsObject = function (attrs) {
+ var key, squimattrs = {};
+
+ for (key in attrs) {
+ if (attrs.hasOwnProperty(key)) {
+ squimattrs[key] = obj.squimify(attrs[key]);
+ }
+ }
+
+ return new Obj(squimattrs);
+ };
+
function Env(bindings, parents, inmutable) {
if (bindings === undefined) {
@@ -406,19 +473,42 @@
Env.prototype.equal_p = Env.prototype.eq_p;
- Env.prototype.toJs = function () {
- var i, parents = [];
+ // if shallow is true then don't convert parents toJs
+ Env.prototype.toJs = function (shallow) {
+ var i, parents = [], key, bindings = {};
- for (i = 0; i < this.parents.length; i += 1) {
- parents.push(this.parents[i].toJs());
+ for (key in this.bindings) {
+ bindings[key] = this.bindings[key].toJs();
+ }
+
+ if (!shallow) {
+ for (i = 0; i < this.parents.length; i += 1) {
+ parents.push(this.parents[i].toJs());
+ }
}
return {
- "bindings": this.bindings,
+ "bindings": bindings,
"parents": parents
};
};
+ // convert js object *bindings* to a js object that contains all it's
+ // attributes as squim objects to be used as a bindings object for an environment
+ // return a new Env with *bindings* as bindings of the environment
+ Env.fromJsObject = function (bindings, parents) {
+ var key, squimBindings = {};
+
+ for (key in bindings) {
+ if (bindings.hasOwnProperty(key)) {
+ squimBindings[key] = obj.squimify(bindings[key]);
+ }
+ }
+
+ return new Env(bindings, parent);
+ };
+
+
function Applicative(operative) {
this.operative = operative;
}
@@ -520,7 +610,7 @@
}
if (Util.isArray(names)) {
- iparams = params = obj.util.arrayToPair(names);
+ iparams = params = obj.util.arrayToPair(names, true);
} else if (names instanceof Symbol) {
// NOTE if names is a symbol then bind all args to that symbol
// and return
@@ -617,19 +707,26 @@
}
} else if (item === null) {
return obj.nil;
+ } else if (item instanceof Object) {
+ return Obj.fromJsObject(item);
} else {
throw "unknown type for item: " + item;
}
};
- obj.util.arrayToPair = function (items) {
+ obj.util.arrayToPair = function (items, shallow) {
if (items.length === 0) {
return Pair.nil;
}
- var first = obj.squimify(items[0]);
+ var first;
+ if (shallow) {
+ first = items[0];
+ } else {
+ first = obj.squimify(items[0]);
+ }
- return new Pair(first, obj.util.arrayToPair(items.slice(1)));
+ return new Pair(first, obj.util.arrayToPair(items.slice(1), shallow));
};
obj.util.pairToArray = function (pair, checkItem) {
@@ -675,6 +772,7 @@
obj.Type = Type;
+ obj.Obj = Obj;
obj.Str = Str;
obj.Int = Int;
obj.Bool = Bool;
View
11 test/squim.modules.test.js
@@ -19,7 +19,14 @@
var obj = {}, Types = Squim.types;
function check(expr, expected, compareAsIs, env) {
- var result = Squim.run(expr, env), value;
+ var result, value;
+
+ try {
+ result = Squim.run(expr, env);
+ } catch (error) {
+ console.log(error);
+ throw error;
+ }
if (!compareAsIs) {
value = result.value;
@@ -425,7 +432,7 @@
Q.test("$vau works", function () {
var result = Squim.run('(($vau (a) #ignore ($define! one 1) ($define! two 2) (list one two a)) foo)');
- Q.deepEqual(result.toJs(), [1, 2, 'foo']);
+ Q.deepEqual(result.toJs(), [1, 2, '/foo/']);
result = Squim.run('((wrap ($vau x #ignore x)) 1 2 3)');
Q.deepEqual(result.toJs(), [1, 2, 3]);
View
8 test/squim.parser.test.js
@@ -134,13 +134,13 @@
Q.deepEqual(exp.toJs(), expectedValue);
}
- check("(+ 1 2)", ["+", 1, 2]);
- check("(+ 1 2.3)", ["+", 1, 2.3]);
+ check("(+ 1 2)", ["/+/", 1, 2]);
+ check("(+ 1 2.3)", ["/+/", 1, 2.3]);
check('(display 1 2.3 () "asd" foo)',
- ["display", 1, 2.3, [], "\"asd\"", "foo"]);
+ ["/display/", 1, 2.3, [], "asd", "/foo/"]);
check('(display 1 2.3 (/ 2 3) "asd" foo)',
- ["display", 1, 2.3, ["/", 2, 3], "\"asd\"", "foo"]);
+ ["/display/", 1, 2.3, ["///", 2, 3], "asd", "/foo/"]);
});
Q.test("representation is the same as the parsed input", function () {
View
28 test/squim.types.test.js
@@ -38,6 +38,7 @@
check(new Types.Ignore());
check(new Types.Str("asd"));
check(new Types.Env());
+ check(new Types.Obj({}));
check(new Types.Cc());
});
@@ -83,6 +84,30 @@
Q.equal(bound.right, Types.nil);
});
+ Q.test("Obj", function () {
+ var jsobj = {name: "mariano", age: 27, tags: ["foo", "bar"], foo: {bar: 1, baz: "asd"}},
+ obj = Types.Obj.fromJsObject(jsobj);
+
+ Q.equal(obj.attrs.name.value, jsobj.name);
+ Q.equal(obj.attrs.age.value, jsobj.age);
+ Q.equal(obj.attrs.tags.left.value, "foo");
+ Q.equal(obj.attrs.tags.right.left.value, "bar");
+ Q.equal(obj.attrs.foo.attrs.bar.value, jsobj.foo.bar);
+ Q.equal(obj.attrs.foo.attrs.baz.value, jsobj.foo.baz);
+
+ Q.ok(obj.attrs.name instanceof Types.Str);
+ Q.ok(obj.attrs.age instanceof Types.Int);
+ Q.ok(obj.attrs.tags instanceof Types.Pair);
+ Q.ok(obj.attrs.tags.left instanceof Types.Str);
+ Q.ok(obj.attrs.tags.right.left instanceof Types.Str);
+ Q.ok(obj.attrs.foo instanceof Types.Obj);
+ Q.ok(obj.attrs.foo.attrs.bar instanceof Types.Int);
+ Q.ok(obj.attrs.foo.attrs.baz instanceof Types.Str);
+
+ Q.deepEqual(obj.toJs(), jsobj);
+
+ });
+
Q.test("squimify", function () {
function check(value, expected) {
Q.equal(Types.squimify(value).toString(), expected);
@@ -106,6 +131,9 @@
check([1, 1.2, false, true], "(1 1.2 #f #t)");
check([1, 1.2, false, true, "asd"], '(1 1.2 #f #t "asd")');
check([1, 1.2, [false, true, "asd"], []], '(1 1.2 (#f #t "asd") ())');
+ check({name: "mariano"}, '{"name":"mariano"}');
+ // this assumes attrs will be serialized in the same order
+ check({name: "mariano", age: 27}, '{"name":"mariano","age":27}');
check(new Types.Int(1), "1");
check(new Types.Float(1.2), "1.2");

No commit comments for this range

Something went wrong with that request. Please try again.