|
| 1 | +# Brute Stack Code |
| 2 | + |
| 3 | +## A stack-oriented language |
| 4 | + |
| 5 | +The origin of the Brute Stack Code was because of the Red Power's minecraft mod computer which implemented the FORTH language. This means Brute Stack Code is inspirated from FORTH language |
| 6 | + |
| 7 | +## How it works: |
| 8 | +Brute Stack Code uses a stack (FIFO envorinment) to manipulate data and a RAM-like to store temporary data when executing the program. |
| 9 | + |
| 10 | + |
| 11 | +Programs can only has a stacktrace (function tracing stack) of size 50 elements. |
| 12 | + |
| 13 | + |
| 14 | +The program stack technically is infinite-size and also DOES NOT HOLD float numbers, only 16-bit signed integers. |
| 15 | + |
| 16 | +## Extensions: |
| 17 | +[Official extensions on Brute Stack Code](extensions.md) |
| 18 | + |
| 19 | + |
| 20 | +## Commands: |
| 21 | +All commands on this paper will be defined in the list as: |
| 22 | +COMMANDNAME \[1st hardcoded argument which will be fetched on the command string] \[2nd hardcoded argument which will be fetched on the command string] \[...] \<1st arbitrary argument. Pops a value from the stack and defines as the first argument> \<2nd arbitrary argument. Pops a value from the stack and defines as the second argument> <...> |
| 23 | +### Commands list |
| 24 | +- . \<value>: Pops a value from the stack and displays it on the terminal. |
| 25 | +- .S \<...values>: Pops all values from the stack and displays it on the terminal with spaces between the numbers |
| 26 | +- E: Stops the program |
| 27 | +- \+ \<argument 1> \<argument 2>: Pops two values from the stack and makes a addition operation, then push the result in the stack |
| 28 | +- \- \<argument 1> \<argument 2>: Pops two values from the stack and makes a subtraction operation, then push the result in the stack |
| 29 | +- \* \<argument 1> \<argument 2>: Pops two values from the stack and makes a multiplication operation, then push the result in the stack |
| 30 | +- / \<argument 1> \<argument 2>: Pops two values from the stack and makes a division operation, then push the result in the stack. Note that the result is floored. |
| 31 | +- % \<argument 1> \<argument 2>: Pops two values from the stack and makes a division operation, then push the remainder in the stack |
| 32 | +- | \<argument 1> \<argument 2>: Pops two values from the stack and makes a OR operation, then push the result in the stack |
| 33 | +- ! \<argument 1> \<argument 2>: Pops two values from the stack and makes a NOT (xor of 65535) operation, then push the result in the stack |
| 34 | +- & \<argument 1> \<argument 2>: Pops two values from the stack and makes a AND operation, then push the result in the stack |
| 35 | +- ^ \<argument 1> \<argument 2>: Pops two values from the stack and makes a XOR operation, then push the result in the stack |
| 36 | +- PAGE: Clears the terminal |
| 37 | +- WORDS: Displays all the command names |
| 38 | +- TR \<length> \<...char codes>: Pops a length value and then iterate through 0 to length, popping a char code and displaying it's character on the screen |
| 39 | +- TW \<length>: Pops a length value and then reads length bytes from input and push all of the readed values on the stack |
| 40 | +- RE: Restart the program |
| 41 | +- CL: Clears the stack |
| 42 | +- IFQ \<argument 1> \<argument 2>: Check if both values is equal, if true, executes all commands inside the if statement until a ELSE or END is found |
| 43 | +- INQ \<argument 1> \<argument 2>: Check if both values is NOT equal, if true, executes all commands inside the if statement until a ELSE or END is found |
| 44 | +- IFS \<argument 1> \<argument 2>: Check if both values is smaller, if true, executes all commands inside the if statement until a ELSE or END is found |
| 45 | +- IFB \<argument 1> \<argument 2>: Check if both values is bigger, if true, executes all commands inside the if statement until a ELSE or END is found |
| 46 | +- ELSE: If inside a if statement, skips commands until a END is found. Otherwise should trigger a error |
| 47 | +- END: If inside a if statement, does nothing. Otherwise should trigger a error |
| 48 | +- DF \[...code, stops after a EF command]: Defines a function. First read the function name then reads commands until a EF command is found. If the function is not closed properly, should wait for user input until the user inputs a EF command |
| 49 | +- EF: Ends the function definition. |
| 50 | +- CALL \[function name]: Read a function name then calls the function |
| 51 | +- FF \[function name]: Removes the function from the function definition list. |
| 52 | +- R \<address>: Pops a address from the stack. Reads a 16-bit integer from the memory using the specified address. |
| 53 | +- W \<value> \<address>: Pops a value THEN the address from the stack. Writes a 16-bit integer to the memory using the specified address and value. |
| 54 | +- SIZE: Pushes the memory size |
| 55 | +- EXTS: Pushes a bitfield of the current implemented extensions. |
| 56 | +- " \[...characters] ": Converts all characters inside quotation marks to char codes and pushes it on the stack. |
| 57 | +- (any integer): pushes the integer into the stack |
| 58 | + |
| 59 | +## Tests: |
| 60 | +### Test the IFS and IFB commands: |
| 61 | + |
| 62 | +Pseudo-code (lua): |
| 63 | +```lua |
| 64 | +if 10 < 20 then |
| 65 | + if 20 + 10 > 2 then |
| 66 | + print(0); |
| 67 | + else |
| 68 | + print(1); |
| 69 | + end |
| 70 | +else |
| 71 | + print(2); |
| 72 | +end |
| 73 | +``` |
| 74 | +Brute Stack Code: |
| 75 | +```bsc |
| 76 | +20 10 IFS 2 10 20 + IFB 0 . ELSE 1 . END ELSE 2 . END |
| 77 | +``` |
| 78 | +Returns 0 if everything is ok |
| 79 | + |
| 80 | +Returns 1 if IFB is not working |
| 81 | + |
| 82 | +Returns 2 if IFB is not working |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +### Test the IFQ and INQ commands: |
| 87 | +```lua |
| 88 | +if (2 + 6*5) ~= (8 + 4*6) then |
| 89 | + if (4 + 2*14) == ((360 % 99 - 3)/2+2) then |
| 90 | + print(0); |
| 91 | + else |
| 92 | + print(1); |
| 93 | + end |
| 94 | +else |
| 95 | + print(2); |
| 96 | +end |
| 97 | +``` |
| 98 | +Brute Stack Code: |
| 99 | +```bsc |
| 100 | +2 5 6 * + 8 6 5 * + INQ 4 14 2 * + 2 2 3 99 360 % - / + IFQ 0 . ELSE 1 . END ELSE 2 . END |
| 101 | +``` |
| 102 | +Returns 0 if everything is ok |
| 103 | + |
| 104 | +Returns 1 if IFQ is not working |
| 105 | + |
| 106 | +Returns 2 if INQ is not working |
| 107 | + |
| 108 | + |
| 109 | +## First implementations: |
| 110 | +### Original implementation: |
| 111 | +[CC: Tweaked implementation](original_implementation/README.md) |
| 112 | + |
| 113 | +### Updated implementation: |
| 114 | +[JavaScript implementation](index.js) |
0 commit comments