diff --git a/js-assembler/get-runtime.rkt b/js-assembler/get-runtime.rkt index a60412e..698a2a6 100644 --- a/js-assembler/get-runtime.rkt +++ b/js-assembler/get-runtime.rkt @@ -37,6 +37,8 @@ (define-runtime-path baselib.js "runtime-src/baselib.js") (define-runtime-path baselib_unionfind.js "runtime-src/baselib_unionfind.js") (define-runtime-path baselib_equality.js "runtime-src/baselib_equality.js") +(define-runtime-path baselib_lists.js "runtime-src/baselib_lists.js") +(define-runtime-path baselib_vectors.js "runtime-src/baselib_vectors.js") (define-runtime-path baselib_hash.js "runtime-src/baselib_hash.js") (define-runtime-path baselib_symbol.js "runtime-src/baselib_symbol.js") (define-runtime-path baselib_structs.js "runtime-src/baselib_structs.js") @@ -79,16 +81,20 @@ baselib.js baselib_unionfind.js baselib_equality.js - baselib_hash.js baselib_format.js + + baselib_lists.js + baselib_vectors.js + baselib_chars.js baselib_symbol.js + baselib_strings.js + baselib_bytes.js + baselib_hash.js + baselib_structs.js baselib_arity.js baselib_inspectors.js baselib_exceptions.js - baselib_chars.js - baselib_strings.js - baselib_bytes.js baselib_readergraph.js link.js diff --git a/js-assembler/runtime-src/baselib_arity.js b/js-assembler/runtime-src/baselib_arity.js index 2d16b2a..d528aab 100644 --- a/js-assembler/runtime-src/baselib_arity.js +++ b/js-assembler/runtime-src/baselib_arity.js @@ -35,7 +35,7 @@ } else if (isArityAtLeast(arity)) { return n >= arityAtLeastValue(arity); } else { - while (arity !== plt.types.EMPTY) { + while (arity !== plt.baselib.lists.EMPTY) { if (typeof(arity.first) === 'number') { if (arity.first === n) { return true; } } else if (isArityAtLeast(arity)) { diff --git a/js-assembler/runtime-src/baselib_lists.js b/js-assembler/runtime-src/baselib_lists.js new file mode 100644 index 0000000..2b4ba37 --- /dev/null +++ b/js-assembler/runtime-src/baselib_lists.js @@ -0,0 +1,159 @@ +// list structures (pairs, empty) +(function(baselib) { + var exports = {}; + baselib.lists = exports; + + + + + + Empty = function() { + }; + Empty.EMPTY = new Empty(); + + + Empty.prototype.equals = function(other, aUnionFind) { + return other instanceof Empty; + }; + + Empty.prototype.reverse = function() { + return this; + }; + + Empty.prototype.toWrittenString = function(cache) { return "empty"; }; + Empty.prototype.toDisplayedString = function(cache) { return "empty"; }; + Empty.prototype.toString = function(cache) { return "()"; }; + + + // Empty.append: (listof X) -> (listof X) + Empty.prototype.append = function(b){ + return b; + }; + + + + + ////////////////////////////////////////////////////////////////////// + + // Cons Pairs + + var Cons = function(f, r) { + this.first = f; + this.rest = r; + }; + + Cons.prototype.reverse = function() { + var lst = this; + var ret = Empty.EMPTY; + while (!isEmpty(lst)){ + ret = Cons.makeInstance(lst.first, ret); + lst = lst.rest; + } + return ret; + }; + + Cons.makeInstance = function(f, r) { + return new Cons(f, r); + }; + + // FIXME: can we reduce the recursion on this? + Cons.prototype.equals = function(other, aUnionFind) { + if (! (other instanceof Cons)) { + return false; + } + return (plt.baselib.equality.equals(this.first, other.first, aUnionFind) && + plt.baselib.equality.equals(this.rest, other.rest, aUnionFind)); + }; + + + + + // Cons.append: (listof X) -> (listof X) + Cons.prototype.append = function(b){ + if (b === Empty.EMPTY) + return this; + var ret = b; + var lst = this.reverse(); + while ( !isEmpty(lst) ) { + ret = Cons.makeInstance(lst.first, ret); + lst = lst.rest; + } + + return ret; + }; + + + Cons.prototype.toWrittenString = function(cache) { + cache.put(this, true); + var texts = []; + var p = this; + while ( p instanceof Cons ) { + texts.push(plt.baselib.format.toWrittenString(p.first, cache)); + p = p.rest; + if (typeof(p) === 'object' && cache.containsKey(p)) { + break; + } + } + if ( p !== Empty.EMPTY ) { + texts.push('.'); + texts.push(plt.baselib.format.toWrittenString(p, cache)); + } + return "(" + texts.join(" ") + ")"; + }; + + Cons.prototype.toString = Cons.prototype.toWrittenString; + + Cons.prototype.toDisplayedString = function(cache) { + cache.put(this, true); + var texts = []; + var p = this; + while ( p instanceof Cons ) { + texts.push(plt.baselib.format.toDisplayedString(p.first, cache)); + p = p.rest; + if (typeof(p) === 'object' && cache.containsKey(p)) { + break; + } + } + if ( p !== Empty.EMPTY ) { + texts.push('.'); + texts.push(plt.baselib.format.toDisplayedString(p, cache)); + } + return "(" + texts.join(" ") + ")"; + }; + + + + Cons.prototype.toDomNode = function(cache) { + cache.put(this, true); + var node = document.createElement("span"); + node.appendChild(document.createTextNode("(")); + var p = this; + while ( p instanceof Cons ) { + appendChild(node, plt.baselib.format.toDomNode(p.first, cache)); + p = p.rest; + if ( p !== Empty.EMPTY ) { + appendChild(node, document.createTextNode(" ")); + } + if (typeof(p) === 'object' && cache.containsKey(p)) { + break; + } + } + if ( p !== Empty.EMPTY ) { + appendChild(node, document.createTextNode(".")); + appendChild(node, document.createTextNode(" ")); + appendChild(node, plt.baselib.format.toDomNode(p, cache)); + } + + node.appendChild(document.createTextNode(")")); + return node; + }; + + + + exports.EMPTY = Empty.EMPTY; + exports.Empty = Empty; + exports.Cons = Cons; + + + +})(this['plt'].baselib); \ No newline at end of file diff --git a/js-assembler/runtime-src/baselib_vectors.js b/js-assembler/runtime-src/baselib_vectors.js new file mode 100644 index 0000000..4cb5a00 --- /dev/null +++ b/js-assembler/runtime-src/baselib_vectors.js @@ -0,0 +1,99 @@ +// vectors +(function(baselib) { + var exports = {}; + baselib.vectors = exports; + + + + Vector = function(n, initialElements) { + this.elts = new Array(n); + if (initialElements) { + for (var i = 0; i < n; i++) { + this.elts[i] = initialElements[i]; + } + } else { + for (var i = 0; i < n; i++) { + this.elts[i] = undefined; + } + } + this.mutable = true; + }; + + Vector.makeInstance = function(n, elts) { + return new Vector(n, elts); + } + + Vector.prototype.length = function() { + return this.elts.length; + }; + + Vector.prototype.ref = function(k) { + return this.elts[k]; + }; + + Vector.prototype.set = function(k, v) { + this.elts[k] = v; + }; + + Vector.prototype.equals = function(other, aUnionFind) { + if (other != null && other != undefined && other instanceof Vector) { + if (other.length() != this.length()) { + return false + } + for (var i = 0; i < this.length(); i++) { + if (! plt.baselib.equality.equals(this.elts[i], other.elts[i], aUnionFind)) { + return false; + } + } + return true; + } else { + return false; + } + }; + + Vector.prototype.toList = function() { + var ret = plt.baselib.lists.EMPTY; + for (var i = this.length() - 1; i >= 0; i--) { + ret = plt.baselib.lists.Cons.makeInstance(this.elts[i], ret); + } + return ret; + }; + + Vector.prototype.toWrittenString = function(cache) { + cache.put(this, true); + var texts = []; + for (var i = 0; i < this.length(); i++) { + texts.push(plt.baselib.format.toWrittenString(this.ref(i), cache)); + } + return "#(" + texts.join(" ") + ")"; + }; + + Vector.prototype.toDisplayedString = function(cache) { + cache.put(this, true); + var texts = []; + for (var i = 0; i < this.length(); i++) { + texts.push(plt.baselib.format.toDisplayedString(this.ref(i), cache)); + } + return "#(" + texts.join(" ") + ")"; + }; + + Vector.prototype.toDomNode = function(cache) { + cache.put(this, true); + var node = document.createElement("span"); + node.appendChild(document.createTextNode("#(")); + for (var i = 0; i < this.length(); i++) { + appendChild(node, + plt.baselib.format.toDomNode(this.ref(i), cache)); + if (i !== this.length()-1) { + appendChild(node, document.createTextNode(" ")); + } + } + node.appendChild(document.createTextNode(")")); + return node; + }; + + + + exports.Vector = Vector; + +})(this['plt'].baselib); \ No newline at end of file diff --git a/js-assembler/runtime-src/types.js b/js-assembler/runtime-src/types.js index 8018e44..518c81c 100644 --- a/js-assembler/runtime-src/types.js +++ b/js-assembler/runtime-src/types.js @@ -39,6 +39,8 @@ if (! this['plt']) { this['plt'] = {}; } var Symbol = plt.baselib.Symbol; + var Empty = plt.baselib.lists.Empty; + var Cons = plt.baselib.lists.Cons; ////////////////////////////////////////////////////////////////////// @@ -238,150 +240,6 @@ if (! this['plt']) { this['plt'] = {}; } - - Empty = function() { - }; - Empty.EMPTY = new Empty(); - - - Empty.prototype.equals = function(other, aUnionFind) { - return other instanceof Empty; - }; - - Empty.prototype.reverse = function() { - return this; - }; - - Empty.prototype.toWrittenString = function(cache) { return "empty"; }; - Empty.prototype.toDisplayedString = function(cache) { return "empty"; }; - Empty.prototype.toString = function(cache) { return "()"; }; - - - - // Empty.append: (listof X) -> (listof X) - Empty.prototype.append = function(b){ - return b; - }; - - - - - ////////////////////////////////////////////////////////////////////// - - // Cons Pairs - - var Cons = function(f, r) { - this.first = f; - this.rest = r; - }; - - Cons.prototype.reverse = function() { - var lst = this; - var ret = Empty.EMPTY; - while (!isEmpty(lst)){ - ret = Cons.makeInstance(lst.first, ret); - lst = lst.rest; - } - return ret; - }; - - Cons.makeInstance = function(f, r) { - return new Cons(f, r); - }; - - // FIXME: can we reduce the recursion on this? - Cons.prototype.equals = function(other, aUnionFind) { - if (! (other instanceof Cons)) { - return false; - } - return (equals(this.first, other.first, aUnionFind) && - equals(this.rest, other.rest, aUnionFind)); - }; - - - - - // Cons.append: (listof X) -> (listof X) - Cons.prototype.append = function(b){ - if (b === Empty.EMPTY) - return this; - var ret = b; - var lst = this.reverse(); - while ( !isEmpty(lst) ) { - ret = Cons.makeInstance(lst.first, ret); - lst = lst.rest; - } - - return ret; - }; - - - Cons.prototype.toWrittenString = function(cache) { - cache.put(this, true); - var texts = []; - var p = this; - while ( p instanceof Cons ) { - texts.push(toWrittenString(p.first, cache)); - p = p.rest; - if (typeof(p) === 'object' && cache.containsKey(p)) { - break; - } - } - if ( p !== Empty.EMPTY ) { - texts.push('.'); - texts.push(toWrittenString(p, cache)); - } - return "(" + texts.join(" ") + ")"; - }; - - Cons.prototype.toString = Cons.prototype.toWrittenString; - - Cons.prototype.toDisplayedString = function(cache) { - cache.put(this, true); - var texts = []; - var p = this; - while ( p instanceof Cons ) { - texts.push(toDisplayedString(p.first, cache)); - p = p.rest; - if (typeof(p) === 'object' && cache.containsKey(p)) { - break; - } - } - if ( p !== Empty.EMPTY ) { - texts.push('.'); - texts.push(toDisplayedString(p, cache)); - } - return "(" + texts.join(" ") + ")"; - }; - - - - Cons.prototype.toDomNode = function(cache) { - cache.put(this, true); - var node = document.createElement("span"); - node.appendChild(document.createTextNode("(")); - var p = this; - while ( p instanceof Cons ) { - appendChild(node, toDomNode(p.first, cache)); - p = p.rest; - if ( p !== Empty.EMPTY ) { - appendChild(node, document.createTextNode(" ")); - } - if (typeof(p) === 'object' && cache.containsKey(p)) { - break; - } - } - if ( p !== Empty.EMPTY ) { - appendChild(node, document.createTextNode(".")); - appendChild(node, document.createTextNode(" ")); - appendChild(node, toDomNode(p, cache)); - } - - node.appendChild(document.createTextNode(")")); - return node; - }; - - // isList: Any -> Boolean // Returns true if x is a list (a chain of pairs terminated by EMPTY). var isList = function(x) { @@ -400,89 +258,6 @@ if (! this['plt']) { this['plt'] = {}; } ////////////////////////////////////////////////////////////////////// - Vector = function(n, initialElements) { - this.elts = new Array(n); - if (initialElements) { - for (var i = 0; i < n; i++) { - this.elts[i] = initialElements[i]; - } - } else { - for (var i = 0; i < n; i++) { - this.elts[i] = undefined; - } - } - this.mutable = true; - }; - Vector.makeInstance = function(n, elts) { - return new Vector(n, elts); - } - Vector.prototype.length = function() { - return this.elts.length; - }; - Vector.prototype.ref = function(k) { - return this.elts[k]; - }; - Vector.prototype.set = function(k, v) { - this.elts[k] = v; - }; - - Vector.prototype.equals = function(other, aUnionFind) { - if (other != null && other != undefined && other instanceof Vector) { - if (other.length() != this.length()) { - return false - } - for (var i = 0; i < this.length(); i++) { - if (! equals(this.elts[i], other.elts[i], aUnionFind)) { - return false; - } - } - return true; - } else { - return false; - } - }; - - Vector.prototype.toList = function() { - var ret = Empty.EMPTY; - for (var i = this.length() - 1; i >= 0; i--) { - ret = Cons.makeInstance(this.elts[i], ret); - } - return ret; - }; - - Vector.prototype.toWrittenString = function(cache) { - cache.put(this, true); - var texts = []; - for (var i = 0; i < this.length(); i++) { - texts.push(toWrittenString(this.ref(i), cache)); - } - return "#(" + texts.join(" ") + ")"; - }; - - Vector.prototype.toDisplayedString = function(cache) { - cache.put(this, true); - var texts = []; - for (var i = 0; i < this.length(); i++) { - texts.push(toDisplayedString(this.ref(i), cache)); - } - return "#(" + texts.join(" ") + ")"; - }; - - Vector.prototype.toDomNode = function(cache) { - cache.put(this, true); - var node = document.createElement("span"); - node.appendChild(document.createTextNode("#(")); - for (var i = 0; i < this.length(); i++) { - appendChild(node, - toDomNode(this.ref(i), cache)); - if (i !== this.length()-1) { - appendChild(node, document.createTextNode(" ")); - } - } - node.appendChild(document.createTextNode(")")); - return node; - }; - ////////////////////////////////////////////////////////////////////// @@ -1003,7 +778,7 @@ if (! this['plt']) { this['plt'] = {}; } types.bytes = function(x, mutable) { return new plt.baselib.bytes.Bytes(x, mutable); }; types.bytesImmutable = function(x) { return new plt.baselib.bytes.Bytes(x, false); }; types.keyword = function(k) { return new Keyword(k); }; - types.pair = function(x, y) { return Cons.makeInstance(x, y); }; + types.pair = function(x, y) { return plt.baselib.lists.Cons.makeInstance(x, y); }; types.hash = makeHashEqual; types.hashEq = makeHashEq; // types.jsValue = function(name, val) { return new JsValue(name, val); };