diff --git a/irin-lang.min.js b/irin-lang.min.js index 4cc748d..f6565bd 100644 --- a/irin-lang.min.js +++ b/irin-lang.min.js @@ -1 +1 @@ -(function(){var e;e=function(){function e(e,t){var n,i;!e instanceof String&&t({message:"input file path must be a string."}),this.env.runtime=this.runtime(),i=this,n=!1,this.data.files.push(e),this.parse(e,function(e,r){return r&&(i.data.graph={next:r.graph},i.data.head=i.data.graph,i.data.global=r.variable,n||(i.data.isReady=!0,t())),e?(t(e),n=!0):void 0})}var t;return e.prototype.data={graph:[],head:[],global:{},files:[],isReady:!1},e.prototype.config={includeDepth:50},e.prototype.env={runtime:"node",node:{fs:""}},e.prototype.parse=function(e,t){return this.parseWorker(e,0,t)},e.prototype.parseWorker=function(e,t,n){var i;return t>this.config.includeDepth?(console.error("stack crash"),n({message:"File reading stack more than "+this.config.includeDepth+" time. Maybe it got forever loop.",file:this.data.files.pop()})):(i=this,this.readFile(e,function(e,r){return e?n(e):i.parseProcess(r.toString(),t,n)}))},e.prototype.parseProcess=function(e,n,i){var r,o,a,s,p;return e=this.removeComment(e),e.message?void i({message:e.message,file:this.data.files.pop(),line:e.line}):(e=this.reduceIndent(e),o=e.split("\n"),p={line:0,indent:0,pastIndent:0,readingHeader:!1,functionObject:{},currentFuncName:"",graph:[],variable:{},headerDepth:0,isAddtoFunction:!1},p.head=p.graph,s=this,a=[],(r=function(e,h){var d,l,u,f,c,g;if(e&&(e.file?i(e):(p=a.pop(),s.data.files.pop(),i({message:e.message,file:s.data.files.pop(),line:p.line+1}))),h){for(s.data.files.pop(),p=a.pop(),p.indent=0,p.head=p.graph;p.indent!==p.headerDepth;)p.indent++,p.head=p.head[p.head.length-1].next;p.head.push.apply(p.head,h.graph);for(u in h.variable)p.variable[u]||(p.variable[u]=h.variable[u]);for(u in h.functionObject)p.functionObject[u]?p.functionObject[u].push.apply(p.functionObject[u],h.functionObject[u]):p.functionObject[u]=h.functionObject[u];p.pastIndent=p.headerDepth}for(;p.line-1){if(p.readingHeader)p.readingHeader=!1;else{if(0!==p.line)return void i({message:"Header must declare on top of file only.",file:s.data.files.pop(),line:p.line+1});if(0!==p.indent)return void i({message:"Header must have no indent.",file:s.data.files.pop(),line:p.line+1});p.headerDepth=p.indent,p.readingHeader=!0}p.line++}else{if(p.readingHeader&&c.indexOf("<-")>-1)for(g=c.split("<-"),f=g.length-1,l=0;f>l;)p.variable[g[l].trim()]=g[f].trim(),l++;if(c=c.trim(),!t(c))return void i({message:"Bracket is mismatch.",file:s.data.files.pop(),line:p.line+1});if("->"===c.substring(0,2)&&".irin"===c.substring(c.length-5,c.length))return p.line++,p.headerDepth=p.indent,a.push(p),d=c.trim().substring(2,c.length).trim(),s.data.files.push(d),void s.parseWorker(d,n+1,r);if("->"!==c.substring(0,2)){if(p.isAddtoFunction&&0===p.indent&&(p.isAddtoFunction=!1),p.indent===p.pastIndent)p.head.push({text:c,next:[]}),p.head.length>1&&0===p.head[p.head.length-2].next.length&&(p.head[p.head.length-2].next=p.head[p.head.length-1].next);else if(p.indent>p.pastIndent){for(p.pastIndent=p.indent;p.head[p.head.length-1].next.length;)p.head=p.head[p.head.length-1].next,p.indent++;p.head=p.head[p.head.length-1].next,p.head.push({text:c,next:[]})}else if(p.indentp.pastIndent&&(p.head[p.head.length-1].next=p.functionObject[c]),p.pastIndent=p.indent,p.line++}}return i(void 0,{graph:p.graph,variable:p.variable,functionObject:p.functionObject})})())},e.prototype.countIndent=function(e){var t,n,i;try{i=e.search(/\S/)}catch(n){return t=n,0}return i=-1===i?0:i},e.prototype.buildIndent=function(e,t){var n,i;for(i="",n=0;t>n;)i+=" ",n++;return i+e.trim()},e.prototype.reduceIndent=function(e){var t,n,i;for(n=[],e=e.split("\n"),i=0;i0&&t0&&n.slice(-1)[0]>t;)n.pop();else t>0&&(0===n.length||t>n.slice(-1)[0])&&n.push(t);e[i]=this.buildIndent(e[i],n.length),i++}return e.join("\n")},e.prototype.removeComment=function(e){var t,n,i,r,o,a,s,p;for(p=e.split("\n"),s=[],o=!1,a=0,t=0,n=0,i=p.length;i>n;n++)r=p[n],t++,r.indexOf("###")>-1?o?(o=!1,r=r.substring(r.indexOf("###")+3,r.length)):(o=!0,r=r.substring(0,r.indexOf("###")),a=t):r.indexOf("#")>-1&&(r=r.substring(0,r.indexOf("#"))),0!==r.trim().length&&(o||s.push(r));return o?{message:"multiline comment must have close tag.",line:a+1}:s.join("\n")},e.prototype.toRegular=function(e){var t,n,i,r,o;for(e=this.mergeExpression(e),o="",e=e.replace(/\*/g,"(.+)"),t=0;t0&&" "===e[t-1]){for(o=o.trim(),i=0;in;n++)t=r[n],e=e.replace(new RegExp("\\"+t,"g"),"\\"+t);return e},e.prototype.match=function(e,t){var n,i;return n=new RegExp(this.toRegular(t),"i"),i=e.match(n),i?(i.shift(),i):void 0},e.prototype.mergeExpression=function(e,t){var n,i,r,o,a,s,p,h,d,l;for(i="",l=0,o=0,d=0,s=0;ss;)this.data.global[h[s].trim()]=n,s++}else i=isNaN(parseInt(i))?this.data.global[i]:t[parseInt(i)-1];a=e.slice(0,o)+i,s=a.length-1,e=a+e.slice(d+1)}else i+="}";else i+=r;s++}return e},t=function(e){var t,n,i,r,o;for(r=[],n=0,i=e.length;i>n;n++)if(t=e[n],("{"===t||"("===t||"["===t)&&r.push(t),"}"===t||")"===t||"]"===t){if(0===r.length)return!1;if(o=r.pop(),"}"===t&&"{"!==o)return!1;if("]"===t&&"["!==o)return!1;if(")"===t&&"("!==o)return!1}return r.length>0?!1:!0},e.prototype.inlineToConditionTree=function(e){var t,n,i,r,o,a,s;for(s={data:void 0,prev:void 0},i=s,t="",r=!1,o=0,a=e.length;a>o;o++)if(n=e[o],"?"===n)i.data=t,t="",i.left={data:void 0,prev:i},i=i.left;else if(":"===n){for(i.data=t,t="",i=i.prev;i.right;)i=i.prev;i.right={data:void 0,prev:i},i=i.right}else t+=n;return i.data=t,s},e.prototype.checkOperator=function(e){var t,n,i,r;for(r=["(",")","!","&&","||","==","!=",">=","<=",">","<","*","/","+","-"],t=0,n=r.length;n>t;t++)if(i=r[t],e===i)return!0;return!1},e.prototype.tokenizeOperator=function(e){var t,n,i,r,o,a,s,p,h,d,l,u;for(e=[e],n=[],i=[],l=["(",")","!","&&","||","==","!=",">=","<=",">","<","*","/","+","-"],r=0,o=l.length;o>r;r++){for(d=l[r],p=0,a=e.length;a>p;p++)if(u=e[p],u.indexOf(d)>-1){for(i=u.split(d),h=0,s=i.length;s>h;h++)t=i[h],""!==t&&n.push(t),n.push(d);n.pop()}else n.push(u);e=n,n=[]}return e},e.prototype.convertToRPN=function(e){var t,n,i,r,o,a;for(n=this.tokenizeOperator(e),a=[],o=[],i=0,r=n.length;r>i;i++)if(t=n[i],this.checkOperator(t))if(")"===t){for(;o.length>0&&"("!==o[o.length-1];)a.push(o.pop());o.pop()}else o.push(t);else a.push(t);for(;o.length>0;)a.push(o.pop());return a},e.prototype.castBoolean=function(e){return"boolean"==typeof e?e:"string"==typeof e&&"true"===e?!0:"string"==typeof e&&"false"===e?!1:e},e.prototype.compileOperator=function(e){var t,n,i,r,o,a,s;for(a=this.convertToRPN(e),i=[],r=0,o=a.length;o>r;r++)s=a[r],"!"===s?i.push(!this.castBoolean(i.pop())):"&&"===s?i.push(this.castBoolean(i.pop())&&this.castBoolean(i.pop())):"||"===s?i.push(this.castBoolean(i.pop())||this.castBoolean(i.pop())):"=="===s?i.push(i.pop().toString()===i.pop().toString()):"!="===s?i.push(i.pop().toString()!==i.pop().toString()):">="===s?i.push(parseFloat(i.pop())<=parseFloat(i.pop())):"<="===s?i.push(parseFloat(i.pop())>=parseFloat(i.pop())):">"===s?i.push(parseFloat(i.pop())parseFloat(i.pop())):"*"===s?i.push(parseFloat(i.pop())*parseFloat(i.pop())):"/"===s?i.push(parseFloat(i.pop())/parseFloat(i.pop())):"-"===s?i.push(parseFloat(i.pop())-parseFloat(i.pop())):"+"===s?(t=i.pop(),n=i.pop(),isNaN(parseFloat(t))||isNaN(parseFloat(n))?i.push(t+n):i.push(parseFloat(t)+parseFloat(n))):i.push(s);return i[0]},e.prototype.conditionWorker=function(e){return e.left&&e.right?this.compileOperator(e.data)?this.conditionWorker(e.left):this.conditionWorker(e.right):e.data},e.prototype.inlineCondition=function(e){var t;return t=this.inlineToConditionTree(e),this.conditionWorker(t)},e.prototype.selectChild=function(e,t){var n,i,r,o,a,s;for(this.text=e,this.head=t,a=this.head.next,r=0,o=a.length;o>r;r++)if(i=a[r],n=this.match(this.text,i.text))return s=Math.floor(Math.random()*i.next.length),{node:i.next[s],data:n}},e.prototype.reply=function(e){var t;return this.text=e,this.data.isReady?!this.text instanceof String?"[Log:Error] reply input text must be a String.":(t=this.selectChild(this.text,this.data.head),t&&t.node?(this.data.head=t.node,this.mergeExpression(t.node.text,t.data)):(this.data.head=this.data.graph,t=this.selectChild(this.text,this.data.head),t&&t.node?(this.data.head=t.node,this.mergeExpression(t.node.text,t.data)):"[Log:Error] answer not found.")):"[Log:Error] can't use reply before Irin class callback."},e.prototype.readFile=function(e,t){var n;return"node"===this.env.runtime?this.env.node.fs.readFile(e,"utf8",function(e,n){return t(e,n)}):"browser"===this.env.runtime?(n=new XMLHttpRequest,n.onreadystatechange=function(){return 4===n.readyState&&200===n.status?t(void 0,n.responseText):4===n.readyState?t({message:"ENOENT: no such file or directory, open '"+e+"' "}):void 0},n.open("GET",e,!0),n.send()):void 0},e.prototype.runtime=function(){return"undefined"==typeof window&&"object"==typeof module?(this.env.node.fs=require("fs"),"node"):"browser"},e}(),"object"==typeof module&&module&&"object"==typeof module.exports?module.exports=e:(window.Irin=e,"function"==typeof define&&define.amd&&define("Irin",[],function(){return e}))}).call(this); \ No newline at end of file +(function(){var e;e=function(){function e(e,t){var n,i;!e instanceof String&&t({message:"input file path must be a string."}),this.env.runtime=this.runtime(),i=this,n=!1,this.data.files.push(e),this.parse(e,function(e,r){return r&&(i.data.graph={next:r.graph},i.data.head=i.data.graph,i.data.global=r.variable,n||(i.data.isReady=!0,t())),e?(t(e),n=!0):void 0})}var t;return e.prototype.data={graph:[],head:[],global:{},files:[],isReady:!1},e.prototype.config={includeDepth:50},e.prototype.env={runtime:"node",node:{fs:""}},e.prototype.parse=function(e,t){return this.parseWorker(e,0,t)},e.prototype.parseWorker=function(e,t,n){var i;return t>this.config.includeDepth?(console.error("stack crash"),n({message:"File reading stack more than "+this.config.includeDepth+" time. Maybe it got forever loop.",file:this.data.files.pop()})):(i=this,this.readFile(e,function(e,r){return e?n(e):i.parseProcess(r.toString(),t,n)}))},e.prototype.parseProcess=function(e,n,i){var r,o,a,s,p;return e=this.removeComment(e),e.message?void i({message:e.message,file:this.data.files.pop(),line:e.line}):(e=this.reduceIndent(e),o=e.split("\n"),p={line:0,indent:0,pastIndent:0,readingHeader:!1,functionObject:{},currentFuncName:"",graph:[],variable:{},headerDepth:0,isAddtoFunction:!1},p.head=p.graph,s=this,a=[],(r=function(e,h){var d,l,u,f,c,g;if(e&&(e.file?i(e):(p=a.pop(),s.data.files.pop(),i({message:e.message,file:s.data.files.pop(),line:p.line+1}))),h){for(s.data.files.pop(),p=a.pop(),p.indent=0,p.head=p.graph;p.indent!==p.headerDepth;)p.indent++,p.head=p.head[p.head.length-1].next;p.head.push.apply(p.head,h.graph);for(u in h.variable)p.variable[u]||(p.variable[u]=h.variable[u]);for(u in h.functionObject)p.functionObject[u]?p.functionObject[u].push.apply(p.functionObject[u],h.functionObject[u]):p.functionObject[u]=h.functionObject[u];p.pastIndent=p.headerDepth}for(;p.line-1){if(p.readingHeader)p.readingHeader=!1;else{if(0!==p.line)return void i({message:"Header must declare on top of file only.",file:s.data.files.pop(),line:p.line+1});if(0!==p.indent)return void i({message:"Header must have no indent.",file:s.data.files.pop(),line:p.line+1});p.headerDepth=p.indent,p.readingHeader=!0}p.line++}else{if(p.readingHeader&&c.indexOf("<-")>-1)for(g=c.split("<-"),f=g.length-1,l=0;f>l;)p.variable[g[l].trim()]=g[f].trim(),l++;if(c=c.trim(),!t(c))return void i({message:"Bracket is mismatch.",file:s.data.files.pop(),line:p.line+1});if("->"===c.substring(0,2)&&".irin"===c.substring(c.length-5,c.length))return p.line++,p.headerDepth=p.indent,a.push(p),d=c.trim().substring(2,c.length).trim(),s.data.files.push(d),void s.parseWorker(d,n+1,r);if("->"!==c.substring(0,2)){if(p.isAddtoFunction&&0===p.indent&&(p.isAddtoFunction=!1),p.indent===p.pastIndent)p.head.push({text:c,next:[]}),p.head.length>1&&0===p.head[p.head.length-2].next.length&&(p.head[p.head.length-1].next=p.head[p.head.length-2].next);else if(p.indent>p.pastIndent){for(p.pastIndent=p.indent;p.head[p.head.length-1].next.length;)p.head=p.head[p.head.length-1].next,p.indent++;p.head=p.head[p.head.length-1].next,p.head.push({text:c,next:[]})}else if(p.indentp.pastIndent&&(p.head[p.head.length-1].next=p.functionObject[c]),p.pastIndent=p.indent,p.line++}}return i(void 0,{graph:p.graph,variable:p.variable,functionObject:p.functionObject})})())},e.prototype.countIndent=function(e){var t,n,i;try{i=e.search(/\S/)}catch(n){return t=n,0}return i=-1===i?0:i},e.prototype.buildIndent=function(e,t){var n,i;for(i="",n=0;t>n;)i+=" ",n++;return i+e.trim()},e.prototype.reduceIndent=function(e){var t,n,i;for(n=[],e=e.split("\n"),i=0;i0&&t0&&n.slice(-1)[0]>t;)n.pop();else t>0&&(0===n.length||t>n.slice(-1)[0])&&n.push(t);e[i]=this.buildIndent(e[i],n.length),i++}return e.join("\n")},e.prototype.removeComment=function(e){var t,n,i,r,o,a,s,p;for(p=e.split("\n"),s=[],o=!1,a=0,t=0,n=0,i=p.length;i>n;n++)r=p[n],t++,r.indexOf("###")>-1?o?(o=!1,r=r.substring(r.indexOf("###")+3,r.length)):(o=!0,r=r.substring(0,r.indexOf("###")),a=t):r.indexOf("#")>-1&&(r=r.substring(0,r.indexOf("#"))),0!==r.trim().length&&(o||s.push(r));return o?{message:"multiline comment must have close tag.",line:a+1}:s.join("\n")},e.prototype.toRegular=function(e){var t,n,i,r,o;for(e=this.mergeExpression(e),o="",e=e.replace(/\*/g,"(.+)"),t=0;t0&&" "===e[t-1]){for(o=o.trim(),i=0;in;n++)t=r[n],e=e.replace(new RegExp("\\"+t,"g"),"\\"+t);return e},e.prototype.match=function(e,t){var n;return n=new RegExp(this.toRegular(t),"i"),e.match(n)},e.prototype.mergeExpression=function(e,t){var n,i,r,o,a,s,p,h,d,l;for(i="",l=0,o=0,d=0,s=0;ss;)this.data.global[h[s].trim()]=n,s++}else isNaN(parseInt(i))?""!==i&&(i=this.data.global[i]):i=t[parseInt(i)];a=e.slice(0,o)+i,s=a.length-1,e=a+e.slice(d+1)}else i+="}";else i+=r;s++}return e},t=function(e){var t,n,i,r,o;for(r=[],n=0,i=e.length;i>n;n++)if(t=e[n],("{"===t||"("===t||"["===t)&&r.push(t),"}"===t||")"===t||"]"===t){if(0===r.length)return!1;if(o=r.pop(),"}"===t&&"{"!==o)return!1;if("]"===t&&"["!==o)return!1;if(")"===t&&"("!==o)return!1}return r.length>0?!1:!0},e.prototype.inlineToConditionTree=function(e){var t,n,i,r,o,a,s;for(s={data:void 0,prev:void 0},i=s,t="",r=!1,o=0,a=e.length;a>o;o++)if(n=e[o],"?"===n)i.data=t,t="",i.left={data:void 0,prev:i},i=i.left;else if(":"===n){for(i.data=t,t="",i=i.prev;i.right;)i=i.prev;i.right={data:void 0,prev:i},i=i.right}else t+=n;return i.data=t,s},e.prototype.checkOperator=function(e){var t,n,i,r;for(r=["(",")","!","&&","||","==","!=",">=","<=",">","<","*","/","+","-"],t=0,n=r.length;n>t;t++)if(i=r[t],e===i)return!0;return!1},e.prototype.tokenizeOperator=function(e){var t,n,i,r,o,a,s,p,h,d,l,u;for(e=[e],n=[],i=[],l=["(",")","!","&&","||","==","!=",">=","<=",">","<","*","/","+","-"],r=0,o=l.length;o>r;r++){for(d=l[r],p=0,a=e.length;a>p;p++)if(u=e[p],u.indexOf(d)>-1){for(i=u.split(d),h=0,s=i.length;s>h;h++)t=i[h],""!==t&&n.push(t),n.push(d);n.pop()}else n.push(u);e=n,n=[]}return e},e.prototype.convertToRPN=function(e){var t,n,i,r,o,a;for(n=this.tokenizeOperator(e),a=[],o=[],i=0,r=n.length;r>i;i++)if(t=n[i],this.checkOperator(t))if(")"===t){for(;o.length>0&&"("!==o[o.length-1];)a.push(o.pop());o.pop()}else o.push(t);else a.push(t);for(;o.length>0;)a.push(o.pop());return a},e.prototype.castBoolean=function(e){return"boolean"==typeof e?e:"string"==typeof e&&"true"===e?!0:"string"==typeof e&&"false"===e?!1:e},e.prototype.compileOperator=function(e){var t,n,i,r,o,a,s;for(a=this.convertToRPN(e),i=[],r=0,o=a.length;o>r;r++)s=a[r],"!"===s?i.push(!this.castBoolean(i.pop())):"&&"===s?i.push(this.castBoolean(i.pop())&&this.castBoolean(i.pop())):"||"===s?i.push(this.castBoolean(i.pop())||this.castBoolean(i.pop())):"=="===s?i.push(i.pop().toString()===i.pop().toString()):"!="===s?i.push(i.pop().toString()!==i.pop().toString()):">="===s?i.push(parseFloat(i.pop())<=parseFloat(i.pop())):"<="===s?i.push(parseFloat(i.pop())>=parseFloat(i.pop())):">"===s?i.push(parseFloat(i.pop())parseFloat(i.pop())):"*"===s?i.push(parseFloat(i.pop())*parseFloat(i.pop())):"/"===s?i.push(parseFloat(i.pop())/parseFloat(i.pop())):"-"===s?i.push(parseFloat(i.pop())-parseFloat(i.pop())):"+"===s?(t=i.pop(),n=i.pop(),isNaN(parseFloat(t))||isNaN(parseFloat(n))?i.push(t+n):i.push(parseFloat(t)+parseFloat(n))):i.push(s);return"undefined"===i[0]?void 0:"NaN"===i[0]?NaN:i[0]},e.prototype.conditionWorker=function(e){return e.left&&e.right?this.compileOperator(e.data)?this.conditionWorker(e.left):this.conditionWorker(e.right):e.data},e.prototype.inlineCondition=function(e){var t;return t=this.inlineToConditionTree(e),this.conditionWorker(t)},e.prototype.selectChild=function(e,t){var n,i,r,o,a,s;for(this.text=e,this.head=t,a=this.head.next,r=0,o=a.length;o>r;r++)if(i=a[r],n=this.match(this.text,i.text))return s=Math.floor(Math.random()*i.next.length),{node:i.next[s],data:n}},e.prototype.reply=function(e){var t;return this.text=e,this.data.isReady?!this.text instanceof String?"[Log:Error] reply input text must be a String.":(t=this.selectChild(this.text,this.data.head),t&&t.node?(this.data.head=t.node,this.mergeExpression(t.node.text,t.data)):(this.data.head=this.data.graph,t=this.selectChild(this.text,this.data.head),t&&t.node?(this.data.head=t.node,this.mergeExpression(t.node.text,t.data)):"[Log:Error] answer not found.")):"[Log:Error] can't use reply before Irin class callback."},e.prototype.readFile=function(e,t){var n;return"node"===this.env.runtime?this.env.node.fs.readFile(e,"utf8",function(e,n){return t(e,n)}):"browser"===this.env.runtime?(n=new XMLHttpRequest,n.onreadystatechange=function(){return 4===n.readyState&&200===n.status?t(void 0,n.responseText):4===n.readyState?t({message:"ENOENT: no such file or directory, open '"+e+"' "}):void 0},n.open("GET",e,!0),n.send()):void 0},e.prototype.runtime=function(){return"undefined"==typeof window&&"object"==typeof module?(this.env.node.fs=require("fs"),"node"):"browser"},e}(),"object"==typeof module&&module&&"object"==typeof module.exports?module.exports=e:(window.Irin=e,"function"==typeof define&&define.amd&&define("Irin",[],function(){return e}))}).call(this); \ No newline at end of file diff --git a/package.json b/package.json index 8dc68ae..9f431a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "irin-lang", - "version": "0.0.2", + "version": "0.0.3", "description": "IRIN (Indent to Recognize for Intelligent Natural language) is a scripting language of chatterbots, make you easier to create chatterbot.", "main": "irin-lang.min.js", "scripts": {