2018-12-04 23:34:36 +01:00
|
|
|
/*
|
|
|
|
* Author Pavel Iakovlev
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MEMSIZE 4096*10
|
|
|
|
|
|
|
|
#include "../lib/io.h"
|
|
|
|
#include "../lib/obj/console.h"
|
2018-12-06 09:59:06 +01:00
|
|
|
#include "../lib/array.h"
|
2018-12-04 23:34:36 +01:00
|
|
|
|
|
|
|
byte initConsole = 0;
|
2018-12-06 09:59:06 +01:00
|
|
|
Dictionary functions = {0};
|
|
|
|
Dictionary variables = {0};
|
2018-12-04 23:34:36 +01:00
|
|
|
|
|
|
|
#include "stdcall.h"
|
|
|
|
|
|
|
|
#define bufferSize 10000;
|
|
|
|
#define memoryBrainfuck 30000*4
|
|
|
|
#define memoryByteBF 1
|
|
|
|
#define stackBrainFuck 4*1024
|
|
|
|
|
|
|
|
dword buffer = 0;
|
|
|
|
word bufferSymbol = 0;
|
|
|
|
dword memory = 0;
|
|
|
|
|
|
|
|
dword stack = 0;
|
|
|
|
dword code = 0;
|
|
|
|
byte tempBuffer[100] = {0};
|
|
|
|
|
|
|
|
void consoleInit()
|
|
|
|
{
|
|
|
|
IF(!initConsole)
|
|
|
|
{
|
|
|
|
load_dll(libConsole, #con_init, 0);
|
|
|
|
con_init stdcall (-1, -1, -1, -1, "Lisp interpreter");
|
|
|
|
initConsole = 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dword evalLisp()
|
|
|
|
{
|
|
|
|
byte s = 0;
|
|
|
|
byte args = 0;
|
|
|
|
dword pos = 0;
|
|
|
|
dword name = 0;
|
|
|
|
dword tmp = 0;
|
|
|
|
dword dataArgs = 0;
|
|
|
|
dword posArgs = 0;
|
|
|
|
dword ret = 0;
|
2018-12-06 22:09:52 +01:00
|
|
|
dword p = 0;
|
2018-12-04 23:34:36 +01:00
|
|
|
dataArgs = malloc(16*4);
|
|
|
|
posArgs = dataArgs;
|
|
|
|
name = malloc(100);
|
|
|
|
pos = name;
|
|
|
|
loop()
|
|
|
|
{
|
|
|
|
s = DSBYTE[code];
|
2018-12-06 22:09:52 +01:00
|
|
|
|
2018-12-04 23:34:36 +01:00
|
|
|
while(s == ' ')
|
|
|
|
{
|
|
|
|
code++;
|
|
|
|
s = DSBYTE[code];
|
|
|
|
}
|
2018-12-06 22:09:52 +01:00
|
|
|
|
2018-12-04 23:34:36 +01:00
|
|
|
if (!s) || (s==')')
|
|
|
|
{
|
|
|
|
code++;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-06 22:09:52 +01:00
|
|
|
|
2018-12-04 23:34:36 +01:00
|
|
|
if (!args)
|
|
|
|
{
|
2018-12-06 22:09:52 +01:00
|
|
|
if (s != ')') // name function
|
2018-12-04 23:34:36 +01:00
|
|
|
{
|
2018-12-06 22:09:52 +01:00
|
|
|
while (s != ' ') && (s != ')')
|
2018-12-04 23:34:36 +01:00
|
|
|
{
|
|
|
|
DSBYTE[pos] = s;
|
2018-12-06 22:09:52 +01:00
|
|
|
pos++;
|
2018-12-04 23:34:36 +01:00
|
|
|
code++;
|
|
|
|
s = DSBYTE[code];
|
|
|
|
}
|
|
|
|
code--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(s == '(')
|
|
|
|
{
|
|
|
|
code++;
|
2018-12-06 22:09:52 +01:00
|
|
|
tmp = evalLisp();
|
2018-12-04 23:34:36 +01:00
|
|
|
}
|
|
|
|
else if (s >= '0') && (s <= '9')
|
|
|
|
{
|
|
|
|
tmp = 0;
|
|
|
|
while (s >= '0') && (s <= '9')
|
|
|
|
{
|
|
|
|
tmp *= 10;
|
|
|
|
tmp += s-'0';
|
|
|
|
code++;
|
|
|
|
s = DSBYTE[code];
|
|
|
|
}
|
|
|
|
code--;
|
|
|
|
}
|
2018-12-06 22:09:52 +01:00
|
|
|
else if (s == '"')
|
|
|
|
{
|
|
|
|
tmp = malloc(100);
|
|
|
|
p = tmp;
|
|
|
|
code++;
|
|
|
|
s = DSBYTE[code];
|
|
|
|
while (s != '"') && (s)
|
|
|
|
{
|
|
|
|
DSBYTE[p] = s;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
code++;
|
|
|
|
s = DSBYTE[code];
|
|
|
|
}
|
|
|
|
DSBYTE[p] = 0;
|
|
|
|
}
|
|
|
|
else if(s >= 'A') && (s <= 'z')
|
|
|
|
{
|
|
|
|
tmp = malloc(100);
|
|
|
|
p = tmp;
|
|
|
|
while (s >= 'A') && (s <= 'z')
|
|
|
|
{
|
|
|
|
DSBYTE[p] = s;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
code++;
|
|
|
|
s = DSBYTE[code];
|
|
|
|
}
|
|
|
|
DSBYTE[p] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DSDWORD[posArgs] = tmp;
|
|
|
|
posArgs += 4;
|
2018-12-04 23:34:36 +01:00
|
|
|
}
|
|
|
|
code++;
|
|
|
|
args++;
|
|
|
|
}
|
2018-12-06 09:59:06 +01:00
|
|
|
args--;
|
2018-12-04 23:34:36 +01:00
|
|
|
ret = StdCall(args, name, dataArgs);
|
|
|
|
free(name);
|
|
|
|
free(dataArgs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
dword brainFuckCode = 0;
|
|
|
|
word maxLoop = 1000;
|
2018-12-06 22:09:52 +01:00
|
|
|
dword txt = " (print 1)(print 2)";
|
2018-12-04 23:34:36 +01:00
|
|
|
|
|
|
|
buffer = malloc(bufferSize);
|
|
|
|
memory = malloc(memoryBrainfuck);
|
|
|
|
stack = malloc(stackBrainFuck);
|
|
|
|
|
2018-12-06 09:59:06 +01:00
|
|
|
Init();
|
|
|
|
|
2018-12-04 23:34:36 +01:00
|
|
|
IF(DSBYTE[I_Param])
|
|
|
|
{
|
|
|
|
IF(io.read(I_Param))
|
|
|
|
{
|
|
|
|
code = EAX;
|
|
|
|
loop()
|
|
|
|
{
|
2018-12-06 22:09:52 +01:00
|
|
|
WHILE(DSBYTE[code] == ' ') code++;
|
|
|
|
IF(DSBYTE[code]!='(') BREAK;
|
|
|
|
ELSE code++;
|
2018-12-04 23:34:36 +01:00
|
|
|
evalLisp();
|
2018-12-06 22:09:52 +01:00
|
|
|
code--;
|
|
|
|
IF(DSBYTE[code]!=')') BREAK;
|
|
|
|
ELSE code++;
|
2018-12-04 23:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 09:59:06 +01:00
|
|
|
else
|
2018-12-04 23:34:36 +01:00
|
|
|
{
|
|
|
|
consoleInit();
|
2018-12-06 09:59:06 +01:00
|
|
|
con_printf stdcall ("Lisp interpreter v1.2");
|
2018-12-06 22:09:52 +01:00
|
|
|
while(maxLoop)
|
2018-12-04 23:34:36 +01:00
|
|
|
{
|
|
|
|
con_printf stdcall ("\r\n\r\nEnter code: ");
|
|
|
|
con_gets stdcall(buffer, bufferSize);
|
|
|
|
code = EAX;
|
2018-12-06 22:09:52 +01:00
|
|
|
//code = txt;
|
2018-12-04 23:34:36 +01:00
|
|
|
con_printf stdcall ("Output: ");
|
|
|
|
nextLispLine:
|
2018-12-06 09:59:06 +01:00
|
|
|
while(DSBYTE[code] == ' ') code++;
|
2018-12-06 22:09:52 +01:00
|
|
|
if(DSBYTE[code] == '(') code++;
|
|
|
|
else goto endNext;
|
2018-12-04 23:34:36 +01:00
|
|
|
evalLisp();
|
2018-12-06 22:09:52 +01:00
|
|
|
code--;
|
|
|
|
if(DSBYTE[code]==')') code++;
|
|
|
|
else goto endNext;
|
|
|
|
IF(!DSBYTE[code]) goto endNext;
|
2018-12-04 23:34:36 +01:00
|
|
|
goto nextLispLine;
|
2018-12-06 22:09:52 +01:00
|
|
|
endNext:
|
2018-12-04 23:34:36 +01:00
|
|
|
maxLoop--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IF(initConsole) con_exit stdcall (0);
|
|
|
|
ExitProcess();
|
|
|
|
}
|
|
|
|
|