From 1ffceb0340109999ced34629ade33d10780a520f Mon Sep 17 00:00:00 2001 From: pavelyakov Date: Thu, 6 Dec 2018 21:09:52 +0000 Subject: [PATCH] Lisp v1.2 fixed errors git-svn-id: svn://kolibrios.org@7564 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/lisp/example/str.lisp | 2 +- programs/cmm/lisp/lisp.c | 76 ++++++++++++++++++++++-------- programs/cmm/lisp/stdcall.h | 48 +++++++++++++++++-- 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/programs/cmm/lisp/example/str.lisp b/programs/cmm/lisp/example/str.lisp index e73507cb7f..341ca12e51 100644 --- a/programs/cmm/lisp/example/str.lisp +++ b/programs/cmm/lisp/example/str.lisp @@ -1 +1 @@ -(print (str 123)) \ No newline at end of file +(set var1 "test string")(print (get var1)) \ No newline at end of file diff --git a/programs/cmm/lisp/lisp.c b/programs/cmm/lisp/lisp.c index eafba65838..3b569f21f6 100644 --- a/programs/cmm/lisp/lisp.c +++ b/programs/cmm/lisp/lisp.c @@ -47,6 +47,7 @@ dword evalLisp() dword dataArgs = 0; dword posArgs = 0; dword ret = 0; + dword p = 0; dataArgs = malloc(16*4); posArgs = dataArgs; name = malloc(100); @@ -54,26 +55,29 @@ dword evalLisp() loop() { s = DSBYTE[code]; + while(s == ' ') { code++; s = DSBYTE[code]; } + if (!s) || (s==')') { code++; break; } + if (!args) { - if(s!=' ') && (s!=')') // name function + if (s != ')') // name function { - while (s!=' ') && (s!=')') + while (s != ' ') && (s != ')') { DSBYTE[pos] = s; + pos++; code++; s = DSBYTE[code]; - pos++; } code--; } @@ -83,8 +87,7 @@ dword evalLisp() if(s == '(') { code++; - DSDWORD[posArgs] = evalLisp(); - posArgs += 4; + tmp = evalLisp(); } else if (s >= '0') && (s <= '9') { @@ -97,9 +100,40 @@ dword evalLisp() s = DSBYTE[code]; } code--; - DSDWORD[posArgs] = tmp; - posArgs += 4; } + 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; } code++; args++; @@ -115,7 +149,7 @@ void main() { dword brainFuckCode = 0; word maxLoop = 1000; - dword txt = "(print (str 1) (str 2))"; + dword txt = " (print 1)(print 2)"; buffer = malloc(bufferSize); memory = malloc(memoryBrainfuck); @@ -130,12 +164,13 @@ void main() code = EAX; loop() { - while(DSBYTE[code] == ' ') code++; - if(DSBYTE[code]!='(') break; - else code++; + WHILE(DSBYTE[code] == ' ') code++; + IF(DSBYTE[code]!='(') BREAK; + ELSE code++; evalLisp(); - if(DSBYTE[code]!=')') break; - code++; + code--; + IF(DSBYTE[code]!=')') BREAK; + ELSE code++; } } } @@ -143,21 +178,24 @@ void main() { consoleInit(); con_printf stdcall ("Lisp interpreter v1.2"); - WHILE(maxLoop) + while(maxLoop) { con_printf stdcall ("\r\n\r\nEnter code: "); con_gets stdcall(buffer, bufferSize); code = EAX; - code = txt; + //code = txt; con_printf stdcall ("Output: "); nextLispLine: while(DSBYTE[code] == ' ') code++; - if(DSBYTE[code]!='(') continue; - else code++; + if(DSBYTE[code] == '(') code++; + else goto endNext; evalLisp(); - if(DSBYTE[code]!=')') continue; - code++; + code--; + if(DSBYTE[code]==')') code++; + else goto endNext; + IF(!DSBYTE[code]) goto endNext; goto nextLispLine; + endNext: maxLoop--; } } diff --git a/programs/cmm/lisp/stdcall.h b/programs/cmm/lisp/stdcall.h index 3ecc407d4f..858e1873cf 100644 --- a/programs/cmm/lisp/stdcall.h +++ b/programs/cmm/lisp/stdcall.h @@ -1,6 +1,9 @@ -:dword std_print(dword count, args) + + +dword std_print(dword count, args) { consoleInit(); + count = 1; WHILE(count) { 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 tmp = 0; @@ -52,7 +75,7 @@ void Init() functions.init(100); /* Console functions */ - functions.set("print", #std_print); + /* String functions */ functions.set("str", #std_str); @@ -64,13 +87,30 @@ void Init() functions.set("+", #std_add); functions.set("-", #std_sub); + /* Lisp functions */ + functions.set("set", #std_set); + functions.set("get", #std_get); + variables.init(100); } dword StdCall(dword count, name, args) { dword tmp = 0; + 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; -} \ No newline at end of file +} +