forked from KolibriOS/kolibrios
Brainfuck interpreter ver. 1.01
git-svn-id: svn://kolibrios.org@7556 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
6f6803f8ed
commit
d4f784b890
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Console example in C--
|
* Author Pavel Iakovlev
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define MEMSIZE 4096*10
|
#define MEMSIZE 4096*10
|
||||||
@ -15,17 +15,23 @@
|
|||||||
dword buffer = 0;
|
dword buffer = 0;
|
||||||
word bufferSymbol = 0;
|
word bufferSymbol = 0;
|
||||||
dword memory = 0;
|
dword memory = 0;
|
||||||
dword stack = 0;
|
byte initConsole = 0;
|
||||||
|
|
||||||
|
void consoleInit()
|
||||||
|
{
|
||||||
|
IF(!initConsole)
|
||||||
|
{
|
||||||
|
con_init stdcall (-1, -1, -1, -1, "BrainF*ck interpreter");
|
||||||
|
initConsole = 0xFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void evalBrainFuckCode(dword code)
|
void evalBrainFuckCode(dword code)
|
||||||
{
|
{
|
||||||
byte cmd = 0;
|
|
||||||
dword offsetMemory = 0;
|
dword offsetMemory = 0;
|
||||||
dword countStack = 0;
|
dword countStack = 0;
|
||||||
dword countOffset = memoryBrainfuck / 2 / memoryByteBF;
|
dword countOffset = memoryBrainfuck / 2 / memoryByteBF;
|
||||||
offsetMemory = memory;
|
offsetMemory = memory;
|
||||||
countStack = stack;
|
|
||||||
|
|
||||||
// clear memory
|
// clear memory
|
||||||
EBX = memoryBrainfuck;
|
EBX = memoryBrainfuck;
|
||||||
offsetMemory = memory;
|
offsetMemory = memory;
|
||||||
@ -39,12 +45,9 @@ void evalBrainFuckCode(dword code)
|
|||||||
|
|
||||||
offsetMemory = memory + countOffset;
|
offsetMemory = memory + countOffset;
|
||||||
|
|
||||||
con_printf stdcall ("Output BrainF*ck:\r\n");
|
WHILE(1)
|
||||||
|
|
||||||
while(1)
|
|
||||||
{
|
{
|
||||||
cmd = DSBYTE[code];
|
switch(DSBYTE[code])
|
||||||
switch(cmd)
|
|
||||||
{
|
{
|
||||||
case '+':
|
case '+':
|
||||||
DSBYTE[offsetMemory]++;
|
DSBYTE[offsetMemory]++;
|
||||||
@ -53,18 +56,19 @@ void evalBrainFuckCode(dword code)
|
|||||||
DSBYTE[offsetMemory]--;
|
DSBYTE[offsetMemory]--;
|
||||||
break;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
DSDWORD[countStack] = code;
|
$push code;
|
||||||
countStack += 4;
|
|
||||||
break;
|
break;
|
||||||
case ']':
|
case ']':
|
||||||
IF (DSBYTE[offsetMemory]) code = DSDWORD[countStack - 4];
|
IF (DSBYTE[offsetMemory]) code = DSDWORD[ESP];
|
||||||
ELSE countStack -= 4;
|
ELSE $pop EAX;
|
||||||
break;
|
break;
|
||||||
case ',':
|
case ',':
|
||||||
|
consoleInit();
|
||||||
con_getch stdcall();
|
con_getch stdcall();
|
||||||
DSBYTE[offsetMemory] = AL;
|
DSBYTE[offsetMemory] = AL;
|
||||||
break;
|
break;
|
||||||
case '.':
|
case '.':
|
||||||
|
consoleInit();
|
||||||
DSBYTE[#bufferSymbol] = DSBYTE[offsetMemory];
|
DSBYTE[#bufferSymbol] = DSBYTE[offsetMemory];
|
||||||
con_printf stdcall (#bufferSymbol);
|
con_printf stdcall (#bufferSymbol);
|
||||||
break;
|
break;
|
||||||
@ -74,9 +78,7 @@ void evalBrainFuckCode(dword code)
|
|||||||
case '<':
|
case '<':
|
||||||
offsetMemory--;
|
offsetMemory--;
|
||||||
break;
|
break;
|
||||||
default:
|
case 0:
|
||||||
con_printf stdcall ("\r\n");
|
|
||||||
//ExitProcess();
|
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -91,26 +93,27 @@ void main()
|
|||||||
|
|
||||||
buffer = malloc(bufferSize);
|
buffer = malloc(bufferSize);
|
||||||
memory = malloc(memoryBrainfuck);
|
memory = malloc(memoryBrainfuck);
|
||||||
stack = malloc(stackBrainFuck);
|
|
||||||
|
|
||||||
load_dll(libConsole, #con_init, 0);
|
load_dll(libConsole, #con_init, 0);
|
||||||
con_init stdcall (-1, -1, -1, -1, "BrainF*ck interpreter");
|
|
||||||
//con_set_flags stdcall (0x1F);
|
|
||||||
|
|
||||||
IF(DSBYTE[I_Param])
|
IF(DSBYTE[I_Param])
|
||||||
{
|
{
|
||||||
if(io.read(I_Param)) evalBrainFuckCode(EAX);
|
IF(io.read(I_Param)) evalBrainFuckCode(EAX);
|
||||||
}
|
}
|
||||||
ELSE loop()
|
ELSE
|
||||||
{
|
{
|
||||||
con_printf stdcall ("BrainF*ck interpreter v1.0\r\n");
|
consoleInit();
|
||||||
con_printf stdcall ("\r\nEnter BrainF*ck code:\r\n");
|
con_printf stdcall ("BrainF*ck interpreter v1.1\r\n");
|
||||||
//con_write_string stdcall ("\r\n", 2);
|
loop()
|
||||||
con_gets stdcall(buffer, bufferSize);
|
{
|
||||||
//con_printf stdcall (EAX);
|
con_printf stdcall ("\r\nEnter BrainF*ck code:\r\n");
|
||||||
evalBrainFuckCode(EAX);
|
con_printf stdcall ("Output BrainF*ck:\r\n");
|
||||||
|
con_gets stdcall(buffer, bufferSize);
|
||||||
|
evalBrainFuckCode(EAX);
|
||||||
|
con_printf stdcall ("\r\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
con_exit stdcall (0);
|
IF(initConsole) con_exit stdcall (0);
|
||||||
ExitProcess();
|
ExitProcess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user