@ysharp_design/

Lisp-like languages

C#

No description

fork
loading
Files
  • main.cs
  • main.exe

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.19340005399216564","path":"main.cs","file":{"path":"main.cs","content":{"asEncoding":{"base64":"LyoKICAgIExpc3AtbGlrZSBsYW5ndWFnZXMgKCBodHRwczovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9MaXNwXyUyOHByb2dyYW1taW5nX2xhbmd1YWdlJTI5ICkKCiAgICAoVGFrZSAjOSkKCiAgICBDeXJpbCBKYW5kaWEgKDA3LzIwMTYpCgogICAgUHVibGljIERvbWFpbi4KCiAgICBOTyBXQVJSQU5UWSBFWFBSRVNTRUQgT1IgSU1QTElFRC4gVVNFIEFUIFlPVVIgT1dOIFJJU0suCiAqLwp1c2luZyBTeXN0ZW07CnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwp1c2luZyBTeXN0ZW0uTGlucTsKdXNpbmcgU3lzdGVtLlJlZmxlY3Rpb247CnVzaW5nIFN5c3RlbS5UZXh0Owp1c2luZyBTeXN0ZW0uVGV4dC5SZWd1bGFyRXhwcmVzc2lvbnM7CgpuYW1lc3BhY2UgTGlzcExpa2UKewogICAgcHVibGljIGRlbGVnYXRlIG9iamVjdCBQYXJzZXIoc3RyaW5nIGlucHV0KTsKCiAgICBwdWJsaWMgY2xhc3MgVG9rZW4KICAgIHsKICAgICAgICBwdWJsaWMgVG9rZW4oc3RyaW5nIHBhdHRlcm4sIFBhcnNlciBwYXJzZXIpIDogdGhpcyhwYXR0ZXJuLCBwYXJzZXIsIGZhbHNlKSB7IH0KICAgICAgICBwdWJsaWMgVG9rZW4oc3RyaW5nIHBhdHRlcm4sIFBhcnNlciBwYXJzZXIsIGJvb2wgcHJpb3JpdHkpIHsgUmVnZXggPSBuZXcgUmVnZXgoU3RyaW5nLkZvcm1hdCgiXih7MH0pIiwgIVN0cmluZy5Jc051bGxPckVtcHR5KFBhdHRlcm4gPSBw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CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZhciBmb3VuZCA9IG51bGwgYXMgTWF0Y2g7CiAgICAgICAgICAgICAgICB2YXIgdHVwbGUgPSBMZXhpY29uLkZpcnN0T3JEZWZhdWx0KGN1cnJlbnQgPT4gKGZvdW5kID0gY3VycmVudC5JdGVtMi5SZWdleC5NYXRjaChpbnB1dCkpLlN1Y2Nlc3MgJiYgKGZvdW5kLkxlbmd0aCA+IDApKTsKICAgICAgICAgICAgICAgIHZhciB0b2tlbiA9IHR1cGxlICE9IG51bGwgPyB0dXBsZS5JdGVtMiA6IG51bGw7CiAgICAgICAgICAgICAgICB2YXIgbWF0Y2ggPSB0b2tlbiAhPSBudWxsID8gZm91bmQuVmFsdWUgOiBudWxsOwogICAgICAgICAgICAgICAgcmVtYWluaW5nID0gbWF0Y2ggIT0gbnVsbCA/IGlucHV0LlN1YnN0cmluZyhtYXRjaC5MZW5ndGgpIDogaW5wdXQ7CiAgICAgICAgICAgICAgICByZXR1cm4gdG9rZW4gIT0gbnVsbCA/IFR1cGxlLkNyZWF0ZSh0b2tlbiwgbWF0Y2gsIHRva2VuLlZhbHVlT2YobWF0Y2gpKSA6IG51bGw7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSBJRW51bWVyYWJsZTxUdXBsZTxUb2tlbiwgc3RyaW5nLCBvYmplY3Q+PiBGcm9tKHN0cmluZyBpbnB1dCkKICAgICAgICB7CiAgICAgICAgICAgIFR1cGxlPFRva2VuLCBzdHJpbmcsIG9iamVjdD4gaXRlbTsKICAgICAgICAgICAgd2hpbGUgKChpdGVtID0gTmV4dChpbnB1dCwgcmVmIGlucHV0KSkgIT0gbnVsbCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgeWllbGQgcmV0dXJuIGl0ZW07CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHByaXZhdGUgSUVudW1lcmF0b3I8VHVwbGU8VG9rZW4sIHN0cmluZywgb2JqZWN0Pj4gU291cmNlKHN0cmluZyBpbnB1dCkKICAgICAgICB7CiAgICAgICAgICAgIHZhciBzb3VyY2UgPSBGcm9tKGlucHV0KS5XaGVyZSh0dXBsZSA9PiB0dXBsZS5JdGVtMSAhPSBTcGFjaW5nKS5HZXRFbnVtZXJhdG9yKCk7CiAgICAgICAgICAgIGlmIChzb3VyY2UuTW92ZU5leHQoKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIHNvdXJjZTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0aHJvdyBMYW5ndWFnZS5FcnJvcigidW5leHBlY3RlZCBFT0YiKTsKICAgICAgICB9CgogICAgICAgIHByaXZhdGUgb2JqZWN0IFBhcnNlKElFbnVtZXJhdG9yPFR1cGxlPFRva2VuLCBzdHJpbmcsIG9iamVjdD4+IHNvdXJjZSwgYm9vbCByb290KQogICAgICAgIHsKICAgICAgICAgICAgdmFyIHRva2VuID0gc291cmNlLkN1cnJlbnQuSXRlbTE7CiAgICAgICAgICAgIHZhciB2YWx1ZSA9IHNvdXJjZS5DdXJyZW50Lkl0ZW0zOwogICAgICAgICAgICBpZiAodG9rZW4gPT0gT3BlbmluZykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdmFyIGxpc3QgPSBuZXcgTGlzdDxvYmplY3Q+KCk7CiAgICAgICAgICAgICAgICB3aGlsZSAodG9rZW4gIT0gQ2xvc2luZykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZiAoc291cmNlLk1vdmVOZXh0KCkpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoKHRva2VuID0gc291cmNlLkN1cnJlbnQuSXRlbTEpICE9IENsb3NpbmcpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxpc3QuQWRkKFBhcnNlKHNvdXJjZSwgZmFsc2UpKTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB0aHJvdyBMYW5ndWFnZS5FcnJvcigidW5leHBlY3RlZCBFT0YiKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB2YWx1ZSA9IGxpc3QuVG9BcnJheSgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChyb290ICYmIHNvdXJjZS5Nb3ZlTmV4dCgpKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0aHJvdyBMYW5ndWFnZS5FcnJvcigidW5leHBlY3RlZCBcInswfVwiIiwgVG9TdHJpbmcoc291cmNlLkN1cnJlbnQuSXRlbTIpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgICAgfQoKICAgICAgICBwcm90ZWN0ZWQgSUxpc3Q8VHVwbGU8c3RyaW5nLCBUb2tlbj4+IExleGljb24geyBnZXQ7IHByaXZhdGUgc2V0OyB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgc3RyaW5nIEVzY2FwZShzdHJpbmcgcGF0dGVybikKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBFc2NhcGUocGF0dGVybiwgLTEpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBzdHJpbmcgRXNjYXBlKHN0cmluZyBwYXR0ZXJuLCBpbnQgY291bnQpCiAgICAgICAgewogICAgICAgICAgICBjb3VudCA9IGNvdW50IDwgMCA/IHBhdHRlcm4uTGVuZ3RoIDogY291bnQ7CiAgICAgICAgICAgIHJldHVybiBwYXR0ZXJuLkFnZ3JlZ2F0ZShuZXcgU3RyaW5nQnVpbGRlcigpLCAoZXNjYXBlZCwgY2hhcmFjdGVyKSA9PiBlc2NhcGVkLkFwcGVuZEZvcm1hdCgiezB9ezF9IiwgY291bnQtLSA+IDAgPyAiXFwiIDogU3RyaW5nLkVtcHR5LCBjaGFyYWN0ZXIpKS5Ub1N0cmluZygpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBvYmplY3QgSWRlbnRpdHkoc3RyaW5nIGlucHV0KQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGlucHV0OwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBvYmplY3QgU3ltYm9sKHN0cmluZyBpbnB1dCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBuZXcgU3ltYm9sKGlucHV0KTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgVG9rZW4gVG9rZW4oc3RyaW5nIHBhdHRlcm4sIFBhcnNlciBwYXJzZXIpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gVG9rZW4ocGF0dGVybiwgcGFyc2VyLCAtMSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIFRva2VuIFRva2VuKHN0cmluZyBwYXR0ZXJuLCBQYXJzZXIgcGFyc2VyLCBpbnQgZXNjYXBlQ291bnQpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gVG9rZW4ocGF0dGVybiwgcGFyc2VyLCBlc2NhcGVDb3VudCwgZmFsc2UpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBUb2tlbiBUb2tlbihzdHJpbmcgcGF0dGVybiwgUGFyc2VyIHBhcnNlciwgYm9vbCBwcmlvcml0eSkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBUb2tlbihwYXR0ZXJuLCBwYXJzZXIsIC0xLCBwcmlvcml0eSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIFRva2VuIFRva2VuKHN0cmluZyBwYXR0ZXJuLCBQYXJzZXIgcGFyc2VyLCBpbnQgZXNjYXBlQ291bnQsIGJvb2wgcHJpb3JpdHkpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gbmV3IFRva2VuKEVzY2FwZShwYXR0ZXJuLCBlc2NhcGVDb3VudCksIHBhcnNlciwgcHJpb3JpdHkpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBUdXBsZTxzdHJpbmcsIFBhcnNlcj4gTGV4aWNhbChzdHJpbmcgcGF0dGVybiwgUGFyc2VyIHBhcnNlcikKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBUdXBsZS5DcmVhdGUocGF0dGVybiwgcGFyc2VyKTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgVHVwbGU8c3RyaW5nLCBQYXJzZXIsIGJvb2w+IExleGljYWwoc3RyaW5nIHBhdHRlcm4sIFBhcnNlciBwYXJzZXIsIGJvb2wgcHJpb3JpdHkpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gVHVwbGUuQ3JlYXRlKHBhdHRlcm4sIHBhcnNlciwgcHJpb3JpdHkpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBzdHJpbmcgVG9TdHJpbmcob2JqZWN0IGV4cHJlc3Npb24pCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4KICAgICAgICAgICAgICAgIGV4cHJlc3Npb24gaXMgb2JqZWN0W10gPwogICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICgob2JqZWN0W10pZXhwcmVzc2lvbikuTGVuZ3RoID4gMCA/CiAgICAgICAgICAgICAgICAgICAgKChvYmplY3RbXSlleHByZXNzaW9uKS5BZ2dyZWdhdGUobmV3IFN0cmluZ0J1aWxkZXIoIigiKSwgKHJlc3VsdCwgb2JqKSA9PiByZXN1bHQuQXBwZW5kRm9ybWF0KCIgezB9IiwgVG9TdHJpbmcob2JqKSkpLkFwcGVuZCgiICkiKS5Ub1N0cmluZygpCiAgICAgICAgICAgICAgICAgICAgOgogICAgICAgICAgICAgICAgICAgICIoICkiCiAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICA6CiAgICAgICAgICAgICAgICAoKGV4cHJlc3Npb24gIT0gbnVsbCA/IGV4cHJlc3Npb24uVG9TdHJpbmcoKSA6IG51bGwpID8/ICIobnVsbCkiKS5SZXBsYWNlKCJcclxuIiwgIlxuIikuUmVwbGFjZSgiXFxcbiIsICJcbiIpLlJlcGxhY2UoIlxcdCIsICJcdCIpLlJlcGxhY2UoIlxcbiIsICJcbiIpLlJlcGxhY2UoIlxcciIsICJcciIpLlJlcGxhY2UoIlxcXCIiLCAiXCIiKTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBTeW50YXgoKQogICAgICAgIHsKICAgICAgICAgICAgTGV4aWNvbiA9IG5ldyBMaXN0PFR1cGxlPHN0cmluZywgVG9rZW4+PigpOwogICAgICAgICAgICBJbmNsdWRlKFNwYWNpbmcsIE9wZW5pbmcsIENsb3NpbmcpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIFN5bnRheCBJbmNsdWRlKFRva2VuIHRva2VuKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKExleGljb24uRmlyc3RPckRlZmF1bHQodHVwbGUgPT4gdHVwbGUuSXRlbTEgPT0gdG9rZW4uUGF0dGVybikgPT0gbnVsbCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKHRva2VuLlByaW9yaXR5KQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIExleGljb24uSW5zZXJ0KDAsIFR1cGxlLkNyZWF0ZSh0b2tlbi5QYXR0ZXJuLCB0b2tlbikpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIExleGljb24uQWRkKFR1cGxlLkNyZWF0ZSh0b2tlbi5QYXR0ZXJuLCB0b2tlbikpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIFN5bnRheCBJbmNsdWRlKHBhcmFtcyBUb2tlbltdIHRva2VucykKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiB0b2tlbnMuQWdncmVnYXRlKHRoaXMsIChzeW50YXgsIHRva2VuKSA9PiBzeW50YXguSW5jbHVkZSh0b2tlbikpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIFN5bnRheCBJbmNsdWRlKHBhcmFtcyBUdXBsZTxzdHJpbmcsIFBhcnNlcj5bXSBlbnRyaWVzKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIEluY2x1ZGUoZW50cmllcy5TZWxlY3QoZW50cnkgPT4gbmV3IFRva2VuKGVudHJ5Lkl0ZW0xLCBlbnRyeS5JdGVtMikpLlRvQXJyYXkoKSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgU3ludGF4IEluY2x1ZGUocGFyYW1zIFR1cGxlPHN0cmluZywgUGFyc2VyLCBib29sPltdIGVudHJpZXMpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gSW5jbHVkZShlbnRyaWVzLlNlbGVjdChlbnRyeSA9PiBuZXcgVG9rZW4oZW50cnkuSXRlbTEsIGVudHJ5Lkl0ZW0yLCBlbnRyeS5JdGVtMykpLlRvQXJyYXkoKSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgb2JqZWN0IFBhcnNlKHN0cmluZyBpbnB1dCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBQYXJzZShTb3VyY2UoaW5wdXQpLCB0cnVlKTsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIGNsYXNzIFN5bWJvbAogICAgewogICAgICAgIHB1YmxpYyBTeW1ib2wob2JqZWN0IGlkKSB7IElkID0gTGFuZ3VhZ2UuQ29uc3QoaWQpOyB9CiAgICAgICAgcHVibGljIG92ZXJyaWRlIHN0cmluZyBUb1N0cmluZygpIHsgcmV0dX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gICAgICAgICAgICAgICAgICAgICB2YXIgYXR0cmlidXRlID0gbWVtYmVyLkdldEN1c3RvbUF0dHJpYnV0ZTxCdWlsdGluQXR0cmlidXRlPihmYWxzZSk7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0cmluZyBpZDsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKChhdHRyaWJ1dGUgIT0gbnVsbCkgJiYgIVN0cmluZy5Jc051bGxPckVtcHR5KGlkID0gYXR0cmlidXRlLklkKSkKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgdmFyIG1ldGhvZCA9IG1lbWJlciBhcyBNZXRob2RJbmZvOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdmFyIGZpZWxkID0gbWVtYmVyIGFzIEZpZWxkSW5mbzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9iamVjdCBidWlsdGluOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG1ldGhvZCAhPSBudWxsKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZhciBzaWduYXR1cmUgPSBHZXRTaWduYXR1cmUobWV0aG9kKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2YXIgZGVsZWdhdGVUeXBlID0gR2V0RGVsZWdhdGVUeXBlKHNpZ25hdHVyZSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdmFyIGJ1aWx0aW5UeXBlID0gR2V0QnVpbHRpblR5cGUoc2lnbmF0dXJlKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2YXIgQGRlbGVnYXRlID0gRGVsZWdhdGUuQ3JlYXRlRGVsZWdhdGUoZGVsZWdhdGVUeXBlLCB0aGlzLCBtZXRob2QpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJ1aWx0aW4gPSBBY3RpdmF0b3IuQ3JlYXRlSW5zdGFuY2UoYnVpbHRpblR5cGUsIGlkLCBAZGVsZWdhdGUpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChmaWVsZC5Jc0luaXRPbmx5KQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYnVpbHRpbiA9IGZpZWxkLkdldFZhbHVlKHRoaXMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB0aHJvdyBFcnJvcigiZmllbGQgXCJ7MH1cIiBtdXN0IGJlIHJlYWQtb25seSIsIGZpZWxkLk5hbWUpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIEFkZEJ1aWx0aW4ocmVmbGVjdGVkLCBpZCwgYnVpbHRpbik7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHJlZmxlY3RlZDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICApOwogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSBvYmplY3QgRXZhbHVhdGUoc3RyaW5nIGlucHV0LCBFbnZpcm9ubWVudCBlbnZpcm9ubWVudCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBFdmFsdWF0ZShTeW50YXguUGFyc2UoaW5wdXQpLCBlbnZpcm9ubWVudCk7CiAgICAgICAgfQoKICAgICAgICBwcml2YXRlIG9iamVjdCBFdmFsdWF0ZShvYmplY3QgZXhwcmVzc2lvbiwgRW52aXJvbm1lbnQgZW52aXJvbm1lbnQpCiAgICAgICAgewogICAgICAgICAgICB2YXIgbGlzdCA9IGV4cHJlc3Npb24gYXMgb2JqZWN0W107CiAgICAgICAgICAgIHZhciBzeW1ib2wgPSBTeW1ib2woZXhwcmVzc2lvbik7CiAgICAgICAgICAgIGlmIChlbnZpcm9ubWVudCA9PSBudWxsKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXR1cm4gRXZhbHVhdGUoZXhwcmVzc2lvbiwgbmV3IEVudmlyb25tZW50KHRoaXMsIEJ1aWx0aW5zKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiAobGlzdCAhPSBudWxsKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBvYmplY3QgaWQ7CiAgICAgICAgICAgICAgICBpZiAoKGxpc3QuTGVuZ3RoID4gMCkgJiYgKChzeW1ib2wgPSBTeW1ib2wobGlzdFswXSkpICE9IG51bGwpICYmIEtleXdvcmRzLkNvbnRhaW5zS2V5KGlkID0gc3ltYm9sLklkKSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gS2V5d29yZHNbaWRdLlNlbWFudGljKGxpc3QsIGVudmlyb25tZW50KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnQgbGVuZ3RoOwogICAgICAgICAgICAgICAgICAgIGxpc3QgPSBsaXN0LlNlbGVjdChleHByID0+IEV2YWx1YXRlKGV4cHIsIGVudmlyb25tZW50KSkuVG9BcnJheSgpOwogICAgICAgICAgICAgICAgICAgIGlmICgoKGxlbmd0aCA9IGxpc3QuTGVuZ3RoKSA+IDApICYmICgoc3ltYm9sID0gU3ltYm9sKGxpc3RbMF0pKSAhPSBudWxsKSAmJiAoc3ltYm9sIGlzIENhbGxhYmxlKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHZhciBjYWxsYWJsZSA9IChDYWxsYWJsZSlzeW1ib2w7CiAgICAgICAgICAgICAgICAgICAgICAgIHZhciB2YXJpYWRpYyA9IGNhbGxhYmxlLlZhcmlhZGljOwogICAgICAgICAgICAgICAgICAgICAgICB2YXIgYXJndW1lbnRzID0gbmV3IG9iamVjdFt2YXJpYWRpYyA/IGxlbmd0aCAtIDEgOiBsZW5ndGhdOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAobGVuZ3RoID4gMSkKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgQXJyYXkuQ29weShsaXN0LCAxLCBhcmd1bWVudHMsIHZhcmlhZGljID8gMCA6IDEsIGxlbmd0aCAtIDEpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICh2YXJpYWRpYykKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGNhbGxhYmxlLkludm9rZShlbnZpcm9ubWVudCwgYXJndW1lbnRzKTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFyZ3VtZW50c1swXSA9IGVudmlyb25tZW50OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGNhbGxhYmxlLkludm9rZShhcmd1bWVudHMpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBsaXN0OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChzeW1ib2wgIT0gbnVsbCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIGVudmlyb25tZW50LkdldChzeW1ib2wpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIGV4cHJlc3Npb247CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHByb3RlY3RlZCBTeW1ib2wgU3ltYm9sKG9iamVjdCB2YWx1ZSkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiB2YWx1ZSBhcyBTeW1ib2w7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIG9iamVjdCBDb25zdCgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gQ29uc3QobnVsbCk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIG9iamVjdCBDb25zdChvYmplY3QgaWQpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gaWQgPz8gR3VpZC5OZXdHdWlkKCkuVG9TdHJpbmcoIlAiKTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgU3ltYm9sIE5ldyhvYmplY3QgaWQpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gbmV3IFN5bWJvbChpZCk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIEtleVZhbHVlUGFpcjxvYmplY3QsIEtleXdvcmQ+IEtleXdvcmQoc3RyaW5nIGlkLCBTZW1hbnRpYyBzZW1hbnRpYykKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBuZXcgS2V5VmFsdWVQYWlyPG9iamVjdCwgS2V5d29yZD4oaWQsIG5ldyBLZXl3b3JkKGlkLCBzZW1hbnRpYykpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBLZXlWYWx1ZVBhaXI8b2JqZWN0LCBvYmplY3Q+IEJ1aWx0aW4oc3RyaW5nIGlkLCBvYmplY3Qgd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ZnVuY3Rpb24pKTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgRXhjZXB0aW9uIEVycm9yKHN0cmluZyBtZXNzYWdlLCBwYXJhbXMgb2JqZWN0W10gYXJndW1lbnRzKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIG5ldyBFeGNlcHRpb24oU3RyaW5nLkZvcm1hdChtZXNzYWdlLCBhcmd1bWVudHMpKTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBMYW5ndWFnZSgpCiAgICAgICAgICAgIDogdGhpcyhudWxsKQogICAgICAgIHsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBMYW5ndWFnZShTeW50YXggc3ludGF4KQogICAgICAgIHsKICAgICAgICAgICAgU3ludGF4ID0gc3ludGF4ID8/IG5ldyBTeW50YXgoKTsKICAgICAgICAgICAgS2V5d29yZHMgPSBuZXcgRGljdGlvbmFyeTxvYmplY3QsIEtleXdvcmQ+KCk7CiAgICAgICAgICAgIEJ1aWx0aW5zID0gbmV3IERpY3Rpb25hcnk8b2JqZWN0LCBvYmplY3Q+KEdldFJlZmxlY3RlZEJ1aWx0aW5zKCkpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBDb25zdChvYmplY3RbXSBsaXN0LCBFbnZpcm9ubWVudCBvdXRlcikKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBDb25zdCgpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBOZXcob2JqZWN0W10gbGlzdCwgRW52aXJvbm1lbnQgb3V0ZXIpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gTmV3KEV2YWx1YXRlKGxpc3RbMV0sIG91dGVyKSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgb2JqZWN0IFF1b3RlKG9iamVjdFtdIGxpc3QsIEVudmlyb25tZW50IG91dGVyKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGxpc3RbMV07CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgb2JqZWN0IExpc3Qob2JqZWN0W10gbGlzdCwgRW52aXJvbm1lbnQgb3V0ZXIpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gbGlzdC5Ta2lwKDEpLlNlbGVjdChpdGVtID0+IEV2YWx1YXRlKGl0ZW0sIG91dGVyKSkuVG9BcnJheSgpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBBcHBseShvYmplY3RbXSBsaXN0LCBFbnZpcm9ubWVudCBvdXRlcikKICAgICAgICB7CiAgICAgICAgICAgIHZhciBhcmd1bWVudHMgPSBFdmFsdWF0ZShsaXN0WzJdLCBvdXRlcik7CiAgICAgICAgICAgIGFyZ3VtZW50cyA9ICEoYXJndW1lbnRzIGlzIG9iamVjdFtdKSA/IG5ld1tdIHsgYXJndW1lbnRzIH0gOiAob2JqZWN0W10pYXJndW1lbnRzOwogICAgICAgICAgICByZXR1cm4gRXZhbHVhdGUobmV3W10geyBsaXN0WzFdIH0uQ29uY2F0KChvYmplY3RbXSlhcmd1bWVudHMpLlRvQXJyYXkoKSwgb3V0ZXIpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBMYW1iZGEob2JqZWN0W10gbGlzdCwgRW52aXJvbm1lbnQgb3V0ZXIpCiAgICAgICAgewogICAgICAgICAgICB2YXIgZGlzcGxheSA9IChvYmplY3RbXSlsaXN0WzFdOwogICAgICAgICAgICB2YXIgZm9ybWFscyA9IG5ldyBTeW1ib2xbZGlzcGxheS5MZW5ndGhdOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMDsgaSA8IGZvcm1hbHMuTGVuZ3RoOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZvcm1hbHNbaV0gPSBTeW1ib2woZGlzcGxheVtpXSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIEB0aGlzID0gbnVsbCBhcyBMYW1iZGE7CiAgICAgICAgICAgIEB0aGlzID0KICAgICAgICAgICAgICAgIG5ldyBMYW1iZGEKICAgICAgICAgICAgICAgICgKICAgICAgICAgICAgICAgICAgICBmb3JtYWxzLAogICAgICAgICAgICAgICAgICAgIGRlbGVnYXRlIChvYmplY3RbXSBAcGFyYW1zKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgdmFyIGVudmlyb25tZW50ID0gbmV3IEVudmlyb25tZW50KHRoaXMsIG91dGVyKTsKICAgICAgICAgICAgICAgICAgICAgICAgdmFyIGFjdHVhbCA9IEBwYXJhbXMuTGVuZ3RoOwogICAgICAgICAgICAgICAgICAgICAgICB2YXIgbGVuZ3RoID0gZm9ybWFscy5MZW5ndGg7CiAgICAgICAgICAgICAgICAgICAgICAgIHZhciBpID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGkgPCBsZW5ndGgpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZhciBmb3JtYWwgPSBmb3JtYWxzW2krK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaSA8IGFjdHVhbCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbnZpcm9ubWVudFtmb3JtYWwuSWRdID0gQHBhcmFtc1tpXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbnZpcm9ubWVudFsidGhpcyJdID0gZW52aXJvbm1lbnRbQHRoaXMuSWRdID0gQHRoaXM7CiAgICAgICAgICAgICAgICAgICAgICAgIGVudmlyb25tZW50WyJwYXJhbXMiXSA9IEBwYXJhbXMuU2tpcCgxKS5Ub0FycmF5KCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBFdmFsdWF0ZShsaXN0WzJdLCBlbnZpcm9ubWVudCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgKTsKICAgICAgICAgICAgcmV0dXJuIEB0aGlzOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBMZXQob2JqZWN0W10gbGlzdCwgRW52aXJvbm1lbnQgb3V0ZXIpCiAgICAgICAgewogICAgICAgICAgICB2YXIgZW52aXJvbm1lbnQgPSBuZXcgRW52aXJvbm1lbnQodGhpcywgb3V0ZXIpOwogICAgICAgICAgICB2YXIgYXNzaWdubWVudHMgPSAob2JqZWN0W10pbGlzdFsxXTsKICAgICAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBhc3NpZ25tZW50cy5MZW5ndGg7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdmFyIHBhaXIgPSAob2JqZWN0W10pYXNzaWdubWVudHNbaV07CiAgICAgICAgICAgICAgICBlbnZpcm9ubWVudFtTeW1ib2wocGFpclswXSkuSWRdID0gRXZhbHVhdGUocGFpclsxXSwgb3V0ZXIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBFdmFsdWF0ZShsaXN0WzJdLCBlbnZpcm9ubWVudCk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgb2JqZWN0IENvYWxlc2NlKG9iamVjdFtdIGxpc3QsIEVudmlyb25tZW50IG91dGVyKQogICAgICAgIHsKICAgICAgICAgICAgdGVzdCA9IHRlc3QgPz8gS2V5d29yZHMuVmFsdWVzLkZpcnN0KGtleXdvcmQgPT4ga2V5d29yZC5TZW1hbnRpYyA9PSBUZXN0KTsKICAgICAgICAgICAgcmV0dXJuIFRlc3QobmV3W10geyB0ZXN0LCBuZXdbXSB7IHRlc3QsIGxpc3RbMV0gfSwgbGlzdFsxXSwgbGlzdFsyXSB9LCBvdXRlcik7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgb2JqZWN0IFRlc3Qob2JqZWN0W10gbGlzdCwgRW52aXJvbm1lbnQgb3V0ZXIpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4KICAgICAgICAgICAgICAgIGxpc3QuTGVuZ3RoID4gMiA/CiAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgKGJvb2wpRXZhbHVhdGUobGlzdFsxXSwgb3V0ZXIpID8gRXZhbHVhdGUobGlzdFsyXSwgb3V0ZXIpIDogRXZhbHVhdGUobGlzdFszXSwgb3V0ZXIpCiAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICA6CiAgICAgICAgICAgICAgICBvdXRlci5IYXMobGlzdFsxXSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgTGFuZ3VhZ2UgSW5jbHVkZShLZXlWYWx1ZVBhaXI8b2JqZWN0LCBLZXl3b3JkPiBrZXl3b3JkRGVmaW5pdGlvbikKICAgICAgICB7CiAgICAgICAgICAgIEtleXdvcmRzLkFkZChrZXl3b3JkRGVmaW5pdGlvbi5LZXksIGtleXdvcmREZWZpbml0aW9uLlZhbHVlKTsKICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgTGFuZ3VhZ2UgSW5jbHVkZShwYXJhbXMgS2V5VmFsdWVQYWlyPG9iamVjdCwgS2V5d29yZD5bXSBrZXl3b3JkRGVmaW5pdGlvbnMpCiAgICAgICAgewogICAgICAgICAgICBmb3JlYWNoICh2YXIga2V5d29yZERlZmluaXRpb24gaW4ga2V5d29yZERlZmluaXRpb25zKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBJbmNsdWRlKGtleXdvcmREZWZpbml0aW9uKTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBMYW5ndWFnZSBJbmNsdWRlKEtleVZhbHVlUGFpcjxvYmplY3QsIG9iamVjdD4gYnVpbHRpbkRlZmluaXRpb24pCiAgICAgICAgewogICAgICAgICAgICBBZGRCdWlsdGluKEJ1aWx0aW5zLCBidWlsdGluRGVmaW5pdGlvbi5LZXksIGJ1aWx0aW5EZWZpbml0aW9uLlZhbHVlKTsKICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgTGFuZ3VhZ2UgSW5jbHVkZShwYXJhbXMgS2V5VmFsdWVQYWlyPG9iamVjdCwgb2JqZWN0PltdIGJ1aWx0aW5EZWZpbml0aW9ucykKICAgICAgICB7CiAgICAgICAgICAgIGZvcmVhY2ggKHZhciBidWlsdGluRGVmaW5pdGlvbiBpbiBidWlsdGluRGVmaW5pdGlvbnMpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIEluY2x1ZGUoYnVpbHRpbkRlZmluaXRpb24pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBFdmFsdWF0ZShzdHJpbmcgaW5wdXQpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gRXZhbHVhdGUoaW5wdXQsIG51bGwgYXMgb2JqZWN0W10pOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIG9iamVjdCBFdmFsdWF0ZShzdHJpbmcgaW5wdXQsIHBhcmFtcyBvYmplY3RbXSBhcmd1bWVudHMpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gRXZhbHVhdGUoKGFyZ3VtZW50cyAhPSBudWxsKSAmJiAoYXJndW1lbnRzLkxlbmd0aCA+IDApID8gU3RyaW5nLkZvcm1hdChpbnB1dCwgYXJndW1lbnRzKSA6IGlucHV0LCBudWxsIGFzIEVudmlyb25tZW50KTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBvYmplY3QgRXZhbHVhdGUob2JqZWN0IGV4cHJlc3Npb24pCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gRXZhbHVhdGUoZXhwcmVzc2lvbiwgbnVsbCk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgU3ludGF4IFN5bnRheCB7IGdldDsgcHJpdmF0ZSBzZXQ7IH0KCiAgICAgICAgcHVibGljIElEaWN0aW9uYXJ5PG9iamVjdCwgS2V5d29yZD4gS2V5d29yZHMgeyBnZXQ7IHByaXZhdGUgc2V0OyB9CgogICAgICAgIHB1YmxpYyBJRGljdGlvbmFyeTxvYmplY3QsIG9iamVjdD4gQnVpbHRpbnMgeyBnZXQ7IHByaXZhdGUgc2V0OyB9CiAgICB9CgogICAgcHVibGljIGNsYXNzIE15TGFuZ3VhZ2UgOiBMYW5ndWFnZQogICAgewogICAgICAgIHByaXZhdGUgY2xhc3MgRXF1YWxpdHlDb21wYXJlciA6IElFcXVhbGl0eUNvbXBhcmVyPG9iamVjdD4KICAgICAgICB7CiAgICAgICAgICAgIGludGVybmFsIGJvb2wgRXF1YWwob2JqZWN0IHgsIG9iamVjdCB5KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXR1cm4KICAgICAgICAgICAgICAgICAgICBFcXVhbHMoeCwgeSkgfHwKICAgICAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgICAgICh4ICE9IG51bGwpICYmCiAgICAgICAgICAgICAgICAgICAgICAgICh5ICE9IG51bGwpICYmCiAgICAgICAgICAgICAgICAgICAgICAgICgKICAgICAgICAgICAgICAgICAgICAgICAgICAgICh4IGlzIG9iamVjdFtdKSAmJgogICAgICAgICAgICAgICAgICAgICAgICAgICAgKHkgaXMgb2JqZWN0W10pICYmCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAoKG9iamVjdFtdKXgpLlNlcXVlbmNlRXF1YWwoKG9iamVjdFtdKXksIHRoaXMpCiAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgICB9CgogICAgICAgICAgICBib29sIElFcXVhbGl0eUNvbXBhcmVyPG9iamVjdD4uRXF1YWxzKG9iamVjdCB4LCBvYmplY3QgeSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIEVxdWFsKHgsIHkpOwogICAgICAgICAgICB9CgogICAgICAgICAgICBpbnQgSUVxdWFsaXR5Q29tcGFyZXI8b2JqZWN0Pi5HZXRIYXNoQ29kZShvYmplY3Qgb2JqKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXR1cm4gb2JqICE9IG51bGwgPyBvYmouR2V0SGFzaENvZGUoKSA6IDA7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHByaXZhdGUgc3RhdGljIHJlYWRvbmx5IEVxdWFsaXR5Q29tcGFyZXIgRXF1YWxpdHkgPSBuZXcgRXF1YWxpdHlDb21wYXJlcigpOwoKICAgICAgICBbQnVpbHRpbigidm9pZCIpXQogICAgICAgIHB1YmxpYyByZWFkb25seSBUeXBlIEB2b2lkID0gdHlwZW9mKHZvaWQpOwoKICAgICAgICBbQnVpbHRpbigibnVsbCIpXQogICAgICAgIHB1YmxpYyByZWFkb25seSBvYmplY3QgQG51bGwgPSBudWxsOwoKICAgICAgICBbQnVpbHRpbigiZmFsc2UiKV0KICAgICAgICBwdWJsaWMgcmVhZG9ubHkgYm9vbCBAZmFsc2UgPSBmYWxzZTsKCiAgICAgICAgW0J1aWx0aW4oInRydWUiKV0KICAgICAgICBwdWJsaWMgcmVhZG9ubHkgYm9vbCBAdHJ1ZSA9IHRydWU7CgogICAgICAgIFtCdWlsdGluKCJ0eXBlb2YiKV0KICAgICAgICBwdWJsaWMgVHlwZSBAdHlwZW9mKEVudmlyb25tZW50IGVudmlyb25tZW50LCBvYmplY3QgdmFsdWUpID0+IHZhbHVlICE9IG51bGwgPyB2YWx1ZS5HZXRUeXBlKCkgOiBAdm9pZDsKCiAgICAgICAgW0J1aWx0aW4oInN0cmluZyIpXQogICAgICAgIHB1YmxpYyBzdHJpbmcgQHN0cmluZyhFbnZpcm9ubWVudCBlbnZpcm9ubWVudCwgb2JqZWN0IHZhbHVlKSA9PiBTeW50YXguVG9TdHJpbmcodmFsdWUpOwoKICAgICAgICBbQnVpbHRpbigiZm9ybWF0IildCiAgICAgICAgcHVibGlj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AgICAgICAgICAgKG9iamVjdClTdHJpbmcuQ29uY2F0KEBwYXJhbXMpCiAgICAgICAgICAgIDoKICAgICAgICAgICAgKGludClAcGFyYW1zWzBdICsgKGludClAcGFyYW1zWzFdOwoKICAgICAgICBbQnVpbHRpbigiLSIpXQogICAgICAgIHB1YmxpYyBpbnQgc3VidHJhY3QoRW52aXJvbm1lbnQgZW52aXJvbm1lbnQsIGludCBsZWZ0LCBpbnQgcmlnaHQpID0+IGxlZnQgLSByaWdodDsKCiAgICAgICAgW0J1aWx0aW4oIioiKV0KICAgICAgICBwdWJsaWMgaW50IG11bHRpcGx5KEVudmlyb25tZW50IGVudmlyb25tZW50LCBpbnQgbGVmdCwgaW50IHJpZ2h0KSA9PiBsZWZ0ICogcmlnaHQ7CgogICAgICAgIFtCdWlsdGluKCIvIildCiAgICAgICAgcHVibGljIGludCBkaXZpZGUoRW52aXJvbm1lbnQgZW52aXJvbm1lbnQsIGludCBsZWZ0LCBpbnQgcmlnaHQpID0+IGxlZnQgLyByaWdodDsKCiAgICAgICAgW0J1aWx0aW4oIiUiKV0KICAgICAgICBwdWJsaWMgaW50IG1vZHVsbyhFbnZpcm9ubWVudCBlbnZpcm9ubWVudCwgaW50IGxlZnQsIGludCByaWdodCkgPT4gbGVmdCAlIHJpZ2h0OwogICAgfQoKICAgIGNsYXNzIFByb2dyYW0KICAgIHsKICAgICAgICBzdGF0aWMgdm9pZCBNYWluKHN0cmluZ1tdIGFyZ3MpCiAgICAgICAgewogICAgICAgICAgICB2YXIgbGFuZ3VhZ2UgPSBuZXcgTXlMYW5ndWFnZSgpOwoKICAgICAgICAgICAgbGFuZ3VhZ2UuCiAgICAgICAgICAgICAgICBTeW50YXguCiAgICAgICAgICAgICAgICBJbmNsdWRlCiAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LlRva2VuKCJjb25zdCIsIFN5bnRheC5TeW1ib2wsIDApLAogICAgICAgICAgICAgICAgICAgIFN5bnRheC5Ub2tlbigibmV3IiwgU3ludGF4LlN5bWJvbCwgMCksCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LlRva2VuKCInIiwgU3ludGF4LlN5bWJvbCksCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LlRva2VuKCI6IiwgU3ludGF4LlN5bWJvbCksCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LlRva2VuKCIuIiwgU3ludGF4LlN5bWJvbCksCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LlRva2VuKCI9PiIsIFN5bnRheC5TeW1ib2wpLAogICAgICAgICAgICAgICAgICAgIFN5bnRheC5Ub2tlbigiPSIsIFN5bnRheC5TeW1ib2wpLAogICAgICAgICAgICAgICAgICAgIFN5bnRheC5Ub2tlbigiPz8iLCBTeW50YXguU3ltYm9sKSwKICAgICAgICAgICAgICAgICAgICBTeW50YXguVG9rZW4oIj8iLCBTeW50YXguU3ltYm9sKSwKCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LlRva2VuKCI9PSIsIFN5bnRheC5TeW1ib2wsIHRydWUpLAogICAgICAgICAgICAgICAgICAgIFN5bnRheC5Ub2tlbigiIT0iLCBTeW50YXguU3ltYm9sKQogICAgICAgICAgICAgICAgKS4KICAgICAgICAgICAgICAgIEluY2x1ZGUKICAgICAgICAgICAgICAgICgKICAgICAgICAgICAgICAgICAgICBTeW50YXguTGV4aWNhbCgiZmFsc2UiLCBTeW50YXguU3ltYm9sKSwKICAgICAgICAgICAgICAgICAgICBTeW50YXguTGV4aWNhbCgidHJ1ZSIsIFN5bnRheC5TeW1ib2wpLAogICAgICAgICAgICAgICAgICAgIFN5bnRheC5MZXhpY2FsKCJudWxsIiwgU3ludGF4LlN5bWJvbCksCgogICAgICAgICAgICAgICAgICAgIFN5bnRheC5MZXhpY2FsKCJbMC05XSsiLCBzID0+IGludC5QYXJzZShzKSksCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LkxleGljYWwoIlxcXCIoXFxcXFxcbnxcXFxcdHxcXFxcbnxcXFxccnxcXFxcXFxcInxbXlxcXCJdKSpcXFwiIiwgcyA9PiBzLlN1YnN0cmluZygxLCBzLkxlbmd0aCAtIDIpKSwKCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LkxleGljYWwoIltcXCRfQS1aYS16XVtcXC5cXCRfMC05QS1aYS16XFwtXFwhXSoiLCBTeW50YXguU3ltYm9sKSwKCiAgICAgICAgICAgICAgICAgICAgU3ludGF4LkxleGljYWwoIlxcJlxcJiIsIFN5bnRheC5TeW1ib2wpLAogICAgICAgICAgICAgICAgICAgIFN5bnRheC5MZXhpY2FsKCJcXHxcXHwiLCBTeW50YXguU3ltYm9sKSwKICAgICAgICAgICAgICAgICAgICBTeW50YXguTGV4aWNhbCgiXFwhIiwgU3ludGF4LlN5bWJvbCksCgogICAgICAgICAgICAgICAgICAgIFN5bnRheC5MZXhpY2FsKCJbXFwrXFwtXFwqXFwvXFwlXSIsIFN5bnRheC5TeW1ib2wpCiAgICAgICAgICAgICAgICApOwoKICAgICAgICAgICAgbGFuZ3VhZ2UuCiAgICAgICAgICAgICAgICBJbmNsdWRlCiAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgTGFuZ3VhZ2UuS2V5d29yZCgiY29uc3QiLCBsYW5ndWFnZS5Db25zdCksCiAgICAgICAgICAgICAgICAgICAgTGFuZ3VhZ2UuS2V5d29yZCgibmV3IiwgbGFuZ3VhZ2UuTmV3KSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5LZXl3b3JkKCInIiwgbGFuZ3VhZ2UuUXVvdGUpLAogICAgICAgICAgICAgICAgICAgIExhbmd1YWdlLktleXdvcmQoIjoiLCBsYW5ndWFnZS5MaXN0KSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5LZXl3b3JkKCIuIiwgbGFuZ3VhZ2UuQXBwbHkpLAogICAgICAgICAgICAgICAgICAgIExhbmd1YWdlLktleXdvcmQoIj0+IiwgbGFuZ3VhZ2UuTGFtYmRhKSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5LZXl3b3JkKCI9IiwgbGFuZ3VhZ2UuTGV0KSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5LZXl3b3JkKCI/PyIsIGxhbmd1YWdlLkNvYWxlc2NlKSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5LZXl3b3JkKCI/IiwgbGFuZ3VhZ2UuVGVzdCkKICAgICAgICAgICAgICAgICkuCiAgICAgICAgICAgICAgICBJbmNsdWRlCiAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgTGFuZ3VhZ2UuQnVpbHRpbigid3JpdGUiLCAoRW52aXJvbm1lbnQgZW52aXJvbm1lbnQsIG9iamVjdCB2YWx1ZSkgPT4geyBDb25zb2xlLldyaXRlKHZhbHVlID0gbGFuZ3VhZ2UuQHN0cmluZyhlbnZpcm9ubWVudCwgdmFsdWUpKTsgcmV0dXJuIHZhbHVlOyB9KSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5CdWlsdGluKCJ3cml0ZWxuIiwgKEVudmlyb25tZW50IGVudmlyb25tZW50LCBvYmplY3QgdmFsdWUpID0+IHsgQ29uc29sZS5Xcml0ZUxpbmUodmFsdWUgPSBsYW5ndWFnZS5Ac3RyaW5nKGVudmlyb25tZW50LCB2YWx1ZSkpOyByZXR1cm4gdmFsdWU7IH0pLAoKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5CdWlsdGluKCJzdW0iLCAoRW52aXJvbm1lbnQgZW52aXJvbm1lbnQsIG9iamVjdFtdIEBwYXJhbXMpID0+CiAgICAgICAgICAgICAgICAgICAgICAgIEBwYXJhbXNbMF0gaXMgb2JqZWN0W10gPwogICAgICAgICAgICAgICAgICAgICAgICAoKG9iamVjdFtdKUBwYXJhbXNbMF0pLkNhc3Q8aW50PigpLlN1bSgpCiAgICAgICAgICAgICAgICAgICAgICAgIDoKICAgICAgICAgICAgICAgICAgICAgICAgQHBhcmFtcy5DYXN0PGludD4oKS5TdW0oKQogICAgICAgICAgICAgICAgICAgICksCiAgICAgICAgICAgICAgICAgICAgTGFuZ3VhZ2UuQnVpbHRpbigibGVuZ3RoIiwgKEVudmlyb25tZW50IGVudmlyb25tZW50LCBvYmplY3QgdmFsdWUpID0+CiAgICAgICAgICAgICAgICAgICAgICAgICEodmFsdWUgaXMgb2JqZWN0W10pID8KICAgICAgICAgICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgdmFsdWUgaXMgc3RyaW5nID8KICAgICAgICAgICAgICAgICAgICAgICAgICAgIChvYmplY3QpKChzdHJpbmcpdmFsdWUpLkxlbmd0aAogICAgICAgICAgICAgICAgICAgICAgICAgICAgOgogICAgICAgICAgICAgICAgICAgICAgICAgICAgbnVsbAogICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgIDoKICAgICAgICAgICAgICAgICAgICAgICAgKChvYmplY3RbXSl2YWx1ZSkuTGVuZ3RoCiAgICAgICAgICAgICAgICAgICAgKSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5CdWlsdGluKCJhZGQiLCAoRW52aXJvbm1lbnQgZW52aXJvbm1lbnQsIG9iamVjdFtdIGxpc3QsIG9iamVjdCB2YWx1ZSkgPT4gbGlzdCAhPSBudWxsID8gbGlzdC5Db25jYXQobmV3W10geyB2YWx1ZSB9KS5Ub0FycmF5KCkgOiBudWxsKSwKICAgICAgICAgICAgICAgICAgICBMYW5ndWFnZS5CdWlsdGluKCJhbnkiLCAoRW52aXJvbm1lbnQgZW52aXJvbm1lbnQsIG9iamVjdFtdIGxpc3QsIENhbGxhYmxlIGZpbHRlcikgPT4gKGxpc3QgIT0gbnVsbCkgJiYgKGZpbHRlciAhPSBudWxsKSA/IChvYmplY3QpbGlzdC5BbnkoaXRlbSA9PiAoYm9vbClmaWx0ZXIuSW52b2tlKGVudmlyb25tZW50LCBpdGVtKSkgOiBudWxsKSwKICAgICAgICAgICAgIC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3VhZ2UuQnVpbHRpbigicmVtYWluaW5nIiwgKEVudmlyb25tZW50IGVudmlyb25tZW50LCBvYmplY3RbXSBsaXN0KSA9PiBlbnZpcm9ubWVudC5JbnZva2UoInNraXAiLCBsaXN0LCAxKSksCiAgICAgICAgICAgICAgICAgICAgTGFuZ3VhZ2UuQnVpbHRpbigidGFpbCIsIChFbnZpcm9ubWVudCBlbnZpcm9ubWVudCwgb2JqZWN0W10gbGlzdCkgPT4gZW52aXJvbm1lbnQuSW52b2tlKCJyZW1haW5pbmciLCBuZXdbXSB7IGxpc3QgfSkpLAogICAgICAgICAgICAgICAgICAgIExhbmd1YWdlLkJ1aWx0aW4oInNlbGVjdCIsIChFbnZpcm9ubWVudCBlbnZpcm9ubWVudCwgb2JqZWN0W10gbGlzdCwgQ2FsbGFibGUgbWFwcGVyKSA9PgogICAgICAgICAgICAgICAgICAgICAgICBtYXBwZXIgIT0gbnVsbCA/CiAgICAgICAgICAgICAgICAgICAgICAgIChsaXN0ICE9IG51bGwgPyBsaXN0LlNlbGVjdChpdGVtID0+IG1hcHBlci5JbnZva2UoZW52aXJvbm1lbnQsIGl0ZW0pKS5Ub0FycmF5KCkgOiBudWxsKQogICAgICAgICAgICAgICAgICAgICAgICA6CiAgICAgICAgICAgICAgICAgICAgICAgIG51bGwKICAgICAgICAgICAgICAgICAgICApLAogICAgICAgICAgICAgICAgICAgIExhbmd1YWdlLkJ1aWx0aW4oIndoZXJlIiwgKEVudmlyb25tZW50IGVudmlyb25tZW50LCBvYmplY3RbXSBsaXN0LCBDYWxsYWJsZSBmaWx0ZXIpID0+CiAgICAgICAgICAgICAgICAgICAgICAgIGZpbHRlciAhPSBudWxsID8KICAgICAgICAgICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgZmlsdGVyLkxlbmd0aCA+IDEgPwogICAgICAgICAgICAgICAgICAgICAgICAgICAgKGxpc3QgIT0gbnVsbCA/IGxpc3QuV2hlcmUoKGl0ZW0sIGluZGV4KSA9PiAoYm9vbClmaWx0ZXIuSW52b2tlKGVudmlyb25tZW50LCBpdGVtLCBpbmRleCkpLlRvQXJyYXkoKSA6IG51bGwpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICA6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAobGlzdCAhPSBudWxsID8gbGlzdC5XaGVyZShpdGVtID0+IChib29sKWZpbHRlci5JbnZva2UoZW52aXJvbm1lbnQsIGl0ZW0pKS5Ub0FycmF5KCkgOiBudWxsKQogICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgIDoKICAgICAgICAgICAgICAgICAgICAgICAgbnVsbAogICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICk7CgogICAgICAgICAgICBsYW5ndWFnZS5FdmFsdWF0ZShAIiggd3JpdGVsbiAoID09ICggdHlwZW9mIG51bGwgKSB2b2lkICkgKSIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgpOwoKICAgICAgICAgICAgbGFuZ3VhZ2UuCiAgICAgICAgICAgICAgICBFdmFsdWF0ZQogICAgICAgICAgICAgICAgKEAiCiAgICAgICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICAgICAoIHdyaXRlbG4gKCBzdW0gKCApICkgKQogICAgICAgICAgICAgICAgICAgICAgICAoIHdyaXRlbG4gKCBzdW0gMSApICkKICAgICAgICAgICAgICAgICAgICAgICAgKCB3cml0ZWxuICggc3VtICggMSAyICkgKSApCiAgICAgICAgICAgICAgICAgICAgICAgICggd3JpdGVsbiAoIHN1bSAxIDIgMyApICkKICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAiKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKCiAgICAgICAgICAgIC8qCnRoaXMgICAgICAgaXMKYSAgICAgICAgICAgICAgIG11bHRpCmxpbmUgICAgICAgICAgICAgICAgICAgICAgIHRleHQKY29uc3RhbnQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZGVtbwoid2l0aCJzb21lInF1b3RlcwogICAgICAgICAgICAqLwogICAgICAgICAgICBsYW5ndWFnZS4KICAgICAgICAgICAgICAgIEV2YWx1YXRlCiAgICAgICAgICAgICAgICAoQCIKICAgICAgICAgICAgICAgICAgICAoIHdyaXRlbG4KICAgICAgICAgICAgICAgICAgICAgICAgIiJ0aGlzXHRpc1xyXG5hXHRcdG11bHRpXG5saW5lXHRcdFx0dGV4dFwKY29uc3RhbnRcdFx0XHRcdGRlbW9cblwiIndpdGhcIiJzb21lXCIicXVvdGVzIiIKICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAiKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKCiAgICAgICAgICAgIGxhbmd1YWdlLgogICAgICAgICAgICAgICAgRXZhbHVhdGUKICAgICAgICAgICAgICAgIChAIgogICAgICAgICAgICAgICAgICAgICggPQogICAgICAgICAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAoCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yZWFjaAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICggPT4KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCB1cHBlcl9ib3VuZCB0b2RvICkgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPyAoICE9IHVwcGVyX2JvdW5kIDAgKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCBjb25jYXQgKCB0aGlzICggLSB1cHBlcl9ib3VuZCAxICkgdG9kbyApICggOiAoIDogdXBwZXJfYm91bmQgKCB0b2RvIHVwcGVyX2JvdW5kICkgKSApICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICggOiAoIDogMCAoIHRvZG8gMCApICkgKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG4hCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCA9PgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoIG4gKSAoCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA/ICggIT0gbiAwICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICggKiBuICggdGhpcyAoIC0gbiAxICkgKSApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgd3JpdGVsbgogICAgICAgICAgICAgICAgICAgICAgICAgICAgKCAuICsgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZWxlY3QKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCBmb3JlYWNoIDEyIG4hICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCA9PgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCBwYWlyICkgKCA9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoIGlucHV0ICggZmlyc3RPZiBwYWlyICkgKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoIHJlc3VsdCAoIGxhc3RPZiBwYWlyICkgKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoIGZvcm1hdCAiIlxyXG57MH0hID0gezF9IiIgKCBpbnB1dCByZXN1bHQgKSApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAiKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIi4uLiIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgpOwoKICAgICAgICAgICAgdmFyIGZhY3RvcmlhbCA9IChMYW1iZGEpbGFuZ3VhZ2UuCiAgICAgICAgICAgICAgICBFdmFsdWF0ZQogICAgICAgICAgICAgICAgKEAiCiAgICAgICAgICAgICAgICAgICAgKCA9PiAoIG4gKSAoCiAgICAgICAgICAgICAgICAgICAgICAgICAgICA/ICggIT0gbiAwICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICggKiBuICggdGhpcyAoIC0gbiAxICkgKSApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAxCiAgICAgICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICAgICAiKTsKCiAgICAgICAgICAgIHZhciBzdyA9IG5ldyBTeXN0ZW0uRGlhZ25vc3RpY3MuU3RvcHdhdGNoKCk7CiAgICAgICAgICAgIHZhciBuID0gMTI7CiAgICAgICAgICAgIHZhciByID0gMDsKICAgICAgICAgICAgaW50IGs7CiAgICAgICAgICAgIHN3LlN0YXJ0KCk7CiAgICAgICAgICAgIGZvciAoayA9IDA7IGsgPCAxMDAwMDsgaysrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByID0gKGludClmYWN0b3JpYWwuSW52b2tlKG51bGwsIG4pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN3LlN0b3AoKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoInswfSEgPSB7MX0iLCBuLCByKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoImluIHswfSBtcyAoZm9yIHsxfSB0aW1lcykiLCBzdy5FbGFwc2VkTWlsbGlzZWNvbmRzLCBrLlRvU3RyaW5nKCIwLDAiKSk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CgogICAgICAgICAgICAvL0NvbnNvbGUuUmVhZEtleSgpOwogICAgICAgIH0KICAgIH0KfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
    Lisp-like languages ( https://en.wikipedia.org/wiki/Lisp_%28programming_language%29 )

    (Take #9)

    Cyril Jandia (07/2016)

    Public Domain.

    NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace LispLike
{
    public delegate object Parser(string input);

    public class Token
    {
        public Token(string pattern, Parser parser) : this(pattern, parser, false) { }
        public Token(string pattern, Parser parser, bool priority) { Regex = new Regex(String.Format("^({0})", !String.IsNullOrEmpty(Pattern = pattern) ? Pattern : ".*"), RegexOptions.Compiled); ValueOf = parser; Priority = priority; }
        public string Pattern { get; private set; }
        public Regex Regex { get; private set; }
        public Parser ValueOf { get; private set; }
        public bool Priority { get; private set; }
    }

    public class Syntax
    {
        private const string Spaced = "s+";
        private const string Opened = "(";
        private const string Closed = ")";

        private static readonly Token Spacing = Token(Spaced, Identity, 1);
        private static readonly Token Opening = Token(Opened, Identity);
        private static readonly Token Closing = Token(Closed, Identity);

        private Tuple<Token, string, object> Next(string input, ref string remaining)
        {
            if (!String.IsNullOrEmpty(input))
            {
                var found = null as Match;
                var tuple = Lexicon.FirstOrDefault(current => (found = current.Item2.Regex.Match(input)).Success && (found.Length > 0));
                var token = tuple != null ? tuple.Item2 : null;
                var match = token != null ? found.Value : null;
                remaining = match != null ? input.Substring(match.Length) : input;
                return token != null ? Tuple.Create(token, match, token.ValueOf(match)) : null;
            }
            else
            {
                return null;
            }
        }

        private IEnumerable<Tuple<Token, string, object>> From(string input)
        {
            Tuple<Token, string, object> item;
            while ((item = Next(input, ref input)) != null)
            {
                yield return item;
            }
        }

        private IEnumerator<Tuple<Token, string, object>> Source(string input)
        {
            var source = From(input).Where(tuple => tuple.Item1 != Spacing).GetEnumerator();
            if (source.MoveNext())
            {
                return source;
            }
            throw Language.Error("unexpected EOF");
        }

        private object Parse(IEnumerator<Tuple<Token, string, object>> source, bool root)
        {
            var token = source.Current.Item1;
            var value = source.Current.Item3;
            if (token == Opening)
            {
                var list = new List<object>();
                while (token != Closing)
                {
                    if (source.MoveNext())
                    {
                        if ((token = source.Current.Item1) != Closing)
                        {
                            list.Add(Parse(source, false));
                        }
                    }
                    else
                    {
                        throw Language.Error("unexpected EOF");
                    }
                }
                value = list.ToArray();
            }
            if (root && source.MoveNext())
            {
                throw Language.Error("unexpected \"{0}\"", ToString(source.Current.Item2));
            }
            return value;
        }

        protected IList<Tuple<string, Token>> Lexicon { get; private set; }

        public static string Escape(string pattern)
        {
            return Escape(pattern, -1);
        }

        public static string Escape(string pattern, int count)
        {
            count = count < 0 ? pattern.Length : count;
            return pattern.Aggregate(new StringBuilder(), (escaped, character) => escaped.AppendFormat("{0}{1}", count-- > 0 ? "\\" : String.Empty, character)).ToString();
        }

        public static object Identity(string input)
        {
            return input;
        }

        public static object Symbol(string input)
        {
            return new Symbol(input);
        }

        public static Token Token(string pattern, Parser parser)
        {
            return Token(pattern, parser, -1);
        }

        public static Token Token(string pattern, Parser parser, int escapeCount)
        {
            return Token(pattern, parser, escapeCount, false);
        }

        public static Token Token(string pattern, Parser parser, bool priority)
        {
            return Token(pattern, parser, -1, priority);
        }

        public static Token Token(string pattern, Parser parser, int escapeCount, bool priority)
        {
            return new Token(Escape(pattern, escapeCount), parser, priority);
        }

        public static Tuple<string, Parser> Lexical(string pattern, Parser parser)
        {
            return Tuple.Create(pattern, parser);
        }

        public static Tuple<string, Parser, bool> Lexical(string pattern, Parser parser, bool priority)
        {
            return Tuple.Create(pattern, parser, priority);
        }

        public static string ToString(object expression)
        {
            return
                expression is object[] ?
                (
                    ((object[])expression).Length > 0 ?
                    ((object[])expression).Aggregate(new StringBuilder("("), (result, obj) => result.AppendFormat(" {0}", ToString(obj))).Append(" )").ToString()
                    :
                    "( )"
                )
                :
                ((expression != null ? expression.ToString() : null) ?? "(null)").Replace("\r\n", "\n").Replace("\\\n", "\n").Replace("\\t", "\t").Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\\"", "\"");
        }

        public Syntax()
        {
            Lexicon = new List<Tuple<string, Token>>();
            Include(Spacing, Opening, Closing);
        }

        public Syntax Include(Token token)
        {
            if (Lexicon.FirstOrDefault(tuple => tuple.Item1 == token.Pattern) == null)
            {
                if (token.Priority)
                {
                    Lexicon.Insert(0, Tuple.Create(token.Pattern, token));
                }
                else
                {
                    Lexicon.Add(Tuple.Create(token.Pattern, token));
                }
            }
            return this;
        }

        public Syntax Include(params Token[] tokens)
        {
            return tokens.Aggregate(this, (syntax, token) => syntax.Include(token));
        }

        public Syntax Include(params Tuple<string, Parser>[] entries)
        {
            return Include(entries.Select(entry => new Token(entry.Item1, entry.Item2)).ToArray());
        }

        public Syntax Include(params Tuple<string, Parser, bool>[] entries)
        {
            return Include(entries.Select(entry => new Token(entry.Item1, entry.Item2, entry.Item3)).ToArray());
        }

        public object Parse(string input)
        {
            return Parse(Source(input), true);
        }
    }

    public class Symbol
    {
        public Symbol(object id) { Id = Language.Const(id); }
        public override string ToString() { return Id.ToString(); }
        public object Id { get; private set; }
    }

    public delegate object Semantic(object[] arguments, Environment environment);

    public class Keyword : Symbol
    {
        public Keyword(string id, Semantic semantic) : base(id) { Semantic = semantic; }
        public Semantic Semantic { get; private set; }
    }

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
    public class BuiltinAttribute : Attribute
    {
        public BuiltinAttribute(string id) { Id = id; }
        public string Id { get; private set; }
    }

    public abstract class Callable : Symbol
    {
        protected Callable(int length) : this(null, length) { }
        protected Callable(string id, int length) : base(id) { Length = length; }
        public abstract object Invoke(params object[] arguments);
        public Delegate Delegate { get; protected set; }
        public int Length { get; protected set; }
        public bool Variadic { get; protected set; }
    }

    public class Lambda : Callable
    {
        public Lambda(Symbol[] formals, Func<object[], object> closure) : base(formals.Length) { Formals = formals; Delegate = (Closure = closure); }
        public override int GetHashCode() { return Closure.GetHashCode(); }
        public override string ToString() { return String.Format("{0} => ...", Syntax.ToString(Formals)); }
        public override object Invoke(params object[] arguments) { return Closure(arguments); }
        public Symbol[] Formals { get; private set; }
        public Func<object[], object> Closure { get; private set; }
    }

    public abstract class Builtin : Callable
    {
        protected Builtin(string id, int length) : base(id, length) { }
        protected int Count(object[] arguments) { return arguments != null ? arguments.Length : 0; }
    }

    public class Builtin<TResult> : Builtin
    {
        public Builtin(string id, Func<Environment, TResult> function) : base(id, 0) { Delegate = (Function = function); }
        public override object Invoke(params object[] arguments) { var count = Count(arguments); return Function(count > 0 ? (Environment)arguments[0] : null); }
        public Func<Environment, TResult> Function { get; private set; }
    }

    public class Builtin<T1, TResult> : Builtin
    {
        public Builtin(string id, Func<Environment, T1, TResult> function) : base(id, 1) { Delegate = (Function = function); var arg1 = Function.Method.GetParameters()[1]; Variadic = (arg1.Name == "params") && (typeof(T1) == typeof(object[])); }
        public override object Invoke(params object[] arguments) { var count = Count(arguments); return Function(count > 0 ? (Environment)arguments[0] : null, count > 1 ? (T1)arguments[1] : default(T1)); }
        public Func<Environment, T1, TResult> Function { get; private set; }
    }

    public class Builtin<T1, T2, TResult> : Builtin
    {
        public Builtin(string id, Func<Environment, T1, T2, TResult> function) : base(id, 2) { Delegate = (Function = function); }
        public override object Invoke(params object[] arguments) { var count = Count(arguments); return Function(count > 0 ? (Environment)arguments[0] : null, count > 1 ? (T1)arguments[1] : default(T1), count > 2 ? (T2)arguments[2] : default(T2)); }
        public Func<Environment, T1, T2, TResult> Function { get; private set; }
    }

    public class Builtin<T1, T2, T3, TResult> : Builtin
    {
        public Builtin(string id, Func<Environment, T1, T2, T3, TResult> function) : base(id, 3) { Delegate = (Function = function); }
        public override object Invoke(params object[] arguments) { var count = Count(arguments); return Function(count > 0 ? (Environment)arguments[0] : null, count > 1 ? (T1)arguments[1] : default(T1), count > 2 ? (T2)arguments[2] : default(T2), count > 3 ? (T3)arguments[3] : default(T3)); }
        public Func<Environment, T1, T2, T3, TResult> Function { get; private set; }
    }

    public class Builtin<T1, T2, T3, T4, TResult> : Builtin
    {
        public Builtin(string id, Func<Environment, T1, T2, T3, T4, TResult> function) : base(id, 4) { Delegate = (Function = function); }
        public override object Invoke(params object[] arguments) { var count = Count(arguments); return Function(count > 0 ? (Environment)arguments[0] : null, count > 1 ? (T1)arguments[1] : default(T1), count > 2 ? (T2)arguments[2] : default(T2), count > 3 ? (T3)arguments[3] : default(T3), count > 4 ? (T4)arguments[4] : default(T4)); }
        public Func<Environment, T1, T2, T3, T4, TResult> Function { get; private set; }
    }

    public class Builtin<T1, T2, T3, T4, T5, TResult> : Builtin
    {
        public Builtin(string id, Func<Environment, T1, T2, T3, T4, T5, TResult> function) : base(id, 5) { Delegate = (Function = function); }
        public override object Invoke(params object[] arguments) { var count = Count(arguments); return Function(count > 0 ? (Environment)arguments[0] : null, count > 1 ? (T1)arguments[1] : default(T1), count > 2 ? (T2)arguments[2] : default(T2), count > 3 ? (T3)arguments[3] : default(T3), count > 4 ? (T4)arguments[4] : default(T4), count > 5 ? (T5)arguments[5] : default(T5)); }
        public Func<Environment, T1, T2, T3, T4, T5, TResult> Function { get; private set; }
    }

    public class Environment : Dictionary<object, object>
    {
        private object[] GetBindings() { var bindings = this.Where(pair => !Equals(pair.Key, String.Empty)).Select(pair => new[] { pair.Key, pair.Value }); var keyed = new HashSet<object>(bindings.Select(pair => ((object[])pair)[0])); var bound = (Outer != null ? Outer.Bindings.Where(pair => !keyed.Contains(((object[])pair)[0])).Concat(bindings) : bindings).ToArray(); return bound; }
        private bool TryGet(object id, ref object value) { var key = id != null ? id.ToString() : null; if (key != null) { if (!ContainsKey(key)) { return Outer != null ? Outer.TryGet(key, ref value) : false; } else { value = key.Length > 0 ? this[key] : Bindings; return true; } } else return false; }
        public static readonly Builtin<object[], object> Bound = new Builtin<object[], object>(String.Empty, (@in, @params) => (@params != null) && (@params.Length > 0) ? @in.Get(@params[0]) : @in.Bindings);
        public Environment(Language language, Environment outer) : this(language, null as IDictionary<object, object>) { Outer = outer; }
        public Environment(Language language, IDictionary<object, object> builtins) { this[String.Empty] = Bound; Language = language; if (builtins != null) { builtins.Aggregate(this, (scope, pair) => { scope.Add(pair.Key, pair.Value); return scope; }); } }
        public object Invoke(object id, params object[] arguments) { var callable = Get(id) as Callable; if (callable == null) { throw Language.Error("not callable \"{0}\"", Syntax.ToString(id)); } var argv = new object[arguments != null ? arguments.Length + 1 : 1]; var argc = argv.Length; if (argc > 1) { Array.Copy(arguments, 0, argv, 1, argc - 1); } argv[0] = this; return callable.Invoke(argv); }
        public object Get(object id) { object value = null; if (!TryGet(id, ref value)) { throw Language.Error("undefined \"{0}\"", Syntax.ToString(id)); } return value; }
        public bool Has(object id) { object value = null; return TryGet(id, ref value); }
        public Language Language { get; private set; }
        public Environment Outer { get; private set; }
        public object[] Bindings { get { return GetBindings(); } }
    }

    public class Language
    {
        private Keyword test;

        private Type[] GetSignature(MethodInfo method)
        {
            var parameters = method.GetParameters();
            var length = parameters.Length;
            return parameters.Select(parameter => parameter.ParameterType).Concat(new[] { method.ReturnType }).ToArray();
        }

        private Type GetDelegateType(Type[] signature)
        {
            var delegateTypes = new[] { null, typeof(Func<,>), typeof(Func<,,>), typeof(Func<,,,>), typeof(Func<,,,,>), typeof(Func<,,,,,>), typeof(Func<,,,,,,>) };
            return delegateTypes[signature.Length - 1].MakeGenericType(signature);
        }

        private Type GetBuiltinType(Type[] signature)
        {
            var builtinTypes = new[] { null, typeof(Builtin<>), typeof(Builtin<,>), typeof(Builtin<,,>), typeof(Builtin<,,,>), typeof(Builtin<,,,,>), typeof(Builtin<,,,,,>) };
            return builtinTypes[signature.Length - 1].MakeGenericType(signature.Skip(1).ToArray());
        }

        private void AddBuiltin(IDictionary<object, object> builtins, object id, object value)
        {
            builtins.Add(id, value);
        }

        private IDictionary<object, object> GetReflectedBuiltins()
        {
            return
                GetType().
                GetMembers().
                Aggregate
                (
                    new Dictionary<object, object>(),
                    (reflected, member) =>
                    {
                        var attribute = member.GetCustomAttribute<BuiltinAttribute>(false);
                        string id;
                        if ((attribute != null) && !String.IsNullOrEmpty(id = attribute.Id))
                        {
                            var method = member as MethodInfo;
                            var field = member as FieldInfo;
                            object builtin;
                            if (method != null)
                            {
                                var signature = GetSignature(method);
                                var delegateType = GetDelegateType(signature);
                                var builtinType = GetBuiltinType(signature);
                                var @delegate = Delegate.CreateDelegate(delegateType, this, method);
                                builtin = Activator.CreateInstance(builtinType, id, @delegate);
                            }
                            else
                            {
                                if (field.IsInitOnly)
                                {
                                    builtin = field.GetValue(this);
                                }
                                else
                                {
                                    throw Error("field \"{0}\" must be read-only", field.Name);
                                }
                            }
                            AddBuiltin(reflected, id, builtin);
                        }
                        return reflected;
                    }
                );
        }

        private object Evaluate(string input, Environment environment)
        {
            return Evaluate(Syntax.Parse(input), environment);
        }

        private object Evaluate(object expression, Environment environment)
        {
            var list = expression as object[];
            var symbol = Symbol(expression);
            if (environment == null)
            {
                return Evaluate(expression, new Environment(this, Builtins));
            }
            else if (list != null)
            {
                object id;
                if ((list.Length > 0) && ((symbol = Symbol(list[0])) != null) && Keywords.ContainsKey(id = symbol.Id))
                {
                    return Keywords[id].Semantic(list, environment);
                }
                else
                {
                    int length;
                    list = list.Select(expr => Evaluate(expr, environment)).ToArray();
                    if (((length = list.Length) > 0) && ((symbol = Symbol(list[0])) != null) && (symbol is Callable))
                    {
                        var callable = (Callable)symbol;
                        var variadic = callable.Variadic;
                        var arguments = new object[variadic ? length - 1 : length];
                        if (length > 1)
                        {
                            Array.Copy(list, 1, arguments, variadic ? 0 : 1, length - 1);
                        }
                        if (variadic)
                        {
                            return callable.Invoke(environment, arguments);
                        }
                        else
                        {
                            arguments[0] = environment;
                            return callable.Invoke(arguments);
                        }
                    }
                    else
                    {
                        return list;
                    }
                }
            }
            else if (symbol != null)
            {
                return environment.Get(symbol);
            }
            else
            {
                return expression;
            }
        }

        protected Symbol Symbol(object value)
        {
            return value as Symbol;
        }

        public static object Const()
        {
            return Const(null);
        }

        public static object Const(object id)
        {
            return id ?? Guid.NewGuid().ToString("P");
        }

        public static Symbol New(object id)
        {
            return new Symbol(id);
        }

        public static KeyValuePair<object, Keyword> Keyword(string id, Semantic semantic)
        {
            return new KeyValuePair<object, Keyword>(id, new Keyword(id, semantic));
        }

        public static KeyValuePair<object, object> Builtin(string id, object value)
        {
            return new KeyValuePair<object, object>(id, value);
        }

        public static KeyValuePair<object, object> Builtin<TResult>(string id, Func<Environment, TResult> function)
        {
            return new KeyValuePair<object, object>(id, new Builtin<TResult>(id, function));
        }

        public static KeyValuePair<object, object> Builtin<T1, TResult>(string id, Func<Environment, T1, TResult> function)
        {
            return new KeyValuePair<object, object>(id, new Builtin<T1, TResult>(id, function));
        }

        public static KeyValuePair<object, object> Builtin<T1, T2, TResult>(string id, Func<Environment, T1, T2, TResult> function)
        {
            return new KeyValuePair<object, object>(id, new Builtin<T1, T2, TResult>(id, function));
        }

        public static KeyValuePair<object, object> Builtin<T1, T2, T3, TResult>(string id, Func<Environment, T1, T2, T3, TResult> function)
        {
            return new KeyValuePair<object, object>(id, new Builtin<T1, T2, T3, TResult>(id, function));
        }

        public static KeyValuePair<object, object> Builtin<T1, T2, T3, T4, TResult>(string id, Func<Environment, T1, T2, T3, T4, TResult> function)
        {
            return new KeyValuePair<object, object>(id, new Builtin<T1, T2, T3, T4, TResult>(id, function));
        }

        public static KeyValuePair<object, object> Builtin<T1, T2, T3, T4, T5, TResult>(string id, Func<Environment, T1, T2, T3, T4, T5, TResult> function)
        {
            return new KeyValuePair<object, object>(id, new Builtin<T1, T2, T3, T4, T5, TResult>(id, function));
        }

        public static Exception Error(string message, params object[] arguments)
        {
            return new Exception(String.Format(message, arguments));
        }

        public Language()
            : this(null)
        {
        }

        public Language(Syntax syntax)
        {
            Syntax = syntax ?? new Syntax();
            Keywords = new Dictionary<object, Keyword>();
            Builtins = new Dictionary<object, object>(GetReflectedBuiltins());
        }

        public object Const(object[] list, Environment outer)
        {
            return Const();
        }

        public object New(object[] list, Environment outer)
        {
            return New(Evaluate(list[1], outer));
        }

        public object Quote(object[] list, Environment outer)
        {
            return list[1];
        }

        public object List(object[] list, Environment outer)
        {
            return list.Skip(1).Select(item => Evaluate(item, outer)).ToArray();
        }

        public object Apply(object[] list, Environment outer)
        {
            var arguments = Evaluate(list[2], outer);
            arguments = !(arguments is object[]) ? new[] { arguments } : (object[])arguments;
            return Evaluate(new[] { list[1] }.Concat((object[])arguments).ToArray(), outer);
        }

        public object Lambda(object[] list, Environment outer)
        {
            var display = (object[])list[1];
            var formals = new Symbol[display.Length];
            for (var i = 0; i < formals.Length; i++)
            {
                formals[i] = Symbol(display[i]);
            }
            var @this = null as Lambda;
            @this =
                new Lambda
                (
                    formals,
                    delegate (object[] @params)
                    {
                        var environment = new Environment(this, outer);
                        var actual = @params.Length;
                        var length = formals.Length;
                        var i = 0;
                        while (i < length)
                        {
                            var formal = formals[i++];
                            if (i < actual)
                            {
                                environment[formal.Id] = @params[i];
                            }
                        }
                        environment["this"] = environment[@this.Id] = @this;
                        environment["params"] = @params.Skip(1).ToArray();
                        return Evaluate(list[2], environment);
                    }
                );
            return @this;
        }

        public object Let(object[] list, Environment outer)
        {
            var environment = new Environment(this, outer);
            var assignments = (object[])list[1];
            for (var i = 0; i < assignments.Length; i++)
            {
                var pair = (object[])assignments[i];
                environment[Symbol(pair[0]).Id] = Evaluate(pair[1], outer);
            }
            return Evaluate(list[2], environment);
        }

        public object Coalesce(object[] list, Environment outer)
        {
            test = test ?? Keywords.Values.First(keyword => keyword.Semantic == Test);
            return Test(new[] { test, new[] { test, list[1] }, list[1], list[2] }, outer);
        }

        public object Test(object[] list, Environment outer)
        {
            return
                list.Length > 2 ?
                (
                    (bool)Evaluate(list[1], outer) ? Evaluate(list[2], outer) : Evaluate(list[3], outer)
                )
                :
                outer.Has(list[1]);
        }

        public Language Include(KeyValuePair<object, Keyword> keywordDefinition)
        {
            Keywords.Add(keywordDefinition.Key, keywordDefinition.Value);
            return this;
        }

        public Language Include(params KeyValuePair<object, Keyword>[] keywordDefinitions)
        {
            foreach (var keywordDefinition in keywordDefinitions)
            {
                Include(keywordDefinition);
            }
            return this;
        }

        public Language Include(KeyValuePair<object, object> builtinDefinition)
        {
            AddBuiltin(Builtins, builtinDefinition.Key, builtinDefinition.Value);
            return this;
        }

        public Language Include(params KeyValuePair<object, object>[] builtinDefinitions)
        {
            foreach (var builtinDefinition in builtinDefinitions)
            {
                Include(builtinDefinition);
            }
            return this;
        }

        public object Evaluate(string input)
        {
            return Evaluate(input, null as object[]);
        }

        public object Evaluate(string input, params object[] arguments)
        {
            return Evaluate((arguments != null) && (arguments.Length > 0) ? String.Format(input, arguments) : input, null as Environment);
        }

        public object Evaluate(object expression)
        {
            return Evaluate(expression, null);
        }

        public Syntax Syntax { get; private set; }

        public IDictionary<object, Keyword> Keywords { get; private set; }

        public IDictionary<object, object> Builtins { get; private set; }
    }

    public class MyLanguage : Language
    {
        private class EqualityComparer : IEqualityComparer<object>
        {
            internal bool Equal(object x, object y)
            {
                return
                    Equals(x, y) ||
                    (
                        (x != null) &&
                        (y != null) &&
                        (
                            (x is object[]) &&
                            (y is object[]) &&
                            ((object[])x).SequenceEqual((object[])y, this)
                        )
                    );
            }

            bool IEqualityComparer<object>.Equals(object x, object y)
            {
                return Equal(x, y);
            }

            int IEqualityComparer<object>.GetHashCode(object obj)
            {
                return obj != null ? obj.GetHashCode() : 0;
            }
        }

        private static readonly EqualityComparer Equality = new EqualityComparer();

        [Builtin("void")]
        public readonly Type @void = typeof(void);

        [Builtin("null")]
        public readonly object @null = null;

        [Builtin("false")]
        public readonly bool @false = false;

        [Builtin("true")]
        public readonly bool @true = true;

        [Builtin("typeof")]
        public Type @typeof(Environment environment, object value) => value != null ? value.GetType() : @void;

        [Builtin("string")]
        public string @string(Environment environment, object value) => Syntax.ToString(value);

        [Builtin("format")]
        public string format(Environment environment, string template, object[] arguments) => String.Format(template, arguments);

        [Builtin("!=")]
        public bool notEqual(Environment environment, object left, object right) => !equal(null, left, right);

        [Builtin("==")]
        public bool equal(Environment environment, object left, object right) => Equality.Equal(left, right);

        [Builtin("&&")]
        public bool and(Environment environment, bool left, bool right) => left && right;

        [Builtin("||")]
        public bool or(Environment environment, bool left, bool right) => left || right;

        [Builtin("!")]
        public bool not(Environment environment, bool value) => !value;

        [Builtin("+")]
        public object add(Environment environment, object[] @params) =>
            @params[0] is string ?
            (object)String.Concat(@params)
            :
            (int)@params[0] + (int)@params[1];

        [Builtin("-")]
        public int subtract(Environment environment, int left, int right) => left - right;

        [Builtin("*")]
        public int multiply(Environment environment, int left, int right) => left * right;

        [Builtin("/")]
        public int divide(Environment environment, int left, int right) => left / right;

        [Builtin("%")]
        public int modulo(Environment environment, int left, int right) => left % right;
    }

    class Program
    {
        static void Main(string[] args)
        {
            var language = new MyLanguage();

            language.
                Syntax.
                Include
                (
                    Syntax.Token("const", Syntax.Symbol, 0),
                    Syntax.Token("new", Syntax.Symbol, 0),
                    Syntax.Token("'", Syntax.Symbol),
                    Syntax.Token(":", Syntax.Symbol),
                    Syntax.Token(".", Syntax.Symbol),
                    Syntax.Token("=>", Syntax.Symbol),
                    Syntax.Token("=", Syntax.Symbol),
                    Syntax.Token("??", Syntax.Symbol),
                    Syntax.Token("?", Syntax.Symbol),

                    Syntax.Token("==", Syntax.Symbol, true),
                    Syntax.Token("!=", Syntax.Symbol)
                ).
                Include
                (
                    Syntax.Lexical("false", Syntax.Symbol),
                    Syntax.Lexical("true", Syntax.Symbol),
                    Syntax.Lexical("null", Syntax.Symbol),

                    Syntax.Lexical("[0-9]+", s => int.Parse(s)),
                    Syntax.Lexical("\\\"(\\\\\\n|\\\\t|\\\\n|\\\\r|\\\\\\\"|[^\\\"])*\\\"", s => s.Substring(1, s.Length - 2)),

                    Syntax.Lexical("[\\$_A-Za-z][\\.\\$_0-9A-Za-z\\-\\!]*", Syntax.Symbol),

                    Syntax.Lexical("\\&\\&", Syntax.Symbol),
                    Syntax.Lexical("\\|\\|", Syntax.Symbol),
                    Syntax.Lexical("\\!", Syntax.Symbol),

                    Syntax.Lexical("[\\+\\-\\*\\/\\%]", Syntax.Symbol)
                );

            language.
                Include
                (
                    Language.Keyword("const", language.Const),
                    Language.Keyword("new", language.New),
                    Language.Keyword("'", language.Quote),
                    Language.Keyword(":", language.List),
                    Language.Keyword(".", language.Apply),
                    Language.Keyword("=>", language.Lambda),
                    Language.Keyword("=", language.Let),
                    Language.Keyword("??", language.Coalesce),
                    Language.Keyword("?", language.Test)
                ).
                Include
                (
                    Language.Builtin("write", (Environment environment, object value) => { Console.Write(value = [email protected](environment, value)); return value; }),
                    Language.Builtin("writeln", (Environment environment, object value) => { Console.WriteLine(value = [email protected](environment, value)); return value; }),

                    Language.Builtin("sum", (Environment environment, object[] @params) =>
                        @params[0] is object[] ?
                        ((object[])@params[0]).Cast<int>().Sum()
                        :
                        @params.Cast<int>().Sum()
                    ),
                    Language.Builtin("length", (Environment environment, object value) =>
                        !(value is object[]) ?
                        (
                            value is string ?
                            (object)((string)value).Length
                            :
                            null
                        )
                        :
                        ((object[])value).Length
                    ),
                    Language.Builtin("add", (Environment environment, object[] list, object value) => list != null ? list.Concat(new[] { value }).ToArray() : null),
                    Language.Builtin("any", (Environment environment, object[] list, Callable filter) => (list != null) && (filter != null) ? (object)list.Any(item => (bool)filter.Invoke(environment, item)) : null),
                    Language.Builtin("concat", (Environment environment, object[] left, object[] right) => (left != null) && (right != null) ? left.Concat(right).ToArray() : null),
                    Language.Builtin("firstOf", (Environment environment, object[] list) => (list != null) && (list.Length > 0) ? list[0] : null),
                    Language.Builtin("head", (Environment environment, object[] list) => environment.Invoke("firstOf", new[] { list })),
                    Language.Builtin("lastOf", (Environment environment, object[] list) => list != null ? environment.Invoke("firstOf", new[] { list.Reverse().ToArray() }) : null),
                    Language.Builtin("skip", (Environment environment, object[] list, int count) => (list != null) && (list.Length > 0) ? list.Skip(count).ToArray() : null),
                    Language.Builtin("remaining", (Environment environment, object[] list) => environment.Invoke("skip", list, 1)),
                    Language.Builtin("tail", (Environment environment, object[] list) => environment.Invoke("remaining", new[] { list })),
                    Language.Builtin("select", (Environment environment, object[] list, Callable mapper) =>
                        mapper != null ?
                        (list != null ? list.Select(item => mapper.Invoke(environment, item)).ToArray() : null)
                        :
                        null
                    ),
                    Language.Builtin("where", (Environment environment, object[] list, Callable filter) =>
                        filter != null ?
                        (
                            filter.Length > 1 ?
                            (list != null ? list.Where((item, index) => (bool)filter.Invoke(environment, item, index)).ToArray() : null)
                            :
                            (list != null ? list.Where(item => (bool)filter.Invoke(environment, item)).ToArray() : null)
                        )
                        :
                        null
                    )
                );

            language.Evaluate(@"( writeln ( == ( typeof null ) void ) )");
            Console.WriteLine();

            language.
                Evaluate
                (@"
                    (
                        ( writeln ( sum ( ) ) )
                        ( writeln ( sum 1 ) )
                        ( writeln ( sum ( 1 2 ) ) )
                        ( writeln ( sum 1 2 3 ) )
                    )
                ");
            Console.WriteLine();

            /*
this       is
a               multi
line                       text
constant                               demo
"with"some"quotes
            */
            language.
                Evaluate
                (@"
                    ( writeln
                        ""this\tis\r\na\t\tmulti\nline\t\t\ttext\
constant\t\t\t\tdemo\n\""with\""some\""quotes""
                    )
                ");
            Console.WriteLine();

            language.
                Evaluate
                (@"
                    ( =
                        (
                            (
                                foreach
                                ( =>
                                    ( upper_bound todo ) (
                                        ? ( != upper_bound 0 )
                                        ( concat ( this ( - upper_bound 1 ) todo ) ( : ( : upper_bound ( todo upper_bound ) ) ) )
                                        ( : ( : 0 ( todo 0 ) ) )
                                    )
                                )
                            )
                            (
                                n!
                                ( =>
                                    ( n ) (
                                        ? ( != n 0 )
                                        ( * n ( this ( - n 1 ) ) )
                                        1
                                    )
                                )
                            )
                        )
                        (
                            writeln
                            ( . + (
                                    select
                                    ( foreach 12 n! )
                                    ( =>
                                        ( pair ) ( =
                                            (
                                                ( input ( firstOf pair ) )
                                                ( result ( lastOf pair ) )
                                            )
                                            ( format ""\r\n{0}! = {1}"" ( input result ) )
                                        )
                                    )
                                )
                            )
                        )
                    )
                ");
            Console.WriteLine();
            Console.WriteLine("...");
            Console.WriteLine();

            var factorial = (Lambda)language.
                Evaluate
                (@"
                    ( => ( n ) (
                            ? ( != n 0 )
                            ( * n ( this ( - n 1 ) ) )
                            1
                        )
                    )
                ");

            var sw = new System.Diagnostics.Stopwatch();
            var n = 12;
            var r = 0;
            int k;
            sw.Start();
            for (k = 0; k < 10000; k++)
            {
                r = (int)factorial.Invoke(null, n);
            }
            sw.Stop();
            Console.WriteLine("{0}! = {1}", n, r);
            Console.WriteLine();
            Console.WriteLine("in {0} ms (for {1} times)", sw.ElapsedMilliseconds, k.ToString("0,0"));
            Console.WriteLine();

            //Console.ReadKey();
        }
    }
}