diff --git a/build/scscript.js b/build/scscript.js index 338feca..296e2bd 100644 --- a/build/scscript.js +++ b/build/scscript.js @@ -1,7 +1,7 @@ (function(global) { "use strict"; -var sc = { VERSION: "0.0.21" }; +var sc = { VERSION: "0.0.22" }; // src/sc/sc.js (function(sc) { @@ -18,6 +18,16 @@ var sc = { VERSION: "0.0.21" }; installer(sc); }; + // istanbul ignore next + SCScript.stdout = function(msg) { + console.log(msg); + }; + + // istanbul ignore next + SCScript.stderr = function(msg) { + console.error(msg); + }; + SCScript.VERSION = sc.VERSION; global.SCScript = sc.SCScript = SCScript; @@ -3975,6 +3985,29 @@ var sc = { VERSION: "0.0.21" }; })(sc); +// src/sc/lang/io.js +(function(sc) { + + var io = {}; + + var SCScript = sc.SCScript; + var buffer = ""; + + io.post = function(msg) { + var items; + + items = (buffer + msg).split("\n"); + buffer = items.pop(); + + items.forEach(function(msg) { + SCScript.stdout(msg); + }); + }; + + sc.lang.io = io; + +})(sc); + // src/sc/lang/dollarSC.js (function(sc) { @@ -4233,6 +4266,7 @@ var sc = { VERSION: "0.0.21" }; newClass._Spec = constructor; constructor.prototype.__class = newClass; constructor.prototype.__Spec = constructor; + constructor.prototype.__className = className; metaClass._Spec = constructor; metaClass._isMetaClass = true; @@ -5009,10 +5043,27 @@ var sc = { VERSION: "0.0.21" }; }; // TODO: implements dump - // TODO: implements post - // TODO: implements postln - // TODO: implements postc - // TODO: implements postcln + + spec.post = function() { + this.asString().post(); + return this; + }; + + spec.postln = function() { + this.asString().postln(); + return this; + }; + + spec.postc = function() { + this.asString().postc(); + return this; + }; + + spec.postcln = function() { + this.asString().postcln(); + return this; + }; + // TODO: implements postcs // TODO: implements totalFree // TODO: implements largestFreeBlock @@ -9932,6 +9983,17 @@ var sc = { VERSION: "0.0.21" }; // TODO: implements writeInputSpec // TODO: implements case // TODO: implements makeEnvirValPairs + + spec.asString = function() { + var items = []; + this.do($SC.Function(function($elem) { + items.push($elem.__str__()); + })); + + return $SC.String( + this.__className + "[ " + items.join(", ") + " ]" + ); + }; }); })(sc); @@ -12288,6 +12350,12 @@ var sc = { VERSION: "0.0.21" }; spec.includes = function($item) { return $SC.Boolean(this._.indexOf($item) !== -1); }; + + spec.asString = function() { + return $SC.String("[ " + this._.map(function(elem) { + return elem.__str__(); + }).join(", ") + " ]"); + }; }); sc.lang.klass.refine("RawArray", function(spec, utils) { @@ -12307,6 +12375,7 @@ var sc = { VERSION: "0.0.21" }; (function(sc) { var fn = sc.lang.fn; + var io = sc.lang.io; var $SC = sc.lang.$SC; sc.lang.klass.refine("String", function(spec, utils) { @@ -12476,10 +12545,26 @@ var sc = { VERSION: "0.0.21" }; return $SC("String"); }; - // TODO: implements postln - // TODO: implements post - // TODO: implements postcln - // TODO: implements postc + spec.postln = function() { + io.post(this.__str__() + "\n"); + return this; + }; + + spec.post = function() { + io.post(this.__str__()); + return this; + }; + + spec.postcln = function() { + io.post("// " + this.__str__() + "\n"); + return this; + }; + + spec.postc = function() { + io.post("// " + this.__str__()); + return this; + }; + // TODO: implements postf // TODO: implements format // TODO: implements matchRegexp diff --git a/package.json b/package.json index 577f4ea..d621268 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scscript", - "version": "0.0.21", + "version": "0.0.22", "author": "Nao Yonamine ", "homepage": "http://mohayonao.github.io/SCScript/", "bugs": "https://github.com/mohayonao/SCScript/issues", diff --git a/src/sc/lang/classlib.js b/src/sc/lang/classlib.js index 54e7630..9543218 100644 --- a/src/sc/lang/classlib.js +++ b/src/sc/lang/classlib.js @@ -5,6 +5,7 @@ require("./klass"); require("./iterator"); require("./fn"); + require("./io"); require("../libs/random"); require("../libs/mathlib"); diff --git a/src/sc/lang/classlib/Collections/ArrayedCollection.js b/src/sc/lang/classlib/Collections/ArrayedCollection.js index 7dfd48b..4f22a56 100644 --- a/src/sc/lang/classlib/Collections/ArrayedCollection.js +++ b/src/sc/lang/classlib/Collections/ArrayedCollection.js @@ -754,6 +754,12 @@ spec.includes = function($item) { return $SC.Boolean(this._.indexOf($item) !== -1); }; + + spec.asString = function() { + return $SC.String("[ " + this._.map(function(elem) { + return elem.__str__(); + }).join(", ") + " ]"); + }; }); sc.lang.klass.refine("RawArray", function(spec, utils) { diff --git a/src/sc/lang/classlib/Collections/ArrayedCollection_test.js b/src/sc/lang/classlib/Collections/ArrayedCollection_test.js index efad39c..ef82c8d 100644 --- a/src/sc/lang/classlib/Collections/ArrayedCollection_test.js +++ b/src/sc/lang/classlib/Collections/ArrayedCollection_test.js @@ -1225,6 +1225,14 @@ }, ]); }); + it("#asString", function() { + var instance, test; + + instance = this.createInstance([ 1, 2, 3 ]); + + test = instance.asString(); + expect(test).to.be.a("SCString").that.equals("[ 1, 2, 3 ]"); + }); }); describe("SCRawArray", function() { diff --git a/src/sc/lang/classlib/Collections/Collection.js b/src/sc/lang/classlib/Collections/Collection.js index f29a200..b49a521 100644 --- a/src/sc/lang/classlib/Collections/Collection.js +++ b/src/sc/lang/classlib/Collections/Collection.js @@ -936,6 +936,17 @@ // TODO: implements writeInputSpec // TODO: implements case // TODO: implements makeEnvirValPairs + + spec.asString = function() { + var items = []; + this.do($SC.Function(function($elem) { + items.push($elem.__str__()); + })); + + return $SC.String( + this.__className + "[ " + items.join(", ") + " ]" + ); + }; }); })(sc); diff --git a/src/sc/lang/classlib/Collections/Collection_test.js b/src/sc/lang/classlib/Collections/Collection_test.js index 5ce0cd9..019fd91 100644 --- a/src/sc/lang/classlib/Collections/Collection_test.js +++ b/src/sc/lang/classlib/Collections/Collection_test.js @@ -1208,5 +1208,13 @@ }); it.skip("#makeEnvirValPairs", function() { }); + it("#asString", function() { + var instance, test; + + instance = this.createInstance([ 1, 2, 3 ]); + + test = instance.asString(); + expect(test).to.be.a("SCString").that.equals("Array[ 1, 2, 3 ]"); + }); }); })(); diff --git a/src/sc/lang/classlib/Collections/String.js b/src/sc/lang/classlib/Collections/String.js index 6a02f46..bdd9146 100644 --- a/src/sc/lang/classlib/Collections/String.js +++ b/src/sc/lang/classlib/Collections/String.js @@ -4,6 +4,7 @@ require("./ArrayedCollection"); var fn = sc.lang.fn; + var io = sc.lang.io; var $SC = sc.lang.$SC; sc.lang.klass.refine("String", function(spec, utils) { @@ -173,10 +174,26 @@ return $SC("String"); }; - // TODO: implements postln - // TODO: implements post - // TODO: implements postcln - // TODO: implements postc + spec.postln = function() { + io.post(this.__str__() + "\n"); + return this; + }; + + spec.post = function() { + io.post(this.__str__()); + return this; + }; + + spec.postcln = function() { + io.post("// " + this.__str__() + "\n"); + return this; + }; + + spec.postc = function() { + io.post("// " + this.__str__()); + return this; + }; + // TODO: implements postf // TODO: implements format // TODO: implements matchRegexp diff --git a/src/sc/lang/classlib/Collections/String_test.js b/src/sc/lang/classlib/Collections/String_test.js index 4c8b534..b4a692a 100644 --- a/src/sc/lang/classlib/Collections/String_test.js +++ b/src/sc/lang/classlib/Collections/String_test.js @@ -232,14 +232,50 @@ test = instance.species(); expect(test).to.equal(SCString); }); - it.skip("#postln", function() { - }); - it.skip("#post", function() { - }); - it.skip("#postcln", function() { - }); - it.skip("#postc", function() { - }); + it("#postln", sinon.test(function() { + var instance, test; + + this.stub(sc.lang.io, "post"); + + instance = this.createInstance("post"); + + test = instance.postln(); + expect(test).to.equal(instance); + expect(sc.lang.io.post).to.be.calledWith("post\n"); + })); + it("#post", sinon.test(function() { + var instance, test; + + this.stub(sc.lang.io, "post"); + + instance = this.createInstance("post"); + + test = instance.post(); + expect(test).to.equal(instance); + expect(sc.lang.io.post).to.be.calledWith("post"); + })); + it("#postcln", sinon.test(function() { + var instance, test; + + this.stub(sc.lang.io, "post"); + + instance = this.createInstance("post"); + + test = instance.postcln(); + expect(test).to.equal(instance); + expect(sc.lang.io.post).to.be.calledWith("// post\n"); + })); + it("#postc", sinon.test(function() { + var instance, test; + + this.stub(sc.lang.io, "post"); + + instance = this.createInstance("post"); + + test = instance.postc(); + expect(test).to.equal(instance); + expect(sc.lang.io.post).to.be.calledWith("// post"); + })); it.skip("#postf", function() { }); it.skip("#format", function() { diff --git a/src/sc/lang/classlib/Core/Kernel_test.js b/src/sc/lang/classlib/Core/Kernel_test.js index c95860d..6cc56f9 100644 --- a/src/sc/lang/classlib/Core/Kernel_test.js +++ b/src/sc/lang/classlib/Core/Kernel_test.js @@ -3,8 +3,50 @@ require("./Kernel"); - describe("class Kernel", function() { + var $SC = sc.lang.$SC; + + describe("SCKernel", function() { it.skip("write later", function() { }); }); + + describe("SCInterpreter", function() { + var SCInterpreter, $interpreter; + before(function() { + SCInterpreter = $SC("Interpreter"); + $interpreter = sc.lang.klass.$interpreter; + }); + it(".new", function() { + expect(function() { + SCInterpreter.new(); + }).to.throw("Interpreter.new is illegal."); + }); + it("#<>a..z / #clearAll", function() { + var instance; + + instance = $interpreter; + + "abcdefghijklmnopqrstuvwxyz".split("").forEach(function(ch) { + var test, $value; + + $value = sc.test.object(); + + test = instance[ch](); + expect(test).to.be.a("SCNil"); + + test = instance[ch + "_"]($value); + expect(test).to.equal(instance); + + test = instance[ch](); + expect(test).to.equal($value); + }); + + instance.clearAll(); + + "abcdefghijklmnopqrstuvwxyz".split("").forEach(function(ch) { + var test = instance[ch](); + expect(test).to.be.a("SCNil"); + }); + }); + }); })(); diff --git a/src/sc/lang/classlib/Core/Object.js b/src/sc/lang/classlib/Core/Object.js index 338ef50..2beaa60 100644 --- a/src/sc/lang/classlib/Core/Object.js +++ b/src/sc/lang/classlib/Core/Object.js @@ -43,10 +43,27 @@ }; // TODO: implements dump - // TODO: implements post - // TODO: implements postln - // TODO: implements postc - // TODO: implements postcln + + spec.post = function() { + this.asString().post(); + return this; + }; + + spec.postln = function() { + this.asString().postln(); + return this; + }; + + spec.postc = function() { + this.asString().postc(); + return this; + }; + + spec.postcln = function() { + this.asString().postcln(); + return this; + }; + // TODO: implements postcs // TODO: implements totalFree // TODO: implements largestFreeBlock diff --git a/src/sc/lang/classlib/Core/Object_test.js b/src/sc/lang/classlib/Core/Object_test.js index 14ed89f..98ed7f2 100644 --- a/src/sc/lang/classlib/Core/Object_test.js +++ b/src/sc/lang/classlib/Core/Object_test.js @@ -92,14 +92,66 @@ }); it.skip("#dump", function() { }); - it.skip("#post", function() { - }); - it.skip("#postln", function() { - }); - it.skip("#postc", function() { - }); - it.skip("#postcln", function() { - }); + it("#post", sinon.test(function() { + var instance, test; + var post; + + post = this.spy(); + + instance = this.createInstance(); + this.stub(instance, "asString", function() { + return { post: post }; + }); + + test = instance.post(); + expect(test).to.equal(instance); + expect(post).to.be.calledWith(undefined); + })); + it("#postln", sinon.test(function() { + var instance, test; + var postln; + + postln = this.spy(); + + instance = this.createInstance(); + this.stub(instance, "asString", function() { + return { postln: postln }; + }); + + test = instance.postln(); + expect(test).to.equal(instance); + expect(postln).to.be.calledWith(undefined); + })); + it("#postc", sinon.test(function() { + var instance, test; + var postc; + + postc = this.spy(); + + instance = this.createInstance(); + this.stub(instance, "asString", function() { + return { postc: postc }; + }); + + test = instance.postc(); + expect(test).to.equal(instance); + expect(postc).to.be.calledWith(undefined); + })); + it("#postcln", sinon.test(function() { + var instance, test; + var postcln; + + postcln = this.spy(); + + instance = this.createInstance(); + this.stub(instance, "asString", function() { + return { postcln: postcln }; + }); + + test = instance.postcln(); + expect(test).to.equal(instance); + expect(postcln).to.be.calledWith(undefined); + })); it.skip("#postcs", function() { }); it.skip("#totalFree", function() { diff --git a/src/sc/lang/io.js b/src/sc/lang/io.js new file mode 100644 index 0000000..fccf533 --- /dev/null +++ b/src/sc/lang/io.js @@ -0,0 +1,24 @@ +(function(sc) { + "use strict"; + + require("./sc"); + + var io = {}; + + var SCScript = sc.SCScript; + var buffer = ""; + + io.post = function(msg) { + var items; + + items = (buffer + msg).split("\n"); + buffer = items.pop(); + + items.forEach(function(msg) { + SCScript.stdout(msg); + }); + }; + + sc.lang.io = io; + +})(sc); diff --git a/src/sc/lang/io_test.js b/src/sc/lang/io_test.js new file mode 100644 index 0000000..21690e6 --- /dev/null +++ b/src/sc/lang/io_test.js @@ -0,0 +1,26 @@ +(function() { + "use strict"; + + require("./io"); + + var io = sc.lang.io; + + describe("sc.lang.io", function() { + it("post", sinon.test(function() { + this.stub(sc.SCScript, "stdout"); + + io.post("abc"); + expect(sc.SCScript.stdout).to.be.not.called; + sc.SCScript.stdout.reset(); + + io.post("d\n"); + expect(sc.SCScript.stdout).to.be.calledWith("abcd"); + sc.SCScript.stdout.reset(); + + io.post("abc\ndef\n"); + expect(sc.SCScript.stdout).to.be.calledWith("abc"); + expect(sc.SCScript.stdout).to.be.calledWith("def"); + sc.SCScript.stdout.reset(); + })); + }); +})(); diff --git a/src/sc/lang/klass/klass.js b/src/sc/lang/klass/klass.js index e4811b4..4fd0af8 100644 --- a/src/sc/lang/klass/klass.js +++ b/src/sc/lang/klass/klass.js @@ -117,6 +117,7 @@ newClass._Spec = constructor; constructor.prototype.__class = newClass; constructor.prototype.__Spec = constructor; + constructor.prototype.__className = className; metaClass._Spec = constructor; metaClass._isMetaClass = true; diff --git a/src/sc/sc.js b/src/sc/sc.js index c163fbf..a9757bd 100644 --- a/src/sc/sc.js +++ b/src/sc/sc.js @@ -13,6 +13,16 @@ installer(sc); }; + // istanbul ignore next + SCScript.stdout = function(msg) { + console.log(msg); + }; + + // istanbul ignore next + SCScript.stderr = function(msg) { + console.error(msg); + }; + SCScript.VERSION = sc.VERSION; global.SCScript = sc.SCScript = SCScript;