Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IMPORTANT NOTE:
- THIS IS NOT THE FINAL PASTEBIN.
- I just wanted to get something up for you guys to see until I have the final version complete.
- Some pre-read info:
- Whenever I comment something not on the same line as the thing I'm commenting on, I always put the comment on the line(s) above it.
- for example:
- // this is a comment regarding the foo() function
- func foo() {}
- // this comment does not relate to the foo() function
- All current functions with explanations
- (not all of these are implemented in the interpreter because I stopped work on the project before it was done)
- These two are separated because they can be used within functions instead of as functions
- - if valid meaning if the function this is used in allows use of it.
- - Treat x as cell position meaning x is a number that represents a
- cell instead of a number value input
- 'x if valid, treat x as cell position
- - this is used because my interpreter offset cell positions by about 5 as
- to leave the first few cells as "buffers" or "temporary" cells. these are
- what would be used whenever a traditional brainfuck algorithm calls for a
- temp cell. (temp0, temp1, etc.)
- - this modifier allows c to be treated as the absolute position in the cell
- array instead of the position + buffer offset
- ^c use c as absolute position in array
- - cell c = value x (allows ^c and 'x)
- =c:x cell c = x
- - just duplicates cell c to cell x (allows ^c and ^x)
- - in bf code this looks like this: c[temp+x+c]temp[-c+temp]
- &c:x duplicate cell c to cell x
- - literally just moves the value of c to position x. deletes whatever was at x. (allows ^c and ^x)
- @c:x allocate cell c to cell x
- - in bf code: c[-] (allows ^c)
- /c clear cell c (set to 0)
- - (allows ^c)
- :c move pointer to cell c
- +c:x:y cell c = cell x + y
- +c:x:'y cell c = cell x + cell y
- -c:x:y cell c = cell x - y
- -c:x:'y cell c = cell x - cell y
- *c:x:y cell c = cell x * y
- *c:x:'y cell c = cell x * cell y
- .n:c creates a new variable called (n) at cell c
- %x:y reassign variable (i honestly don't remember what this is supposed to do)
- ~c get user input at cell c
- A:c:x cell c = ASCII value of x (x is any character)
- B:"str" insert brainfuck directly into code
- T:"str" output a string of text
- P:c output number value at cell c
- ?=c:x:(code)\ if cell c == x
- ?=c:'x:(code)\ if cell c == cell x
- !=c:x:(code)\ if cell c != x
- !=c:'x:(code)\ if cell c != cell x
- >c:x:(code)\ if cell c > x
- >c:'x:(code)\ if cell c > cell x
- <c:x:(code)\ if cell c < x
- <c:'x:(code)\ if cell c < cell x
- >=c:x:(code)\ if cell c >= x
- >=c:'x:(code)\ if cell c >= cell x
- <=c:x:(code)\ if cell c <= x
- <=c:'x:(code)\ if cell c <= cell x
- @{ forever loop start
- @} forever loop end
- ==================================================================================
- interpreter code
- -written in processing
- processing download: processing.org/download
- file info:
- in the project folder, there is another folder called "data"
- in the data folder, there is a .txt file called "sb.txt" where you input your shitbrain code.
- the program outputs another text file called "bf.txt" in the same folder and automatically opens it
- here is the entirety of the interpreter code in its current state:
- (i am aware it is very unoptimized but it is also nearly a year old and
- i also only have a little more than a year coding experience so yeah)
- ```
- /*
- 'x if valid, treat x as cell position
- ^c use x as absolute position in array
- =c:x cell c = x
- &c:x duplicate cell c to cell x
- @c:x allocate cell c to cell x
- /c clear cell c (set to 0)
- :c move pointer to cell c
- +c:x:y cell c = cell x + y
- +c:x:'y cell c = cell x + cell y
- -c:x:y cell c = cell x - y
- -c:x:'y cell c = cell x - cell y
- *c:x:y cell c = cell x * y
- *c:x:'y cell c = cell x * cell y
- .n:c creates a new variable called (n) at cell c
- %x:y reassign variable
- ~c get user input at cell c
- A:c:x cell c = ASCII value of x (x is any character)
- B:"str" insert brainfuck directly into code
- T:"str" output a string of text
- P:c output number value at cell c
- ?=c:x:(code)\ if cell c == x
- ?=c:'x:(code)\ if cell c == cell x
- !=c:x:(code)\ if cell c != x
- !=c:'x:(code)\ if cell c != cell x
- >c:x:(code)\ if cell c > x
- >c:'x:(code)\ if cell c > cell x
- <c:x:(code)\ if cell c < x
- <c:'x:(code)\ if cell c < cell x
- >=c:x:(code)\ if cell c >= x
- >=c:'x:(code)\ if cell c >= cell x
- <=c:x:(code)\ if cell c <= x
- <=c:'x:(code)\ if cell c <= cell x
- */
- String[] sb; //shitbrain input code
- String[] bf; //brainfuck output code
- String[] comments; //comments in sb code
- ArrayList<String> b;
- ArrayList<String> vars;
- ArrayList<String> varLocs;
- int buffers = 4;
- File bFuck;
- boolean open = true;
- void setup() {
- //size(400, 400);
- //textSize(80);
- //textAlign(CENTER);
- //fill(0);
- //text("converting...", width/2, height/2);
- bFuck = dataFile("bf.txt");
- vars = new ArrayList<String>(1);
- varLocs = new ArrayList<String>(1);
- ArrayList<String> s = new ArrayList<String>();
- String[] c = loadStrings("sb.txt"); //load code
- for (int i = 0; i < c.length; i++) {
- s.add(c[i]); //copy code to array
- }
- for (int i = s.size()-1; i >= 0; i--) {
- if (!s.get(i).substring(0, 1).equals("T")) {
- s.set(i, split(s.get(i), ";")[0]); //ignore comments
- }
- if (s.get(i).length() <= 1 || s.get(i).equals("\n")) {
- s.remove(i); //remove
- } else {
- if (!s.get(i).substring(0, 1).equals("B") && !s.get(i).substring(0, 1).equals("T")
- && !s.get(i).substring(0, 1).equals("A")) {
- //remove spaces
- s.set(i, s.get(i).replace(" ", ""));
- //s.set(i, s.get(i).replace(str(char(9)), ""));
- }
- //println(s.get(i).substring(0, 1));
- }
- }
- sb = new String[s.size()+1];
- for (int i = 1; i < sb.length; i++) {
- sb[i] = s.get(i-1); //copy s to sb
- }
- b = new ArrayList<String>();
- b.add("-");
- for (int i = 1; i < sb.length; i++) {
- b.add(convert(sb[i], i));
- }
- for (int i = b.size()-1; i >= 1; i--) {
- if (b.get(i).length() <= 1 || b.get(i).equals("\n")) {
- b.remove(i); //remove
- }
- }
- bf = new String[b.size()];
- for (int i = 0; i < b.size(); i++) {
- bf[i] = b.get(i);
- }
- //for (int i = 0; i < bf.length; i++) {
- // bf[i] = bf[i].replace(" ", "");
- //}
- //pCode();
- saveStrings("data/bf.txt", bf);
- if (open) launch(bFuck.toString());
- exit();
- }
- //----------------------------------------------------------------------------------------------------
- String convert(String code, int index) {
- String op = code.substring(0, 1);
- String ln = "";
- String name;
- String pos;
- String arg0, arg1, arg2, act0;
- String move0, move1, move2, b1, b2, b3;
- switch(op) {
- default:
- throw new UnsupportedOperationException("operator '"+op+"' is invalid. at shitbrain line "+index);
- //----------------------------------------------------------------------------------------------------
- case ".":
- name = split(code.substring(1), ":")[0];
- pos = split(code.substring(1), ":")[1];
- move0 = "+[-<+]-";
- int loc; //int(split(code.substring(1), ":")[1])
- if (!pos.substring(0, 1).equals("^")) {
- loc = int(pos)+buffers;
- } else {
- loc = int(pos.substring(1));
- }
- vars.add(name);
- for (int i = 0; i < loc; i++) {
- move0+= ">";
- }
- varLocs.add(move0);
- break;
- //----------------------------------------------------------------------------------------------------
- case ":":
- move0 = code.substring(1);
- ln = toPos(move0);
- break;
- //----------------------------------------------------------------------------------------------------
- case "=":
- arg0 = split(code.substring(1), ":")[0];
- arg1 = split(code.substring(1), ":")[1];
- act0 = "+[-<+]->";
- for (int i = 0; i < factorsOf(int(arg1))[0]; i++) {
- act0 += "+";
- }
- act0 += "["+toPos(arg0);
- for (int i = 0; i < factorsOf(int(arg1))[1]; i++) {
- act0 += "+";
- }
- act0 += "+[-<+]->-]";
- ln = act0;
- break;
- //----------------------------------------------------------------------------------------------------
- case "/":
- arg0 = code.substring(1);
- ln = toPos(arg0) + " [-]";
- break;
- //----------------------------------------------------------------------------------------------------
- case "&":
- arg0 = split(code.substring(1), ":")[0];
- arg1 = split(code.substring(1), ":")[1];
- move0 = toPos(arg0);
- move1 = toPos(arg1);
- ln = move1+"\n\n[-]"+move0+"[-+[-<+]->+"+move1+"+"+move0+"]\n[+[-<+]->-"+move0+"+]+[-<+]->[-"+move0+"++[-<+]->]\n";
- break;
- //----------------------------------------------------------------------------------------------------
- case "@":
- arg0 = split(code.substring(1), ":")[0];
- arg1 = split(code.substring(1), ":")[1];
- move1 = toPos(arg1);
- move0 = toPos(arg0);
- ln = move1+"[-]"+move0+"[-"+move1+"+"+move0+"]";
- break;
- //----------------------------------------------------------------------------------------------------
- case "B":
- arg0 = code.substring(2);
- ln = arg0;
- break;
- //----------------------------------------------------------------------------------------------------
- case "P":
- arg0 = code.substring(2);
- move0 = toPos(arg0);
- ln = move0+ ".";
- break;
- //----------------------------------------------------------------------------------------------------
- case "T":
- arg0 = code.substring(2);
- act0 = "";
- ln = "";
- for (int j = 0; j < code.substring(2).length(); j++) {
- act0 = "+[-<+]->";
- int f1a = factorsOf(int(arg0.charAt(j)))[0];
- int f2a = factorsOf(int(arg0.charAt(j)))[1];
- int f1b = factorsOf(int(arg0.charAt(j))+1)[0];
- int f2b = factorsOf(int(arg0.charAt(j))+1)[1];
- int f1c = factorsOf(int(arg0.charAt(j))-1)[0];
- int f2c = factorsOf(int(arg0.charAt(j))-1)[1];
- if (abs(f1a-f2a) < abs(f1b-f2b)) {
- for (int i = 0; i < f1a; i++) {
- act0 += "+";
- }
- act0 += "[>";
- for (int i = 0; i < f2a; i++) {
- act0 += "+";
- }
- act0 += "<-]";
- ln += act0+">.[-]\n";
- } else if (abs(f1b-f2b) < abs(f1c-f2c)) {
- for (int i = 0; i < f1b; i++) {
- act0 += "+";
- }
- act0 += "[>";
- for (int i = 0; i < f2b; i++) {
- act0 += "+";
- }
- act0 += "<-]";
- ln += act0+">-.[-]\n";
- } else {
- for (int i = 0; i < f1c; i++) {
- act0 += "+";
- }
- act0 += "[>";
- for (int i = 0; i < f2c; i++) {
- act0 += "+";
- }
- act0 += "<-]";
- ln += act0+">+.[-]\n";
- }
- }
- ln += "++[<+++++>-]<.[-]";
- break;
- //----------------------------------------------------------------------------------------------------
- case "~":
- arg0 = code.substring(1);
- move0 = "+[-<+]-";
- if (arg0.substring(0, 1).equals("^")) {
- for (int i = 0; i < int(arg0.substring(1)); i++) {
- move0 += ">";
- }
- } else {
- for (int i = 0; i < buffers; i++) {
- move0 += ">";
- }
- for (int i = 0; i < int(arg0); i++) {
- move0 += ">";
- }
- }
- for (int v = 0; v < vars.size(); v++) {
- if (arg0.equals(vars.get(v))) {
- move0 = varLocs.get(v);
- break;
- }
- }
- ln = move0 + ",";
- break;
- //----------------------------------------------------------------------------------------------------
- case "A":
- move0 = "+[-<+]- ";
- arg0 = split(code.substring(2), ":")[0];
- arg1 = split(code.substring(2), ":")[1];
- act0 = "+[-<+]->";
- int f1a = factorsOf(int(arg1.charAt(0)))[0];
- int f2a = factorsOf(int(arg1.charAt(0)))[1];
- int f1b = factorsOf(int(arg1.charAt(0))+1)[0];
- int f2b = factorsOf(int(arg1.charAt(0))+1)[1];
- int f1c = factorsOf(int(arg1.charAt(0))-1)[0];
- int f2c = factorsOf(int(arg1.charAt(0))-1)[1];
- if (abs(f1a-f2a) < abs(f1b-f2b)) {
- for (int i = 0; i < f1a; i++) {
- act0 += "+";
- }
- act0 += "[>";
- for (int i = 0; i < f2a; i++) {
- act0 += "+";
- }
- act0 += "<-]";
- ln += act0+">[-]";
- } else if (abs(f1b-f2b) < abs(f1c-f2c)) {
- for (int i = 0; i < f1b; i++) {
- act0 += "+";
- }
- act0 += "[>";
- for (int i = 0; i < f2b; i++) {
- act0 += "+";
- }
- act0 += "<-]";
- ln += act0+">-[-]";
- } else {
- for (int i = 0; i < f1c; i++) {
- act0 += "+";
- }
- act0 += "[>";
- for (int i = 0; i < f2c; i++) {
- act0 += "+";
- }
- act0 += "<-]";
- ln = move0+"[-]"+act0;
- }
- break;
- //----------------------------------------------------------------------------------------------------
- case "+":
- move0 = "+[-<+]- ";
- move1 = "+[-<+]- ";
- arg0 = split(code.substring(1), ":")[0];
- arg1 = split(code.substring(1), ":")[1];
- arg2 = split(code.substring(1), ":")[2];
- b1 = "+[-<+]->";
- b2 = "+[-<+]->>";
- b3 = "+[-<+]->>>";
- move0 = toPos(arg0);
- move1 = toPos(arg1);
- move2 = toPos(arg2);
- ln = move1+"\n\n[-"+b1+"+>+"+move1+"]"+b2+"[-"+move1+"+"+b2+"]\n";
- ln += move2+"[-"+b2+"+>+"+move2+"]"+b3+"[-"+move2+"+"+b3+"]\n";
- ln += b2+"[-<+>]\n";
- ln += move0+"[-]\n";
- ln += b1+"[-"+move0+"+"+b1+"]\n";
- break;
- //----------------------------------------------------------------------------------------------------
- case "\\":
- b1 = "+[-<+]->";
- ln = "\n>]"+b1+"[-]";
- break;
- //----------------------------------------------------------------------------------------------------
- case "?":
- arg0 = split(code.substring(2), ":")[0];
- arg1 = split(code.substring(1), ":")[1];
- move0 = toPos(arg0);
- move1 = toPos(arg1);
- b1 = "+[-<+]->";
- b2 = "+[-<+]->>";
- b3 = "+[-<+]->>>";
- ln = "\n\n"+move0+"[-"+b1+"+>+"+move0+"]\n"; //copy arg0 to b1 and b2
- ln += b2+"[-"+move0+"+"+b2+"]\n"; //move b2 back to arg0
- ln += move1+"[-"+b2+"+>+"+move1+"]\n"; //copy arg1 to b2 and b3
- ln += b3+"[-"+move1+"+"+b3+"]\n"; //move b3 back to arg1
- ln += "<[-<->]\n"; //subtract b2 from b1
- ln += "+<[>-]>[-"; //check if b1 is 0
- //in \\ func, clear b1 and add >]
- //copy arg0 to b1
- //copy arg1 to b2
- //subtract b1 and b2 (result in b1)
- //if the b1 is 0 run code
- //else clear b1
- break;
- }
- return ln;
- }
- //----------------------------------------------------------------------------------------------------
- void pCode() {
- for (int i = 0; i < bf.length; i++) {
- println(bf[i]);
- }
- }
- //----------------------------------------------------------------------------------------------------
- String toPos(String a) {
- String toPos = "+[-<+]-";
- //println(a);
- if (a.substring(0, 1).equals("^")) {
- for (int i = 0; i < int(a.substring(1)); i++) {
- toPos += ">";
- }
- } else {
- for (int i = 0; i < buffers; i++) {
- toPos += ">";
- }
- for (int i = 0; i < int(a); i++) {
- toPos += ">";
- }
- }
- for (int v = 0; v < vars.size(); v++) {
- if (a.equals(vars.get(v))) {
- toPos = varLocs.get(v);
- break;
- }
- }
- return toPos;
- }
- //----------------------------------------------------------------------------------------------------
- int[] factorsOf(int num) {
- int[] factors = new int[0];
- int[] ret = new int[2];
- for (int i = 1; i < ceil(sqrt(num))+1; i++) {
- if (num % i == 0) {
- factors = append(factors, i);
- }
- }
- ret[0] = factors[factors.length-1];
- ret[1] = num/ret[0];
- return ret;
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement