Sabdt

From Esolang
Jump to navigation Jump to search

Sabdt (sab*dit) or "strings are best data type" is an esolang made by User:Aspwil (talk). It is simplistic in nature and relatively easy to code in. It was spawned from the idea that many esolangs are based around numbers, changing values of numbers in cells, printing out cells converted to chars, or taking in text and converting it to numbers etc. This esolang has no inbuilt number system, so the only things you can natively do with it are manipulate and compare strings (along with flow control, variables, functions, input and printing)

Language overview

Basics

The most basic functions in Sabdt are commenting, printing, concatenation, variable declaration, and user input.


Commenting is done with //.


Printing is done with the pr command:

pr "hello world";

will print hello world.


Concatenation is done with the + sign:

pr "a"+"b";

will print ab.


User input is obtained with the char $ and is read in as a line:

pr $;

will print the user's input.


Variables can only hold strings. They can be used in exactly the same way as normal strings. all variable names have to be numbers.

0:"hello world";
pr 0;

will print hello world.


Variables can also be assigned recursively:

0:"hello ";
0:0+"world";
pr 0;

will print hello world.

Flow Control

There are 2 flow control statements available in Sabdt, the if-elseif statement and the while loop. Both take comparisons as their inputs.


Comparisons are a statement that is either true or false. The comparison operators are:

string1 = string2 is true if the content of the strings are the same, ex. "a"="a".

string1 != string2 is true if the content of the strings are not the same, ex. "a"!="b".

These can be strung together to make more complex statements with the use of the && (and) and || (or) operators:

string1 = string2 && string3 = string4 is true if the first two strings are the same and the second two are the same, ex. "a"="a"&&"b"="b".

string1 = string2 || string3 = string4 is true if the first two strings are the same or the second two are the same, ex. "a"="a"||"b"="c".


The if statement looks like this:

if(comp){
  //code here
}el if(comp){
  //code here
}el{
  //code here
}

and the while loop looks like this:

wh(comp){
//code here
}

Regex

Regular expressions are the most useful tool in the Sabdt toolbox. They are called upon a variable or string using string.r("regex") and return the first matched section, or the first group.

0:"abcde5fg";
pr 0.r("\d");

and

pr "abcde5fg".r("\d");

will both print 5

these can also be nested

pr "abcde5fg".r("\d.").r(".$");

will print f


A second regex function also exists: string.rdel("regex")

This function when called will return the string with the selection removed.

Functions

There are a few functions built into by Sabdt. All functions take in strings and return one string.

The map function takes in a string and returns a string.

ma(s1){
  "a">"b";
  "c">"d";
}

This function will return the string corresponding to the input. If no matching string is found it will return "", an empty string.

This function can also evaluate and return a non-literal string, such as "a">0; or "a">0+""; and even the output of a function "a">_1(0); (note this will still run any print commands specified in the function).

so ma("a"){"a">"b";"c">"d";} will return "b"

ma("c"){"a">"b";"c">"d";} will return "d"

and ma("b"){"a">"b";"c">"d";} will return "" since there is no matching string.


Function declaration

You can add custom functions with this format:

_num(number of inputs){

//code here, inputs are ^1 to ^x where x is the number of inputs

ret "return string";

}

and call them like this: _1(1,2,3...x)

EX.

_1(0){
  ret "hello world";
}

will return "hello world"

_1(1){
  ret ^1;
}

will return the input.

Variables defined in a function are only in the scope of that function, they can't be called outside of said function and vice versa. Because of this, variable names can be used both in and out of a function without altering each other.

EX:

0:"a";
_1(1){
  0:^1;
  ret 0;
}
_1("b");
pr 0;

will print a, not b.

Functions can call other functions or themselves.

Functions can have a function deceleration inside of themselves, but this function can only be called from within the function it's defined in.

Libraries

Libraries are sets of functions that can be called through the library name, they allow for modular code, or code that can be shared between programs as in sharing code without rewriting to deal with using the same variable numbers

libraries are defined with a vertical bar | followed by a name made up of only alphabetical and numerical characters

EX:

|exlib{
  _0(1){
    pr ^1;
    ret "";
  }
}

and functions are called from a library using name_num(inputs)

for the example above if you ran exlib_0("hello world"); the program would print out "hello world"


they can also run code that is not a function definition although that code will only be run when the library is first loaded

EX:

|exlib{
  _0(1){
    pr ^1;
    ret "";
  }
pr "library exlib loaded";
}

will print "library exlib loaded" once the library is loaded


library functions can only be called after the library definition in code. libraries can be loaded anywhere in the code (not just at the beginning). libraries can be over written if a new library is loaded with the same name.

there is a second way to define a library, calling |"string"; where the string is the libraries code

EX:

|"exlib{_0(1){pr ^1;ret \"\";}}";

does the same thing as

|exlib{
  _0(1){
    pr ^1;
    ret "";
  }
}

a string containing the entire libraries defined code cane be gotten by useing lib_data

so we can do this:

|exlib{
  _0(1){
    pr ^1;
    ret "";
  }
}
pr exlib_data;

will print exlib{_0(1){pr ^1;ret "";}}

this is interesting because it allows for modifying libraries on the fly

as example calling |lib_data; creates a new library with the same name as the original library (overwriting it) with same content as the original (which is completely useless)

this also allows you to convert a sting into run-able code on the fly

for instance:

0:"pr \"hello world\";";
|"templib{_0(0){"+0+"ret \"\";}}";
templib_0();

will print "hello world"

another neat effect is the ability to add new functions to already existing libraries during execution

EX:

|exlib{
  _0(0){
    pr "you have called _0()";
  }
}
|exlib_data.rdel(".$")+"_1{pr \"you have called _1()\";}"+"}";
exlib_1();

will print "you have called _1()"

Code examples

warning: these programs were created by Aspwil (talk) and at the time of creation there is no interpreter so they may have errors, this warning will be removed once they are verified to work

Hello world

pr "hello world";

More fancy hello world

0:"dlrow olleh";
wh(0!=""){
  pr 0.r(".$");
  0:0.r("(.)(?!$)");
}

this code takes the "dlrow olleh" string and prints it out in reverse

Truth machine

if($="1"){
  wh(""=""){
    pr "1";
  }
}el{
  pr "0";
}

Positive integers addition

numbers must be in the "x" format where there are only numbers and no spaces

0:$;
1:$;
wh(1!="0"){
  2:1.r(".$");
  3:1.rdel(".$");
  if(2!="0"){ 
    1:3+ma(2){"1">"0";"2">"1";"3">"2";"4">"3";"5">"4";"6">"5";"7">"6";"8">"7";"9">"8";};
  }el{
    4:"9";
    wh(3.r(".$")="0"){
      3:3.rdel(".$");
      4:4+"9";
    }
	3:3.rdel(".$")+ma(3.r(".$")){"1">"0";"2">"1";"3">"2";"4">"3";"5">"4";"6">"5";"7">"6";"8">"7";"9">"8";};
    1:3+4;
  }
  5:1.r(".$");
  6:1.rdel(".$");
  if(5!="9"){ 
    0:6+ma(5){"0">"1";"1">"2";"2">"3";"3">"4";"4">"5";"5">"6";"6">"7";"7">"8";"8">"9";};
  }el{
    7:"0";
    wh(6.r(".$")="9"){
      6:6.r("(.)(?!$)");
      7:7+"0";
    }
	6:6.rdel(".$")+ma(6.r(".$")){"0">"1";"1">"2";"2">"3";"3">"4";"4">"5";"5">"6";"6">"7";"7">"8";"8">"9";};
    0:6+7;
  }
}
pr 0;

Positive Int Multiplication

_0(1){//decrese num func
  0:^1.r(".$");
  1:^1.rdel(".$");
  if(0!="0"){ 
    ret 1+ma(0){"1">"0";"2">"1";"3">"2";"4">"3";"5">"4";"6">"5";"7">"6";"8">"7";"9">"8";};
  }el{
    2:"9";
    wh(1.r(".$")="0"){
      1:1.rdel(".$");
  	  2:2+"9";
    }
    1:1.rdel(".$")+ma(1.r(".$")){"1">"0";"2">"1";"3">"2";"4">"3";"5">"4";"6">"5";"7">"6";"8">"7";"9">"8";};
    ret 1+2;
  }
}
_1(1){//increse num func
  0:^1.r(".$");
  1:^1.rdel(".$");
  if(0!="9"){ 
    ret 1+ma(0){"0">"1";"1">"2";"2">"3";"3">"4";"4">"5";"5">"6";"6">"7";"7">"8";"8">"9";};
  }el{
    2:"0";
    wh(1.r(".$")="9"){
      1:1.rdel(".$");
      2:2+"0";
    }
	1:1.rdel(".$")+ma(1.r(".$")){"0">"1";"1">"2";"2">"3";"3">"4";"4">"5";"5">"6";"6">"7";"7">"8";"8">"9";};
    ret 1+2;
  }
}

_2(2){//add 2 nums func
  0:^1;
  1:^2;
  wh(1!="0"){
    0:_1(0);
    1:_0(1);
  }
  ret 0;
}
//mult 2 nums
0:$;
1:$;
2:"0";
wh(1!="0"){
  2:_2(2,0);
  1:_0(1);
}

Brainfuck Interpreter (Nested Loops up to 254 times)

//libraries
//incdec is a library to increse/decrese numbers by 1 (wraped as 255)
|incdec{
  _0(1){//increase num by 1
    ret ma(^1){
      "255">"000";"254">"255";"253">"254";"252">"253";"251">"252";"250">"251";"249">"250";"248">"249";
      "247">"248";"246">"247";"245">"246";"244">"245";"243">"244";"242">"243";"241">"242";"240">"241";
      "239">"240";"238">"239";"237">"238";"236">"237";"235">"236";"234">"235";"233">"234";"232">"233";
      "231">"232";"230">"231";"229">"230";"228">"229";"227">"228";"226">"227";"225">"226";"224">"225";
      "223">"224";"222">"223";"221">"222";"220">"221";"219">"220";"218">"219";"217">"218";"216">"217";
      "215">"216";"214">"215";"213">"214";"212">"213";"211">"212";"210">"211";"209">"210";"208">"209";
      "207">"208";"206">"207";"205">"206";"204">"205";"203">"204";"202">"203";"201">"202";"200">"201";
      "199">"200";"198">"199";"197">"198";"196">"197";"195">"196";"194">"195";"193">"194";"192">"193";
      "191">"192";"190">"191";"189">"190";"188">"189";"187">"188";"186">"187";"185">"186";"184">"185";
      "183">"184";"182">"183";"181">"182";"180">"181";"179">"180";"178">"179";"177">"178";"176">"177";
      "175">"176";"174">"175";"173">"174";"172">"173";"171">"172";"170">"171";"169">"170";"168">"169";
      "167">"168";"166">"167";"165">"166";"164">"165";"163">"164";"162">"163";"161">"162";"160">"161";
      "159">"160";"158">"159";"157">"158";"156">"157";"155">"156";"154">"155";"153">"154";"152">"153";
      "151">"152";"150">"151";"149">"150";"148">"149";"147">"148";"146">"147";"145">"146";"144">"145";
      "143">"144";"142">"143";"141">"142";"140">"141";"139">"140";"138">"139";"137">"138";"136">"137";
      "135">"136";"134">"135";"133">"134";"132">"133";"131">"132";"130">"131";"129">"130";"128">"129";
      "127">"128";"126">"127";"125">"126";"124">"125";"123">"124";"122">"123";"121">"122";"120">"121";
      "119">"120";"118">"119";"117">"118";"116">"117";"115">"116";"114">"115";"113">"114";"112">"113";
      "111">"112";"110">"111";"109">"110";"108">"109";"107">"108";"106">"107";"105">"106";"104">"105";
      "103">"104";"102">"103";"101">"102";"100">"101";"099">"100";"098">"099";"097">"098";"096">"097";
      "095">"096";"094">"095";"093">"094";"092">"093";"091">"092";"090">"091";"089">"090";"088">"089";
      "087">"088";"086">"087";"085">"086";"084">"085";"083">"084";"082">"083";"081">"082";"080">"081";
      "079">"080";"078">"079";"077">"078";"076">"077";"075">"076";"074">"075";"073">"074";"072">"073";
      "071">"072";"070">"071";"069">"070";"068">"069";"067">"068";"066">"067";"065">"066";"064">"065";
      "063">"064";"062">"063";"061">"062";"060">"061";"059">"060";"058">"059";"057">"058";"056">"057";
      "055">"056";"054">"055";"053">"054";"052">"053";"051">"052";"050">"051";"049">"050";"048">"049";
      "047">"048";"046">"047";"045">"046";"044">"045";"043">"044";"042">"043";"041">"042";"040">"041";
      "039">"040";"038">"039";"037">"038";"036">"037";"035">"036";"034">"035";"033">"034";"032">"033";
      "031">"032";"030">"031";"029">"030";"028">"029";"027">"028";"026">"027";"025">"026";"024">"025";
      "023">"024";"022">"023";"021">"022";"020">"021";"019">"020";"018">"019";"017">"018";"016">"017";
      "015">"016";"014">"015";"013">"014";"012">"013";"011">"012";"010">"011";"009">"010";"008">"009";
      "007">"008";"006">"007";"005">"006";"004">"005";"003">"004";"002">"003";"001">"002";"000">"001";
    };
  }
  _1(1){//decreese num by 1
    ret ma(^1){
      "255">"254";"254">"253";"253">"252";"252">"251";"251">"250";"250">"249";"249">"248";"248">"247";
      "247">"246";"246">"245";"245">"244";"244">"243";"243">"242";"242">"241";"241">"240";"240">"239";
      "239">"238";"238">"237";"237">"236";"236">"235";"235">"234";"234">"233";"233">"232";"232">"231";
      "231">"230";"230">"229";"229">"228";"228">"227";"227">"226";"226">"225";"225">"224";"224">"223";
      "223">"222";"222">"221";"221">"220";"220">"219";"219">"218";"218">"217";"217">"216";"216">"215";
      "215">"214";"214">"213";"213">"212";"212">"211";"211">"210";"210">"209";"209">"208";"208">"207";
      "207">"206";"206">"205";"205">"204";"204">"203";"203">"202";"202">"201";"201">"200";"200">"199";
      "199">"198";"198">"197";"197">"196";"196">"195";"195">"194";"194">"193";"193">"192";"192">"191";
      "191">"190";"190">"189";"189">"188";"188">"187";"187">"186";"186">"185";"185">"184";"184">"183";
      "183">"182";"182">"181";"181">"180";"180">"179";"179">"178";"178">"177";"177">"176";"176">"175";
      "175">"174";"174">"173";"173">"172";"172">"171";"171">"170";"170">"169";"169">"168";"168">"167";
      "167">"166";"166">"165";"165">"164";"164">"163";"163">"162";"162">"161";"161">"160";"160">"159";
      "159">"158";"158">"157";"157">"156";"156">"155";"155">"154";"154">"153";"153">"152";"152">"151";
      "151">"150";"150">"149";"149">"148";"148">"147";"147">"146";"146">"145";"145">"144";"144">"143";
      "143">"142";"142">"141";"141">"140";"140">"139";"139">"138";"138">"137";"137">"136";"136">"135";
      "135">"134";"134">"133";"133">"132";"132">"131";"131">"130";"130">"129";"129">"128";"128">"127";
      "127">"126";"126">"125";"125">"124";"124">"123";"123">"122";"122">"121";"121">"120";"120">"119";
      "119">"118";"118">"117";"117">"116";"116">"115";"115">"114";"114">"113";"113">"112";"112">"111";
      "111">"110";"110">"109";"109">"108";"108">"107";"107">"106";"106">"105";"105">"104";"104">"103";
      "103">"102";"102">"101";"101">"100";"100">"099";"099">"098";"098">"097";"097">"096";"096">"095";
      "095">"094";"094">"093";"093">"092";"092">"091";"091">"090";"090">"089";"089">"088";"088">"087";
      "087">"086";"086">"085";"085">"084";"084">"083";"083">"082";"082">"081";"081">"080";"080">"079";
      "079">"078";"078">"077";"077">"076";"076">"075";"075">"074";"074">"073";"073">"072";"072">"071";
      "071">"070";"070">"069";"069">"068";"068">"067";"067">"066";"066">"065";"065">"064";"064">"063";
      "063">"062";"062">"061";"061">"060";"060">"059";"059">"058";"058">"057";"057">"056";"056">"055";
      "055">"054";"054">"053";"053">"052";"052">"051";"051">"050";"050">"049";"049">"048";"048">"047";
      "047">"046";"046">"045";"045">"044";"044">"043";"043">"042";"042">"041";"041">"040";"040">"039";
      "039">"038";"038">"037";"037">"036";"036">"035";"035">"034";"034">"033";"033">"032";"032">"031";
      "031">"030";"030">"029";"029">"028";"028">"027";"027">"026";"026">"025";"025">"024";"024">"023";
      "023">"022";"022">"021";"021">"020";"020">"019";"019">"018";"018">"017";"017">"016";"016">"015";
      "015">"014";"014">"013";"013">"012";"012">"011";"011">"010";"010">"009";"009">"008";"008">"007";
      "007">"006";"006">"005";"005">"004";"004">"003";"003">"002";"002">"001";"001">"000";"000">"255";
    };
  }
}
//charnum library, adds function to convert num to char and back
|charnum{
  _0(1){//convert char to num
    ret ma(^1){
      "\n">"010";
      " ">"032";"!">"033";""">"034";"#">"035";"$">"036";"%">"037";"&">"038";"'">"039";
      "(">"040";")">"041";"*">"042";"+">"043";",">"044";"-">"045";".">"046";"/">"047";
      "0">"048";"1">"049";"2">"050";"3">"051";"4">"052";"5">"053";"6">"054";"7">"055";
      "8">"056";"9">"057";":">"058";";">"059";"<">"060";"=">"061";">">"062";"?">"063";
      "@">"064";"A">"065";"B">"066";"C">"067";"D">"068";"E">"069";"F">"070";"G">"071";
      "H">"072";"I">"073";"J">"074";"K">"075";"L">"076";"M">"077";"N">"078";"O">"079";
      "P">"080";"Q">"081";"R">"082";"S">"083";"T">"084";"U">"085";"V">"086";"W">"087";
      "X">"088";"Y">"089";"Z">"090";"[">"091";"\">"092";"]">"093";"^">"094";"_">"095";
      "`">"096";"a">"097";"b">"098";"c">"099";"d">"100";"e">"101";"f">"102";"g">"103";
      "h">"104";"i">"105";"j">"106";"k">"107";"l">"108";"m">"109";"n">"110";"o">"111";
      "p">"112";"q">"113";"r">"114";"s">"115";"t">"116";"u">"117";"v">"118";"w">"119";
      "x">"120";"y">"121";"z">"122";"{">"123";"|">"124";"}">"125";"~">"126";
    };
  }
  _1(1){//convert num to char
    ret ma(^1){
      "010">"\n"; //new line char
      "032">" ";"033">"!";"034">""";"035">"#";"036">"$";"037">"%";"038">"&";"039">"'";
      "040">"(";"041">")";"042">"*";"043">"+";"044">",";"045">"-";"046">".";"047">"/";
      "048">"0";"049">"1";"050">"2";"051">"3";"052">"4";"053">"5";"054">"6";"055">"7";
      "056">"8";"057">"9";"058">":";"059">";";"060">"<";"061">"=";"062">">";"063">"?";
      "064">"@";"065">"A";"066">"B";"067">"C";"068">"D";"069">"E";"070">"F";"071">"G";
      "072">"H";"073">"I";"074">"J";"075">"K";"076">"L";"077">"M";"078">"N";"079">"O";
      "080">"P";"081">"Q";"082">"R";"083">"S";"084">"T";"085">"U";"086">"V";"087">"W";
      "088">"X";"089">"Y";"090">"Z";"091">"[";"092">"\";"093">"]";"094">"^";"095">"_";
      "096">"`";"097">"a";"098">"b";"099">"c";"100">"d";"101">"e";"102">"f";"103">"g";
      "104">"h";"105">"i";"106">"j";"107">"k";"108">"l";"109">"m";"110">"n";"111">"o";
      "112">"p";"113">"q";"114">"r";"115">"s";"116">"t";"117">"u";"118">"v";"119">"w";
      "120">"x";"121">"y";"122">"z";"123">"{";"124">"|";"125">"}";"126">"~";
    };
  }
}

0:""; //previous tape
1:"000"; //current cell
2:""; //next tape
3:$; //brain fuck code
6:""; //previous brain fuck code
wh(3!=""){
  4:3.r("."); //get next instruction
  6:6+3.r("."); //add next instruction to previous brain fuck code
  3:3.rdel("."); //remove instruction from brainfuck code
  
  if(4=">"){ 
    0:0+1; //add crrent cell to previous tape
    if(2!=""){
      //do fancy code to read next tape cell
      1:2.r("...");
      2:2.rdel("...");
    }el{
      1:"000";
    }
  }el if{4="<"}{
    2:1+2; //add current cell to next tape
    if(0!=""){
      //do fancy code to read previous tape cell
      1:0.r("...$");
      0:0.rdel("...$");
    }el{
      pr "error: negative cell value"
      3:"";
    }
  }el if{4="+"}{
    1:incdec_0(1); //increse current cell by 1
  }el if{4="-"}{
    1:incdec_1(1); //decreese current cell by 1
  }el if{4="."}{
    if(charnum_1(1)!=""){//if the current cell converts to a char
      pr charnum_1(1);
    }el{
      pr " ";
    }  
  }el if{4=","}{
    5:$.r("."); //input char
    if(charnum_0(5)!=""){//if the current char converts to a num
      1:charnum_0(5);
    }el{
      pr "error input num is not supported";
      3:"";
    }
  }el if{4="["}{
    if(1="000"){
      7:"001";//nest depth
      wh(7!="000"){//while nest depth does not equil 0 (no nests)
        if(3.r(".")="]"){//if next char is ] decrese nest depth by 1
          7:incdec_1(7);
        }
        if(3.r(".")="["){//if next char is [ increse nest depth by 1
          7:incdec_0(7);
        }
        6:6+3.r("."); //add next instruction to previous brain fuck code
        3:3.rdel("."); //remove instruction from brainfuck code
      }
    }
  }el if{4="]"}{
    if(1="000"){
      7:"001";//nest depth
      wh(7!="000"){//while nest depth does not equil 0 (no nests)
        if(6.r(".$")="]"){//if last char of previous brain fuck code is ] increse nest depth by 1
          7:incdec_0(7);
        }
        if(6.r(".$")="["){//if last char of previous brain fuck code is [ decrese nest depth by 1
          7:incdec_1(7);
        }
        3:6.r(".$")+3; //move last instruction from previous brain fuck code to beginging of brain fuck code
        6:6.rdel(".$"); //remove last instruction from previous brainfuck code
      }
    }
  }
}