diff options
Diffstat (limited to 'treetree.js')
-rwxr-xr-x | treetree.js | 77 |
1 files changed, 60 insertions, 17 deletions
diff --git a/treetree.js b/treetree.js index 2b186ae..d0d863e 100755 --- a/treetree.js +++ b/treetree.js @@ -47,16 +47,23 @@ function extractCmd(code,startat){ var i; while((code[startat]==" "||code[startat]=="\t"||code[startat]=="\n")&&startat<code.length)startat++; if(startat==code.length)return false; - if(code[startat]=='"'||code[startat]=="'"||code[startat]=="{"){ + if(code[startat]=='"'){ //string var term; term=""; for(i=startat+1;i<code.length;i++){ - if(code[i]==code[startat]||(code[startat]=="{"&&code[i]=="}"))break; + if(code[i]=="\"")break; term+=code[i]; } - if(code[startat]=="'"||code[startat]=="{")return extractCmd(code,i+1); //'this is a comment,' {and this too.} return [["str",term],i+1]; - } else if(code[startat].match(/[0-9]|-|\./)){ + } else if(code[startat]=="'"||code[startat]=="{"){ //comment + var term; + term=""; + for(i=startat+1;i<code.length;i++){ + if((code[startat]=="{"&&code[i]=="}")||(code[startat]=="'"&&code[i]=="\n"))break; + term+=code[i]; + } + return extractCmd(code,i+1); //{this is a comment,} 'and the rest of the line now too + } else if(code[startat].match(/[0-9]|-|\./)){ //number var neg,dot; i=startat; neg=code[startat]=="-"; @@ -70,17 +77,17 @@ function extractCmd(code,startat){ if(!code[i].match(/[0-9]/))break; } return [[dot?"flt":"int",parseFloat(code.slice(startat,i))],i]; - } else if(code[startat]=="#"){ + } else if(code[startat]=="#"){ //labeldef var term; for(i=startat+1;i<code.length;i++) if(code[i]==" "||code[i]=="\t"||code[i]=="\n")break; return [["lbl",code.slice(startat+1,i)],i]; - } else if(code[startat]=="@"){ + } else if(code[startat]=="@"){ //labelref var term; for(i=startat+1;i<code.length;i++) if(code[i]==" "||code[i]=="\t"||code[i]=="\n")break; return [["ref",code.slice(startat+1,i)],i]; - } else { + } else { //command or shit var term; if(code.length-startat<3)throw new Error("Invalid command '"+code.slice(startat)+"' at end of code with less than 3 characters!"); term=code.slice(startat,startat+3).toUpperCase(); @@ -302,9 +309,45 @@ function runCmds(cmds,TR){ else TR=[TR[0].length,TR,false]; break; case "SPL": - throw new TTerror("SPL not implemented!"); + if(TR==false)throw new TTerror("Cannot SPL with a non-existant root node!"); + if(TR[1]==false)throw new TTerror("No string in tree for SPL!"); + if(TR[2]!=false||TR[1][2]!=false)throw new TTerror("Unexpected secondary children while SPL'ing!"); + if(typeof TR[0]!="number"||TR[0]!=~~TR[0])throw new TTerror("Root node is not an integer in SPL!"); + if(typeof TR[1][0]!="string")throw new TTerror("Root's primary child is not a string in SPL!"); + if(TR[0]<0||TR[0]>TR[1][0].length)throw new TTerror("Invalid split position "+TR[0]+" in SPL!"); + if(TR[0]==0)TR[0]=""; + else { + TR[0]=TR[1][0].slice(0,TR[0]); + TR[1][0]=TR[1][0].slice(TR[0]); + } + break; + case "SUM": + var total,item,addto; + if(TR==false||TR[1]==false)throw new TTerror("Not enough tree items in SUM!"); + if(TR[2]!=false&&TR[1][2]!=false)throw new TTerror("Only one base node can have secondary children in SUM!"); + if(TR[2]==false&&TR[1][2]==false){ + TR[0]+=TR[1][0]; + TR[1]=TR[1][1]; + break; + } + if(TR[2]!=false){ + addto=TR[1][0]; + TR[1]=TR[1][1]; + TR[0]=TR[0]+addto; + for(item=TR[2];item!=false;item=TR[2]){ + if(item[1]!=false)throw new TTerror("No primary children allowed in chain of secondary children (in SUM)!"); + item[0]=item[0]+addto; + } + } else { + addto=TR[0]; + TR=TR[1]; + TR[0]=addto+TR[0]; + for(item=TR[2];item!=false;item=TR[2]){ + if(item[1]!=false)throw new TTerror("No primary children allowed in chain of secondary children (in SUM)!"); + item[0]=addto+item[0]; + } + } break; - case "SUM":break; case "DIF":break; case "PRO":break; case "QUO":break; @@ -326,7 +369,7 @@ function runCmds(cmds,TR){ case "TAU":break; case "E__":break; case "RND": - TR=[~~(Math.pow(2,31)*Math.random()),TR,false]; + TR=[~~(/*Math.pow(2,31)*/10*Math.random()),TR,false]; break; case "JMP":break; case "EXJ":break; @@ -373,14 +416,14 @@ this node) and the third containing the second child. An absent child is of course indicated with the literal value `false`. For example, the following program: 1 2 3 SWP 4 -makes the tree (*'s indicating absence of children, / is a primary child, -- +makes the tree (*'s indicating absence of children, | is a primary child, -- is a secondary child): - 4 * - / - 3--2 * - * / - 1 * - * +4* +| +3--2* +* | + 1* + * and is represented in `TR` like: [4, [3, |