/*
    Copyright (C) 2009 Tim Hentenaar.
    All Rights Reserved.
    http://hentenaar.com
*/
var EDITOR={BUFMAX:112,prog_lines:[],buffer:'',cur_x:0,cur_y:0,buf_cur:0,uflag:0,cur_bg:'',cur_tm:undefined,init:function(){this.cur_x=1;this.cur_y=SCREEN.HEIGHT-2;this.buf_cur=0;this.buffer='';this.toggle_cursor();},parse_line:function(line){this.uflag=1;this.toggle_cursor();this.uflag=0;var e,x,line_no='';line=line.replace(/^\s+/g,'');if(line=='')return;if((e=line.match(/^([0-9]+)\s+/))){line_no=e[1];line=line.substr(e[0].length);}if(line_no!=''&&parseInt(line_no)<=0)return this.show_error('BAD LINE NUMBER');line=line.replace(/^\s+/g,'').replace(/^let/i,'');if(!((e=line.match(/^([a-z]+)/i))&&(x=STATEMENTS[e[1].toUpperCase()])&&typeof(x)=="function")){if((e=line.match(/^[a-z@_]+[a-z0-9@_]*\$?\s*(\(((\s*[0-9]+,?\s*){1,3})\))?\s*=\s*/i)))x=STATEMENTS['LET'];else if(!((e=line.match(/^([a-z]+)/i))&&line_no==''&&(x=STATEMENTS['imm$'+e[1].toUpperCase()])&&typeof(x)=="function"))return this.show_error('INCORRECT STATEMENT');}if(line_no!='')this.prog_lines[line_no]=line;else{x(line);SCREEN.set_bg_color(8);}},show_error:function(err){if(INTERPRETER.status!=INTERPRETER.STATUS_BREAK&&INTERPRETER.lineno>0)err+=' IN '+INTERPRETER.lineno;SCREEN.scroll_back();SCREEN.scroll_back();SCREEN.draw_ti_text(1,SCREEN.HEIGHT-2,'* '+err);SCREEN.scroll_back();},show_warning:function(warn){SCREEN.scroll_back();SCREEN.draw_ti_text(1,SCREEN.HEIGHT-2,'* WARNING:');SCREEN.scroll_back();SCREEN.draw_ti_text(3,SCREEN.HEIGHT-2,warn);SCREEN.scroll_back();},toggle_cursor:function(){if(INTERPRETER.status==INTERPRETER.STATUS_RUNNING)return;if(this.cur_bg!=''||this.uflag){document.getElementById('ti_cell_'+((this.cur_y*SCREEN.WIDTH)+this.cur_x)).style.background=this.cur_bg;this.cur_bg='';}else if(!this.uflag){this.cur_bg=document.getElementById('ti_cell_'+((this.cur_y*SCREEN.WIDTH)+this.cur_x)).style.background;document.getElementById('ti_cell_'+((this.cur_y*SCREEN.WIDTH)+this.cur_x)).style.backgroundColor='black';}if(!this.uflag)this.cur_tm=setTimeout('EDITOR.toggle_cursor();',500);else clearTimeout(this.cur_tm);},reset_buffer:function(){SCREEN.scroll_back();this.buffer='';this.cur_x=1;this.cur_y=SCREEN.HEIGHT-2;this.buf_cur=0;SCREEN.draw_ti_text(0,SCREEN.HEIGHT-2,">");this.toggle_cursor();},key_press:function(e){var cc=((e.charCode==undefined||e.charCode==0)?e.keyCode:e.charCode);if(cc==8){if(this.buffer.length>=1&&this.buf_cur>0){this.uflag=1;this.toggle_cursor();this.uflag=0;this.buffer=(this.buffer.substr(0,this.buf_cur-1)+this.buffer.substr(this.buf_cur));this.buf_cur--;document.getElementById('ti_cell_'+((this.cur_y*SCREEN.WIDTH)+--this.cur_x)).style.background='transparent';if(this.cur_x<1){this.cur_x=SCREEN.WIDTH;this.cur_y--;}this.toggle_cursor();}}else if(cc==13||((cc==38||cc==40)&&!e.shiftKey)){this.parse_line(this.buffer);if(!this.buffer.match(/^\s*run\s*/i))this.reset_buffer();}else if(cc>=33&&cc<=43&&!e.shiftKey){switch(cc){case 37:if(this.buf_cur>0){this.buf_cur--;this.uflag=1;this.toggle_cursor();this.uflag=0;if(--this.cur_x<1){this.cur_x=SCREEN.WIDTH;this.cur_y--;}this.toggle_cursor();}break;case 39:if(this.buf_cur<this.buffer.length){this.buf_cur++;this.uflag=1;this.toggle_cursor();this.uflag=0;if(++this.cur_x>SCREEN.WIDTH){this.cur_x=1;this.cur_y++;}this.toggle_cursor();}break;}}else if(cc>=32&&cc<=127&&!e.ctrlKey&&!e.altKey){if(this.buffer.length<this.BUFMAX){this.uflag=1;this.toggle_cursor();this.uflag=0;if(this.cur_x>SCREEN.WIDTH){SCREEN.scroll_back();this.cur_x=1;}if(!this.buffer.length||this.buf_cur==this.buffer.length-1)this.buffer+=String.fromCharCode(cc);else this.buffer=(this.buffer.substr(0,this.buf_cur)+String.fromCharCode(cc)+this.buffer.substr(this.buf_cur+1));SCREEN.draw_ti_text(this.cur_x,this.cur_y,String.fromCharCode(cc));this.cur_x++;this.buf_cur++;this.toggle_cursor();}}}};var FUNCTIONS={ABS:function(line){if((line=line.replace(/^abs\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.abs(parseFloat(res)));},ASC:function(line){if((line=line.replace(/^asc\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)!='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return String(res.charCodeAt(1));},ATN:function(line){if((line=line.replace(/^atn\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.atan(parseFloat(res)));},CHR$:function(line){if((line=line.replace(/^chr\$\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return'"'+String.fromCharCode(res)+'"';},COS:function(line){if((line=line.replace(/^cos\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.cos(parseFloat(res)));},EOF:function(line){return INTERPRETER.error('FILE ACCESS NOT IMPLEMENTED');},EXP:function(line){if((line=line.replace(/^exp\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.exp(parseFloat(res)));},INT:function(line){if((line=line.replace(/^int\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return String(parseInt(res));},LOG:function(line){if((line=line.replace(/^log\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.log(parseFloat(res)));},POS:function(line){if((line=line.replace(/^pos\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var x=line.split(/,/);if(x.length!=3)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=String(INTERPRETER.eval_expr(x[0]));x[1]=String(INTERPRETER.eval_expr(x[1]));x[2]=String(INTERPRETER.eval_expr(x[2]));if(INTERPRETER.errflag)return;if(x[0].charAt(0)!='"'||x[1].charAt(0)!='"'||x[2].charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');if(parseInt(x[2])<1)return INTERPRETER.error('BAD VALUE');return String(x[0].substr(parseInt(x[2])).indexOf(x[1]));},$rand:function(divisor){INTERPRETER.rndseed=((INTERPRETER.rndseed*0x6fe5)&0xffff)+0x7ab9;return((((INTERPRETER.rndseed&0xff00)>>8)|((INTERPRETER.rndseed&0x00ff)<<8))%(!divisor?1:divisor));},RND:function(line){var rands=[],x='0.',i;if(line.replace(/\s*/,'').length>3)return INTERPRETER.error('INCORRECT STATEMENT');for(i=0;i<63;i++)if((rands[0]=FUNCTIONS['$rand'](100))!=0)break;if(rands[0]==0)return'0';for(i=1;i<7;i++)rands[i]=FUNCTIONS['$rand'](100);if(rands[5]>=0x32)rands[4]++;if(rands[0]<0x10)x+='00';for(i=0;i<5;i++)x+=((rands[i]<10)?'0':'')+rands[i];INTERPRETER.lastrnd=rands[rands.length-1];return x;},SEG$:function(line){if((line=line.replace(/^seg\$\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var x=line.split(/,/);if(x.length!=3)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=String(INTERPRETER.eval_expr(x[0]));x[1]=String(INTERPRETER.eval_expr(x[1]));x[2]=String(INTERPRETER.eval_expr(x[2]));if(INTERPRETER.errflag)return;if(x[0].charAt(0)!='"'||x[1].charAt(0)=='"'||x[2].charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');if(parseInt(x[1])<0)return INTERPRETER.error('BAD VALUE');return'"'+String(x[0].substr(parseInt(x[1]),parseInt(x[2])))+'"';},SGN:function(line){if((line=line.replace(/^sgn\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return String((parseInt(res)<0)?-1:(parseInt(res)>0)?1:0);},SIN:function(line){if((line=line.replace(/^sin\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.sin(parseFloat(res)));},SQR:function(line){if((line=line.replace(/^sqr\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.sqrt(parseFloat(res)));},STR$:function(line){if((line=line.replace(/^str\$\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var x=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(x.charAt(0)!='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');if(parseInt(x)<0)return INTERPRETER.error('BAD VALUE');return'"'+x+'"';},TAB:function(line){if((line=line.replace(/^tab\s*\(/i,'').replace(/\s*\)\s*$/,''))==''||!INTERPRETER.inprint)return INTERPRETER.error('INCORRECT STATEMENT');var x='',i,res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');for(i=0;i<parseInt(res);i++)x+=' ';return'"'+x+'"';},TAN:function(line){if((line=line.replace(/^tan\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(Math.tan(parseFloat(res)));},VAL:function(line){if((line=line.replace(/^val\s*\(/i,'').replace(/\s*\)\s*$/,''))=='')return INTERPRETER.error('INCORRECT STATEMENT');var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflag)return;if(res.charAt(0)!='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');return INTERPRETER.eval_float(res.replace(/^"/,'').replace(/"$/,''));},};var INTERPRETER={STATUS_IDLE:0,STATUS_RUNNING:1,STATUS_BREAK:2,status:0,errflg:0,warnflg:0,traceflg:0,newflg:1,lineno:0,inprint:0,rndseed:0x3567,lastrnd:0x0d,g_stack:[],f_stack:{},f_step:{},f_limit:{},p_lines:[],p_lineno:[],vars:[],breaks:[],key:0,last_key:0,p_pos:0,key_press:function(e){var cc=((e.charCode==undefined||e.charCode==0)?e.keyCode:e.charCode);this.last_key=this.key;if(!e.shiftKey){switch(cc){case 37:this.key=8;break;case 38:this.key=11;break;case 39:this.key=9;break;case 40:this.key=10;break;default:this.key=cc;}}else this.key=cc;},error:function(err){EDITOR.show_error(err);this.status=this.STATUS_IDLE;this.errflg=1;},warning:function(warn){EDITOR.show_warning(warn);this.warnflg=1;},run_one:function(){if(this.errflg||++this.p_pos>=this.p_lines.length){this.vars=[];this.g_stack=[];this.f_stack={};this.f_limit={};this.f_step={};if(!this.errflg){SCREEN.scroll_back();SCREEN.scroll_back();SCREEN.draw_ti_text(1,SCREEN.HEIGHT-2,'** DONE **');SCREEN.scroll_back();}SCREEN.set_bg_color(8);this.lineno=0;this.status=this.STATUS_IDLE;EDITOR.reset_buffer();return;}this.lineno=this.p_lineno[this.p_pos];var line=this.p_lines[this.p_pos];if(this.traceflg){STATEMENTS['PRINT'](this.lineno);}if((e=line.match(/^let\s+/i)))line=line.substr(e[0].length);if((e=line.match(/^[a-z@_]+[a-z0-9@_]*\$?\s*(\(((\s*[0-9]+,?\s*){1,3})\))?\s*=\s*/i))){if((res=STATEMENTS['LET']))res(line);else this.error('INCORRECT STATEMENT');}else{if(!(e=line.match(/^([a-z]+)\s*/i)))this.error('INCORRECT STATEMENT');else{if((res=STATEMENTS[e[1].toUpperCase()])&&typeof(res)=='function')res(line.substr(e[0].length));else{if(line.match(/^go\s+to\s+/i)&&((res=STATEMENTS['GOTO'])&&typeof(res)=='function'))res(line.substr(e[0].length));else this.error('INCORRECT STATEMENT');}}}if(this.status!=this.STATUS_BREAK)setTimeout('INTERPRETER.run_one();',1);},run:function(lines,startln){var e,i,res,ln;if(lines.length==0)return this.error('CAN\'T DO THAT');this.vars=[];this.g_stack=[];this.f_stack={};this.f_limit={};this.f_step={};this.errflg=0;this.warnflg=0;SCREEN.set_bg_color(4);this.status=this.STATUS_RUNNING;this.p_pos=-1;this.p_lines=[];this.p_lineno=[];for(ln in lines){if(isNaN(startln)||parseInt(ln)>=startln){this.p_lines.push(lines[ln]);this.p_lineno.push(ln);}}setTimeout('INTERPRETER.run_one();',1);},eval_array_var:function(line){var e,i,b;if((e=line.match(/^([a-z@_]+[a-z0-9@_]*\$?)\(((\s*[0-9]+,?\s*){1,3})\)/i))){e[2]=e[2].replace(/\s/g,'');if(e[2].charAt(e[2].length-1)==',')this.error('INCORRECT STATEMENT');else{if((i=this.vars[e[1].toUpperCase()])&&i.dim){var x=i.dim.split(',');var y=e[2].split(',');if(x.length!=y.length)return this.error('BAD SUBSCRIPT');for(b=0;b<x.length;b++)if(x.length!=y.length||parseInt(y[b])>parseInt(x[b])-1)return this.error('BAD SUBSCRIPT');return{'name':e[1].toUpperCase(),'index':e[2],'value':i.arr[e[2]],'explen':e[0].length};}else if(i&&!i.dim)return this.error('NAME CONFLICT');else if(this.status==this.STATUS_IDLE){var y=e[2].split(',');var x,z='';for(x=0;x<y.length;x++)z+='10,';z=z.replace(/,$/,'');for(i in y)if(parseInt(y[i])>10)return this.error('BAD SUBSCRIPT');this.vars[e[1].toUpperCase()]={name:e[1].toUpperCase(),dim:z,arr:{}};return{'name':e[1].toUpperCase(),'index':e[2],'explen':e[0].length};}}}else this.error('BAD NAME');return;},eval_float:function(x){if(isNaN(parseFloat(x))||parseFloat(x)==Number.POSITIVE_INFINITY){this.warning('NUMBER TOO BIG');return'9.99999e+128';}if(parseFloat(x)==Number.NEGATIVE_INFINITY){this.warning('NUMBER TOO BIG');return'-9.99999e+128';}if(parseFloat(x)<0){if(parseFloat(x)<-9.9999999999999e+127){this.warning('NUMBER TOO BIG');return'-9.99999e+128';}if(parseFloat(x)>-1e-128)return'-1e-128';return String(parseFloat(x).toPrecision(10));}if(parseFloat(x)>0){if(parseFloat(x)<1e-128)return'1e-128';if(parseFloat(x)>9.9999999999999e+127){this.warning('NUMBER TOO BIG');return'-9.99999e+128';}return String(parseFloat(x).toPrecision(10));}return'0';},eval_expr:function(line){var toks=[],stack=[],plvl=0,s='',last_tok_op=0,j;if(INTERPRETER.errflg)return'';for(var i=0;i<line.length;i++){while(i<line.length&&line.charAt(i)==' ')i++;if(line.charAt(i).match(/[\*\/\^\(\)=<>&]/)||(!last_tok_op&&line.charAt(i).match(/[\+\-]/))){if(last_tok_op&&!line.charAt(i).match(/[\(\)]/)&&!line.charAt(i-1).match(/[\(\)]/)){this.error('INCORRECT STATEMENT');return;}last_tok_op=1;if(line.charAt(i)==')'){last_tok_op=0;while(stack.length>1&&stack[stack.length-1]!='('){toks[toks.length]=stack[stack.length-1];stack.length--;}stack.length--;plvl--;}else{if(line.charAt(i).match(/[\+\-&]/)){for(j=stack.length-1;j>=0;j--){if(stack[j].match(/[\*\/\^]/)){toks[toks.length]=stack[j];stack.splice(j,1);}else if(stack[j]=='(')break;}}else if(line.charAt(i).match(/[\*\/]/)){for(j=stack.length-1;j>=0;j--){if(stack[j]=='^'||(stack[j]=='/'&&line.charAt(i)=='*')){toks[toks.length]=stack[j];stack.splice(j,1);}else if(stack[j]=='(')break;}}else if(line.substr(i,2).match(/^[=<>]/)){for(j=stack.length-1;j>=0;j--){if(stack[j]=='(')break;toks[toks.length]=stack[j];stack.splice(j,1);}if((e=line.substr(i,2).match(/^(=|<>|<=|>=)/))){stack[stack.length]=e[1];i+=stack[stack.length-1].length-1;e=9909;}}if(e!=9909)stack[stack.length]=line.charAt(i);if(line.charAt(i)=='(')plvl++;}}else if((s=line.substr(i).match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))!=null){last_tok_op=0;toks[toks.length]=this.eval_float(s[0]);i+=s[0].length-1;}else{last_tok_op=0;var e;if((e=line.substr(i).match(/^([\+\-]?)([a-z@_]+[a-z0-9@_]*\$?)/i))){if(line.charAt(i+e[0].length)!='('){i+=e[0].length-1;if(this.vars[e[2].toUpperCase()]){toks[toks.length]=this.vars[e[2].toUpperCase()];if(e[1].length>0&&toks[toks.length-1].charAt(0)!='"')toks.splice(toks.length,0,e[1]+"1","*");}else if(e[2].toUpperCase()=='PI')toks[toks.length]='3.14159265359';else{var fnc=FUNCTIONS[e[2].toUpperCase()];if(fnc&&typeof(fnc)=='function'){toks[toks.length]=fnc(line.substr(i,j-i-1));if(e[1].length>0&&toks[toks.length-1].charAt(0)!='"')toks.splice(toks.length,0,e[1]+"1","*");}else toks[toks.length]=e[2].match(/\$$/)?'""':'0';}}else{var pl=1;for(j=i+e[0].length+1;j<line.length&&pl;j++){if(line.charAt(j)=='(')pl++;if(line.charAt(j)==')')pl--;}if(pl==0){var fnc=FUNCTIONS[e[2].toUpperCase()];if(fnc&&typeof(fnc)=='function'){toks[toks.length]=fnc(line.substr(i,j-i));if(e[1].length>0&&toks[toks.length-1].charAt(0)!='"')toks.splice(toks.length,0,e[1]+"1","*");}else if((fnc=this.vars[e[2].toUpperCase()])&&typeof(fnc)=='object'){if((s=eval_array_var(line.substr(i)))){toks[toks.length]=s.value?s.value:((e[2].charAt(e[2].length-1)=='$')?'""':'0')}else if(this.status!=this.STATUS_IDLE)return this.error('INCORRECT STATEMENT');}else toks[toks.length]=(e[2].charAt(e[2].length-1)=='$')?'""':'0';if(this.errflg)return;i=j-1;}else{this.error('INCORRECT STATEMENT');return;}}}else if(line.charAt(i)=='"'){for(j=i+1;j<line.length;j++)if(line.charAt(j)=='"'&&line.charAt(j-1)!='\\')break;if(line.charAt(j)!='"')return this.error('INCORRECT STATEMENT');toks[toks.length]=line.substr(i,j-i+1);i=j;}else{this.error('INCORRECT STATEMENT');return;}}}if(plvl>0){this.error('INCORRECT STATEMENT');return;}while(stack.length>0){toks[toks.length]=stack[stack.length-1];stack.length--;}for(i=0;i<toks.length;i++){while(i<toks.length&&!toks[i].match(/^[\+\-\*\/\^=<>&]$/)&&toks[i]!='<>')i++;if(i>toks.length)break;if(toks.length>=3){switch(toks[i]){case'=':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=(this.eval_float(toks[i-2])==this.eval_float(toks[i-1]))?'-1':'0';else if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]=(String(toks[i-2].substr(1,toks[i-2].length-2))==String(toks[i-1].substr(1,toks[i-1].length-2)))?'-1':'0';else return this.error('INCORRECT STATEMENT');break;case'<>':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=(this.eval_float(toks[i-2])!=this.eval_float(toks[i-1]))?'-1':'0';else if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]=(String(toks[i-2].substr(1,toks[i-2].length-2))!=String(toks[i-1].substr(1,toks[i-1].length-2)))?'-1':'0';else return this.error('INCORRECT STATEMENT');break;case'<':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=(this.eval_float(toks[i-2])<this.eval_float(toks[i-1]))?'-1':'0';else if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]=(String(toks[i-2].substr(1,toks[i-2].length-2))<String(toks[i-1].substr(1,toks[i-1].length-2)))?'-1':'0';else return this.error('INCORRECT STATEMENT');break;case'>':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=(this.eval_float(toks[i-2])>this.eval_float(toks[i-1]))?'-1':'0';else if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]=(String(toks[i-2].substr(1,toks[i-2].length-2))>String(toks[i-1].substr(1,toks[i-1].length-2)))?'-1':'0';else return this.error('INCORRECT STATEMENT');break;case'<=':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=(this.eval_float(toks[i-2])<=this.eval_float(toks[i-1]))?'-1':'0';else if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]=(String(toks[i-2].substr(1,toks[i-2].length-2))<=String(toks[i-1].substr(1,toks[i-1].length-2)))?'-1':'0';else return this.error('INCORRECT STATEMENT');break;case'>=':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=(this.eval_float(toks[i-2])>=this.eval_float(toks[i-1]))?'-1':'0';else if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]=(String(toks[i-2].substr(1,toks[i-2].length-2))>=String(toks[i-1].substr(1,toks[i-1].length-2)))?'-1':'0';else return this.error('INCORRECT STATEMENT');break;case'^':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=String(Math.pow(this.eval_float(toks[i-2]),this.eval_float(toks[i-1])));else return this.error('INCORRECT STATEMENT');break;case'*':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=String(this.eval_float(toks[i-2])*this.eval_float(toks[i-1]));else return this.error('INCORRECT STATEMENT');break;case'/':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?$/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?$/i)){if(this.eval_float(toks[i-1])==0)toks[i-2]=this.eval_float((toks[i-2].charAt(0)=='-')?Number.NEGATIVE_INFINITY:Number.POSITIVE_INFINITY);else toks[i-2]=String(this.eval_float(toks[i-2])/this.eval_float(toks[i-1]));}else return this.error('INCORRECT STATEMENT');break;case'+':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=String(this.eval_float(toks[i-2])+this.eval_float(toks[i-1]));else return this.error('INCORRECT STATEMENT');break;case'-':if(toks[i-2].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i)&&toks[i-1].match(/^[\+\-]?[0-9]+(\.[0-9]+(e[\+\-]?[0-9]+)?)?/i))toks[i-2]=String(this.eval_float(toks[i-2])-this.eval_float(toks[i-1]));else return this.error('INCORRECT STATEMENT');break;case'&':if(toks[i-2].charAt(0)=='"'&&toks[i-1].charAt(0)=='"')toks[i-2]='"'+String(toks[i-2].substr(1,toks[i-2].length-2))+String(toks[i-1].substr(1,toks[i-1].length-2))+'"';else return this.error('INCORRECT STATEMENT');break;}toks.splice(i-1,2);i=-1;}}if(toks.length>1){if(toks[toks.length-1]=='-'){toks[toks.length-2]=-this.eval_float(toks[toks.length-2]);toks.length--;}else if(toks[toks.length-1]=='+')toks.length--;}return toks;},};var SCREEN={IMG_PATH:'/basic/img',WIDTH:32,HEIGHT:24,SCALE:1,COLOR_TBL:['transparent','black','#21c842','#5edc78','#5455ed','#7d76fc','#d4524d','#42ebf5','#fc5554','#ff7978','#d4c154','#e6ce80','#21b03b','#c95bba','#cccccc','white'],FG_TBL:[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],BG_TBL:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],SCREENCOL:8,USER_CHAR_TBL:{},CHARS:[],screen_id:'',init:function(scrid){var x=document.getElementById(scrid);if(!x)return;this.screen_id=scrid;this.SCREENCOL=8;this.CHARS=[];x.innerHTML='';x.style.width=(32*8*this.SCALE);x.style.height=(24*8*this.SCALE);x.style.backgroundColor=this.ti_color_to_css(this.SCREENCOL);for(var i=0;i<this.HEIGHT;i++){for(var j=0;j<this.WIDTH;j++){var c=document.createElement('div');c.id='ti_cell_'+((this.WIDTH*i)+j);c.className='cell';c.style.width=(8*this.SCALE)+'px';c.style.height=(8*this.SCALE)+'px';this.CHARS[(this.WIDTH*i)+j]=32;x.appendChild(c);}}if(INTERPRETER.newflg){this.FG_TBL=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2];this.BG_TBL=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];this.USER_CHAR_TBL={};this.draw_ti_text(1,this.HEIGHT-4,"TI BASIC READY");INTERPRETER.newflg=0;}if(INTERPRETER.status!=INTERPRETER.STATUS_RUNNING)this.draw_ti_text(0,this.HEIGHT-2,">");EDITOR.init();},create_sub_cells:function(x,y){var c;if(!(c=document.getElementById('ti_cell_'+((this.WIDTH*y)+x))))return;for(var z=0;z<64;z++){var sc=document.createElement('div');sc.id='ti_cell_'+((this.WIDTH*y)+x)+'_sc_'+z;sc.className='cell';sc.style.width=this.SCALE+'px';sc.style.height=this.SCALE+'px';c.appendChild(sc);}},set_scale:function(val){var x=document.getElementById('screen');if(val==this.SCALE)return;if(!x)return;this.SCALE=val;x.style.width=(32*8*this.SCALE);x.style.height=(24*8*this.SCALE);for(var i=0;i<this.HEIGHT;i++){for(var j=0;j<this.WIDTH;j++){var c=document.getElementById('ti_cell_'+((this.WIDTH*i)+j));c.style.width=(8*this.SCALE);c.style.height=(8*this.SCALE);if(c.innerHTML!=''){for(var z=0;z<(64*this.SCALE);z++){var sc=document.getElementById('ti_cell_'+((this.WIDTH*i)+j)+'_sc_'+z);if(sc){sc.style.width=this.SCALE+'px';sc.style.height=this.SCALE+'px';}}}if(c.style.backgroundImage!=''&&c.style.backgroundImage!='initial'){var z=c.style.backgroundImage.replace(/ti-font-\d/,'ti-font-'+this.SCALE);var p=c.style.backgroundPosition;var pos=p.split(' ');pos[0]=pos[0].replace('-','').replace('px','');pos[1]=pos[1].replace('-','').replace('px','');if(this.SCALE==2){pos[0]*=2;pos[1]*=2;}else{pos[0]/=2;pos[1]/=2;}c.style.background=c.style.backgroundColor+' '+z+' -'+pos[0]+'px -'+pos[1]+'px no-repeat';}}}},ti_color_to_css:function(color){if(color<1||color>16)return'transparent';return this.COLOR_TBL[color-1];},set_bg_color:function(color){var x=document.getElementById(this.screen_id);if(!x)return;this.SCREENCOL=color;x.style.backgroundColor=this.ti_color_to_css(this.SCREENCOL);},draw_ti_text:function(x,y,str){var start=((this.WIDTH*y)+x)-1;for(var i=0;i<str.length;i++){var row,roff,c=document.getElementById('ti_cell_'+(++start));if(!c)return;var code=str.charCodeAt(i);if(this.USER_CHAR_TBL[code])this.draw_ti_char(x,y,code);else{if(code<32||code>126)code=32;if(code<=59){row=0;roff=code-32;}else if(code<=87){row=1;roff=code-60;}else if(code<=115){row=2;roff=code-88;}else if(code<=127){row=3;roff=code-116;}c.innerHTML='';c.style.background=this.ti_color_to_css(this.BG_TBL[parseInt((code-32)/8)])+' url('+this.IMG_PATH+'/ti-font-'+this.SCALE+'-'+this.FG_TBL[parseInt((code-32)/8)]+'.png) -'+(roff*8*this.SCALE)+'px -'+(row*8*this.SCALE)+'px no-repeat';this.CHARS[start]=code;}}},draw_ti_char:function(x,y,code){if(!this.USER_CHAR_TBL[code])this.draw_ti_text(x,y,String.fromCharCode(code));else if(this.USER_CHAR_TBL[code]){var i,j,k,q,a,z=document.getElementById('ti_cell_'+((this.WIDTH*y)+x));if(!z||String(this.USER_CHAR_TBL[code]).length!=16)return;if(z.innerHTML=='')this.create_sub_cells(x,y);z.background='transparent';for(i=0,k=0;i<8;i++,k++){if((j=parseInt('0x'+String(this.USER_CHAR_TBL[code]).substr(i+k,2)))){a=0;while(a<=7){if((q=document.getElementById('ti_cell_'+((this.WIDTH*y)+x)+'_sc_'+(8*i+7-a)))){if(j&1)q.style.backgroundColor=this.ti_color_to_css(this.FG_TBL[parseInt((code-32)/8)]);else q.style.backgroundColor=this.ti_color_to_css(this.BG_TBL[parseInt((code-32)/8)]);}else alert('ti_cell_'+((this.WIDTH*y)+x)+'_sc_'+(8*i+7-a));j>>=1;a++;}}}this.CHARS[(this.WIDTH*y)+x]=code;}},scroll_back:function(){for(var i=0;i<this.HEIGHT;i++){for(var j=0;j<this.WIDTH;j++){if(!i){document.getElementById('ti_cell_'+j).style.background='transparent';this.CHARS[j]=32;}else{var c=document.getElementById('ti_cell_'+((i*this.WIDTH)+j));var pc=document.getElementById('ti_cell_'+(((i-1)*this.WIDTH)+j));this.CHARS[((i-1)*this.WIDTH)+j]=this.CHARS[(i*this.WIDTH)+j];if(c.innerHTML!=''){if(pc.innerHTML=='')this.create_sub_cells(j,i-1);for(var z=0;z<64;z++){var psc=document.getElementById('ti_cell_'+(((i-1)*this.WIDTH)+j)+'_sc_'+z);var sc=document.getElementById('ti_cell_'+((i*this.WIDTH)+j)+'_sc_'+z);if(!sc||!psc)break;psc.style.background=sc.style.background;sc.style.background='transparent';}}if(c.style.background!='transparent')pc.style.background=c.style.background;c.style.background='transparent';}}}},key_press:function(e){if(INTERPRETER.status==INTERPRETER.STATUS_RUNNING)INTERPRETER.key_press(e);else EDITOR.key_press(e);}};var STATEMENTS={CALL:function(line){var e,sub,res;if((e=line.match(/^(call\s+)?([a-z]+)/i))){if((sub=SUBROUTINES[e[2].toUpperCase()])&&typeof(sub)=='function'){res=sub(line.substr(e[0].length+1,line.length-e[0].length-1));}}else return INTERPRETER.error('INCORRECT STATEMENT');},CLOSE:function(line){return INTERPRETER.error('FILE ACCESS NOT IMPLEMENTED');},DATA:function(line){return INTERPRETER.error('DATA NOT IMPLEMENTED');},DEF:function(line){return INTERPRETER.error('DEF NOT IMPLEMENTED');},DIM:function(line){var arr=[],j=0,e;line=line.replace(/^dim\s*/i,'').replace(/\s+$/i,'');for(var i=0;i<line.length;i++){if(line.charAt(i)==')'&&(line.charAt(i+1)==','||i==line.length-1)){arr[arr.length]=line.substr(j,i+1);j=i+2;}}for(i=0;i<arr.length;i++){if((e=arr[i].match(/^([a-z@_]+[a-z0-9@_]*\$?)\(((\s*[0-9]+,?\s*){1,3})\)/i))){e[2]=e[2].replace(/\s/g,'');if(e[2].charAt(e[2].length-1)==',')return INTERPRETER.error('INCORRECT STATEMENT');else{if(INTERPRETER.vars[e[1].toUpperCase()])return INTERPRETER.error('NAME CONFLICT');INTERPRETER.vars[e[1].toUpperCase()]={dim:e[2],arr:{}};}}return INTERPRETER.error('INCORRECT STATEMENT');}},DISPLAY:function(line){return INTERPRETER.error('DISPLAY NOT IMPLEMENTED');},END:function(line){INTERPRETER.p_pos=INTERPRETER.p_lines.length;},FOR:function(line){var e,z;line=line.replace(/^for\s+/i,'');z=line.split(/\s+to\s+/);STATEMENTS['LET'](z[0]);if(INTERPRETER.errflg)return;if((e=z[0].match(/^([a-z@_]+[a-z0-9@_]*)\s*=/i)))z[0]=e[0].replace('=','');else return INTERPRETER.error('INCORRECT STATEMENT');INTERPRETER.f_stack[z[0].toUpperCase()]=INTERPRETER.p_pos+1;if((e=line.match(/to\s+([0-9]+)\s*(step\s+[0-9]+)?/i))){INTERPRETER.f_limit[z[0].toUpperCase()]=parseInt(e[1]);INTERPRETER.f_step[z[0].toUpperCase()]=((line.match(/step/i)&&e[2]!='')?parseInt(e[2].replace(/step\s+/i,'')):1);}else{INTERPRETER.f_stack[z[0].toUpperCase()]=null;return INTERPRETER.error('INCORRECT STATEMENT');}},NEXT:function(line){line=line.replace(/^next\s+/i).replace(/\s+/g,'');if(line.match(/\$\s*$/))return INTERPRETER.error('STRING-NUMBER MISMATCH');if(!INTERPRETER.f_stack[line.toUpperCase()])return INTERPRETER.error('NEXT WITHOUT FOR');INTERPRETER.vars[line.toUpperCase()]=String(parseInt(INTERPRETER.vars[line.toUpperCase()])+parseInt(INTERPRETER.f_step[line.toUpperCase()]));if(!(parseInt(INTERPRETER.vars[line.toUpperCase()])>INTERPRETER.f_limit[line.toUpperCase()]))INTERPRETER.p_pos=INTERPRETER.f_stack[line.toUpperCase()]-1;else{INTERPRETER.f_stack[line.toUpperCase()]=null;INTERPRETER.f_limit[line.toUpperCase()]=null;INTERPRETER.f_step[line.toUpperCase()]=null;}},GOSUB:function(line){line=line.replace(/^gosub\s+/i,'');if(isNaN(parseInt(line)))return INTERPRETER.error('INCORRECT STATEMENT');for(var i=0;i<INTERPRETER.p_lineno.length;i++){if(parseInt(INTERPRETER.p_lineno[i])==parseInt(line)){INTERPRETER.g_stack[INTERPRETER.g_stack.length]=INTERPRETER.p_pos;INTERPRETER.p_pos=i-1;return;}}INTERPRETER.error('BAD LINE NUMBER');},GOTO:function(line){line=line.replace(/^go\s?to\s+/i,'');if(isNaN(parseInt(line)))return INTERPRETER.error('INCORRECT STATEMENT');for(var i=0;i<INTERPRETER.p_lineno.length;i++){if(parseInt(INTERPRETER.p_lineno[i])==parseInt(line)){INTERPRETER.p_pos=i-1;return;}}INTERPRETER.error('BAD LINE NUMBER');},IF:function(line){var e;line=line.replace(/^if\s+/i,'');if((e=line.match(/then\s+([0-9]+)\s+(else\s+[0-9]+)?/i))){line=line.replace(e[0],'').replace(/^\s+/g,'').replace(/\s+$/g,'');var res=parseInt(INTERPRETER.eval_expr(line));if(INTERPRETER.errflg)return;if(isNaN(res))return INTERPRETER.error('BAD EXPRESSION');if(res!=0)return STATEMENTS['GOTO'](e[1]);else if(e[2]&&(e=e[2].match(/else\s+([0-9]+)/i)))return STATEMENTS['GOTO'](e[1]);return;}return INTERPRETER.error('INCORRECT STATEMENT');},INPUT:function(line){return INTERPRETER.error('INPUT NOT IMPLEMENTED');},LET:function(line){var e,i,res,v;line=line.replace(/^let\s+/i,'');if((e=line.match(/^([a-z@_]+[a-z0-9@_]*\$?)/i)))v=e[1];else return INTERPRETER.error('INCORRECT STATEMENT');if(line.charAt(v.length)=='('){if((i=INTERPRETER.eval_array_var(line))){if((e=line.substr(i.explen).match(/^\s*=\s*(.+)$/))){res=String(INTERPRETER.eval_expr(e[1])).replace(/^\s+/,'').replace(/\s+$/,'');if((res.charAt(0)=='"'&&i.name.match(/\$$/))||(!isNaN(parseFloat(res))&&i.name.match(/[^\$]$/)))INTERPRETER.vars[i.name].arr[i.index]=res;else INTERPRETER.error('STRING-NUMBER MISMATCH');}else INTERPRETER.error('INCORRECT STATEMENT');}else if(i&&!i.dim)INTERPRETER.error('NAME CONFLICT');}else{line=line.substr(v.length);if((i=line.match(/^\s*=\s*(.+)$/))){if(INTERPRETER.vars[v.toUpperCase()]&&INTERPRETER.vars[v.toUpperCase()].dim)return INTERPRETER.error('NAME CONFLICT');res=String(INTERPRETER.eval_expr(i[1])).replace(/^\s+/,'').replace(/\s+$/,'');if((res.charAt(0)=='"'&&v.match(/\$$/))||(!isNaN(parseFloat(res))&&v.match(/[^\$]$/)))INTERPRETER.vars[v.toUpperCase()]=res;else INTERPRETER.error('STRING-NUMBER MISMATCH');}else INTERPRETER.error('INCORRECT STATEMENT');}},PRINT:function(line){var fmt=(line=line.replace(/^print\s*/i,'')).split(/[,;:]/);INTERPRETER.inprint=1;if(line!=''){var res=String(INTERPRETER.eval_expr(line));if(INTERPRETER.errflg){INTERPRETER.inprint=0;return;}if(res.charAt(0)=='"')res=res.substr(1,res.length-2);else{res=String(parseFloat(res).toPrecision(10)).replace(/^0+\./,'.').replace(/0+$/,'').replace(/\.$/,'');if(res=='')res='0';}}SCREEN.scroll_back();if(line!='')SCREEN.draw_ti_text(1,SCREEN.HEIGHT-2,res);INTERPRETER.inprint=0;},ON:function(line){var gs,e;line=line.replace(/^on\s+/i,'');if((e=line.match(/(gosub|goto)\s+([0-9,]+)+\s*$/i))){line=line.replace(e[0],'').replace(/^\s+/g,'').replace(/\s+$/g,'');var res=parseInt(INTERPRETER.eval_expr(line));if(INTERPRETER.errflg||isNaN(res)||res==0)return;var ll=e[2].split(/,/);if(!STATEMENTS[e[1].toUpperCase()]||res>ll.length)return INTERPRETER.error('INCORRECT STATEMENT');return STATEMENTS[e[1].toUpperCase()](ll[res-1]);}else return INTERPRETER.error('INCORRECT STATEMENT');},OPEN:function(line){return INTERPRETER.error('FILE ACCESS NOT IMPLEMENTED');},OPTION:function(line){if(!line.match(/^(option\s+)?\s*base\s+[01]/i))return INTERPRETER.error('INCORRECT STATEMENT');return;},RANDOMIZE:function(line){var s,x,ex=0;if((line=line.replace(/^randomize\s*/i,'').replace(/\s*$/,''))==''){INTERPRETER.rndseed=(INTERPRETER.rndseed&0xff00)|INTERPRETER.lastrnd;return;}s=INTERPRETER.eval_expr(line);if(INTERPRETER.errflg)return;x=parseInt(s)<0?(~parseInt(s)+1):parseInt(s);while(parseInt(parseInt(x)/100)>0){ex++;x/=100;}INTERPRETER.rndseed=0x4000+(ex<<8)+x;if(parseInt(s)<0)INTERPRETER.rndseed=~(INTERPRETER.rndseed-1);},READ:function(line){return INTERPRETER.error('READ NOT IMPLEMENTED');},REM:function(line){return;},RETURN:function(line){line=line.replace(/^return\s+/i,'');if(line!='')return INTERPRETER.error('INCORRECT STATEMENT');if(!INTERPRETER.g_stack.length)return INTERPRETER.error('RETURN WITHOUT GOSUB');var x=INTERPRETER.g_stack.pop();INTERPRETER.p_pos=x;},STOP:function(line){STATEMENTS['END'](line);},imm$LIST:function(line){var i;if(INTERPRETER.status==INTERPRETER.STATUS_RUNNING||EDITOR.prog_lines.length==0)return INTERPRETER.error('CAN\'T DO THAT');for(i in EDITOR.prog_lines){SCREEN.scroll_back();SCREEN.draw_ti_text(1,SCREEN.HEIGHT-2,i+' '+EDITOR.prog_lines[i]);}},imm$RUN:function(line){line=line.replace(/^run\s*/i,'');if(INTERPRETER.status==INTERPRETER.STATUS_RUNNING)return INTERPRETER.error('CAN\'T DO THAT');if(line!=''&&!EDITOR.prog_lines[parseInt(line)])return INTERPRETER.error('BAD LINE NUMBER');setTimeout('INTERPRETER.run(EDITOR.prog_lines,'+parseInt(line)+');',1);},imm$NEW:function(line){if(INTERPRETER.status==INTERPRETER.STATUS_RUNNING)return INTERPRETER.error('CAN\'T DO THAT');INTERPRETER.newflg=1;setTimeout('SCREEN.init(SCREEN.screen_id);',1);EDITOR.prog_lines=[];},imm$EDIT:function(line){return INTERPRETER.error('EDIT NOT IMPLEMENTED');},imm$BYE:function(line){STATEMENTS['imm$NEW'](line);},imm$RESEQUENCE:function(line){return INTERPRETER.error('RESEQUENCE NOT IMPLEMENTED');},imm$RES:function(line){STATEMENTS['imm$RESEQUENCE'](line);},imm$NUMBER:function(line){return INTERPRETER.error('NUMBER NOT IMPLEMENTED');},imm$NUM:function(line){STATEMENTS['imm$NUMBER'](line);},imm$DELETE:function(line){return INTERPRETER.error('FILE ACCESS NOT IMPLEMENTED');},imm$OLD:function(line){return INTERPRETER.error('FILE ACCESS NOT IMPLEMENTED');},BREAK:function(line){if(INTERPRETER.status==INTERPRETER.STATUS_IDLE)return STATEMENTS['imm$BREAK'];INTERPRETER.status=INTERPRETER.STATUS_BREAK;EDITOR.show_error('BREAKPOINT AT '+INTERPRETER.lineno);SCREEN.set_bg_color(8);EDITOR.reset_buffer();},imm$BREAK:function(line){var u=line.match(/^un/)?1:0;line=line.replace(/^(un)?break\s*/i,'');if(line=='')return INTERPRETER.error('CAN\'T DO THAT');var ll=line.split(/,/);var x;for(var i in ll){x=0;for(var j=0;j<INTERPRETER.breaks.length;j++){if(INTERPRETER.breaks[j]==i){if(u)INTERPRETER.breaks.splice(j,1);else x++;break;}}if(!x)INTERPRETER.breaks[INTERPRETER.breaks.length]=i;}},imm$UNBREAK:function(line){return STATEMENTS['imm$BREAK'](line);},imm$CON:function(line){line=line.replace(/^con(tinue)?\s*/i);if(line!='')return INTERPRETER.error('INVALID STATEMENT');INTERPRETER.breakflg=0;setTimeout('INTERPRETER.run_one();',1);},imm$CONTINUE:function(line){STATEMENTS['imm$CON'](line);},imm$TRACE:function(line){line=line.replace(/^trace\s*/i,'');if(line!='')return INTERPRETER.error('INCORRECT STATEMENT');INTERPRETER.traceflg=1;},imm$UNTRACE:function(line){line=line.replace(/^untrace\s*/i,'');if(line!='')return INTERPRETER.error('INCORRECT STATEMENT');INTERPRETER.traceflg=0;},};var SUBROUTINES={CHAR:function(line){var x=line.replace(/\s*,\s*/g,',').replace(/\)\s*$/,'').split(/,/);if(x.length!=2)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=String(INTERPRETER.eval_expr(x[0]));x[1]=String(INTERPRETER.eval_expr(x[1]));if(parseInt(x[0])>159)return INTERPRETER.error('BAD VALUE');if(INTERPRETER.errflg)return;SCREEN.USER_CHAR_TBL[parseInt(x[0])]=x[1].replace(/"/g,'');for(var i=0;i<SCREEN.CHARS.length;i++)if(SCREEN.CHARS[i]==parseInt(x[0]))setTimeout('SCREEN.draw_ti_char('+parseInt(i%SCREEN.WIDTH)+','+parseInt(i/SCREEN.WIDTH)+','+parseInt(x[0])+');',1);},CLEAR:function(line){if(line.length!=0)return INTERPRETER.error('INCORRECT STATEMENT');setTimeout('SCREEN.init(SCREEN.screen_id);',1);},COLOR:function(line){var x=line.replace(/\s*,\s*/g,',').split(/,/);if(x.length!=3)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=parseInt(INTERPRETER.eval_expr(x[0]));x[1]=parseInt(INTERPRETER.eval_expr(x[1]));x[2]=parseInt(INTERPRETER.eval_expr(x[2]));if(INTERPRETER.errflg)return;if(x[0]<1||x[0]>16||x[1]<1||x[1]>16||x[2]<1||x[2]>16)return INTERPRETER.error('BAD VALUE');SCREEN.FG_TBL[x[0]]=x[1];SCREEN.BG_TBL[x[0]]=x[2];for(var i=0;i<SCREEN.CHARS.length;i++)if(parseInt((SCREEN.CHARS[i]-32)/8)==parseInt(x[0]))setTimeout('SCREEN.draw_ti_char('+parseInt(i%SCREEN.WIDTH)+','+parseInt(i/SCREEN.WIDTH)+','+SCREEN.CHARS[i]+');',1);},GCHAR:function(line){var x=line.replace(/\s*,\s*/g,',').split(/,/);if(x.length!=3)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=parseInt(INTERPRETER.eval_expr(x[0]))-1;x[1]=parseInt(INTERPRETER.eval_expr(x[1]))-1;if(INTERPRETER.errflg)return;STATEMENTS['LET'](x[2]+'='+SCREEN.CHARS[(SCREEN.WIDTH*x[1])+x[0]]);},HCHAR:function(line){var r=0,x=line.replace(/\s*,\s*/g,',').split(/,/);if(x.length<3||x.length>4)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=parseInt(INTERPRETER.eval_expr(x[0]))-1;x[1]=parseInt(INTERPRETER.eval_expr(x[1]))-1;x[2]=parseInt(INTERPRETER.eval_expr(x[2]));if(x.length>3)r=parseInt(INTERPRETER.eval_expr(x[3]));if(INTERPRETER.errflg)return;for(var i=0;i<r;i++)setTimeout('SCREEN.draw_ti_char('+(x[0]+i)+','+x[1]+','+x[2]+');',1);},JOYST:function(line){return INTERPRETER.error('JOYSTICK NOT IMPLEMENTED');},KEY:function(line){var x=line.replace(/\s*,\s*/g,',').split(/,/);if(x.length!=3)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=parseInt(INTERPRETER.eval_expr(x[0]))-1;if(INTERPRETER.errflg)return;STATEMENTS['LET'](x[1]+'='+INTERPRETER.key);STATEMENTS['LET'](x[2]+'='+(INTERPRETER.key==0)?0:(INTERPRETER.key==INTERPRETER.last_key)?-1:1);},SCREEN:function(line){var res=INTERPRETER.eval_expr('('+line);if(INTERPRETER.errflg)return;if(res[0].charAt(0)=='"')return INTERPRETER.error('STRING-NUMBER MISMATCH');SCREEN.set_bg_color(parseInt(res[0]));},SOUND:function(line){return INTERPRETER.error('SOUND NOT IMPLEMENTED');},VCHAR:function(line){var r=0,x=line.replace(/\s*,\s*/g,',').split(/,/);if(x.length<3||x.length>4)return INTERPRETER.error('INCORRECT STATEMENT');x[0]=parseInt(INTERPRETER.eval_expr(x[0]))-1;x[1]=parseInt(INTERPRETER.eval_expr(x[1]))-1;x[2]=parseInt(INTERPRETER.eval_expr(x[2]));if(x.length>3)r=parseInt(INTERPRETER.eval_expr(x[3]));if(INTERPRETER.errflg)return;for(var i=0;i<r;i++)setTimeout('SCREEN.draw_ti_char('+x[0]+','+(x[1]+i)+','+x[2]+');',1);},};
