Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////
- //Primitive types
- let TYPE_INT = 9;
- let TYPE_REAL = 2; let TYPE_NUMBER = 2;
- let TYPE_BOOL = 2+10;
- let TYPE_CHAR = -1;
- let TYPE_ARRAY = 10;
- let TYPE_STRING = 3; //10+1;
- //Improved typeof function
- // -Lumps int and real together since I usually just care if it's a number
- // -Distinguishes between array and string
- // -Distinguishes between real and bool
- // -Lump enemy and boss together
- function typeof(obj){
- WriteLog(ToString(obj)~" has native type "~TypeToString(Obj_GetType(obj)));
- alternative(Obj_GetType(obj))
- case(TYPE_INT){return TYPE_REAL;}
- case(TYPE_REAL){
- if(ToString(obj)=="true" || ToString(obj)=="false"){return TYPE_BOOL;}
- else{ return TYPE_REAL; }
- }
- case(TYPE_ARRAY){
- if(length(obj) > 0){
- let contents = typeof(obj[0]); //recursive
- if(contents==TYPE_CHAR){return TYPE_STRING;}
- }
- return TYPE_ARRAY;
- }
- case(TYPE_CHAR){return TYPE_CHAR;}
- // case(OBJ_ENEMY,OBJ_ENEMY_BOSS){return OBJ_ENEMY;}
- case(103,104){return 103;} //so can use in package
- others{return Obj_GetType(obj);}
- // others{return atoi(ToString(Obj_GetType(obj)));} //alright listen up fucker
- }
- //////////////////////////////
- //smarter, easier-to-remember tostring
- function tost(n){
- let str = ToString(n);
- let arr = SplitString(n,'.');
- if(length(arr)==2){
- if( ator(arr[1])==0 ){
- str = arr[0];
- }
- else if( rtoa(ator(n))==rtoa(n) ){
- let end = min(2,length(arr[1]));
- str = arr[0]~"."~arr[1][0..end];
- }
- }
- return str;
- }
- function cast(v,type){
- let r;
- WriteLog("Casting "~tost(v)~" to type "~ToString(type));
- WriteLog("(Types "~TypeToString(typeof(v))~", "~TypeToString(typeof(type))~")");
- alternative(typeof(type))
- case(TYPE_NUMBER){
- WriteLog("That type... is a NUMBER.");
- let s = ToString(v); //for the sake of convenience
- alternative(atoi(ToString(type))) //alright fucker you wanna play ball?
- case(TYPE_INT){ r = atoi(s); }
- case(TYPE_REAL){ r = ator(s); }
- case(TYPE_STRING){ r = tost(v); }
- case(TYPE_BOOL){ r = (s=="true" || atoi(s)!=0); }
- case(TYPE_CHAR){ r = s[0]; }
- case(TYPE_ARRAY){ r = [v]; }
- others{ r = cast(v,TYPE_NUMBER); }
- // others{ r = NULL; }
- }
- case(TYPE_ARRAY,TYPE_STRING){
- WriteLog("That type is an ARRAY of depth "~tost(depth(type)));
- // if(length(type)>0){ r = cast(v,typeof(type[0])); }
- if(length(type)>0){ r = cast(v,type[0]); }
- else{ r = [v]; }
- }
- others{
- WriteLog("Gee I've never seen a type whose type was "~TypeToString(type)~" before");
- r = type;
- // r = NULL;
- }
- WriteLog("Result: "~tost(r)~" (type "~TypeToString(typeof(r))~")");
- if(!compatible(r,type)){ WriteLog("UWU we made a fucky wucky"); }
- return r;
- }
- function compatible(a,b){
- WriteLog("Are...");
- WriteLog(a);
- WriteLog(b);
- WriteLog("(Types "~tost(typeof(a))~", "~tost(typeof(b))~")");
- WriteLog("(Depths "~tost(depth(a))~","~tost(depth(b))~")");
- WriteLog("(Lengths "~tost(slength(a))~","~tost(slength(b))~")");
- WriteLog("...Compatible?");
- if(typeof(a)==typeof(b)){
- if(typeof(a)!=TYPE_ARRAY){
- WriteLog("Yes ("~tost(typeof(a))~"=="~tost(typeof(b))~"!="~tost(TYPE_ARRAY)~")");
- return true;
- }
- else{
- if(length(a)==0 || length(b)==0){ return true; }
- else{
- WriteLog("We need to go deeper");
- return compatible(a[0],b[0]);
- }
- }
- }
- else{
- return false;
- }
- }
- //safe length
- function slength(a){
- if(typeof(a)!=TYPE_ARRAY && typeof(a)!=TYPE_STRING){
- WriteLog(ToString(a)~" is not an array or string -> length 0");
- return 0;
- }
- else{return length(a);}
- }
- //Depth of an array
- function depth(a){
- if(slength(a)>0){
- return depth(a[0]) + 1;
- }
- else{ return 0; }
- }
- function TypeToString(t){
- let r;
- alternative(t)
- case(TYPE_INT) {r = "TYPE_INT";}
- case(TYPE_REAL) {r = "TYPE_REAL";}
- case(TYPE_ARRAY) {r = "TYPE_ARRAY";}
- case(TYPE_STRING){r = "TYPE_STRING";}
- case(TYPE_BOOL) {r = "TYPE_BOOL";}
- case(TYPE_CHAR) {r = "TYPE_CHAR";}
- others{ r = "TYPE_COMPLEX"; }
- return r~"("~itoa(t)~")";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement