Upd: Lisp interpreter v1.5

git-svn-id: svn://kolibrios.org@7744 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
pavelyakov 2020-03-21 23:26:21 +00:00
parent a1815a31ee
commit bf41544e29
2 changed files with 153 additions and 20 deletions

View File

@ -19,6 +19,13 @@ Dictionary variables = {0};
#define memoryByteBF 1 #define memoryByteBF 1
#define stackBrainFuck 4*1024 #define stackBrainFuck 4*1024
#define TStr 1
#define TInt 2
#define TSym 3
#define TBol 4
#define TLen 4*5
dword buffer = 0; dword buffer = 0;
word bufferSymbol = 0; word bufferSymbol = 0;
dword memory = 0; dword memory = 0;
@ -44,18 +51,20 @@ dword evalLisp()
dword pos = 0; dword pos = 0;
dword name = 0; dword name = 0;
dword tmp = 0; dword tmp = 0;
dword tmp2 = 0;
dword dataArgs = 0; dword dataArgs = 0;
dword posArgs = 0; dword posArgs = 0;
dword ret = 0; dword ret = 0;
dword p = 0; dword p = 0;
dword i = 0;
dword ii = 0;
dataArgs = malloc(16*4); dataArgs = malloc(16*4);
posArgs = dataArgs; posArgs = dataArgs;
loop() loop()
{ {
s = DSBYTE[code]; s = DSBYTE[code];
while (s == ' ') || (s == 9) || (s == 10) || (s == 13)
while (s == ' ') || (s == 9)
{ {
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
@ -109,18 +118,31 @@ dword evalLisp()
s = DSBYTE[code]; s = DSBYTE[code];
} }
args++; args++;
DSDWORD[posArgs] = tmp; EDX = malloc(TLen);
DSDWORD[EDX] = tmp;
DSDWORD[EDX+4] = TInt;
DSDWORD[posArgs] = EDX;
posArgs += 4; posArgs += 4;
continue; continue;
} }
else if (s == '"') else if (s == '"')
{ {
tmp = malloc(100); i = 1;
tmp = malloc(1<<i);
p = tmp; p = tmp;
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
ii = 0;
while (s != '"') && (s) while (s != '"') && (s)
{ {
ii++;
if (1<<i < ii)
{
i++;
tmp2 = p-tmp;
tmp = realloc(tmp, 1<<i);
p = tmp+tmp2;
}
DSBYTE[p] = s; DSBYTE[p] = s;
p++; p++;
@ -128,22 +150,33 @@ dword evalLisp()
s = DSBYTE[code]; s = DSBYTE[code];
} }
DSBYTE[p] = 0; DSBYTE[p] = 0;
EDX = malloc(TLen);
DSDWORD[EDX] = tmp;
DSDWORD[EDX+4] = TStr;
DSDWORD[EDX+8] = p-tmp;
DSDWORD[posArgs] = EDX;
posArgs += 4;
code++;
args++;
continue;
} }
else if(s >= 'A') && (s <= 'z') else
{ {
tmp = malloc(100); tmp = malloc(20);
p = tmp; p = tmp;
while (s >= 'A') && (s <= 'z') while (s) && (s != ')') && (s != '(') && (s != ' ') && (s != 10) && (s != 13)
{ {
DSBYTE[p] = s; DSBYTE[p] = s;
p++; p++;
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
} }
DSBYTE[p] = 0; DSBYTE[p] = 0;
args++; args++;
DSDWORD[posArgs] = tmp; EDX = malloc(TLen);
DSDWORD[EDX] = tmp;
DSDWORD[EDX+4] = TSym;
DSDWORD[posArgs] = EDX;
posArgs += 4; posArgs += 4;
continue; continue;
} }
@ -184,7 +217,7 @@ void main()
else else
{ {
consoleInit(); consoleInit();
con_printf stdcall ("Lisp interpreter v1.4\r\n"); con_printf stdcall ("Lisp interpreter v1.5\r\n");
while(maxLoop) while(maxLoop)
{ {
con_printf stdcall ("\r\n$ "); con_printf stdcall ("\r\n$ ");

View File

@ -13,6 +13,12 @@
WHILE(count > 0) WHILE(count > 0)
{ {
name = DSDWORD[args]; name = DSDWORD[args];
IF (DSDWORD[name+4] == TSym) name = DSDWORD[name];
ELSE
{
con_printf stdcall ("Error variable!");
ExitProcess();
}
args += 4; args += 4;
value = DSDWORD[args]; value = DSDWORD[args];
args += 4; args += 4;
@ -23,8 +29,15 @@
:dword std_get(dword count, args) :dword std_get(dword count, args)
{ {
dword name = 0;
IF(!count) RETURN 0; IF(!count) RETURN 0;
RETURN variables.get(DSDWORD[args]); name = DSDWORD[args];
IF (DSDWORD[name+4] != TSym)
{
con_printf stdcall ("Error variable!");
ExitProcess();
}
RETURN variables.get(DSDWORD[name]);
} }
:dword std_str(dword count, args) :dword std_str(dword count, args)
@ -71,30 +84,115 @@
:dword std_print(dword count, args) :dword std_print(dword count, args)
{ {
dword ret = 0; dword ret = 0;
dword arg = 0;
dword val = 0;
consoleInit(); consoleInit();
IF (!count) con_printf stdcall ("nil");
WHILE(count) WHILE(count)
{ {
IF(!DSDWORD[args]) con_printf stdcall ("nil"); arg = DSDWORD[args];
ELSE con_printf stdcall (DSDWORD[args]); REPEAT1:
IF (DSDWORD[arg+4] == TInt) val = itoa(DSDWORD[arg]);
ELSE IF (DSDWORD[arg+4] == TStr) val = DSDWORD[arg];
ELSE IF (DSDWORD[arg+4] == TSym)
{
arg = std_get(1, args);
goto REPEAT1;
}
IF(!arg) con_printf stdcall ("nil");
ELSE con_printf stdcall (val);
args+=4; args+=4;
count--; count--;
} }
RETURN ret; RETURN ret;
} }
:dword std_len(dword count, args)
{
dword ret = 0;
dword arg = 0;
dword val = 0;
ret = malloc(TLen);
DSDWORD[ret] = 0;
DSDWORD[ret+4] = TInt;
WHILE(count)
{
arg = DSDWORD[args];
REPEAT1:
IF (DSDWORD[arg+4] == TStr) val = DSDWORD[arg];
ELSE IF (DSDWORD[arg+4] == TSym)
{
arg = std_get(1, args);
goto REPEAT1;
}
ELSE return ret;
DSDWORD[ret] += DSDWORD[arg+8];
args+=4;
count--;
}
RETURN ret;
}
:dword std_cmp(dword count, args)
{
dword ret = 0;
dword arg = 0;
dword val = 0;
dword tmp = 0;
dword x = 0;
dword y = 0;
byte start = 0;
ret = malloc(TLen);
DSDWORD[ret] = 0;
DSDWORD[ret+4] = TInt;
IF (!count) return ret;
while(count)
{
arg = DSDWORD[args];
REPEAT2:
IF (DSDWORD[arg+4] == TSym)
{
arg = std_get(1, args);
goto REPEAT2;
}
IF (!start)
{
start = 1;
tmp = arg;
args+=4;
count--;
continue;
}
IF (DSDWORD[tmp+4] != DSDWORD[arg+4]) return ret;
IF (DSDWORD[tmp+4] == TInt)
{
IF (DSDWORD[tmp] != DSDWORD[arg]) return ret;
}
ELSE IF (DSDWORD[tmp+4] == TStr)
{
/*IF (!DSDWORD[tmp+8]) DSDWORD[tmp+8] = crc32(DSDWORD[tmp]);
IF (!DSDWORD[arg+8]) DSDWORD[arg+8] = crc32(DSDWORD[arg]);
IF (DSDWORD[tmp+8] != DSDWORD[arg+8]) return ret;*/
IF (strcmp(DSDWORD[tmp], DSDWORD[arg])) return ret;
}
args+=4;
count--;
}
DSDWORD[ret] = 1;
return ret;
}
:dword std_input(dword count, args) :dword std_input(dword count, args)
{ {
dword buf = 0; dword buf = 0;
consoleInit(); consoleInit();
buf = malloc(100); buf = malloc(100);
WHILE(count) IF (count) std_print(count, args);
{
con_printf stdcall (DSDWORD[args]);
args+=4;
count--;
}
con_gets stdcall(buf, 100); con_gets stdcall(buf, 100);
RETURN EAX; EDX = malloc(TLen);
DSDWORD[EDX] = buf;
DSDWORD[EDX+4] = TStr;
RETURN EDX;
} }
void Init() void Init()
@ -107,6 +205,7 @@ void Init()
/* String functions */ /* String functions */
functions.set("str", #std_str); functions.set("str", #std_str);
functions.set("len", #std_len);
/* System functions */ /* System functions */
functions.set("exit", #std_exit); functions.set("exit", #std_exit);
@ -114,6 +213,7 @@ void Init()
/* Math functions */ /* Math functions */
functions.set("+", #std_add); functions.set("+", #std_add);
functions.set("-", #std_sub); functions.set("-", #std_sub);
functions.set("==", #std_cmp);
/* Lisp functions */ /* Lisp functions */
functions.set("set", #std_set); functions.set("set", #std_set);