nctref/tests/bf.nct
2023-08-27 19:48:06 +03:00

81 lines
1.3 KiB
Plaintext

@section(".data");
local u8[16384] data:;
local u8[16384] code:;
local u32[64] stck:;
@section(".text");
extern u32(u32, u8*, u32) write;
extern u32(u32, u8*, u32) read;
read(0, &code, 16384);
u32 codePtr = 0;
u32 dataPtr = 0;
u32 stckPtr = 16rFFFFFFFF;
loop {
if(code[codePtr] == 62) {
dataPtr = dataPtr + 1;
}
if(code[codePtr] == 60) {
dataPtr = dataPtr - 1;
}
if(code[codePtr] == 43) {
data[dataPtr] = data[dataPtr] + 1;
}
if(code[codePtr] == 45) {
data[dataPtr] = data[dataPtr] - 1;
}
if(code[codePtr] == 46) {
u32 z = &data + dataPtr;
write(1, z, 1);
}
if(code[codePtr] == 44) {
u32 z = &data + dataPtr;
read(0, z, 1);
}
if(code[codePtr] == 91) {
if(data[dataPtr] == 0) {
u32 depth = 0;
loop {
if(code[codePtr] == 91) {
depth = depth + 1;
}
if(code[codePtr] == 93) {
depth = depth - 1;
}
if(depth == 0) {
break;
}
codePtr = codePtr + 1;
}
}
if(data[dataPtr] != 0) {
stckPtr = stckPtr + 1;
stck[stckPtr] = codePtr;
}
codePtr = codePtr + 1;
continue;
}
if(code[codePtr] == 93) {
if(data[dataPtr] == 0) {
stckPtr = stckPtr - 1;
}
if(data[dataPtr] != 0) {
codePtr = stck[stckPtr];
}
}
if(code[codePtr] == 0) {
loop {}
}
codePtr = codePtr + 1;
}
codePtr;
dataPtr;
stckPtr;