Advertisement
Guest User

type functions

a guest
May 15th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.05 KB | None | 0 0
  1. ///////////////////////////////////////////////
  2. //Primitive types
  3. let TYPE_INT = 9;
  4. let TYPE_REAL = 2; let TYPE_NUMBER = 2;
  5. let TYPE_BOOL = 2+10;
  6. let TYPE_CHAR = -1;
  7. let TYPE_ARRAY = 10;
  8. let TYPE_STRING = 3; //10+1;
  9. //Improved typeof function
  10. // -Lumps int and real together since I usually just care if it's a number
  11. // -Distinguishes between array and string
  12. // -Distinguishes between real and bool
  13. // -Lump enemy and boss together
  14. function typeof(obj){
  15.     WriteLog(ToString(obj)~" has native type "~TypeToString(Obj_GetType(obj)));
  16.     alternative(Obj_GetType(obj))
  17.     case(TYPE_INT){return TYPE_REAL;}
  18.     case(TYPE_REAL){
  19.         if(ToString(obj)=="true" || ToString(obj)=="false"){return TYPE_BOOL;}
  20.         else{ return TYPE_REAL; }
  21.     }
  22.     case(TYPE_ARRAY){
  23.         if(length(obj) > 0){
  24.             let contents = typeof(obj[0]); //recursive
  25.             if(contents==TYPE_CHAR){return TYPE_STRING;}
  26.         }
  27.         return TYPE_ARRAY;
  28.     }
  29.     case(TYPE_CHAR){return TYPE_CHAR;}
  30.     // case(OBJ_ENEMY,OBJ_ENEMY_BOSS){return OBJ_ENEMY;}
  31.     case(103,104){return 103;} //so can use in package
  32.     others{return Obj_GetType(obj);}
  33.     // others{return atoi(ToString(Obj_GetType(obj)));} //alright listen up fucker
  34. }
  35. //////////////////////////////
  36.  
  37.  
  38.  
  39. //smarter, easier-to-remember tostring
  40. function tost(n){
  41.     let str = ToString(n);
  42.     let arr = SplitString(n,'.');
  43.     if(length(arr)==2){
  44.         if( ator(arr[1])==0 ){
  45.             str = arr[0];
  46.         }
  47.         else if( rtoa(ator(n))==rtoa(n) ){
  48.             let end = min(2,length(arr[1]));
  49.             str = arr[0]~"."~arr[1][0..end];
  50.         }
  51.     }
  52.     return str;
  53. }
  54.  
  55.  
  56.  
  57. function cast(v,type){
  58.     let r;
  59.    
  60.     WriteLog("Casting "~tost(v)~" to type "~ToString(type));
  61.     WriteLog("(Types "~TypeToString(typeof(v))~", "~TypeToString(typeof(type))~")");
  62.    
  63.     alternative(typeof(type))
  64.     case(TYPE_NUMBER){
  65.         WriteLog("That type... is a NUMBER.");
  66.         let s = ToString(v); //for the sake of convenience
  67.         alternative(atoi(ToString(type))) //alright fucker you wanna play ball?
  68.         case(TYPE_INT){     r = atoi(s); }
  69.         case(TYPE_REAL){    r = ator(s); }
  70.         case(TYPE_STRING){  r = tost(v); }
  71.         case(TYPE_BOOL){    r = (s=="true" || atoi(s)!=0); }
  72.         case(TYPE_CHAR){    r = s[0]; }
  73.         case(TYPE_ARRAY){   r = [v]; }
  74.         others{ r = cast(v,TYPE_NUMBER); }
  75.         // others{ r = NULL; }
  76.     }
  77.     case(TYPE_ARRAY,TYPE_STRING){
  78.         WriteLog("That type is an ARRAY of depth "~tost(depth(type)));
  79.         // if(length(type)>0){ r = cast(v,typeof(type[0])); }
  80.         if(length(type)>0){ r = cast(v,type[0]); }
  81.         else{ r = [v]; }
  82.     }
  83.     others{
  84.         WriteLog("Gee I've never seen a type whose type was "~TypeToString(type)~" before");
  85.         r = type;
  86.         // r = NULL;
  87.     }
  88.     WriteLog("Result: "~tost(r)~" (type "~TypeToString(typeof(r))~")");
  89.     if(!compatible(r,type)){ WriteLog("UWU we made a fucky wucky"); }
  90.     return r;
  91. }
  92.  
  93.  
  94. function compatible(a,b){
  95.     WriteLog("Are...");
  96.     WriteLog(a);
  97.     WriteLog(b);
  98.     WriteLog("(Types "~tost(typeof(a))~", "~tost(typeof(b))~")");
  99.     WriteLog("(Depths "~tost(depth(a))~","~tost(depth(b))~")");
  100.     WriteLog("(Lengths "~tost(slength(a))~","~tost(slength(b))~")");
  101.     WriteLog("...Compatible?");
  102.     if(typeof(a)==typeof(b)){
  103.         if(typeof(a)!=TYPE_ARRAY){
  104.             WriteLog("Yes ("~tost(typeof(a))~"=="~tost(typeof(b))~"!="~tost(TYPE_ARRAY)~")");
  105.             return true;
  106.         }
  107.         else{
  108.             if(length(a)==0 || length(b)==0){ return true; }
  109.             else{
  110.                 WriteLog("We need to go deeper");
  111.                 return compatible(a[0],b[0]);
  112.             }
  113.         }
  114.     }
  115.     else{
  116.         return false;
  117.     }
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. //safe length
  126. function slength(a){
  127.     if(typeof(a)!=TYPE_ARRAY && typeof(a)!=TYPE_STRING){
  128.         WriteLog(ToString(a)~" is not an array or string -> length 0");
  129.         return 0;
  130.     }
  131.     else{return length(a);}
  132. }
  133. //Depth of an array
  134. function depth(a){
  135.     if(slength(a)>0){
  136.         return depth(a[0]) + 1;
  137.     }
  138.     else{ return 0; }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. function TypeToString(t){
  154.     let r;
  155.     alternative(t)
  156.     case(TYPE_INT)   {r = "TYPE_INT";}
  157.     case(TYPE_REAL)  {r = "TYPE_REAL";}
  158.     case(TYPE_ARRAY) {r = "TYPE_ARRAY";}
  159.     case(TYPE_STRING){r = "TYPE_STRING";}
  160.     case(TYPE_BOOL)  {r = "TYPE_BOOL";}
  161.     case(TYPE_CHAR)  {r = "TYPE_CHAR";}
  162.     others{ r = "TYPE_COMPLEX"; }
  163.     return r~"("~itoa(t)~")";
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement