UNPKG

cosmicos

Version:
1,982 lines 61.6 kB
(function ($hx_exports) { "use strict"; $hx_exports.cosmicos = $hx_exports.cosmicos || {}; var EReg = function(r,opt) { opt = opt.split("u").join(""); this.r = new RegExp(r,opt); }; EReg.__name__ = true; EReg.prototype = { match: function(s) { if(this.r.global) this.r.lastIndex = 0; this.r.m = this.r.exec(s); this.r.s = s; return this.r.m != null; } ,__class__: EReg }; var HxOverrides = function() { }; HxOverrides.__name__ = true; HxOverrides.cca = function(s,index) { var x = s.charCodeAt(index); if(x != x) return undefined; return x; }; HxOverrides.substr = function(s,pos,len) { if(pos != null && pos != 0 && len != null && len < 0) return ""; if(len == null) len = s.length; if(pos < 0) { pos = s.length + pos; if(pos < 0) pos = 0; } else if(len < 0) len = s.length + len - pos; return s.substr(pos,len); }; HxOverrides.iter = function(a) { return { cur : 0, arr : a, hasNext : function() { return this.cur < this.arr.length; }, next : function() { return this.arr[this.cur++]; }}; }; var IMap = function() { }; IMap.__name__ = true; Math.__name__ = true; var Std = function() { }; Std.__name__ = true; Std.string = function(s) { return js.Boot.__string_rec(s,""); }; Std["int"] = function(x) { return x | 0; }; Std.parseInt = function(x) { var v = parseInt(x,10); if(v == 0 && (HxOverrides.cca(x,1) == 120 || HxOverrides.cca(x,1) == 88)) v = parseInt(x); if(isNaN(v)) return null; return v; }; var StringBuf = function() { this.b = ""; }; StringBuf.__name__ = true; StringBuf.prototype = { add: function(x) { this.b += Std.string(x); } ,__class__: StringBuf }; var StringTools = function() { }; StringTools.__name__ = true; StringTools.hex = function(n,digits) { var s = ""; var hexChars = "0123456789ABCDEF"; do { s = hexChars.charAt(n & 15) + s; n >>>= 4; } while(n > 0); if(digits != null) while(s.length < digits) s = "0" + s; return s; }; var cosmicos = {}; cosmicos.BigInteger = function() { if(cosmicos.BigInteger.BI_RC == null || cosmicos.BigInteger.BI_RC.length == 0) cosmicos.BigInteger.initBiRc(); if(cosmicos.BigInteger.BI_RM.length == 0) throw "BI_RM not initialized"; this.chunks = new Array(); var _g = cosmicos.BigInteger.defaultAm; switch(_g) { case 1: this.am = $bind(this,this.am1); break; case 2: this.am = $bind(this,this.am2); break; case 3: this.am = $bind(this,this.am3); break; default: throw "am error"; null; } }; cosmicos.BigInteger.__name__ = true; cosmicos.BigInteger.initBiRc = function() { cosmicos.BigInteger.BI_RC = new Array(); var rr = HxOverrides.cca("0",0); var _g = 0; while(_g < 10) { var vv = _g++; cosmicos.BigInteger.BI_RC[rr] = vv; rr++; } rr = HxOverrides.cca("a",0); var _g1 = 10; while(_g1 < 37) { var vv1 = _g1++; cosmicos.BigInteger.BI_RC[rr] = vv1; rr++; } rr = HxOverrides.cca("A",0); var _g2 = 10; while(_g2 < 37) { var vv2 = _g2++; cosmicos.BigInteger.BI_RC[rr] = vv2; rr++; } }; cosmicos.BigInteger.get_ZERO = function() { return cosmicos.BigInteger.nbv(0); }; cosmicos.BigInteger.get_ONE = function() { return cosmicos.BigInteger.nbv(1); }; cosmicos.BigInteger.nbv = function(i) { var r = cosmicos.BigInteger.nbi(); r.fromInt(i); return r; }; cosmicos.BigInteger.nbi = function() { return new cosmicos.BigInteger(); }; cosmicos.BigInteger.ofString = function(s,base) { var me = cosmicos.BigInteger.nbi(); var fromStringExt = function(s1,b) { me.fromInt(0); var cs = Math.floor(0.6931471805599453 * cosmicos.BigInteger.DB / Math.log(b)); var d = Std["int"](Math.pow(b,cs)); var mi = false; var j = 0; var w = 0; var _g1 = 0; var _g = s1.length; while(_g1 < _g) { var i = _g1++; var x = cosmicos.BigInteger.intAt(s1,i); if(x < 0) { if(s1.charAt(i) == "-" && me.sign == 0) mi = true; continue; } w = b * w + x; if(++j >= cs) { me.dMultiply(d); me.dAddOffset(w,0); j = 0; w = 0; } } if(j > 0) { me.dMultiply(Std["int"](Math.pow(b,j))); me.dAddOffset(w,0); } if(mi) cosmicos.BigInteger.get_ZERO().subTo(me,me); return me; }; var k; if(base == 16) k = 4; else if(base == 10) return fromStringExt(s,base); else if(base == 256) k = 8; else if(base == 8) k = 3; else if(base == 2) k = 1; else if(base == 32) k = 5; else if(base == 4) k = 2; else return fromStringExt(s,base); me.t = 0; me.sign = 0; var i1 = s.length; var mi1 = false; var sh = 0; while(--i1 >= 0) { var x1; if(k == 8) x1 = HxOverrides.cca(s,i1) & 255; else x1 = cosmicos.BigInteger.intAt(s,i1); if(x1 < 0) { if(s.charAt(i1) == "-") mi1 = true; continue; } mi1 = false; if(sh == 0) { me.chunks[me.t] = x1; me.t++; } else if(sh + k > cosmicos.BigInteger.DB) { me.chunks[me.t - 1] |= (x1 & (1 << cosmicos.BigInteger.DB - sh) - 1) << sh; me.chunks[me.t] = x1 >> cosmicos.BigInteger.DB - sh; me.t++; } else me.chunks[me.t - 1] |= x1 << sh; sh += k; if(sh >= cosmicos.BigInteger.DB) sh -= cosmicos.BigInteger.DB; } if(k == 8 && (HxOverrides.cca(s,0) & 128) != 0) { me.sign = -1; if(sh > 0) me.chunks[me.t - 1] |= (1 << cosmicos.BigInteger.DB - sh) - 1 << sh; } me.clamp(); if(mi1) cosmicos.BigInteger.get_ZERO().subTo(me,me); return me; }; cosmicos.BigInteger.ofInt = function(x) { var i = cosmicos.BigInteger.nbi(); i.fromInt(x); return i; }; cosmicos.BigInteger.ofInt32 = function(x) { var i = cosmicos.BigInteger.nbi(); i.fromInt32(x); return i; }; cosmicos.BigInteger.ofBytes = function(r,unsigned,pos,len) { if(pos == null) pos = 0; if(len == null) len = r.length - pos; if(len == 0) return cosmicos.BigInteger.get_ZERO(); var bi = cosmicos.BigInteger.nbi(); bi.sign = 0; bi.t = 0; var i = pos + len; var sh = 0; while(--i >= pos) { var x; if(i < len) x = r.b[i] & 255; else x = 0; if(sh == 0) { bi.chunks[bi.t] = x; bi.t++; } else if(sh + 8 > cosmicos.BigInteger.DB) { bi.chunks[bi.t - 1] |= (x & (1 << cosmicos.BigInteger.DB - sh) - 1) << sh; bi.chunks[bi.t] = x >> cosmicos.BigInteger.DB - sh; bi.t++; } else bi.chunks[bi.t - 1] |= x << sh; sh += 8; if(sh >= cosmicos.BigInteger.DB) sh -= cosmicos.BigInteger.DB; } if(!unsigned && (r.b[0] & 128) != 0) { bi.sign = -1; if(sh > 0) bi.chunks[bi.t - 1] |= (1 << cosmicos.BigInteger.DB - sh) - 1 << sh; } bi.clamp(); return bi; }; cosmicos.BigInteger.op_and = function(x,y) { return x & y; }; cosmicos.BigInteger.op_or = function(x,y) { return x | y; }; cosmicos.BigInteger.op_xor = function(x,y) { return x ^ y; }; cosmicos.BigInteger.op_andnot = function(x,y) { return x & ~y; }; cosmicos.BigInteger.nbits = function(x) { var r = 1; var t; if((t = x >>> 16) != 0) { x = t; r += 16; } if((t = x >> 8) != 0) { x = t; r += 8; } if((t = x >> 4) != 0) { x = t; r += 4; } if((t = x >> 2) != 0) { x = t; r += 2; } if((t = x >> 1) != 0) { x = t; r += 1; } return r; }; cosmicos.BigInteger.cbit = function(x) { var r = 0; while(x != 0) { x &= x - 1; ++r; } return r; }; cosmicos.BigInteger.intAt = function(s,i) { var c = cosmicos.BigInteger.BI_RC[HxOverrides.cca(s,i)]; if(c == null) return -1; return c; }; cosmicos.BigInteger.int2charCode = function(n) { return HxOverrides.cca(cosmicos.BigInteger.BI_RM,n); }; cosmicos.BigInteger.lbit = function(x) { if(x == 0) return -1; var r = 0; if((x & 65535) == 0) { x >>= 16; r += 16; } if((x & 255) == 0) { x >>= 8; r += 8; } if((x & 15) == 0) { x >>= 4; r += 4; } if((x & 3) == 0) { x >>= 2; r += 2; } if((x & 1) == 0) ++r; return r; }; cosmicos.BigInteger.dumpBi = function(r) { var s; s = "sign: " + (r.sign == null?"null":"" + r.sign); s += " t: " + r.t; s += Std.string(r.chunks); return s; }; cosmicos.BigInteger.prototype = { fromInt: function(x) { this.t = 1; this.chunks[0] = 0; if(x < 0) this.sign = -1; else this.sign = 0; if(x > 0) this.chunks[0] = x; else if(x < -1) this.chunks[0] = x + cosmicos.BigInteger.DV; else this.t = 0; } ,fromInt32: function(x) { this.fromInt(x); } ,toInt: function() { if(this.sign < 0) { if(this.t == 1) return this.chunks[0] - cosmicos.BigInteger.DV; else if(this.t == 0) return -1; } else if(this.t == 1) return this.chunks[0]; else if(this.t == 0) return 0; return (this.chunks[1] & (1 << 32 - cosmicos.BigInteger.DB) - 1) << cosmicos.BigInteger.DB | this.chunks[0]; } ,toInt32: function() { return this.toInt(); } ,toString: function() { return this.toRadix(10); } ,toHex: function() { return this.toRadix(16); } ,toBytes: function() { var i = this.t; var r = new Array(); r[0] = this.sign; var p = cosmicos.BigInteger.DB - i * cosmicos.BigInteger.DB % 8; var d; var k = 0; if(i-- > 0) { if(p < cosmicos.BigInteger.DB && (d = this.chunks[i] >> p) != (this.sign & cosmicos.BigInteger.DM) >> p) { r[k] = d | this.sign << cosmicos.BigInteger.DB - p; k++; } while(i >= 0) { if(p < 8) { d = (this.chunks[i] & (1 << p) - 1) << 8 - p; --i; d |= this.chunks[i] >> (p += cosmicos.BigInteger.DB - 8); } else { d = this.chunks[i] >> (p -= 8) & 255; if(p <= 0) { p += cosmicos.BigInteger.DB; --i; } } if((d & 128) != 0) d |= -256; if(k == 0 && (this.sign & 128) != (d & 128)) ++k; if(k > 0 || d != this.sign) { r[k] = d; k++; } } } var bb = new haxe.io.BytesBuffer(); var _g1 = 0; var _g = r.length; while(_g1 < _g) { var i1 = _g1++; bb.b.push(r[i1]); } return bb.getBytes(); } ,toBytesUnsigned: function() { var bb = new haxe.io.BytesBuffer(); var k = 8; var km = 255; var d = 0; var i = this.t; var p = cosmicos.BigInteger.DB - i * cosmicos.BigInteger.DB % k; var m = false; var c = 0; if(i-- > 0) { if(p < cosmicos.BigInteger.DB && (d = this.chunks[i] >> p) > 0) { m = true; bb.b.push(d); c++; } while(i >= 0) { if(p < k) { d = (this.chunks[i] & (1 << p) - 1) << k - p; d |= this.chunks[--i] >> (p += cosmicos.BigInteger.DB - k); } else { d = this.chunks[i] >> (p -= k) & km; if(p <= 0) { p += cosmicos.BigInteger.DB; --i; } } if(d > 0) m = true; if(m) { bb.b.push(d); c++; } } } return bb.getBytes(); } ,toRadix: function(b) { if(b == null) b = 10; if(b < 2 || b > 36) throw "invalid base for conversion"; if(this.sigNum() == 0) return "0"; var cs = Math.floor(0.6931471805599453 * cosmicos.BigInteger.DB / Math.log(b)); var a = Std["int"](Math.pow(b,cs)); var d = cosmicos.BigInteger.nbv(a); var y = cosmicos.BigInteger.nbi(); var z = cosmicos.BigInteger.nbi(); var r = ""; this.divRemTo(d,y,z); while(y.sigNum() > 0) { r = (function($this) { var $r; var _this = cosmicos.I32.baseEncode(cosmicos.I32.add(a,z.toInt32()),b); $r = HxOverrides.substr(_this,1,null); return $r; }(this)) + r; y.divRemTo(d,y,z); } return cosmicos.I32.baseEncode(z.toInt32(),b) + r; } ,abs: function() { if(this.sign < 0) return this.neg(); else return this; } ,add: function(a) { var r = cosmicos.BigInteger.nbi(); this.addTo(a,r); return r; } ,compare: function(a) { var r = this.sign - a.sign; if(r != 0) return r; var i = this.t; r = i - a.t; if(r != 0) return r; while(--i >= 0) { r = this.chunks[i] - a.chunks[i]; if(r != 0) return r; } return 0; } ,div: function(a) { var r = cosmicos.BigInteger.nbi(); this.divRemTo(a,r,null); return r; } ,divideAndRemainder: function(a) { var q = cosmicos.BigInteger.nbi(); var r = cosmicos.BigInteger.nbi(); this.divRemTo(a,q,r); return [q,r]; } ,eq: function(a) { return this.compare(a) == 0; } ,isEven: function() { return (this.t > 0?this.chunks[0] & 1:this.sign) == 0; } ,max: function(a) { if(this.compare(a) > 0) return this; else return a; } ,min: function(a) { if(this.compare(a) < 0) return this; else return a; } ,mod: function(a) { var r = cosmicos.BigInteger.nbi(); this.abs().divRemTo(a,null,r); if(this.sign < 0 && r.compare(cosmicos.BigInteger.get_ZERO()) > 0) a.subTo(r,r); return r; } ,modInt: function(n) { if(n <= 0) return 0; var d = cosmicos.BigInteger.DV % n; var r; if(this.sign < 0) r = n - 1; else r = 0; if(this.t > 0) { if(d == 0) r = this.chunks[0] % n; else { var i = this.t - 1; while(i >= 0) { r = (d * r + this.chunks[i]) % n; --i; } } } return r; } ,modInverse: function(m) { var ac = m.isEven(); if(this.isEven() && ac || m.sigNum() == 0) return cosmicos.BigInteger.get_ZERO(); var u = m.clone(); var v = this.clone(); var a = cosmicos.BigInteger.nbv(1); var b = cosmicos.BigInteger.nbv(0); var c = cosmicos.BigInteger.nbv(0); var d = cosmicos.BigInteger.nbv(1); while(u.sigNum() != 0) { while(u.isEven()) { u.rShiftTo(1,u); if(ac) { if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); } a.rShiftTo(1,a); } else if(!b.isEven()) b.subTo(m,b); b.rShiftTo(1,b); } while(v.isEven()) { v.rShiftTo(1,v); if(ac) { if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); } c.rShiftTo(1,c); } else if(!d.isEven()) d.subTo(m,d); d.rShiftTo(1,d); } if(u.compare(v) >= 0) { u.subTo(v,u); if(ac) a.subTo(c,a); b.subTo(d,b); } else { v.subTo(u,v); if(ac) c.subTo(a,c); d.subTo(b,d); } } if(v.compare(cosmicos.BigInteger.get_ONE()) != 0) return cosmicos.BigInteger.get_ZERO(); if(d.compare(m) >= 0) return d.sub(m); if(d.sigNum() < 0) d.addTo(m,d); else return d; return d; } ,mul: function(a) { var r = cosmicos.BigInteger.nbi(); this.multiplyTo(a,r); return r; } ,neg: function() { var r = cosmicos.BigInteger.nbi(); cosmicos.BigInteger.get_ZERO().subTo(this,r); return r; } ,remainder: function(a) { var r = cosmicos.BigInteger.nbi(); this.divRemTo(a,null,r); return r; } ,sub: function(a) { var r = cosmicos.BigInteger.nbi(); this.subTo(a,r); return r; } ,and: function(a) { var r = cosmicos.BigInteger.nbi(); this.bitwiseTo(a,cosmicos.BigInteger.op_and,r); return r; } ,andNot: function(a) { var r = cosmicos.BigInteger.nbi(); this.bitwiseTo(a,cosmicos.BigInteger.op_andnot,r); return r; } ,bitCount: function() { var r = 0; var x = this.sign & cosmicos.BigInteger.DM; var _g1 = 0; var _g = this.t; while(_g1 < _g) { var i = _g1++; r += cosmicos.BigInteger.cbit(this.chunks[i] ^ x); } return r; } ,bitLength: function() { if(this.t <= 0) return 0; return cosmicos.BigInteger.DB * (this.t - 1) + cosmicos.BigInteger.nbits(this.chunks[this.t - 1] ^ this.sign & cosmicos.BigInteger.DM); } ,complement: function() { var r = cosmicos.BigInteger.nbi(); var _g1 = 0; var _g = this.t; while(_g1 < _g) { var i = _g1++; r.chunks[i] = cosmicos.BigInteger.DM & ~this.chunks[i]; } r.t = this.t; r.sign = ~this.sign; return r; } ,clearBit: function(n) { return this.changeBit(n,cosmicos.BigInteger.op_andnot); } ,flipBit: function(n) { return this.changeBit(n,cosmicos.BigInteger.op_xor); } ,getLowestSetBit: function() { var _g1 = 0; var _g = this.t; while(_g1 < _g) { var i = _g1++; if(this.chunks[i] != 0) return i * cosmicos.BigInteger.DB + cosmicos.BigInteger.lbit(this.chunks[i]); } if(this.sign < 0) return this.t * cosmicos.BigInteger.DB; return -1; } ,not: function() { return this.complement(); } ,or: function(a) { var r = cosmicos.BigInteger.nbi(); this.bitwiseTo(a,cosmicos.BigInteger.op_or,r); return r; } ,setBit: function(n) { return this.changeBit(n,cosmicos.BigInteger.op_or); } ,shl: function(n) { var r = cosmicos.BigInteger.nbi(); if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r); return r; } ,shr: function(n) { var r = cosmicos.BigInteger.nbi(); if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r); return r; } ,testBit: function(n) { var j = Math.floor(n / cosmicos.BigInteger.DB); if(j >= this.t) return this.sign != 0; return (this.chunks[j] & 1 << n % cosmicos.BigInteger.DB) != 0; } ,xor: function(a) { var r = cosmicos.BigInteger.nbi(); this.bitwiseTo(a,cosmicos.BigInteger.op_xor,r); return r; } ,addTo: function(a,r) { var i = 0; var c = 0; var m = Std["int"](Math.min(a.t,this.t)); while(i < m) { c += this.chunks[i] + a.chunks[i]; r.chunks[i] = c & cosmicos.BigInteger.DM; i++; c >>= cosmicos.BigInteger.DB; } if(a.t < this.t) { c += a.sign; while(i < this.t) { c += this.chunks[i]; r.chunks[i] = c & cosmicos.BigInteger.DM; i++; c >>= cosmicos.BigInteger.DB; } c += this.sign; } else { c += this.sign; while(i < a.t) { c += a.chunks[i]; r.chunks[i] = c & cosmicos.BigInteger.DM; i++; c >>= cosmicos.BigInteger.DB; } c += a.sign; } if(c < 0) r.sign = -1; else r.sign = 0; if(c > 0) { r.chunks[i] = c; i++; } else if(c < -1) { r.chunks[i] = cosmicos.BigInteger.DV + c; i++; } r.t = i; r.clamp(); } ,copyTo: function(r) { var _g1 = 0; var _g = this.chunks.length; while(_g1 < _g) { var i = _g1++; r.chunks[i] = this.chunks[i]; } r.t = this.t; r.sign = this.sign; } ,divRemTo: function(m,q,r) { var pm = m.abs(); if(pm.t <= 0) return; var pt = this.abs(); if(pt.t < pm.t) { if(q != null) q.fromInt(0); if(r != null) this.copyTo(r); return; } if(r == null) r = cosmicos.BigInteger.nbi(); var y = cosmicos.BigInteger.nbi(); var ts = this.sign; var ms = m.sign; var nsh = cosmicos.BigInteger.DB - cosmicos.BigInteger.nbits(pm.chunks[pm.t - 1]); if(nsh > 0) { pt.lShiftTo(nsh,r); pm.lShiftTo(nsh,y); } else { pt.copyTo(r); pm.copyTo(y); } var ys = y.t; var y0 = y.chunks[ys - 1]; if(y0 == 0) return; var yt; yt = y0 * 1.0 * ((1 << cosmicos.BigInteger.F1) * 1.0) + (ys > 1?(y.chunks[ys - 2] >> cosmicos.BigInteger.F2) * 1.0:0.0); var d1 = cosmicos.BigInteger.FV / yt; var d2 = (1 << cosmicos.BigInteger.F1) * 1.0 / yt; var e = (1 << cosmicos.BigInteger.F2) * 1.0; var i = r.t; var j = i - ys; var t; if(q == null) t = cosmicos.BigInteger.nbi(); else t = q; y.dlShiftTo(j,t); if(r.compare(t) >= 0) { r.chunks[r.t] = 1; r.t++; r.subTo(t,r); } cosmicos.BigInteger.get_ONE().dlShiftTo(ys,t); t.subTo(y,y); while(y.t < ys) { y.chunks[y.t] = 0; y.t++; } while(--j >= 0) { var qd; if(r.chunks[--i] == y0) qd = cosmicos.BigInteger.DM; else qd = Math.floor(r.chunks[i] * 1.0 * d1 + (r.chunks[i - 1] * 1.0 + e) * d2); r.chunks[i] += y.am(0,qd,r,j,0,ys); if(r.chunks[i] < qd) { y.dlShiftTo(j,t); r.subTo(t,r); while(r.chunks[i] < --qd) r.subTo(t,r); } } if(q != null) { r.drShiftTo(ys,q); if(ts != ms) cosmicos.BigInteger.get_ZERO().subTo(q,q); } r.t = ys; r.clamp(); if(nsh > 0) r.rShiftTo(nsh,r); if(ts < 0) cosmicos.BigInteger.get_ZERO().subTo(r,r); } ,multiplyLowerTo: function(a,n,r) { var i = Std["int"](Math.min(this.t + a.t,n)); r.sign = 0; r.t = i; while(i > 0) { --i; r.chunks[i] = 0; } var j = r.t - this.t; while(i < j) { r.chunks[i + this.t] = this.am(0,a.chunks[i],r,i,0,this.t); ++i; } j = Std["int"](Math.min(a.t,n)); while(i < j) { this.am(0,a.chunks[i],r,i,0,n - i); ++i; } r.clamp(); } ,multiplyTo: function(a,r) { var x = this.abs(); var y = a.abs(); var i = x.t; r.t = i + y.t; while(--i >= 0) r.chunks[i] = 0; var _g1 = 0; var _g = y.t; while(_g1 < _g) { var i1 = _g1++; r.chunks[i1 + x.t] = x.am(0,y.chunks[i1],r,i1,0,x.t); } r.sign = 0; r.clamp(); if(this.sign != a.sign) cosmicos.BigInteger.get_ZERO().subTo(r,r); } ,multiplyUpperTo: function(a,n,r) { --n; var i = r.t = this.t + a.t - n; r.sign = 0; while(--i >= 0) r.chunks[i] = 0; i = Std["int"](Math.max(n - this.t,0)); var _g1 = i; var _g = a.t; while(_g1 < _g) { var x = _g1++; r.chunks[this.t + x - n] = this.am(n - x,a.chunks[x],r,0,0,this.t + x - n); } r.clamp(); r.drShiftTo(1,r); } ,squareTo: function(r) { if(r == this) throw "can not squareTo self"; var x = this.abs(); var i = r.t = 2 * x.t; while(--i >= 0) r.chunks[i] = 0; i = 0; while(i < x.t - 1) { var c = x.am(i,x.chunks[i],r,2 * i,0,1); if((r.chunks[i + x.t] += x.am(i + 1,2 * x.chunks[i],r,2 * i + 1,c,x.t - i - 1)) >= cosmicos.BigInteger.DV) { r.chunks[i + x.t] -= cosmicos.BigInteger.DV; r.chunks[i + x.t + 1] = 1; } i++; } if(r.t > 0) { var rv = x.am(i,x.chunks[i],r,2 * i,0,1); r.chunks[r.t - 1] += rv; } r.sign = 0; r.clamp(); } ,subTo: function(a,r) { var i = 0; var c = 0; var m = Std["int"](Math.min(a.t,this.t)); while(i < m) { c += this.chunks[i] - a.chunks[i]; r.chunks[i] = c & cosmicos.BigInteger.DM; i++; c >>= cosmicos.BigInteger.DB; } if(a.t < this.t) { c -= a.sign; while(i < this.t) { c += this.chunks[i]; r.chunks[i] = c & cosmicos.BigInteger.DM; i++; c >>= cosmicos.BigInteger.DB; } c += this.sign; } else { c += this.sign; while(i < a.t) { c -= a.chunks[i]; r.chunks[i] = c & cosmicos.BigInteger.DM; i++; c >>= cosmicos.BigInteger.DB; } c -= a.sign; } if(c < 0) r.sign = -1; else r.sign = 0; if(c < -1) { r.chunks[i] = cosmicos.BigInteger.DV + c; i++; } else if(c > 0) { r.chunks[i] = c; i++; } r.t = i; r.clamp(); } ,clamp: function() { var c = this.sign & cosmicos.BigInteger.DM; while(this.t > 0 && this.chunks[this.t - 1] == c) --this.t; } ,clone: function() { var r = cosmicos.BigInteger.nbi(); this.copyTo(r); return r; } ,gcd: function(a) { var x; if(this.sign < 0) x = this.neg(); else x = this.clone(); var y; if(a.sign < 0) y = a.neg(); else y = a.clone(); if(x.compare(y) < 0) { var t = x; x = y; y = t; } var i = x.getLowestSetBit(); var g = y.getLowestSetBit(); if(g < 0) return x; if(i < g) g = i; if(g > 0) { x.rShiftTo(g,x); y.rShiftTo(g,y); } while(x.sigNum() > 0) { if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x); if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y); if(x.compare(y) >= 0) { x.subTo(y,x); x.rShiftTo(1,x); } else { y.subTo(x,y); y.rShiftTo(1,y); } } if(g > 0) y.lShiftTo(g,y); return y; } ,padTo: function(n) { while(this.t < n) { this.chunks[this.t] = 0; this.t++; } } ,shortValue: function() { if(this.t == 0) return this.sign; else return this.chunks[0] << 16 >> 16; } ,byteValue: function() { if(this.t == 0) return this.sign; else return this.chunks[0] << 24 >> 24; } ,sigNum: function() { if(this.sign < 0) return -1; else if(this.t <= 0 || this.t == 1 && this.chunks[0] <= 0) return 0; else return 1; } ,dAddOffset: function(n,w) { while(this.t <= w) { this.chunks[this.t] = 0; this.t++; } this.chunks[w] += n; while(this.chunks[w] >= cosmicos.BigInteger.DV) { this.chunks[w] -= cosmicos.BigInteger.DV; if(++w >= this.t) { this.chunks[this.t] = 0; this.t++; } ++this.chunks[w]; } } ,dlShiftTo: function(n,r) { if(r == null) return; var i = this.t - 1; while(i >= 0) { r.chunks[i + n] = this.chunks[i]; i--; } i = n - 1; while(i >= 0) { r.chunks[i] = 0; i--; } r.t = this.t + n; r.sign = this.sign; } ,drShiftTo: function(n,r) { if(r == null) return; var i = n; while(i < this.t) { r.chunks[i - n] = this.chunks[i]; i++; } r.t = Std["int"](Math.max(this.t - n,0)); r.sign = this.sign; } ,invDigit: function() { if(this.t < 1) return 0; var x = this.chunks[0]; if((x & 1) == 0) return 0; var y = x & 3; y = y * (2 - (x & 15) * y) & 15; y = y * (2 - (x & 255) * y) & 255; y = y * (2 - ((x & 65535) * y & 65535)) & 65535; y = y * (2 - x * y % cosmicos.BigInteger.DV) % cosmicos.BigInteger.DV; if(y > 0) return cosmicos.BigInteger.DV - y; else return -y; } ,bitwiseTo: function(a,op,r) { var f; var m = Std["int"](Math.min(a.t,this.t)); var _g = 0; while(_g < m) { var i = _g++; r.chunks[i] = op(this.chunks[i],a.chunks[i]); } if(a.t < this.t) { f = a.sign & cosmicos.BigInteger.DM; var _g1 = m; var _g2 = this.t; while(_g1 < _g2) { var i1 = _g1++; r.chunks[i1] = op(this.chunks[i1],f); } r.t = this.t; } else { f = this.sign & cosmicos.BigInteger.DM; var _g11 = m; var _g3 = a.t; while(_g11 < _g3) { var i2 = _g11++; r.chunks[i2] = op(f,a.chunks[i2]); } r.t = a.t; } r.sign = op(this.sign,a.sign); r.clamp(); } ,changeBit: function(n,op) { var r = cosmicos.BigInteger.get_ONE().shl(n); this.bitwiseTo(r,op,r); return r; } ,chunkSize: function(r) { return Math.floor(0.6931471805599453 * cosmicos.BigInteger.DB / Math.log(r)); } ,dMultiply: function(n) { this.chunks[this.t] = this.am(0,n - 1,this,0,0,this.t); this.t++; this.clamp(); } ,lShiftTo: function(n,r) { var bs = n % cosmicos.BigInteger.DB; var cbs = cosmicos.BigInteger.DB - bs; var bm = (1 << cbs) - 1; var ds = Math.floor(n / cosmicos.BigInteger.DB); var c = this.sign << bs & cosmicos.BigInteger.DM; var i; var i1 = this.t - 1; while(i1 >= 0) { r.chunks[i1 + ds + 1] = this.chunks[i1] >> cbs | c; c = (this.chunks[i1] & bm) << bs; i1--; } i1 = ds - 1; while(i1 >= 0) { r.chunks[i1] = 0; i1--; } r.chunks[ds] = c; r.t = this.t + ds + 1; r.sign = this.sign; r.clamp(); } ,rShiftTo: function(n,r) { r.sign = this.sign; var ds = Math.floor(n / cosmicos.BigInteger.DB); if(ds >= this.t) { r.t = 0; return; } var bs = n % cosmicos.BigInteger.DB; var cbs = cosmicos.BigInteger.DB - bs; var bm = (1 << bs) - 1; r.chunks[0] = this.chunks[ds] >> bs; var _g1 = ds + 1; var _g = this.t; while(_g1 < _g) { var i = _g1++; r.chunks[i - ds - 1] |= (this.chunks[i] & bm) << cbs; r.chunks[i - ds] = this.chunks[i] >> bs; } if(bs > 0) r.chunks[this.t - ds - 1] |= (this.sign & bm) << cbs; r.t = this.t - ds; r.clamp(); } ,am1: function(i,x,w,j,c,n) { while(--n >= 0) { var v = x * this.chunks[i] + w.chunks[j] + c; i++; c = Math.floor(v / 67108864); w.chunks[j] = v & 67108863; j++; } return c; } ,am2: function(i,x,w,j,c,n) { var xl = x & 32767; var xh = x >> 15; while(--n >= 0) { var l = this.chunks[i] & 32767; var h = this.chunks[i] >> 15; i++; var m = xh * l + h * xl; l = xl * l + ((m & 32767) << 15) + w.chunks[j] + (c & 1073741823); c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30); w.chunks[j] = l & 1073741823; j++; } return c; } ,am3: function(i,x,w,j,c,n) { var xl = x & 16383; var xh = x >> 14; while(--n >= 0) { var l = this.chunks[i] & 16383; var h = this.chunks[i] >> 14; i++; var m = xh * l + h * xl; l = xl * l + ((m & 16383) << 14) + w.chunks[j] + c; c = (l >> 28) + (m >> 14) + xh * h; w.chunks[j] = l & 268435455; j++; } return c; } ,__class__: cosmicos.BigInteger }; cosmicos.CosFunction = $hx_exports.cosmicos.CosFunction = function(fn,meta) { this.fn = fn; this.meta = meta; }; cosmicos.CosFunction.__name__ = true; cosmicos.CosFunction.prototype = { __class__: cosmicos.CosFunction }; cosmicos.Cursor = function(e) { this.at = 0; this.len = cosmicos.Parse.car(e); this.e = cosmicos.Parse.cdr(e); }; cosmicos.Cursor.__name__ = true; cosmicos.Cursor.prototype = { length: function() { return this.len; } ,next: function() { var result = null; if(this.len == 1) { result = this.e; this.e = null; } else if(this.at == this.len - 1) { result = cosmicos.Parse.cdr(this.e); this.e = null; } else { result = cosmicos.Parse.car(this.e); if(this.at != this.len - 2) this.e = cosmicos.Parse.cdr(this.e); } this.at++; return result; } ,__class__: cosmicos.Cursor }; cosmicos.Evaluate = $hx_exports.cosmicos.Evaluate = function() { this.mem = new cosmicos.Memory(null); this.vocab = new cosmicos.Vocab(); this.id_lambda = this.vocab.get("?"); this.id_lambda0 = this.vocab.get("??"); this.id_define = this.vocab.get("@"); this.id_if = this.vocab.get("if"); this.id_assign = this.vocab.get("assign"); this.id_translate = -1; }; cosmicos.Evaluate.__name__ = true; cosmicos.Evaluate.isBi = function(x) { return js.Boot.__instanceof(x,cosmicos.BigInteger); }; cosmicos.Evaluate.isBi2 = function(x,y) { return js.Boot.__instanceof(x,cosmicos.BigInteger) || js.Boot.__instanceof(y,cosmicos.BigInteger); }; cosmicos.Evaluate.bi = function(x) { if(js.Boot.__instanceof(x,cosmicos.BigInteger)) return x; return cosmicos.BigInteger.ofInt(x); }; cosmicos.Evaluate.main = function() { }; cosmicos.Evaluate.dummy = function() { var v = new cosmicos.ManuscriptStyle(); }; cosmicos.Evaluate.prototype = { evaluateInContext: function(e0,c) { var _g = this; var more = false; do { if(typeof(e0) == "string") { var str = e0; if(str.length == 0 || str.charAt(0) == "1") return str.length; return str; } if(((e0 | 0) === e0) || js.Boot.__instanceof(e0,cosmicos.BigInteger)) return e0; var cursor = new cosmicos.Cursor(e0); var x = this.evaluateInContext(cursor.next(),c); if(x == this.id_lambda) { var k2 = [this.evaluateInContext(cursor.next(),c)]; var e2 = [cursor.next()]; return (function(e2,k2) { return function(v) { var c2 = new cosmicos.Memory(c,k2[0],v); return _g.evaluateInContext(e2[0],c2); }; })(e2,k2); } else if(x == this.id_lambda0) { var k21 = [this.evaluateInContext(cursor.next(),c)]; var e21 = [cursor.next()]; return new cosmicos.CosFunction((function(e21,k21) { return function(v1) { var c21 = new cosmicos.Memory(c,k21[0],v1); return _g.evaluateInContext(e21[0],c21); }; })(e21,k21),true); } else if(x == this.id_assign) { var k22 = this.evaluateInContext(cursor.next(),c); var v2 = this.evaluateInContext(cursor.next(),c); var c22 = new cosmicos.Memory(c,k22,v2); e0 = cursor.next(); c = c22; more = true; continue; } else if(x == this.id_define) { var k23 = cursor.next(); var v21 = this.evaluateInContext(cursor.next(),c); var code = this.evaluateInContext(k23,c); c.add(code,v21); return 1; } else if(x == this.id_if) { var choice = this.evaluateInContext(cursor.next(),c); if(choice != 0) { e0 = cursor.next(); more = true; continue; } else { cursor.next(); e0 = cursor.next(); more = true; continue; } } else { var x0 = x; var len = cursor.length(); if(((x | 0) === x) || js.Boot.__instanceof(x,cosmicos.BigInteger)) { var j = x; x = c.get(j); if(len > 0) { if(x == null) console.log("Problem with " + j + " (" + this.vocab.reverse(j) + ")"); } } else if(typeof(x) == "string") { var str1 = x; var u = cosmicos.BigInteger.ofInt(0); var two = cosmicos.BigInteger.ofInt(2); var _g1 = 0; var _g2 = str1.length; while(_g1 < _g2) { var j1 = _g1++; u = u.mul(two); if(str1.charAt(j1) == ":") u = u.add(cosmicos.BigInteger.get_ONE()); } x = u; } var _g3 = 1; while(_g3 < len) { var i = _g3++; var v3 = [cursor.next()]; try { if(js.Boot.__instanceof(x,cosmicos.CosFunction)) x = x.fn((function(v3) { return function(x1) { return _g.evaluateInContext(v3[0],c); }; })(v3)); else x = x(this.evaluateInContext(v3[0],c)); } catch( e ) { console.log("Problem evaluating " + Std.string(x) + " with " + v3[0] + " in " + Std.string(x0) + " (" + this.vocab.reverse(x0) + ") from " + Std.string(cosmicos.Parse.deconsify(e0))); throw e; } } return x; } } while(more); return null; } ,evaluateExpression: function(e) { return this.evaluateInContext(e,this.mem); } ,evaluateLine: function(str) { var lst = cosmicos.Parse.stringToList(str,this.vocab); cosmicos.Parse.encodeSymbols(lst,this.vocab); cosmicos.Parse.removeSlashMarker(lst); lst = cosmicos.Parse.consify(lst); if(this.id_translate >= 0) { var translate = this.mem.get(this.id_translate); if(translate != null) lst = translate(lst); } var lst2 = cosmicos.Parse.deconsify(lst); var v = this.evaluateExpression(lst); if(!((v | 0) === v)) { } return v; } ,numberizeLine: function(str) { var lst = cosmicos.Parse.stringToList(str,this.vocab); cosmicos.Parse.encodeSymbols(lst,this.vocab); return lst; } ,codifyLine: function(str) { var lst = cosmicos.Parse.stringToList(str,this.vocab); cosmicos.Parse.encodeSymbols(lst,this.vocab); return cosmicos.Parse.codify(lst); } ,nestedLine: function(str) { var lst = cosmicos.Parse.stringToList(str,this.vocab); cosmicos.Parse.encodeSymbols(lst,null); cosmicos.Parse.recoverList(lst); return lst; } ,getVocab: function() { return this.vocab; } ,applyOldOrder: function() { var _g = this; this.mem = new cosmicos.Memory(null); this.vocab.clear(); this.vocab.check("intro",0); this.vocab.check("<",1); this.vocab.check("=",2); this.vocab.check(">",3); this.vocab.check("not",4); this.vocab.check("and",5); this.vocab.check("or",6); this.vocab.check("demo",7); this.vocab.check("equal",8); this.vocab.check("*",9); this.vocab.check("+",10); this.vocab.check("-",11); this.id_lambda = this.vocab.check("?",12); this.id_define = this.vocab.check("define",13); this.id_assign = this.vocab.check("assign",14); this.id_if = this.vocab.check("if",15); this.vocab.check("vector",16); this.vocab.check("unused1",17); this.vocab.check("unused2",18); this.vocab.check("forall",19); this.vocab.check("exists",20); this.vocab.check("cons",21); this.vocab.check("car",22); this.vocab.check("cdr",23); this.vocab.check("number?",24); this.id_translate = this.vocab.check("translate",25); this.vocab.check("lambda",26); this.vocab.check("make-cell",27); this.vocab.check("set!",28); this.vocab.check("get!",29); this.vocab.check("all",30); this.vocab.check("natural-set",31); this.vocab.check("undefined",32); this.vocab.check("!",33); this.vocab.check("div",34); this.vocab.check("primer",35); this.mem.add(this.vocab.get("intro"),function(x) { return 1; }); this.addStdMin(); this.evaluateLine("@ not | ? 0 | if $0 0 1"); this.evaluateLine("@ and | ? 0 | ? 1 | if $0 $1 0"); this.evaluateLine("@ or | ? 0 | ? 1 | if $0 1 $1"); this.mem.add(this.vocab.get("make-cell"),function(x1) { return { data : x1}; }); this.mem.add(this.vocab.get("get!"),function(x2) { return x2.data; }); this.mem.add(this.vocab.get("set!"),function(x3) { return function(y) { x3.data = y; return 1; }; }); this.mem.add(this.vocab.get("number?"),function(x4) { return ((x4 | 0) === x4) || js.Boot.__instanceof(x4,cosmicos.BigInteger) || typeof(x4) == "string"; }); this.mem.add(this.vocab.get("translate"),function(x5) { if(((x5 | 0) === x5) || js.Boot.__instanceof(x5,cosmicos.BigInteger) || typeof(x5) == "string") return x5; var rep = function(x6) { }; var len = cosmicos.Parse.car(x5); if(len == 0) return x5; var current = _g.mem.get(_g.id_translate); if(len == 1) return cosmicos.Parse.cons(1,current(cosmicos.Parse.cdr(x5))); var rep1 = function(r,len1,rec) { if(len1 == 2) return cosmicos.Parse.cons(current(cosmicos.Parse.car(r)),current(cosmicos.Parse.cdr(r))); return cosmicos.Parse.cons(current(cosmicos.Parse.car(r)),rec(cosmicos.Parse.cdr(r),len1 - 1,rec)); }; return cosmicos.Parse.cons(len,rep1(cosmicos.Parse.cdr(x5),len,rep1)); }); this.mem.add(this.vocab.get("forall"),function(f) { if(f(-5) != 0 && f(10) != 0 && f(15) != 0 && f(18) != 0) return 1; else return 0; }); this.mem.add(this.vocab.get("exists"),function(f1) { var _g1 = -10; while(_g1 < 20) { var i = _g1++; if(f1(i) != 0) return 1; } return 0; }); this.mem.add(this.vocab.get("all"),function(f2) { var lst = []; var _g2 = -50; while(_g2 < 50) { var i1 = _g2++; if(f2(i1) != 0) lst.push(i1); } return cosmicos.Parse.consify(lst); }); this.mem.add(this.vocab.get("natural-set"),(this.mem.get(this.vocab.get("all")))(function(x7) { return x7 >= 0; })); this.mem.add(this.vocab.get("div"),function(x8) { return function(y1) { if(cosmicos.Evaluate.isBi2(x8,y1)) return cosmicos.Evaluate.bi(x8).div(cosmicos.Evaluate.bi(y1)); return x8 / y1 | 0; }; }); this.mem.add(this.vocab.get("demo"),function(x9) { return x9; }); this.evaluateLine("@ is:int $number?"); this.evaluateLine("@ unary-v | ? v | ? x | if (= $x 0) $v (unary-v | + $v 1)"); this.evaluateLine("@ unary | unary-v 0"); this.evaluateLine("@ has-divisor-within | ? top | ? x | if (< $top 2) 0 | if (= $x | * $top | div $x $top) 1 | has-divisor-within (- $top 1) $x"); this.evaluateLine("@ is:prime | ? x | if (< $x 2) 0 | not | has-divisor-within (- $x 1) $x"); this.evaluateLine("@ has-square-divisor-within | ? top | ? x | if (< $top 0) 0 | if (= $x | * $top $top) 1 | has-square-divisor-within (- $top 1) $x"); this.evaluateLine("@ is:square | ? x | has-square-divisor-within $x $x"); this.id_lambda0 = this.vocab.get("??"); } ,addStdMin: function() { this.mem.add(this.vocab.get("+"),function(x) { return function(y) { if(cosmicos.Evaluate.isBi2(x,y)) return cosmicos.Evaluate.bi(x).add(cosmicos.Evaluate.bi(y)); return x + y; }; }); this.mem.add(this.vocab.get("-"),function(x1) { return function(y1) { if(cosmicos.Evaluate.isBi2(x1,y1)) return cosmicos.Evaluate.bi(x1).sub(cosmicos.Evaluate.bi(y1)); return x1 - y1; }; }); this.mem.add(this.vocab.get("="),function(x2) { return function(y2) { if(cosmicos.Evaluate.isBi2(x2,y2)) if(cosmicos.Evaluate.bi(x2).compare(cosmicos.Evaluate.bi(y2)) == 0) return 1; else return 0; if(x2 == y2) return 1; else return 0; }; }); this.mem.add(this.vocab.get("*"),function(x3) { return function(y3) { if(cosmicos.Evaluate.isBi2(x3,y3)) return cosmicos.Evaluate.bi(x3).mul(cosmicos.Evaluate.bi(y3)); return x3 * y3; }; }); this.mem.add(this.vocab.get("<"),function(x4) { return function(y4) { if(cosmicos.Evaluate.isBi2(x4,y4)) if(cosmicos.Evaluate.bi(x4).compare(cosmicos.Evaluate.bi(y4)) < 0) return 1; else return 0; if(x4 < y4) return 1; else return 0; }; }); this.mem.add(this.vocab.get(">"),function(x5) { return function(y5) { if(cosmicos.Evaluate.isBi2(x5,y5)) if(cosmicos.Evaluate.bi(x5).compare(cosmicos.Evaluate.bi(y5)) > 0) return 1; else return 0; if(x5 > y5) return 1; else return 0; }; }); this.mem.add(this.vocab.get("pure"),function(v) { if(v) return function(x6) { return function(y6) { return x6; }; }; else return function(x7) { return function(y7) { return y7; }; }; }); this.evaluateLine("@ eval | ? x | x 1"); } ,addStd: function() { this.addStdMin(); this.evaluateLine("@ not | ? x | if $x 0 1"); this.evaluateLine("@ and | ? x | ? y | if $x $y 0"); this.evaluateLine("@ or | ? x | ? y | if $x 1 $y"); } ,addPrimer: function(primer) { this.mem.add(this.vocab.get("primer"),cosmicos.Parse.consify(cosmicos.Parse.integrate(primer))); } ,__class__: cosmicos.Evaluate }; cosmicos.Constants = function() { }; cosmicos.Constants.__name__ = true; cosmicos.I32 = function() { }; cosmicos.I32.__name__ = true; cosmicos.I32.B4 = function(v) { return v >>> 24 & -1; }; cosmicos.I32.B3 = function(v) { return v >>> 16 & 255 & -1; }; cosmicos.I32.B2 = function(v) { return v >>> 8 & 255 & -1; }; cosmicos.I32.B1 = function(v) { return v & 255 & -1; }; cosmicos.I32.abs = function(v) { return Std["int"](Math.abs(v)); }; cosmicos.I32.add = function(a,b) { return a + b; }; cosmicos.I32.alphaFromArgb = function(v) { return v >>> 24 & -1; }; cosmicos.I32.and = function(a,b) { return a & b; }; cosmicos.I32.baseEncode = function(v,radix) { if(radix < 2 || radix > 36) throw "radix out of range"; var sb = ""; var av = Std["int"](Math.abs(v)); var radix32 = radix; while(true) { var r32 = av % radix32; sb = "0123456789abcdefghijklmnopqrstuvwxyz".charAt(r32 & -1) + sb; av = (av - r32) / radix32 | 0; if(av == 0) break; } if(v < 0) return "-" + sb; return sb; }; cosmicos.I32.complement = function(v) { return ~v; }; cosmicos.I32.compare = function(a,b) { return a - b; }; cosmicos.I32.div = function(a,b) { return a / b | 0; }; cosmicos.I32.encodeBE = function(i) { var b = haxe.io.Bytes.alloc(4); b.b[0] = i >>> 24 & -1 & 255; b.b[1] = i >>> 16 & 255 & -1 & 255; b.b[2] = i >>> 8 & 255 & -1 & 255; b.b[3] = i & 255 & -1 & 255; return b; }; cosmicos.I32.encodeLE = function(i) { var b = haxe.io.Bytes.alloc(4); b.b[0] = i & 255 & -1 & 255; b.b[1] = i >>> 8 & 255 & -1 & 255; b.b[2] = i >>> 16 & 255 & -1 & 255; b.b[3] = i >>> 24 & -1 & 255; return b; }; cosmicos.I32.decodeBE = function(s,pos) { if(pos == null) pos = 0; var b0 = s.b[pos + 3]; var b1 = s.b[pos + 2]; var b2 = s.b[pos + 1]; var b3 = s.b[pos]; b1 = b1 << 8; b2 = b2 << 16; b3 = b3 << 24; var a = b0 + b1; a = a + b2; a = a + b3; return a; }; cosmicos.I32.decodeLE = function(s,pos) { if(pos == null) pos = 0; var b0 = s.b[pos]; var b1 = s.b[pos + 1]; var b2 = s.b[pos + 2]; var b3 = s.b[pos + 3]; b1 = b1 << 8; b2 = b2 << 16; b3 = b3 << 24; var a = b0 + b1; a = a + b2; a = a + b3; return a; }; cosmicos.I32.eq = function(a,b) { return a == b; }; cosmicos.I32.gt = function(a,b) { return a > b; }; cosmicos.I32.gteq = function(a,b) { return a >= b; }; cosmicos.I32.lt = function(a,b) { return a < b; }; cosmicos.I32.lteq = function(a,b) { return a <= b; }; cosmicos.I32.make = function(high,low) { return (high << 16) + low; }; cosmicos.I32.makeColor = function(alpha,rgb) { return alpha << 24 | rgb & 16777215; }; cosmicos.I32.mod = function(a,b) { return a % b; }; cosmicos.I32.mul = function(a,b) { return a * b; }; cosmicos.I32.neg = function(v) { return -v; }; cosmicos.I32.ofInt = function(v) { return v; }; cosmicos.I32.or = function(a,b) { return a | b; }; cosmicos.I32.packBE = function(l) { var sb = new haxe.io.BytesBuffer(); var _g1 = 0; var _g = l.length; while(_g1 < _g) { var i = _g1++; sb.b.push(l[i] >>> 24 & -1); sb.b.push(l[i] >>> 16 & 255 & -1); sb.b.push(l[i] >>> 8 & 255 & -1); sb.b.push(l[i] & 255 & -1); } return sb.getBytes(); }; cosmicos.I32.packLE = function(l) { var sb = new haxe.io.BytesBuffer(); var _g1 = 0; var _g = l.length; while(_g1 < _g) { var i = _g1++; sb.b.push(l[i] & 255 & -1); sb.b.push(l[i] >>> 8 & 255 & -1); sb.b.push(l[i] >>> 16 & 255 & -1); sb.b.push(l[i] >>> 24 & -1); } return sb.getBytes(); }; cosmicos.I32.rgbFromArgb = function(v) { return v & 16777215; }; cosmicos.I32.sub = function(a,b) { return a - b; }; cosmicos.I32.shl = function(v,bits) { return v << bits; }; cosmicos.I32.shr = function(v,bits) { return v >> bits; }; cosmicos.I32.toColor = function(v) { return { alpha : v >>> 24 & -1, color : v & 16777215}; }; cosmicos.I32.toFloat = function(v) { return v * 1.0; }; cosmicos.I32.toInt = function(v) { return v & -1; }; cosmicos.I32.toNativeArray = function(v) { return v; }; cosmicos.I32.unpackLE = function(s) { if(s == null || s.length == 0) return new Array(); if(s.length % 4 != 0) throw "Buffer not multiple of 4 bytes"; var a = new Array(); var pos = 0; var i = 0; var len = s.length; while(pos < len) { a[i] = cosmicos.I32.decodeLE(s,pos); pos += 4; i++; } return a; }; cosmicos.I32.unpackBE = function(s) { if(s == null || s.length == 0) return new Array(); if(s.length % 4 != 0) throw "Buffer not multiple of 4 bytes"; var a = new Array(); var pos = 0; var i = 0; while(pos < s.length) { a[i] = cosmicos.I32.decodeBE(s,pos); pos += 4; i++; } return a; }; cosmicos.I32.ushr = function(v,bits) { return v >>> bits; }; cosmicos.I32.xor = function(a,b) { return a ^ b; }; cosmicos.I32.intToHex = function(j) { var sb = new StringBuf(); var i = 8; while(i-- > 0) { var v = j >>> i * 4 & 15; sb.add(StringTools.hex(v).toLowerCase()); } return sb.b; }; cosmicos.I32.int32ToHex = function(j) { var sb = new StringBuf(); var i = 8; var f = 15; while(i-- > 0) { var v = j >>> i * 4 & f & -1; sb.add(StringTools.hex(v).toLowerCase()); } return sb.b; }; cosmicos.ManuscriptStyle = $hx_exports.cosmicos.ManuscriptStyle = function() { }; cosmicos.ManuscriptStyle.__name__ = true; cosmicos.ManuscriptStyle.prototype = { render: function(x) { this.out = new Array(); this.need_ws = false; this.renderNest(x,false); return this.out; } ,ws: function() { if(this.need_ws) { this.out.push(" "); this.need_ws = false; } } ,nws: function() { this.need_ws = true; } ,renderInt: function(x) { this.ws(); this.out.push(x); this.nws(); } ,renderString: function(x) { this.ws(); if(x == "") { this.out.push(""); return; } var lst = x.split(":"); var _g = 0; while(_g < lst.length) { var e = lst[_g]; ++_g; this.out.push(e); } this.nws(); } ,renderNest: function(x,nested) { var offset = 0; var parens = nested; if(x.length >= 1) { var e = x[0]; if(e == -1 || e == -2) { offset = 1; if(e == -1) { this.ws(); this.out.push("|"); this.nws(); } if(e == -2) { this.ws(); this.out.push("$"); } parens = false; } } if(parens) this.out.push("("); var _g1 = offset; var _g = x.length; while(_g1 < _g) { var i = _g1++; var e1 = x[i]; if(typeof(e1) == "string") this.renderString(e1); if(((e1 | 0) === e1)) this.renderInt(e1); if((e1 instanceof Array) && e1.__enum__ == null) { this.ws(); this.renderNest(e1,true); this.nws(); } } if(parens) this.out.push(")"); } ,__class__: cosmicos.ManuscriptStyle }; cosmicos.Memory = $hx_exports.cosmicos.Memory = function(parent,key,val) { if(key == null) key = -1; this.parent = parent; this.key = key; this.val = val; if(key == -1) this.block = new haxe.ds.IntMap(); }; cosmicos.Memory.__name__ = true; cosmicos.Memory.prototype = { add: function(key,val) { if(this.block != null) { var value = val; this.block.set(key,value); return; } if(this.parent != null) this.parent.add(key,val); } ,get: function(key) { if(this.block == null) { if(this.key == key) return this.val; if(this.parent == null) return null; return this.parent.get(key); } return this.block.get(key); } ,__class__: cosmicos.Memory }; cosmicos.Parse = $hx_exports.cosmicos.Parse = function() { }; cosmicos.Parse.__name__ = true; cosmicos.Parse.stringToList = function(x,vocab) { var result = []; x = " " + x + " );"; var cache = ""; var level = 0; var slashed = false; var _g1 = 0; var _g = x.length; while(_g1 < _g) { var i = _g1++; var ch = x.charAt(i); if(ch == "\n" || ch == "\r" || ch == ";") ch = " "; if(ch == "(") { level++; if(level == 1) continue; } if((ch == "/" || ch == "|") && level == 0) { level = 1; slashed = true; continue; } if(ch == ")") { level--; if(level == 0) { var r = cosmicos.Parse.stringToList(cache,vocab); result.push(r); if(slashed) { result[result.length - 1].unshift(-1); slashed = false; } cache = ""; continue; } } if(ch != " " || level > 0) cache += ch; if(level == 0 && ch == " " && cache.length > 0) { if(cache.charAt(0) == "$") { cache = HxOverrides.substr(cache,1,cache.length - 1); result.push([-2,cache]); } else result.push(cache); cache = ""; } } return result; }; cosmicos.Parse.encodeSymbols = function(e,vocab) { var _g1 = 0; var _g = e.length; while(_g1 < _g) { var i = _g1++; var v = e[i]; if((v instanceof Array) && v.__enum__ == null) { var ei = v; cosmicos.Parse.encodeSymbols(ei,vocab); } else if(v == -1 || v == -2) continue; else { var str = v; var ch0 = str.charAt(0); if(ch0 < "0" || ch0 > "9") { if(ch0 == ":" || ch0 == ".") v = str; else if(ch0 == "U" && new EReg("^U1*U$","").match(str)) v = HxOverrides.substr(str,1,str.length - 2); else if(ch0 == "\"") { var len = str.length; var u = cosmicos.BigInteger.ofInt(0); var times = cosmicos.BigInteger.ofInt(256); var _g3 = 1; var _g2 = len - 1; while(_g3 < _g2) { var j = _g3++; u = u.mul(times); u = u.add(cosmicos.BigInteger.ofInt(HxOverrides.cca(str,j))); } v = u; } else if(new EReg("^.*-in-unary$","").match(str) && vocab != null) { var v0 = vocab.get(HxOverrides.substr(str,0,str.length - 9)); var u1 = ""; var _g21 = 0; while(_g21 < v0) { var j1 = _g21++; u1 += "1"; } v = u1; } else if(vocab != null) v = vocab.get(str); } else v = Std.parseInt(str); e[i] = v; } } }; cosmicos.Parse.removeSlashMarker = function(e) { var _g1 = 0; var _g = e.length; while(_g1 < _g) { var i = _g1++; var v = e[i]; if((v instanceof Array) && v.__enum__ == null) { var ei = v; cosmicos.Parse.removeSlashMarker(ei); } } if(e.length > 0) { if(e[0] == -1 || e[0] == -2) e.shift(); } }; cosmicos.Parse.cons = function(x,y) { return function(f) { return (f(x))(y); }; }; cosmicos.Parse.car = function(x) { return x(function(a) { return function(b) { return a; }; }); }; cosmicos.Parse.cdr = function(x) { return x(function(a) { return function(b) { return b; }; }); }; cosmicos.Parse.textify = function(e,vocab) { var txt = ""; if((e instanceof Array) && e.__enum__ == null) { var lst = e; var len = lst.length; txt += "("; var _g = 0; while(_g < len) { var i = _g++; if(i > 0) txt += " "; txt += cosmicos.Parse.textify(lst[i],vocab); } txt += ")"; return txt; } var v = vocab.reverse(e); if(v == null) return "" + Std.string(e); return Std.string(e) + "-" + v; }; cosmicos.Parse.deconsify = function(e) { if(((