Advertisement
Guest User

typeof function

a guest
May 16th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. //Typeof for primitives
  2. function typeof(v){
  3.     let s = ToString(v);
  4.    
  5.     if(s=="true" || s=="false"){return TYPE_BOOL;} //TODO can't dist true and "true"
  6.    
  7.     //Try to distinguish TYPE_REAL
  8.     if(length(SplitString(s,"1234567890.-"))==0){   //represents a valid number and nothing else
  9.         if(length(SplitString(s,"-")) == 1){
  10.             if(length(SplitString(s,".")) == 2){
  11.                 if( length(SplitString(s,".")[1]) == 6 ){ //six decimal places
  12.                     if( ator(s~"1") != 0 ){ //represents a valid number
  13.                         return TYPE_REAL;
  14.                     } //else{WriteLog("not a valid number");}
  15.                 } // else{WriteLog("wrong number of decimal places");}
  16.             } // else{WriteLog("wrong number of .");}
  17.         } // else{WriteLog("too many -");}
  18.     } // else{WriteLog("non-numeric characters");}
  19.  
  20.     //Try to distinguish TYPE_CHAR
  21.     if( length(s)==1 ){    //TODO: can't distinguish 'a' and "a"
  22.         // if( (v+0)!=ator(v) ){ //no good, assumes type
  23.         // if (s[0]+0)!=ator(s) ){ //cast to real ASCII value - still doesn't actually help atm
  24.             return TYPE_CHAR;
  25.         // }
  26.     }
  27.  
  28.     //Try to distinguish TYPE_ARRAY
  29.     if(strcontains(s,"[]")){
  30.         if(s[0]=='[' && s[length(s)-1]==']'){ //same number of right and left
  31.             if(length(SplitString(s,"[")) == length(SplitString(s,"]"))){ //balanced
  32.                 if(length(SplitString(s,"[,]"))!=0 || !strcontains(s,",") ){
  33.                     if(s!="[]"){ // ToString([]) is blank
  34.                         return TYPE_ARRAY;                     
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40.    
  41.     if(length(SplitString(s,"#"))==2 && length(SplitString(s,"."))==2 ){ //might be undefined
  42.         if(s[0..3]=="1.#" || s[0..4]=="-1.#"){
  43.             return TYPE_UNDEFINED;
  44.         }
  45.     }
  46.    
  47.     return TYPE_STRING;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement