Lisp v1.2 fixed errors

git-svn-id: svn://kolibrios.org@7564 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
pavelyakov 2018-12-06 21:09:52 +00:00
parent a592ede59c
commit 1ffceb0340
3 changed files with 102 additions and 24 deletions

View File

@ -1 +1 @@
(print (str 123)) (set var1 "test string")(print (get var1))

View File

@ -47,6 +47,7 @@ dword evalLisp()
dword dataArgs = 0; dword dataArgs = 0;
dword posArgs = 0; dword posArgs = 0;
dword ret = 0; dword ret = 0;
dword p = 0;
dataArgs = malloc(16*4); dataArgs = malloc(16*4);
posArgs = dataArgs; posArgs = dataArgs;
name = malloc(100); name = malloc(100);
@ -54,26 +55,29 @@ dword evalLisp()
loop() loop()
{ {
s = DSBYTE[code]; s = DSBYTE[code];
while(s == ' ') while(s == ' ')
{ {
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
} }
if (!s) || (s==')') if (!s) || (s==')')
{ {
code++; code++;
break; break;
} }
if (!args) if (!args)
{ {
if(s!=' ') && (s!=')') // name function if (s != ')') // name function
{ {
while (s!=' ') && (s!=')') while (s != ' ') && (s != ')')
{ {
DSBYTE[pos] = s; DSBYTE[pos] = s;
pos++;
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
pos++;
} }
code--; code--;
} }
@ -83,8 +87,7 @@ dword evalLisp()
if(s == '(') if(s == '(')
{ {
code++; code++;
DSDWORD[posArgs] = evalLisp(); tmp = evalLisp();
posArgs += 4;
} }
else if (s >= '0') && (s <= '9') else if (s >= '0') && (s <= '9')
{ {
@ -97,10 +100,41 @@ dword evalLisp()
s = DSBYTE[code]; s = DSBYTE[code];
} }
code--; code--;
}
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; DSDWORD[posArgs] = tmp;
posArgs += 4; posArgs += 4;
} }
}
code++; code++;
args++; args++;
} }
@ -115,7 +149,7 @@ void main()
{ {
dword brainFuckCode = 0; dword brainFuckCode = 0;
word maxLoop = 1000; word maxLoop = 1000;
dword txt = "(print (str 1) (str 2))"; dword txt = " (print 1)(print 2)";
buffer = malloc(bufferSize); buffer = malloc(bufferSize);
memory = malloc(memoryBrainfuck); memory = malloc(memoryBrainfuck);
@ -130,12 +164,13 @@ void main()
code = EAX; code = EAX;
loop() loop()
{ {
while(DSBYTE[code] == ' ') code++; WHILE(DSBYTE[code] == ' ') code++;
if(DSBYTE[code]!='(') break; IF(DSBYTE[code]!='(') BREAK;
else code++; ELSE code++;
evalLisp(); evalLisp();
if(DSBYTE[code]!=')') break; code--;
code++; IF(DSBYTE[code]!=')') BREAK;
ELSE code++;
} }
} }
} }
@ -143,21 +178,24 @@ void main()
{ {
consoleInit(); consoleInit();
con_printf stdcall ("Lisp interpreter v1.2"); con_printf stdcall ("Lisp interpreter v1.2");
WHILE(maxLoop) while(maxLoop)
{ {
con_printf stdcall ("\r\n\r\nEnter code: "); con_printf stdcall ("\r\n\r\nEnter code: ");
con_gets stdcall(buffer, bufferSize); con_gets stdcall(buffer, bufferSize);
code = EAX; code = EAX;
code = txt; //code = txt;
con_printf stdcall ("Output: "); con_printf stdcall ("Output: ");
nextLispLine: nextLispLine:
while(DSBYTE[code] == ' ') code++; while(DSBYTE[code] == ' ') code++;
if(DSBYTE[code]!='(') continue; if(DSBYTE[code] == '(') code++;
else code++; else goto endNext;
evalLisp(); evalLisp();
if(DSBYTE[code]!=')') continue; code--;
code++; if(DSBYTE[code]==')') code++;
else goto endNext;
IF(!DSBYTE[code]) goto endNext;
goto nextLispLine; goto nextLispLine;
endNext:
maxLoop--; maxLoop--;
} }
} }

View File

@ -1,6 +1,9 @@
:dword std_print(dword count, args)
dword std_print(dword count, args)
{ {
consoleInit(); consoleInit();
count = 1;
WHILE(count) WHILE(count)
{ {
con_printf stdcall (DSDWORD[args]); con_printf stdcall (DSDWORD[args]);
@ -9,6 +12,26 @@
} }
} }
:dword std_set(dword count, args)
{
dword name = 0;
dword value = 0;
WHILE(count>0)
{
name = DSDWORD[args];
args += 4;
value = DSDWORD[args];
args += 4;
variables.set(name, value);
count-=2;
}
}
:dword std_get(dword count, args)
{
RETURN variables.get(DSDWORD[args]);
}
:dword std_str(dword count, args) :dword std_str(dword count, args)
{ {
dword tmp = 0; dword tmp = 0;
@ -52,7 +75,7 @@ void Init()
functions.init(100); functions.init(100);
/* Console functions */ /* Console functions */
functions.set("print", #std_print);
/* String functions */ /* String functions */
functions.set("str", #std_str); functions.set("str", #std_str);
@ -64,13 +87,30 @@ void Init()
functions.set("+", #std_add); functions.set("+", #std_add);
functions.set("-", #std_sub); functions.set("-", #std_sub);
/* Lisp functions */
functions.set("set", #std_set);
functions.set("get", #std_get);
variables.init(100); variables.init(100);
} }
dword StdCall(dword count, name, args) dword StdCall(dword count, name, args)
{ {
dword tmp = 0; dword tmp = 0;
functions.get(name); functions.get(name);
IF(EAX)RETURN EAX(count, args); IF(EAX) RETURN EAX(count, args);
IF(!strcmp(name, "print"))
{
consoleInit();
count = 1;
WHILE(count)
{
con_printf stdcall (DSDWORD[args]);
args += 4;
count--;
}
}
RETURN 0; RETURN 0;
} }