2018-11-17 13:27:27 +01:00
|
|
|
/*
|
2018-11-19 09:42:26 +01:00
|
|
|
* Author Pavel Iakovlev
|
2018-11-17 13:27:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define MEMSIZE 4096*10
|
|
|
|
|
2018-11-18 11:55:53 +01:00
|
|
|
#include "../lib/io.h"
|
2018-11-17 13:27:27 +01:00
|
|
|
#include "../lib/obj/console.h"
|
|
|
|
|
|
|
|
#define bufferSize 10000;
|
|
|
|
#define memoryBrainfuck 30000
|
|
|
|
#define memoryByteBF 1
|
|
|
|
#define stackBrainFuck 4*1024
|
|
|
|
|
|
|
|
dword buffer = 0;
|
|
|
|
word bufferSymbol = 0;
|
|
|
|
dword memory = 0;
|
2018-11-19 09:42:26 +01:00
|
|
|
byte initConsole = 0;
|
2018-11-19 10:04:24 +01:00
|
|
|
dword stack = 0;
|
2018-11-19 09:42:26 +01:00
|
|
|
|
|
|
|
void consoleInit()
|
|
|
|
{
|
|
|
|
IF(!initConsole)
|
|
|
|
{
|
2018-11-19 10:04:24 +01:00
|
|
|
load_dll(libConsole, #con_init, 0);
|
2018-11-19 09:42:26 +01:00
|
|
|
con_init stdcall (-1, -1, -1, -1, "BrainF*ck interpreter");
|
|
|
|
initConsole = 0xFF;
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 13:27:27 +01:00
|
|
|
|
|
|
|
void evalBrainFuckCode(dword code)
|
|
|
|
{
|
|
|
|
dword offsetMemory = 0;
|
|
|
|
dword countStack = 0;
|
|
|
|
dword countOffset = memoryBrainfuck / 2 / memoryByteBF;
|
|
|
|
offsetMemory = memory;
|
|
|
|
// clear memory
|
|
|
|
EBX = memoryBrainfuck;
|
|
|
|
offsetMemory = memory;
|
|
|
|
WHILE(EBX)
|
|
|
|
{
|
|
|
|
EBX--;
|
|
|
|
DSBYTE[offsetMemory] = 0;
|
|
|
|
offsetMemory++;
|
|
|
|
}
|
|
|
|
//--------
|
|
|
|
|
|
|
|
offsetMemory = memory + countOffset;
|
2018-11-19 10:04:24 +01:00
|
|
|
countStack = stack;
|
2018-11-17 13:27:27 +01:00
|
|
|
|
2018-11-19 09:42:26 +01:00
|
|
|
WHILE(1)
|
2018-11-17 13:27:27 +01:00
|
|
|
{
|
2018-11-19 09:42:26 +01:00
|
|
|
switch(DSBYTE[code])
|
2018-11-17 13:27:27 +01:00
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
DSBYTE[offsetMemory]++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
DSBYTE[offsetMemory]--;
|
|
|
|
break;
|
|
|
|
case '[':
|
2018-11-19 10:04:24 +01:00
|
|
|
DSDWORD[countStack] = code;
|
|
|
|
countStack += 4;
|
2018-11-17 13:27:27 +01:00
|
|
|
break;
|
|
|
|
case ']':
|
2018-11-19 10:04:24 +01:00
|
|
|
IF (DSBYTE[offsetMemory]) code = DSDWORD[countStack - 4];
|
|
|
|
ELSE countStack -= 4;
|
2018-11-17 13:27:27 +01:00
|
|
|
break;
|
|
|
|
case ',':
|
2018-11-19 09:42:26 +01:00
|
|
|
consoleInit();
|
2018-11-17 13:27:27 +01:00
|
|
|
con_getch stdcall();
|
|
|
|
DSBYTE[offsetMemory] = AL;
|
|
|
|
break;
|
|
|
|
case '.':
|
2018-11-19 09:42:26 +01:00
|
|
|
consoleInit();
|
2018-11-17 13:27:27 +01:00
|
|
|
DSBYTE[#bufferSymbol] = DSBYTE[offsetMemory];
|
|
|
|
con_printf stdcall (#bufferSymbol);
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
offsetMemory++;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
offsetMemory--;
|
|
|
|
break;
|
2018-11-19 09:42:26 +01:00
|
|
|
case 0:
|
2018-11-17 13:27:27 +01:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
code++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:55:53 +01:00
|
|
|
|
2018-11-17 13:27:27 +01:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
dword brainFuckCode = 0;
|
|
|
|
|
|
|
|
buffer = malloc(bufferSize);
|
|
|
|
memory = malloc(memoryBrainfuck);
|
2018-11-19 10:04:24 +01:00
|
|
|
stack = malloc(stackBrainFuck);
|
2018-11-18 11:55:53 +01:00
|
|
|
|
|
|
|
IF(DSBYTE[I_Param])
|
|
|
|
{
|
2018-11-19 09:42:26 +01:00
|
|
|
IF(io.read(I_Param)) evalBrainFuckCode(EAX);
|
2018-11-18 11:55:53 +01:00
|
|
|
}
|
2018-11-19 09:42:26 +01:00
|
|
|
ELSE
|
2018-11-17 13:27:27 +01:00
|
|
|
{
|
2018-11-19 09:42:26 +01:00
|
|
|
consoleInit();
|
2018-11-19 10:04:24 +01:00
|
|
|
con_printf stdcall ("BrainF*ck interpreter v1.01\r\n");
|
2018-11-19 09:42:26 +01:00
|
|
|
loop()
|
|
|
|
{
|
|
|
|
con_printf stdcall ("\r\nEnter BrainF*ck code:\r\n");
|
|
|
|
con_gets stdcall(buffer, bufferSize);
|
|
|
|
evalBrainFuckCode(EAX);
|
2018-11-19 10:04:24 +01:00
|
|
|
con_printf stdcall ("\r\nOutput BrainF*ck:\r\n");
|
2018-11-19 09:42:26 +01:00
|
|
|
}
|
2018-11-17 13:27:27 +01:00
|
|
|
}
|
|
|
|
ExitProcess();
|
2018-11-19 10:04:24 +01:00
|
|
|
IF(initConsole) con_exit stdcall (0);
|
2018-11-17 13:27:27 +01:00
|
|
|
}
|
|
|
|
|