Skip to content

Commit 9089f3a

Browse files
committed
Add instructions ., , and %, void values, quine example
Signed-off-by: Mavridis Philippe <[email protected]>
1 parent c6b33d0 commit 9089f3a

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ Below is a short summary of the currently available instructions:
4949
> right-shift value by 1
5050
\ reset value in current cell
5151
* replace value with a random number between 0 and the original value
52+
% replace value with the instruction at position indicated by original value
5253
~ swap value with register
54+
. assign current value to register
55+
, assign current register to value
5356
```
5457

5558
### Pointer instructions
@@ -78,10 +81,12 @@ following instructions to jump back to them.
7881

7982
### Conditions
8083
```
81-
& if current value is zero, skip next instruction
82-
` if current value is zero, exit
84+
& if current value is void, skip next instruction
85+
` if current value is void, exit
8386
```
8487

88+
The following control characters are considered void values: 0 (NUL), 10 (LF), 13 (CR).
89+
8590
### Input/output
8691
```
8792
: get value and place it in current cell

examples/quine.duh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
|.%`;~+_

keycodes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
* Date: 2021/11/21 *
55
***************************/
66

7+
/* Control characters */
8+
#define CR 13
9+
#define LF 10
10+
711
/* Value instructions */
812
#define VAL_INCR 43 // + increase value
913
#define VAL_DECR 45 // - decrease value
1014
#define VAL_RSET 92 // \ reset value
1115
#define VAL_RAND 42 // * 0 < random < current value
16+
#define VAL_INST 37 // % get instruction at value position
1217
#define VAL_SWAP 126 // ~ swap value and register
18+
#define VAL_VTOR 46 // . assign value to register
19+
#define VAL_RTOV 44 // , assign register to value
1320
#define VAL_LSFT 60 // < left shift by 1
1421
#define VAL_RSFT 62 // > right shift by 1
1522

main.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ void term_setup()
5252
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
5353
}
5454

55+
int is_void(int value)
56+
{
57+
return !value || value == CR || value == LF;
58+
}
59+
5560
int main( int argc, char* argv[] )
5661
{
5762
if( argc != 2 )
@@ -147,7 +152,6 @@ int main( int argc, char* argv[] )
147152
while(! feof(source) )
148153
{
149154
tmp = getc(source);
150-
151155
if( tmp == JMP_MARK )
152156
break;
153157
}
@@ -184,12 +188,12 @@ int main( int argc, char* argv[] )
184188
}
185189

186190
case CND_SKIP:
187-
if(!*memptr)
191+
if (is_void(*memptr))
188192
fseek(source, 1, SEEK_CUR);
189193
break;
190194

191195
case CND_TERM:
192-
if(!*memptr)
196+
if (is_void(*memptr))
193197
{
194198
cleanup();
195199
return 0;
@@ -200,6 +204,15 @@ int main( int argc, char* argv[] )
200204
*memptr = get_rand(*memptr);
201205
break;
202206

207+
case VAL_INST:
208+
{
209+
int old = ftell(source);
210+
fseek(source, *memptr, SEEK_SET);
211+
*memptr = getc(source);
212+
fseek(source, old, SEEK_SET);
213+
break;
214+
}
215+
203216
case VAL_SWAP:
204217
{
205218
wchar_t tmp = *memptr;
@@ -208,6 +221,14 @@ int main( int argc, char* argv[] )
208221
break;
209222
}
210223

224+
case VAL_VTOR:
225+
reg = *memptr;
226+
break;
227+
228+
case VAL_RTOV:
229+
*memptr = reg;
230+
break;
231+
211232
#ifdef DEBUG
212233
case 84: // T stack trace
213234
debug_print_trace();
@@ -241,7 +262,7 @@ int main( int argc, char* argv[] )
241262

242263
#ifdef SAFETY
243264
/* Check position in memory */
244-
if( memptr == &memory[ sizeof(memory) / sizeof(memory[0]) ] )
265+
if( memptr >= &memory[ sizeof(memory) / sizeof(memory[0]) ] )
245266
{
246267
printf("\nFATAL: tried to access out-of-bounds memory!\n");
247268

0 commit comments

Comments
 (0)