Lisp v1.3 stable version

git-svn-id: svn://kolibrios.org@7565 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
pavelyakov 2018-12-07 11:22:15 +00:00
parent 1ffceb0340
commit 5cafe795a0
2 changed files with 71 additions and 59 deletions

View File

@ -50,8 +50,7 @@ dword evalLisp()
dword p = 0; dword p = 0;
dataArgs = malloc(16*4); dataArgs = malloc(16*4);
posArgs = dataArgs; posArgs = dataArgs;
name = malloc(100);
pos = name;
loop() loop()
{ {
s = DSBYTE[code]; s = DSBYTE[code];
@ -61,35 +60,45 @@ dword evalLisp()
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
} }
if (!s) return 0;
if (!s) || (s==')') if (s==')')
{ {
code++; code++;
break; args--;
ret = StdCall(args, name, dataArgs);
free(name);
//free(dataArgs);
return ret;
} }
if(s == '(')
if (!args) {
code++;
DSDWORD[posArgs] = evalLisp();
args++;
posArgs += 4;
continue;
}
else if (!args)
{ {
if (s != ')') // name function if (s != ')') // name function
{ {
while (s != ' ') && (s != ')') name = malloc(100);
pos = name;
while (s) && (s != ' ') && (s != ')')
{ {
DSBYTE[pos] = s; DSBYTE[pos] = s;
pos++; pos++;
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
} }
code--; DSBYTE[pos] = 0;
args++;
continue;
} }
} }
else else
{ {
if(s == '(') if (s >= '0') && (s <= '9')
{
code++;
tmp = evalLisp();
}
else if (s >= '0') && (s <= '9')
{ {
tmp = 0; tmp = 0;
while (s >= '0') && (s <= '9') while (s >= '0') && (s <= '9')
@ -99,7 +108,10 @@ dword evalLisp()
code++; code++;
s = DSBYTE[code]; s = DSBYTE[code];
} }
code--; args++;
DSDWORD[posArgs] = tmp;
posArgs += 4;
continue;
} }
else if (s == '"') else if (s == '"')
{ {
@ -130,6 +142,10 @@ dword evalLisp()
s = DSBYTE[code]; s = DSBYTE[code];
} }
DSBYTE[p] = 0; DSBYTE[p] = 0;
args++;
DSDWORD[posArgs] = tmp;
posArgs += 4;
continue;
} }
DSDWORD[posArgs] = tmp; DSDWORD[posArgs] = tmp;
@ -141,7 +157,7 @@ dword evalLisp()
args--; args--;
ret = StdCall(args, name, dataArgs); ret = StdCall(args, name, dataArgs);
free(name); free(name);
free(dataArgs); //free(dataArgs);
return ret; return ret;
} }
@ -149,7 +165,7 @@ void main()
{ {
dword brainFuckCode = 0; dword brainFuckCode = 0;
word maxLoop = 1000; word maxLoop = 1000;
dword txt = " (print 1)(print 2)"; dword txt = "(print (input \"test:\"))";
buffer = malloc(bufferSize); buffer = malloc(bufferSize);
memory = malloc(memoryBrainfuck); memory = malloc(memoryBrainfuck);
@ -177,7 +193,7 @@ void main()
else else
{ {
consoleInit(); consoleInit();
con_printf stdcall ("Lisp interpreter v1.2"); con_printf stdcall ("Lisp interpreter v1.3");
while(maxLoop) while(maxLoop)
{ {
con_printf stdcall ("\r\n\r\nEnter code: "); con_printf stdcall ("\r\n\r\nEnter code: ");
@ -185,17 +201,7 @@ void main()
code = EAX; code = EAX;
//code = txt; //code = txt;
con_printf stdcall ("Output: "); con_printf stdcall ("Output: ");
nextLispLine:
while(DSBYTE[code] == ' ') code++;
if(DSBYTE[code] == '(') code++;
else goto endNext;
evalLisp(); evalLisp();
code--;
if(DSBYTE[code]==')') code++;
else goto endNext;
IF(!DSBYTE[code]) goto endNext;
goto nextLispLine;
endNext:
maxLoop--; maxLoop--;
} }
} }

View File

@ -1,17 +1,5 @@
/* Lisp functions */
dword std_print(dword count, args)
{
consoleInit();
count = 1;
WHILE(count)
{
con_printf stdcall (DSDWORD[args]);
args+=4;
count--;
}
}
:dword std_set(dword count, args) :dword std_set(dword count, args)
{ {
dword name = 0; dword name = 0;
@ -29,17 +17,20 @@ dword std_print(dword count, args)
:dword std_get(dword count, args) :dword std_get(dword count, args)
{ {
IF(!count) RETURN 0;
RETURN variables.get(DSDWORD[args]); RETURN variables.get(DSDWORD[args]);
} }
:dword std_str(dword count, args) :dword std_str(dword count, args)
{ {
dword tmp = 0; dword tmp = 0;
IF(!count) RETURN "";
tmp = malloc(15); tmp = malloc(15);
itoa_(tmp,DSDWORD[args]); itoa_(tmp,DSDWORD[args]);
RETURN tmp; RETURN tmp;
} }
/* Math functions */
:dword std_add(dword count, args) :dword std_add(dword count, args)
{ {
dword ret = 0; dword ret = 0;
@ -70,12 +61,40 @@ dword std_print(dword count, args)
RETURN ret; RETURN ret;
} }
/* Console functions */
:dword std_print(dword count, args)
{
dword ret = 0;
WHILE(count)
{
con_printf stdcall (DSDWORD[args]);
args+=4;
count--;
}
RETURN ret;
}
:dword std_input(dword count, args)
{
dword buf = 0;
buf = malloc(100);
WHILE(count)
{
con_printf stdcall (DSDWORD[args]);
args+=4;
count--;
}
con_gets stdcall(buf, 100);
RETURN EAX;
}
void Init() void Init()
{ {
functions.init(100); functions.init(100);
/* Console functions */ /* Console functions */
functions.set("print", #std_print);
functions.set("input", #std_input);
/* String functions */ /* String functions */
functions.set("str", #std_str); functions.set("str", #std_str);
@ -96,21 +115,8 @@ void Init()
dword StdCall(dword count, name, args) dword StdCall(dword count, name, args)
{ {
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;
} }