Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Typeof for primitives
- function typeof(v){
- let s = ToString(v);
- if(s=="true" || s=="false"){return TYPE_BOOL;} //TODO can't dist true and "true"
- //Try to distinguish TYPE_REAL
- if(length(SplitString(s,"1234567890.-"))==0){ //represents a valid number and nothing else
- if(length(SplitString(s,"-")) == 1){
- if(length(SplitString(s,".")) == 2){
- if( length(SplitString(s,".")[1]) == 6 ){ //six decimal places
- if( ator(s~"1") != 0 ){ //represents a valid number
- return TYPE_REAL;
- } //else{WriteLog("not a valid number");}
- } // else{WriteLog("wrong number of decimal places");}
- } // else{WriteLog("wrong number of .");}
- } // else{WriteLog("too many -");}
- } // else{WriteLog("non-numeric characters");}
- //Try to distinguish TYPE_CHAR
- if( length(s)==1 ){ //TODO: can't distinguish 'a' and "a"
- // if( (v+0)!=ator(v) ){ //no good, assumes type
- // if (s[0]+0)!=ator(s) ){ //cast to real ASCII value - still doesn't actually help atm
- return TYPE_CHAR;
- // }
- }
- //Try to distinguish TYPE_ARRAY
- if(strcontains(s,"[]")){
- if(s[0]=='[' && s[length(s)-1]==']'){ //same number of right and left
- if(length(SplitString(s,"[")) == length(SplitString(s,"]"))){ //balanced
- if(length(SplitString(s,"[,]"))!=0 || !strcontains(s,",") ){
- if(s!="[]"){ // ToString([]) is blank
- return TYPE_ARRAY;
- }
- }
- }
- }
- }
- if(length(SplitString(s,"#"))==2 && length(SplitString(s,"."))==2 ){ //might be undefined
- if(s[0..3]=="1.#" || s[0..4]=="-1.#"){
- return TYPE_UNDEFINED;
- }
- }
- return TYPE_STRING;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement