var Nil=null, Undefined=undefined; var True=true, False=false; var SERVER=(typeof window)=="undefined"; function stop(errno) { if (SERVER) process.exit(errno); else nofunc(); } var ERRLI=-1,ERRCOL=-1; function errlicolSet(LI,COL) { ERRLI=LI,ERRCOL=COL; } function error(msg) { if (ERRLI>0) msg+="; LI="+ERRLI.toString(); if (ERRCOL>0) msg+="; COL="+ERRCOL.toString(); (SERVER?console.log:alert)(msg); stop(1); } function prototype(X) { var RES=Object.getPrototypeOf(X); if (RES==Nil) return Object.prototype; return RES; } function constructor(X) { return prototype(X).constructor; } function isa0(O,C,REC) { if (isNil(O)) return False; if (constructor(O)==C) return True; if (isUndefined(REC)) REC=True; if (!REC || constructor(O)==Object) return False; return isa0(prototype(O),C,REC); } function eqNil(O) { return O===Nil; } function isDefined(O) { return O!==Undefined; } function isUndefined(O) { return O===Undefined; } function isNil(O) { return isUndefined(O) || O==BoxedUndefined || eqNil(O) || O==BoxedNil; } function isSymbol(O) { return isa0(O,Symbol); } function isBoolean(O) { return isa0(O,Boolean); } function isNumber(O) { return isa0(O,Number); } function isString(O) { return isa0(O,String); } function isNumStr(O) { return isNumber(O) || strIsNum(O); } function isAtom(O) { return isNil(O) || isSymbol(O) || isBoolean(O) || isNumber(O) || isString(O); } function isArray(O) { return isa0(O,Array); } function isFunction(O) { return isa0(O,Function); } var _SY={}; function sy(S) { if (isString(S)) { _SY[S]=1;return Symbol.for(S); } else if (isSymbol(S)) return Symbol.keyFor(S); else error("sy"); } function syExists(S) { return isDefined(_SY[S]); } var CharNatNone=0,CharNatUnknown=CharNatNone, CharNatAlf=1,CharNatQuote=2,CharNatDQuote=3, CharNatOmg=4,CharNatDigit=5,CharNatBlank=6 var CharNat=[]; function asc(c) { return c.charCodeAt(0)&255; } function chr(i) { return String.fromCharCode(i); } function charnat(c) { return CharNat[asc(c)]; } function charnatSet(c,n) { return CharNat[asc(c)]=n; } function charIsAlpha(C) { return charnat(C)==CharNatAlf; } function charIsOmg(C) { return charnat(C)==CharNatOmg; } function charIsDigit(C) { return charnat(C)==CharNatDigit; } function charIsBlank(C) { return charnat(C)==CharNatBlank; } function charIsUnknown(C) { return charnat(C)==CharNatUnknown; } function charIs(C,N) { return charnat(C)==N; } function strIs(S,N) { if (!isString(S)) return False; for (var I=0;I=0 && chars.indexOf(a[i])!=-1) { a[i]=null; i--; } } for (i=0;ilength(S) || length(A[1])>length(S)) return False; return startsWith(S,0,A[0]) && endsWith(substring(S,length(A[0]),length(S)),A[1]); } if (length(A)==3) { if (A[0]=="" && A[1]=="" && A[2]=="") return True; if (A[0]=="" && A[1]!="" && A[2]=="") return strFind(S,A[1])!=-1; } error("strMatch"); } function substring(S,I0,I1) { return S.substring(I0,I1); } function splitTrim(s,chars) { var a=s.split(chars); for (var i=0;i0) res+="|"; res+=display(o[i]); } res+="]"; displaying.pop(); } else { displaying.push(o); res="{"; var first=true; for (var val in o) { if (!first) res+="|"; else first=false; if (val!="parent") { res+=val+"="+display(o[val]); } } res+="}"; displaying.pop(); } return res; } var origin; if (SERVER) origin=global; else origin=window; function basicsInit() { charsInit(); } function fileExists(fname) { if (SERVER) return fs.existsSync(fname); else return httpSend("GET","http://localhost:8080/fileExists?"+fname); } function fileIsDir(fname) { var fd=fs.openSync(fname,"r"); var stats=fs.fstatSync(fd); return stats.isDirectory(); } function fileRead(fname,encoding) { if (SERVER) { if (!fileExists(fname)) error("File not found: "+fname); var RES=fs.readFileSync(fname,"latin1"); RES=Buffer.from(RES,"latin1").toString("utf8"); return RES; } else return httpSend("GET","http://localhost:8080/"+fname); } function filePath(fname) { return path.dirname(fname); } function fileName0(fname) { var a=fname.split("/"); return a[length(a)-1]; } function fileName(fname) { var a=fname.split("."); return fileName0(a[0]); } function fileExt(fname) { var a=fname.split("."); return length(a)>1?a[length(a)-1]:Undefined; } function fileWrite(fname,str) { try { fs.writeFileSync(fname,str,"latin1"); } catch (err) { error("Can't write file: "+fname); } } function fileDelete(fname) { fs.unlinkSync(fname); } function dirCreate(fname) { try { fs.mkdirSync(fname); } catch (err) { error("Can't create directory: "+fname); } } function vfileCreate(fname,isDir,parent,val) { return {"fname":fname,"isDir":isDir,isModified:false,"val":val, "parent":parent}; } _READ={}; function fileReadSet(ext,readFunc) { _READ[ext]=readFunc; } function dirRead(fname,mask) { function fileRead(fname) { var dir=path.dirname(fname)+"/"; fname=path.basename(fname); var val=""; var readFunc=_READ[fileExt(fname)]; if (readFunc!=undefined) { val=readFunc(dir+fname); val=replaceAll(val,"\r\n","\n"); } return val; } var predir=path.dirname(fname)+"/"; fname=path.basename(fname); var d=vfileCreate(fname,true,null,{}); var dir=fname; if (predir!="") dir=predir+"/"+fname; var a=fs.readdirSync(dir); for (var i=0;i') c=">"; if (c=='&') c="&"; res+=c; } return res; } function htmlEscapeBlanks(html) { var res=""; for (var i=0;i "; res+=c; } return res; } var OUTS=Nil; function startOutS() { OUTS=""; } function stopOutS() { OUTS=Nil; } function getOutS() { return OUTS; } function _out0(S) { if (!isString(S)) { console.log("<"+display(S)+">");error("out0"); } if (OUTS==Nil) if (SERVER) process.stdout.write(S); else document.write(S); else OUTS+=S; } function out0(S) { return _out0(S); } function _out(S) { if (SERVER || OUTS!=Nil) out0(S); else out0(htmlEscapeBlanks(htmlEscapeChars(S))); } function out(O) { return _out(O); } function outd(O) { out(display(O)); } function br(s) { out0("
"); } function hr(s) { out0("
"); } function esc(C) { if (SERVER) return "\x1b["+C+"m"; else { C=HTMLColor[C]; return C==undefined?"":""; } } function color(C) { if (C[0]=="b") out0(esc(ColorBright)),C=substring(C,1,length(C)); else out0(esc(ColorNoBright)); out0(esc(C)); } function cr() { out("\n"); } function spc(N,C) { var RES=""; if (isUndefined(C)) C=" "; while (N--) RES+=C; return RES; } function indent(N) { while (N--) out(" "); } var OUTINDENT=0; function outIndent() { indent(OUTINDENT); } function outIndentInc(N) { OUTINDENT+=N; } function crIndent() { cr(); outIndent(); } function consoleInit() { ColorBlue=env("TERM")=="cygwin"?"37":"34"; ColorWhite=env("TERM")=="cygwin"?"34":"37"; } function env(VAR) { if (SERVER) return process.env[VAR]; else return ""; } function argvRemove(argv,j) { splice(argv,j,1,[]); } var fs,path,url; function filesInit() { if (SERVER) { fs=require('fs'); path=require('path'); url=require('url'); } consoleInit(); } function setprop(O,NAME,VAL,E,W,C) { if (!isString(NAME)) error("setprop"); if (isUndefined(E)) E=False; if (isUndefined(W)) W=False; if (isUndefined(C)) C=False; Object.defineProperty(O,NAME,{ "value": VAL, "enumerable": E, "writable": W, "configurable": C }); } var getPrototypeOf=Object.getPrototypeOf || function (O) { return O.__proto__; } var setPrototypeOf=Object.setPrototypeOf || function (O,PROTO) { O.__proto__=PROTO; return O; } function hasPrototype(O,P) { O=getPrototypeOf(O); while (O!=Nil) { if (O==P) return True; O=getPrototypeOf(O); } return False; } var SymbolType=Symbol("type"); function _mtypeOf() { var OBJ=this; if (!isNil(this.typeOf)) OBJ=prototype(this); return OBJ[SymbolType]; } function jsprotoCreate(JSPROTO,TYPE,JSPARENT) { var CLASS=Object; if (!isNil(JSPARENT)) CLASS=JSPARENT.constructor; var RES=JSPROTO; if (JSPROTO==Nil) RES=(class extends CLASS {}).prototype; RES[SymbolType]=TYPE; setprop(RES,"typeOf",_mtypeOf); setprop(RES,"init0",function () {},False,True); return RES; } function jsprotoIsAtom(PROTO) { return PROTO==Symbol.prototype || PROTO==Boolean.prototype || PROTO==Number.prototype || PROTO==String.prototype; } function jsprotoInheritsAtom(PROTO) { if (PROTO==Nil) return False; if (jsprotoIsAtom(PROTO)) return True; return jsprotoInheritsAtom(Object.getPrototypeOf(PROTO)); } function _createObject(CONT) { var RES=new this.JSPROTO.constructor(); if (!isUndefined(CONT)) { if (!isContainer(CONT)) error("_createObject"); CONT.store(RES); } RES.init0(); return RES; } function _consObject(JSPROTO,VAL,CONT) { if (!isUndefined(VAL) && constructor(VAL)!=Object && constructor(VAL)!=Array) { if (!isContainer(VAL) || !isUndefined(CONT)) error("_consObject"); CONT=VAL; VAL=Undefined; } var RES=({ "_":_createObject, "JSPROTO":JSPROTO })._(CONT); RES.init(); if (isDefined(VAL)) Object.assign(RES,VAL); return RES; } function _createAtom(VAL,CONT) { var RES=new this.JSPROTO.constructor(VAL); if (!isUndefined(CONT)) CONT.store(RES); RES.init0(); return RES; } function _consAtom(JSPROTO,VAL,CONT) { var RES=({ "_":_createAtom, "JSPROTO":JSPROTO })._(VAL,CONT); RES.init(); return RES; } var TYPEPROTO=jsprotoCreate(Nil,Nil); setprop(TYPEPROTO,"init0",function () { this.NAME=Nil; this.PARENT=Nil; this.JSPROTO=Nil; this.ATTRS=[]; setprop(this,"create",_createObject,False,True); }); var TYPES={}; var type=function (CONS,O) { var PARENT=Nil; if (!isNil(O)) PARENT=O.PARENT; var JSPARENT=Nil; if (PARENT!=Nil) JSPARENT=PARENT.JSPROTO; var JSPROTO=Nil; if (CONS==type) JSPROTO=TYPEPROTO; else if (!isNil(O) && !isNil(O.JSPROTO)) JSPROTO=O.JSPROTO; if (isNil(CONS) || CONS!=type) JSPROTO=jsprotoCreate(JSPROTO,CONS,JSPARENT); var CREATE=jsprotoInheritsAtom(JSPROTO) ?_createAtom :_createObject; if (isNil(CONS)) { CONS=CREATE==_createObject ?function (VAL,CONT) { return _consObject(JSPROTO,VAL,CONT); } :function (VAL,CONT) { return _consAtom(JSPROTO,VAL,CONT); }; } JSPROTO[SymbolType]=CONS; Object.setPrototypeOf(CONS,TYPEPROTO); CONS.init0(); CONS.PARENT=PARENT; CONS.JSPROTO=JSPROTO; if (!isNil(O)) { if (!isNil(O.NAME)) { CONS.NAME=O.NAME; TYPES[O.NAME]=CONS; } CONS.ATTRS=[]; if (!isArray(O.ATTRS)) { if (!isNil(O.ATTRS)) error("type::ATTRS"); } else if (!empty(O.ATTRS)) CONS.setAttrs(O.ATTRS); for (var N in O) if (N!="NAME" && N!="PARENT" && N!="JSPROTO" && N!="ATTRS") CONS[N]=O[N]; } delete CONS.name; if (!empty(CONS.ATTRS)) { setprop(CONS.JSPROTO,"init0",function () { for (var I=0;I")) this.setv(CONS.ATTRS[I].NAME,VAL0); else if (CONS.ATTRS[I].QUALIF.has("v")) this[CONS.ATTRS[I].NAME]=VAL0; } }, False,True); } setprop(CONS,"create",CREATE,False,True); return CONS; } var obj=type(function (VAL,CONT) { if (!isUndefined(VAL) && constructor(VAL)!=Object) { if (!isContainer(VAL)) { if (constructor(VAL)==Array && isDefined(tree)) return tree(VAL,CONT); } else { if (!isUndefined(CONT)) error("obj.cons"); CONT=VAL; VAL=Undefined; } } var RES=obj.create(CONT); if (isDefined(VAL)) Object.assign(RES,VAL); return RES; }, { "NAME":"obj", "PARENT":Nil, "JSPROTO":Object.prototype, "ATTRS":[] }); type=type(type, { "NAME":"type", "PARENT":obj, "ATTRS":[] }); setprop(type,"create",function () { error("type.create"); }); setprop(type,"getByName",function (NAME) { return TYPES[NAME]; }); function isType(O) { return isa(O,type); } setprop(TYPEPROTO,"setMethod",function (NAME,FUNC) { setprop(this.JSPROTO,NAME,FUNC,False,True); }, False,True); obj.setMethod("init",function () {}); type.setMethod("name",function () { return this.NAME; }); type.setMethod("parent",function () { return this.PARENT; }); type.setMethod("inherits",function (T) { var P=this.parent(); if (!isType(T) || P==Nil) return False; if (P==T) return True; else return P.inherits(T); }); type.setMethod("method",function (NAME) { var FUNC=this.JSPROTO[NAME]; return isFunction(FUNC)?FUNC:Undefined; }); type.setMethod("super",function (NAME) { return this.parent().method(NAME); }); obj.setMethod("super",function (NAME,...PARMS) { var M=typeOf(this).super(NAME); if (isNil(M)) error("super::method "+NAME+" doesn't exists in class "+typeOf(this).parent().name()); return M.apply(this,PARMS); }); var nil=type(function (VAL) { var RES=boxit(VAL); if (!isNil(RES.valueOf())) error("nil"); return RES; }, { "NAME":"nil", "PARENT":obj, "ATTRS":[] }); var BoxedNil=nil.create(),BoxedUndefined=nil.create(); nil.setMethod("valueOf",function () { if (this==BoxedUndefined) return Undefined; if (this==BoxedNil) return Nil; error("nil.valueOf"); }); function isUnboxed(O) { if (O==Nil) return True; return isAtom(O) && O.valueOf()===O; } function isBoxed(O) { if (O==Nil) return False; return !isUnboxed(O); } function boxed(O) { if (O==Nil) return boxit(O); if (isUnboxed(O)) return Object(O); return O; } function boxit(O) { if (isUndefined(O)) return BoxedUndefined; if (eqNil(O)) return BoxedNil; return O; } function typeOf(O) { O=boxit(O); if (!isFunction(O.typeOf)) return obj; return O.typeOf(); } var symb=type(function (S) { return Symbol(S); }, { "NAME":"symb", "PARENT":obj, "JSPROTO":Symbol.prototype, "ATTRS":[] }); var bool=type(function (B,CONT) { return bool.create(B,CONT); }, { "NAME":"bool", "PARENT":obj, "JSPROTO":Boolean.prototype, "ATTRS":[] }); var num=type(function (N,CONT) { return num.create(N,CONT); }, { "NAME":"num", "PARENT":obj, "JSPROTO":Number.prototype, "ATTRS":[] }); var str=type(function (S,CONT) { return str.create(S,CONT); }, { "NAME":"str", "PARENT":obj, "JSPROTO":String.prototype, "ATTRS":[] }); var array=type(function (VAL) { if (isString(VAL)) return explode(VAL); var RES=[]; if (isDefined(VAL)) Object.assign(RES,VAL); return RES; }, { "NAME":"array", "PARENT":obj, "JSPROTO":Array.prototype, "ATTRS":[] }); type.setMethod("isAtom",function (STRICT) { if (STRICT) return this==nil || this==symb || this==bool || this==num || this==str; else return this.isAtom(True) || this.inherits(nil) || this.inherits(symb) || this.inherits(bool) || this.inherits(num) || this.inherits(str); }); type.setMethod("root",function () { if (this.isAtom(True)) return this; else if (this.parent()==Nil) return obj; else return this.parent().root(); }); var set=type(function (VAL) { if (isUndefined(VAL)) VAL=[]; return set.create(implode(VAL)); }, { "NAME":"set", "PARENT":str, "ATTRS":[], "ELEMS":Undefined }); function isSet(O) { return isa(O,set); } set.setMethod("contains",function (ELTS) { if (!isString(ELTS)) error("set.contains"); ; for (var I=0;I", "l" ]); function isQualif(O) { return isa(O,qualif); } var addr=type(function (NAME,TYPE,QUALIF,VAL0) { if (isUndefined(TYPE)) return addr.obj(NAME); else { var RES=addr.create(); RES.init(NAME,TYPE,QUALIF,VAL0); return RES; } }, { "NAME":"addr", "PARENT":obj, "ATTRS":[] }); addr.setMethod("init",function (NAME,TYPE,QUALIF,VAL0) { if (isUndefined(TYPE)) TYPE=str; if (isString(QUALIF)) QUALIF=qualif(QUALIF); else if (!isQualif(QUALIF)) error("addr.init"); Object.assign(this,{ "NAME":NAME, "TYPE":TYPE, "QUALIF":QUALIF, "VAL0":VAL0 }); }); setprop(addr,"obj",function (S) { S=trim(S," ",True,True); var A=splitOnce(S," ",["v",Undefined]); var Q=qualif(A[0]); if (Q.inter("vci")=="") Q=Q.union("v"); A=splitOnce(A[1],"=",[Undefined,Undefined]); var VAL0=eval("(function () { return "+A[1]+"; })()"); A=splitOnce(A[0],":",[Undefined,"obj"]); var NAME=A[0],TYPE=type.getByName(A[1]); return addr(NAME,TYPE,Q,VAL0); }); function isAddr(O) { return isa(O,addr); } type.setMethod("attr",function (NAME) { return find(this.ATTRS,function (O) { return O.NAME==NAME; }); }); addr.setMethod("has",function (Q) { return this.QUALIF.has(Q); }); type.setMethod("attrHas",function (NAME,Q) { var A=this.attr(NAME); return isDefined(A) && A.has(Q); }); type.setMethod("setAttr",function (A) { if (isString(A)) A=addr(A); if (!isAddr(A)) error("setAttr"); if (!isNil(this.attr(A.NAME))) error("type.setAttr"); this.ATTRS.push(A); if (A.QUALIF.has("c")) { setprop(this.JSPROTO,A.NAME,A.VAL0,False,True); } }); type.setMethod("attrs",function () { return this.ATTRS; }); type.setMethod("setAttrs",function (L) { for (var I=0;I"); if (isBoxed(VAL) && MODE=="flat" && !PO) error("delete(!PO)::"+N); if (isBoxed(VAL) && (MODE=="full" || PO)) { VAL.delete(MODE); } } this.addFlags("d"); }); setprop(obj,"delete",function (O,MODE) { if (isUnboxed(O)) O.delete(MODE); }); function tree(A,CONT) { if (isAtom(A) || constructor(A)!=Array && !isTemplate(A) || length(A)<1 || !isType(A[0])) return A; var O=length(A)==1?{}:A[1]; if (constructor(O)!=Object) error("tree(1)"); O.$=[]; for (var I=2;I2) error("tree(2)"); return RES; } function range(O,MIN,MAX,LI,COL) { return { "OBJ":O,"MIN":MIN,"MAX":MAX,"NAT":Nil,"LI":LI,"COL":COL }; } function rangeBOF(RG) { return RG.MIN==0; } function rangeEOF(RG) { return RG.MIN>=length(RG.OBJ); } function rangeValue(RG) { return substring(RG.OBJ,RG.MIN,RG.MAX); } function lexerReadString(S,i,C) { while (i=length(S)) error("lexerReadString"); return i; } function lexerReadUntil(S,i,C1,C2,STRICT) { if (STRICT==Undefined) STRICT=True; while (i=length(S)) error("lexerReadUntil"); return i; } function lexerRead(S,i,N1,N2) { while (i=0 && (S[i]=='\"' || charIs("'",CharNatQuote) && S[i]=='\'')) { i0=i; i=lexerReadString(S,i+1,S[i]); BEG=i0,END=i+1; NAT=TokenNatStr; } else if (TOKENIZECOMMENTS && i>=0 && i+1=0 && i+1=length(S)?0:1); NAT=TokenNatComment; } else { var j; j=lexerRead(S,i,charnat(S[i]),charIs(S[i],CharNatAlf)?CharNatDigit:-1); BEG=i,END=j; NAT=charnatToToknat(charnat(S[i])); } if (BEG==-1) error("lexerNext"); RG.MIN=BEG; RG.MAX=END; } else { RG.MIN=RG.MAX=length(S); } RG.NAT=NAT; RG.LI=LI; RG.COL=COL; ERRLI=LI; ERRCOL=COL; S=rangeValue(RG); if (RG.NAT==TokenNatOpn) { var N=length(S),TOK=S; while (N>0) { TOK=substring(S,0,N); if (TOKOPS[TOK]!=Undefined) break; N--; } if (N>0 && N!=length(S)) RG.MAX=RG.MIN+N; } return NAT; } function lexerStart(TEXT) { ERRLI=ERRCOL=1; var RG=range(TEXT,0,0,1,1); lexerNext(RG); return RG; } function lexerEOF(RG) { return rangeEOF(RG); } function tokenize(SRC,KEEP) { var RES=[], RG=lexerStart(SRC); while (!lexerEOF(RG)) { if (KEEP || RG.NAT!=TokenNatSpc && RG.NAT!=TokenNatComment) RES.push(rangeValue(RG)); lexerNext(RG); } return RES; } var PRIOR={},POSTFIX={},MULTI={}; function tokenizeStart(TOKSPEC) { var T=splitTrim(TOKSPEC," "), P=-1; for (var I in T) { if (T[I]==";;") P--; else { var OP=T[I],POST=False; if (OP[length(OP)-1]=="_") POST=True,OP=substring(OP,0,length(OP)-1); var OP0=OP; if (OP[0]=="_") OP=substring(OP,1,length(OP)); TOKOPS[OP]=1; PRIOR[OP0]=P; POSTFIX[OP0]=POST; } } } function tokenizeInit() {} var _SERIALIZING=[], SERIALIZEINDENT=0, SERIALIZING=False; function prettyBis(O,MODE,SKIN,INDENT) { if (INDENT==Undefined) INDENT=False; var OSERIALIZING=SERIALIZING; if (isFunction(MODE)) return MODE(O); if (isType(O)) return O.NAME; if (isNil(O)) return "Nil"; if (isUndefined(O)) return "Undef"; if (isBoolean(O)) return O?"True":"False"; if (isNumber(O)) return O.toString(); if (isSymbol(O)) return isUndefined(sy(O))?O.toString():sy(O); if (isString(O)) return '"'+strEscape(O)+'"'; if (isFunction(O)) return ''; if (contains(_SERIALIZING,O)) { if (isFunction(O.getId)) return "@"+O.getId(); else return "@^^"; } SERIALIZING=True; var RES=""; if (MODE=="name") { if (isArray(O)) RES="[...]"; else { var NAME=O.NAME; if (!isUndefined(NAME)) RES=NAME; else RES="{...}"; } } else { function incIndent(N) { SERIALIZEINDENT+=N; OUTINDENT=SERIALIZEINDENT; } if (isArray(O)) { _SERIALIZING.push(O); RES+="["; if (INDENT) incIndent(2); for (var I=0;I0) RES+=","; if (INDENT) RES+="\n"+spc(SERIALIZEINDENT); RES+=prettyBis(O[I],MODE,SKIN); } if (INDENT) incIndent(-2); RES+="]"; } else { _SERIALIZING.push(O); var TYPE=typeOf(O),PATTERN=Nil; RES+=(TYPE==obj?"":TYPE.NAME)+"{"; if (SKIN!=Undefined) { var SKINT=SKIN[TYPE.NAME]; if (isUndefined(SKINT)) SKINT=SKIN["*"]; if (SKINT!=Undefined) { if (MODE==Undefined) MODE=SKINT["default"]; if (MODE!=Undefined) PATTERN=SKINT[MODE]; } } if (MODE==Undefined) MODE="short"; if (isNil(PATTERN)) PATTERN={"*":["av","full"]}; if (INDENT) incIndent(2); var FIRST=True; for (var NAME of Object.getOwnPropertyNames(O)) { var MODE2=Undefined; if (PATTERN.hasOwnProperty(NAME)) MODE2=PATTERN[NAME]; if (MODE2==Undefined) MODE2=PATTERN["*"]; if (MODE2!=Undefined && !contains(MODE2[0],"-")) { var SA=contains(MODE2[0],"a"), SV=contains(MODE2[0],"v"); if (!FIRST && (SA||SV)) RES+=","; if (SA||SV) if (INDENT) RES+="\n"+spc(SERIALIZEINDENT); if (SA||SV) FIRST=False; if (SA) RES+=isSymbol(NAME)?pretty(NAME):NAME; if (SA&&SV) RES+="="; if (SV) { var VAL; if (NAME=="caller" || NAME=="callee" || NAME=="arguments") VAL=""; else VAL=O[NAME]; RES+=prettyBis(VAL,MODE2[1],SKIN,contains(MODE2[0],"i")); } } } if (INDENT) incIndent(-2); RES+="}"; } } SERIALIZING=OSERIALIZING; return RES; } function pretty(O,MODE,SKIN) { if (!SERIALIZING) _SERIALIZING=[]; if (isUndefined(MODE)) MODE="short"; var RES=prettyBis(O,MODE,SKIN); if (!SERIALIZING) _SERIALIZING=[]; return RES; } var _SFID,_SFOBJ; function sfid(HIDDEN) { HIDDEN=isDefined(HIDDEN); return (HIDDEN?chr(1):chr(2))+_SFID++; } function sfisHiddenId(ID) { if (!isNumber(ID) && !isString(ID)) outd(ID),cr(),error("sfisHiddenId"); return ID[0]==chr(1); } function sfisLocId(ID) { if (!isNumber(ID) && !isString(ID)) error("sfisLocId"); return ID[0]==chr(1) || ID[0]==chr(2); } function sffetch(ID) { return _SFOBJ[ID]; } function sfstore(O,HIDDEN) { if (isUnboxed(O)) return; var ID=O[SymbolId]; if (isUndefined(ID)) ID=sfid(HIDDEN); else if (isDefined(sffetch(ID))) return; O[SymbolId]=ID; _SFOBJ[ID]=O; } function sfrelease() { for (N in _SFOBJ) { if (sfisLocId(N)) delete sffetch(N)[SymbolId]; } } function sfinit() { _SFID=0; _SFOBJ={}; _SFOUT=[]; _SFDONE={}; _SFFIRST=True; _SFINDENT=0; } var _SFSymbElt="ELT"+chr(2),_SFLANG; function parsefList(L,I,STOP,CONT) { var N=length(L),FOUND=False,TYPE=array,RES=[],VAR=Undefined,ELT=Nil,TYNAME; function omg(S) { if (S=="(") return _SFLANG=="lisp"?S:"{"; if (S==")") return _SFLANG=="lisp"?S:"}"; if (S=="=") return _SFLANG=="lisp"?S:":"; error("omg"); } if (STOP==omg(")")) { TYPE=obj; var OBJ0=False; if (_SFLANG=="json" && L[I]=="{") I++,OBJ0=True; else if (I=N || I+1"+I+" "+L[I]); if (_SFLANG=="json" && !OBJ0) if (L[I]!="{") error("parsefList(2)"); else I++; RES=obj({}); } function parseval(VAR,VAL,TYELT) { if (isUndefined(TYELT)) TYELT=obj; if (!empty(TYPE.attrs())) { var A=TYPE.attr(VAR); if (A!=Nil) TYELT=A.TYPE; } if (TYELT==obj) { if (length(VAL)>=2 && VAL[0]=='"' && VAL[length(VAL)-1]=='"') TYELT=str; else ; } if (TYELT==bool) { if (VAL=="True") VAL=True; else if (VAL=="False") VAL=False; else error("parseval(1)"); } else if (TYELT==num) { ; VAL=JSON.parse(VAL); } else if (TYELT==str) VAL=substring(VAL,1,length(VAL)-1); else ; return VAL; } function pushvar(VAL) { if (isString(VAR)) if (syExists(VAR)) VAR=sy(VAR); if (strIsOmg(VAR)) error("pushvar(0)-->"+VAR); if (strIsOmg(VAL)) error("pushvar(1)-->"+VAL); if (STOP==omg(")")) { if (VAR==_SFSymbElt) ELT.push(VAL); else { var TYELT; if (VAR==sy("+o")) TYELT=num; VAL=parseval(VAR,VAL,TYELT); if (VAR==sy("+o")) RES.setId(VAL); else RES[VAR]=VAL; } VAR=Undefined; } else RES.push(VAL); } var FIRST=True; while (I=N || L[I+1]!=omg("=")) { VAR=_SFSymbElt; if (ELT==Nil) ELT=[]; } else { VAR=L[I]; I+=2; if (I>=N || L[I]==omg(")") || L[I]=="]") error("parsefList::Nothing after '='"); } } if ((_SFLANG=="lisp" && L[I]==omg("(")) || (_SFLANG=="json" && L[I]==omg("(")) || (_SFLANG=="json" && L[I+1]==omg("(")) || L[I]=="[") { var A=parsefList(L,I+(_SFLANG=="lisp"?1:0),L[I]=="["?"]":omg(")"),CONT); pushvar(A[0]); I=A[1]; } else pushvar(L[I]),I++; } } if (ELT!=Nil) VAR="$",pushvar(ELT); if (STOP!=omg(")") && STOP!="]" && FOUND) error("parsefList::STOP(1)"); if ((STOP==omg(")") || STOP=="]") && !FOUND) error("parsefList::STOP(2)<"+STOP+">"); if (TYNAME=="type") RES.PARENT=type.getByName(RES.PARENT),RES=type(Nil,RES); else if (TYPE!=obj && TYPE!=array) { RES=TYPE(RES); if (!isNil(CONT)) CONT.store(RES,RES.getId()); } return [RES,I]; } function parsef(S,CONT,LANG) { if (isString(CONT)) LANG=CONT,CONT=Undefined; if (isUndefined(LANG)) LANG="json"; _SFLANG=LANG; if (LANG!="lisp" && LANG!="json") error("parsef(0)"); parsefStart(); var RES=parsefList(tokenize(S),0,"",CONT)[0]; RES=linkf(RES,CONT); return RES; } function parsefStart() { charsInit(); charnatSet("#",CharNatAlf); charnatSet("+",CharNatAlf); charnatSet("%",CharNatAlf); tokenizeStart("( ) [ ] { } = : ,"); } var _SFOUT,_SFDONE,_SFFIRST,_SFINDENT; function sfout(S) { _SFOUT.push(S); return S; } function sfresult() { return _SFOUT.join(""); } function sfattrs(O) { return RES=Object.getOwnPropertyNames(O).concat(Object.getOwnPropertySymbols(O)).filter(function (X) { return X!=SymbolUp && X!=SymbolCont && X!="TO" }); } function serializefBis(O,MODE,MODES) { var ISFIRST,SKIPSPC,SFSDONE; function sfslot(N,SMODE) { if (isUndefined(SMODE)) SMODE={}; if (O.hasOwnProperty(N) && !(N==sy("+o") && sfisHiddenId(O[N]))) { if (!SKIPSPC) { if (_SFLANG=="json" && !ISFIRST) sfout(","); if (isDefined(SMODE["nl"])) sfout("\n"+spc(_SFINDENT)); else if (_SFLANG=="lisp") sfout(" "); } SKIPSPC=False; sfout(isSymbol(N)?sy(N):N),sfout(_SFLANG=="lisp"?"=":":"); if (N==sy("+o")) sfout(O[N]); else serializefBis(O[N],MODE=="flat"?"symb":MODE,MODES); ISFIRST=False; SFSDONE[N]=1; } } if (isNil(O)) return sfout("Nil"); if (isUndefined(O)) return sfout("Undef"); if (isBoolean(O)) return sfout(O?"True":"False"); if (isNumber(O)) return sfout(O.toString()); if (isString(O)) return sfout('"'+strEscape(O)+'"'); if (isFunction(O)) return sfout(''); if (isArray(O)) { ISFIRST=True; sfout("["); for (var I=0;I<"+ REQ.ADDR.address+":"+REQ.ADDR.port+"::"+ REQ.IDCLI+" "+REQ.PATHNAME+" "+REQ.QUERY+">"); if (REQ.METHOD=="GET") out("[]"); else out(BODY.toString()); cr(),out("["+JSON.stringify(ERR)+"]=> "); if (REQ.METHOD=="GET") out("..."); else out(ANSW); cr(),cr(); } var THIS=this; function handler(REQ,ANSW) { var REQLOG=_REQLOG=req(REQ); _CURSRV=THIS; function ret(ERR,CTYPE,MSG,RES) { if (CTYPE=="application/json") RES=isUndefined(RES)?"null":JSON.stringify(RES); var SRES=RES; if (length(SRES)>80) SRES=substring(RES,0,80)+" ..."; log(REQNO++,REQLOG,MSG,ERR,SRES); ANSW.writeHead(ERR, {'Content-Type': CTYPE, 'Access-Control-Allow-Origin': '*' }); ANSW.write(RES); ANSW.end("\n"); _REQLOG=_CURSRV=Nil; } if (endsWith(REQLOG.PATHNAME,".jix")) { if (REQ.method=="POST") { var MSG="",ISOBJ=false; REQ.on('data', function (DATA) { if (ISOBJ) error("serverCreate(multipart obj)"); else { if (typeOf(DATA)==str) MSG+=DATA; else { MSG=DATA; ISOBJ=true; } } }); REQ.on('end',function () { if (ISOBJ) MSG=JSON.parse(MSG.toString()); var PARMS=JSON.parse(MSG); REQLOG.IDCLI=PARMS[1].shift(); var RES=THIS.call(PARMS[0],PARMS[1]); ret(200,"application/json",MSG,RES); }); } else if (REQ.method=="GET") { ret(200,"application/json","",{ _type:"error", MSG:"RPCs must be POST"}); } } else { fs.readFile(__dirname+'/'+REQLOG.PATHNAME,'utf-8',function(ERR,DATA) { ; if (ERR) { ret(404,"text/html","",'

Page Not Found

'); } else { ret(200,"text/html","",DATA); } }); } } if (!isNil(this._RUNNING)) error("server.start"); var SRV=http.createServer(handler); this._RUNNING=SRV; SRV.on('connection', function (SOCK) { SOCK.unref(); }); SRV.listen(this.PORT,"0.0.0.0"); }); server.setMethod("stop",function () { if (isNil(this._RUNNING)) error("server.stop"); this._RUNNING.close(); this._RUNNING=Nil; }); server.setMethod("attach",function (CONT) { if (!isContainer(CONT) || isDefined(CONT.SRV)) error("server.attach"); this.CONT[CONT.IDCONT]=CONT; CONT.SRV=this; }); server.setMethod("container",function (NAME,FOP) { if (FOP) { var CONT=this.container(NAME); if (isNil(CONT)) { CONT=container(NAME,this); } return CONT; } else return find(this.CONT,function (X) { return isDefined(X) && X.NAME==NAME }); }); function _fetchconts() { return RES=server.currentClient().containers(True).map(function (C) { return {"NAME":C.NAME}; }); } server.setMethod("containers",function (FETCH) { if (FETCH) { if (isLocal(this)) ; else if (isRemote(this)) { var L=this.call("_fetchconts",[]); for (var C of L) { this.container(C.NAME,True); } } else if (isClient(this)) { var SRV=server.getById(this.IDSRVPARENT); for (var C of SRV.containers()) { if (isString(C.NAME) && C.NAME!="") { this.container(C.NAME,True); } } } return this.containers(); } else return this.CONT.filter(function (X) { return isDefined(X); }); }); function _grep(NAME,Q) { var CONT=server.getById(server.currentClient().IDSRVPARENT).container(NAME); if (isNil(CONT)) error("_grep"); return serialize(CONT.query(Q),"flat*"); } function _syncobjs(NAME,S) { var CONT=server.getById(server.currentClient().IDSRVPARENT).container(NAME); if (isNil(CONT)) error("_syncobjs"); parse(S,CONT); return True; } function _srvls() { var T={ "l":"LOC", "r":"REM", "c":"CLI" }; function cls(CONT) { outd(CONT.IDCONT),out(" "); outd(CONT.SRV.IDSRV),out(" ["); outd(CONT.NAME),out(";"); outd(CONT.FNAME),out("]"); } function ls(SRV) { out(T[SRV.CATEG]), out(" "),outd(SRV.IDSRV); out(" "),outd(SRV.IDCLI); out(" "),outd(SRV.HREF); out(" "),outd(SRV.ADDR); out(" "),outd(SRV.PORT); for (var C of SRV.containers()) cr(),out(" "),cls(C); } for (var I in server.SRV) ls(server.SRV[I]),cr(); return Nil; } function serversInit() { if (SERVER) { http=require('http'); APIDefault={ "_connect": _connect, "_close": _close, "_srvls": _srvls, "_fetchconts": _fetchconts, "_grep": _grep, "_syncobjs": _syncobjs }; } } function csvparseObjVals(S) { var A=tokenize(S,True); if (length(A)==0) error("csvparseObjVals"); var EXP="v",I=0,RES=[]; while (I=length(A)) RES.push(""); } } return RES; } function csvparseObj(S,TYPE,SLOT,CONT) { var A=csvparseObjVals(S),O={}; if (length(A)!=length(SLOT)) out(display(A)),cr(),out(display(SLOT)),cr(),error("csvparse"); for (var I=0;I1 && VAL[0]=='"' && VAL[length(VAL)-1]=='"') VAL=trim(VAL,'"',True,True); O[SLOT[I]]=VAL; } return TYPE(O,CONT); } function csvparsef(S,CONT,TYPE) { csvparsefStart(); var A=splitTrim(S,"\n"),RES=[]; if (length(A)==0) error("csvparsef"); var SLOT=splitTrim(A[0],","), TYNAME; for (var I=0;I1) TYNAME=A2[0],SLOT[0]=A2[1]; } SLOT[I]=splitTrim(SLOT[I],":")[0]; } if (isType(CONT)) TYPE=CONT,CONT=Undefined; if (isDefined(TYNAME) && isUndefined(TYPE)) { TYPE=type(Nil,{ NAME:TYNAME, PARENT:obj, ATTRS:SLOT }); } if (isUndefined(TYPE)) error("csvparsef(2)"); for (var I=1;I")) return FULLREF?[UP2[0],UP2[1],UP[1]]:UP2[0]; } if (!FULLREF) return UP[0]; } return UP; }); obj.setMethod("upa",function () { var UP=this.up(True,True); if (isUndefined(UP)) return Undefined; var A=typeOf(UP[0]).attr(UP[1]); return A; }); obj.setMethod("multiA",function () { if (!isArray(this)) return Undefined; var A=this.upa(); return isDefined(A) && A.has("*")?A:Undefined; }); obj.setMethod("setUp",function (NEWUP,NAME) { if (!isUndefined(NEWUP) && !isObject(NEWUP)) error("setUp(1)"); var UP=this.up(True,True); if (!isUndefined(UP)) { if (UP[0][UP[1]]!=this) error("setUp(2)"); UP[0][UP[1]]=Undefined; } if (isUndefined(NEWUP)) this[SymbolUp]=Undefined; else this[SymbolUp]=[NEWUP,NAME]; }); obj.setMethod("detach",function () { this.setUp(Undefined); }); container.setMethod("newId",function () { return this.LASTID++; }); container.setMethod("store",function (O,ID,UP,UPNAME) { if (isUndefined(ID)) ID=this.newId(); O.setId(ID); O.setUp(UP,UPNAME); O.setContainerOf(this); }); obj.setMethod("getv",function (NAME) { return this[NAME]; }); obj.setMethod("setv",function (NAME,VAL) { if (!isString(NAME) && !isNumber(NAME)) error("setv::"+NAME); if (this[NAME]==VAL) return; var A=typeOf(this).attr(NAME), MA=this.multiA(), NUM=isNumStr(NAME), SAMECONT=isUnboxed(VAL) || this.containerOf()==VAL.containerOf(); if (NUM && isDefined(MA) && MA.has(">") || isDefined(A) && A.has(">")) { if (NUM && SAMECONT && isDefined(MA) && MA.has("!") || isDefined(A) && A.has("!")) { if (isBoxed(this[NAME])) this[NAME].detach(); } else { if (isBoxed(VAL) && (!SAMECONT || isDefined(VAL.up()))) VAL=copy(VAL,this.containerOf()); } if (isBoxed(VAL)) VAL.setUp(this,NAME); } this[NAME]=VAL; }); obj.setMethod("remove",function (NAME,N) { function rm(A,I) { var VAL=A[I]; if (isBoxed(VAL) && VAL.up(True)==A) VAL.detach(); } if (isArray(this) && isNumStr(NAME)) { if (isUndefined(N)) N=1; if (NAME<0 || NAME>=length(this) || NAME+N>length(this)) error("obj.remove"); var M=N; while (M--) { rm(this,NAME); this.splice0(NAME,1); } for (var I=NAME;I=length(this) || NAME+N>length(this)) error("obj.cut"); for (var I=NAME;I"); var VAL2=VAL; if (isBoxed(VAL) && MODE=="flat" && !PO) error("copy(!PO)::"+N); if (isBoxed(VAL) && (MODE=="full" || PO)) { VAL2=this.copy(VAL,MODE); if (isArray(VAL2) && PO) { for (var I=0;I"),cr(); out(display([RG.MIN,RG.MAX,RG.NAT,RG.LI,RG.COL])),cr(); lexerNext(RG); } out("---\n"); var A=tokenize(S); out(display(A)),cr(); out("---\n"); A=tokenize(S); var person=type(Nil,{ NAME:"person" }); var O=person({ "NAME":"Smith", "FNAME":"John", "AGE":43 }); out(pretty(O,"short")),cr(); out("---\n"); type(Nil,{ NAME:"range" }); var O=parsef('range{+o:"id1",A:1,B:2,C,D,E,F:{}}{Z:1}'); out(pretty(O)),cr(); out("---\n"); out(serializef(O,[["range","flat", [["ID"],["A"],["B"],["F"],["$",{"nl":1}]] ], ["*","full",[]]],"json")),out("_"),cr(); out("---\n"); out(pretty(O)),cr(); } function test2() { var C=container("C1"), person=type(Nil,{ NAME:"person", ATTRS:["NAME=''","FNAME","AGE=0","> YEARS=[]"] }), P=person({ NAME:"Smith", FNAME:"John", AGE:23, YEARS:[{ A:123 },{ B:456 }] }); out(pretty(P)),cr(); var P2=copy(P,C); out(pretty(P2)),cr(); out(pretty(P2.YEARS[0])),cr(); P2.delete(); out(pretty(P2)),cr(); } var SK={ "song":{ "short":{ "Title":["av",""], "Authors":["av",""], "Year":["av",""] } }, "*":{ "short":{ "+o":["av",""], "NAME":["av",""] } } }; function test3() { var C=container("C1"); C.load("./Songs","db"); SK["song"]["short"][SymbolCont]=["-",""]; SK["song"]["short"][SymbolId]=["av",""]; out(pretty(C.$[0],"short",SK)),cr(); } function test4() { var S=fileRead("./Songs.csv"), C=container("C1"); C.load("./Songs","csv"); out(pretty(C.$[0],"short",SK)),cr(); out("---\n"); var L=C.query({ Year:"*5" }); for (var I=0;I