-
Notifications
You must be signed in to change notification settings - Fork 1
Coding Style
Programming in Binary++ can be made much easier by following this style. Since Binary++ ignores everything but sequences of eight 1s and 0s, you can type whatever you want in the file without affecting the actual code.
Trust me, reading your code will make much more sense by doing this. You can find the full mnemonic list in the Instruction Set wiki page.
00000100 01101001 PUSH_STACK 105
00000100 01101000 PUSH_STACK 104
00001010 00000000 WRITE_TO 0
00001010 00000000 WRITE_TO 0
I personally do this. Planning out your code is much easier by first writing it in mnemonics. After you write it, you can match up the instructions and arguments like in the example above.
PUSH_STRING_STACK "Hello, world\0"
WRITE_TO 0
Whenever you begin a loop or have code in an if statement, increment the indentation of the mnemonics and add a pipe (|) to display that. To display else statements, indent but don't use characters.
00000100 00000001 PUSH_STACK 1
00000100 00001010 PUSH_STACK 10
11100010 LESS_THAN
10100000 00000010 IF_RUN_NEXT 2
00000100 00110001 | PUSH_STACK 49
10100001 00000001 | SKIP_NEXT 1
00000100 00110000 PUSH_STACK 48
00001010 00000000 WRITE_TO 0
If you plan on using the memory to store data, then you should write down what goes where. To make things easier, write down the "name" of the memory address next to where you use it. Additionally, you can show the indexes of each address.
[RESERVED, a, b]
[0 , 1, 2]
00000100 00000001 PUSH_STACK 1
00000011 00000001 STORE_MEMORY 1 [a]
00000100 00000010 PUSH_STACK 2
00000011 00000010 STORE_MEMORY 2 [b]
00000010 00000001 LOAD_MEMORY 1 [a]
00000010 00000010 LOAD_MEMORY 2 [b]
10000001 BINARY_ADD
Notes: The 0th memory address is reserved in Binary++.
Nothing is more useful in understanding your code than adding comments. It's inevitable. At one point you simply have to do it. Although any method works, my personal preference is to use double semicolons (;;) followed by the comment. At the beginning of the program, I decribe what the program does and wrap it with double semicolons.
Depending on the complexity of the program, I either align the comments to the start of the line or to match the mnemonics.
;; This program does something! ;;
;; Push 1 and 2 to the stack
00000100 00000001 PUSH_STACK 1
00000100 00000010 PUSH_STACK 2
;; Add them together
BINARY_ADD
;; Check if it's equal to 3
00000100 00000011 PUSH_STACK 3
11100001 EQUAL_TO
;; Print 1 if it is. Otherwise do nothing.
10100000 00000010 IF_RUN_NEXT 2
;; In ASCII, 48 is the value for '0'. So to get '1', we add one to 48.
00000100 00110001 | PUSH_STACK 49
;; This will write the '1' to a stream, which in this case is stdout.
00001010 00000000 | WRITE_TO 0