C-Hex

From Esolang
Jump to navigation Jump to search

C-Hex is a somewhat serious language, it consists of characters that open and close, like [], (), <>, and {}. C ontrol, why control? I've made some EarthBound ROM hacks, and the language, CSS, uses control codes like [01], which can set flags ON or OFF and can also do many other things.

H ex, C-Hex uses only hexadecimal values and it is purposely horrible. Let's take the [00] family, they all print the value of the cell which the pointer is looking at, now look at [04...], it doesn't do an operation with the cell which the pointer is looking at. You have to tell it what cell you want to do the action with, but in others, it just does it without asking for the cell.

There are even more cases of this, which make C-Hex a memory test.

Interpreter

I've finished the interpreter, you can download it here! Download interpreter

Indentation

Indentation is optional, and if you want to use indentation, don't use spaces for it! Spaces can't be used for indentation, and if you do, the program will not be executed.

Structure

{tape_length: 0xFF}
<main>
    (code here)
<end>

Forgot it! C-Hex uses a tape system, somewhat like Brainfuck, you can remove tape_length from the start, but keep in mind that the default tape length is 128.

Examples

{tape_length: 0xFF}
<main>
    [00 00]; (print current cell as hex)
    [00 01]; (print current cell as a letter using ascii)
    [00 02]; (print current cell as a number)
    [01 XX]; (set current cell to the value of XX)
    [03]; (get input, if input is "EEEE" then cells 0, 1, 2 and 3 will all get set to the ascii code of "E")
    [0A]; (get number, luckily the user does not have to use hex values, but will be converted to hex later)
    (also puts the input into the cell which the pointer is pointing to)
<end>

Text inside any [], () or {} is a comment, except <>, don't you dare do anything with that!

(math)
<main>
    [04 OP XX YY]; (do a mathematical op. OP can be 00: + 01: - 02: * 03: / 04: ^ 05: %)
    (result gets put into current cell!)
    [04 00 $00 01]; (the dollar sign is used when you reference a cell $00: the value of cell 0)
    (so, we get the value of cell 0 which is 0 then we add 1 to it and finally put the result into the current cell)
<end>
(labels and jumping)
<label>
    [00 00]; (prints 0)
<end>
<main>
    [02 00 label]; (jumps to label and when it hits <end>, it returns)
    [00 00]; (prints 0)
<end>

OUTPUT: 00. Why? Because it returns to <main> and runs the other control code.

(goto)
<label>
    [00 00]; (prints 0)
<end>
<main>
    [02 01 label]; (more like goto label)
    [00 00]; (prints 0)
<end>

OUTPUT: 0. We do not come back to the <main> label, therefore, it does not print the other 0.

(if and pointer movement)
<main>
    [05 FF]; (moves pointer to cell 255)
    [06 OP XX YY LABEL] (OP can be: 00: == 01: > 02: < 03: != 04: <= 05: >= 06: %)
    (XX can be any value, even cells like $FF, YY is the same and LABEL is the label to jump to if the condition is met)
    (keep in mind that once the label instructions are over it will return to where the if was located)
    [07 OP XX YY LABEL ELSE_LABEL] (like the other one but we can use an else statement)
    (ELSE_LABEL is the place that will be jumped to if the condition isn't met)
<end>

With if statements we can do an incredible amount of things, the thing with this programming language is that it doesn't run short on commands, it is just plain difficult to memorize what each control code does.

(loops)
<main>
   [08 XX LABEL] (a for loop, XX is the number of times it will loop, can also be a cell value, like $FF, LABEL, y'know what it is.)
   [09 OP XX YY LABEL] (same parameters as if, but this time it is a while loop)
<end>

Loops! Now we've unlocked a whole more bunch of things that we can do, and there are even more control codes to review.

(breaking)
{tape_length: 0xFF}
<main>
   (forgot to mention, indentation doesn't matter, and so do ; characters)
   [09 00 01 01 new_for] (while loop, ALWAYS runs)
<end>
<new_for>
       [05 01]; (move the pointer to cell 01)
       [04 00 $01 01]; (add one to cell 01)
       [00 01]; (print the value of the current cell [cell 01] as a number)
       [06 00 $01 05 break_loop]; (if the value of cell 01 is 05 call break_loop)
       [05 02]; (move pointer to cell 02)
       [01 0A][00 01]; (set current cell [cell 02] to 10, print current cell as a character [10 is the ascii code for newline])
<end>
(order doesn't matter)
<break_loop>[FF FF 01] (FF FF 00 stops current label, FF FF 01 stops 2 labels in order, here we STOP break_loop and STOP new_for)<end>
(FF FF 02 stops all labels except main, which then we go to)

OUTPUT: 1\n2\n\n3\n4\n5. Read the comments I've put there to understand what is happening, or else!...

Errors

If the interpreter encounters an error, it will simply print '>:('. That is very useful information, isn't it?...