# Dirty

Dirty is an esoteric programming language by User:Madk which features nice syntactical features, but a totally dirty dialect with which to use them.

## Documentation

Values stored in memory are unsigned 8-bit integers, values stored on the stack are unsigned 16-bit integers, addresses are treated as 16-bit integers, and all arithmetic is done using 16-bit integers.

### Syntax

#### Runtime

```&( x )             : reference to the RAM byte at address x
\$( x )             : reference to the ROM byte at address x
:( x )             : push x onto the stack
!                  : reference to the top stack value, and pop
%                  : reference to the top stack value, don't pop
\                  : terminate program
?( x ){ y }        : if x!=0 then do y (if)
?( x ){ y }~{ z }  : if z!=0 then do y, otherwise do z (if...else)
@( x ){ y }        : while x!=0 do y (while)
@@( x ){ y }       : do y while x!=0 (do...while)
^                  : break loop
*                  : continue loop
```

#### Pre-runtime

```"xyz"              : place raw character data here in ROM
[ x ]              : place raw numeric data here in ROM
[ #x ]             : define a label which references this address in ROM
[ #x=y ]           : define a constant x containing the value y
#x                 : reference a label/constant (read-only)
;                  : for separating expressions and stuff
//                 : begin a comment which ends at the next newline
///                : begin or end a comment block
```

### I/O

```>( x )             : outputs x to the console as a number
>>( x )            : outputs x to the console as an ANSI character
<@( x )            : inputs a numeric byte and puts it in RAM at x
<( x )             : inputs a numeric word and puts it in RAM in x and x+1
<<( x )            : inputs a character and puts it in RAM at x
<&( x )            : inputs a string and puts it in RAM starting at x, null-terminated
```

### Expressions

Operators behave the same as in C/C++ except when otherwise marked

#### Unary

```x++
++x
x--
--x
-x                 : keep in mind that all values are calculated as unsigned 16-bit integers, and so negating a value may not produce the behavior you expect
~x
!x
```

#### Binary

##### Arithmetic
```x+y
x+=y
x-y
x-=y
x/y
x/=y
x*y
x*=y
x^y                : get x raised to the power of y
x^=y               : x=x^y
x%y
x%=y
```
##### Logic
```x|y
x|=y
x&y
x&=y
x~y                : get bitwise x XOR y
x~=y               : x=x~y
x||y
x||=y              : x=x||y
x&&y
x&&=y              : x=x&&y
x~~y               : get logical x XOR y
x~~=y              : x=x~~y
```
##### Assignment
```x=y
x:y                : gets x and then sets x=y (note that the x=y operator sets x=y and then gets x)
```
##### Comparison
```x>y
x>=y
x=>y               : same as x>=y
x<y
x<=y
x=<y               : same as x<=y
x==y
x!=y
x<>y               : same as x!=y
```
##### Bitsmithing
```x<<y
x<<=y
x>>y
x>>=y
x<<<y              : rotate bits of x to the left y times
x<<<=y             : x=x<<<y
x>>>y              : rotate bits of x to the right y times
x>>>=y             : x=x>>>y
```

## Code Snippets

Swap top two stack values

```&(0)=%>>8;&(1)=!;&(2)=%>>8;&(3)=!;:(&(0)<<8|&(1)):(&(2)<<8|&(3))
```

Output ROM starting at x as a null-terminated string

```:(x)@(\$(&(%))){>>(\$(&(%)++))}!;
```

Output RAM starting at x as a null-terminated string

```:(x)@(&(&(%))){>>(&(&(%)++))}!;
```

## Computational class

Dirty can be proven to be Turing-complete through simulation of Brainfuck.

## Examples

### Hello, world!

```:(#text)@(\$(&(%))){>>(\$(&(%)++))}![#text]"Hello, world!"
```

### Brainfuck interpreter

```&(0)=#b;&(2)=0;@(\$(&(0))){?(\$(&(0))==\$(#c)){&(&(256|&(2)))++;&(0)++;*}?(\$(&
(0))==\$(#c+1)){&(&(256|&(2)))--;&(0)++;*}?(\$(&(0))==\$(#c+2)){&(256|&(2))++;&
(0)++;*}?(\$(&(0))==\$(#c+3)){&(256|&(2))--;&(0)++;*}?(\$(&(0))==\$(#c+4)){?(!&
(256|&(2))){&(3)=1;@(&(3)){&(0)++;?(\$(&(0))==\$(#c+5)){&(3)--}~{?(\$(&
(0))==\$(#c+4)){&(3)++}}}}&(0)++;*}?(\$(&(0))==\$(#c+4)){?(&(256|&(2))){&
(3)=1;@(&(3)){&(0)--;?(\$(&(0))==\$(#c+4)){&(3)--}~{?(\$(&(0))==\$(#c+5)){&
(3)++}}}}&(0)++;*}?(\$(&(0))==\$(#c+6)){>>(&(256|&(2)));&(0)++;*}?(\$(&
(0))==\$(#c+7)){<<(&(256|&(2)));&(0)++;*}}[#c]"+-><[].,"
[#b]
"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.
<<+++++++++++++++.>.+++.------.--------.>+.>."

```