From 0cb21a159b8522629d975946f1d442ea2d555d33 Mon Sep 17 00:00:00 2001 From: pavelyakov Date: Tue, 4 Dec 2018 22:34:36 +0000 Subject: [PATCH] New lisp for KolibriOS git-svn-id: svn://kolibrios.org@7562 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/lisp/compile.sh | 5 + programs/cmm/lisp/example/str.lisp | 1 + programs/cmm/lisp/lisp.c | 160 +++++++++++++++++++++++++++++ programs/cmm/lisp/stdcall.h | 24 +++++ 4 files changed, 190 insertions(+) create mode 100755 programs/cmm/lisp/compile.sh create mode 100644 programs/cmm/lisp/example/str.lisp create mode 100644 programs/cmm/lisp/lisp.c create mode 100644 programs/cmm/lisp/stdcall.h diff --git a/programs/cmm/lisp/compile.sh b/programs/cmm/lisp/compile.sh new file mode 100755 index 0000000000..ddd47b53e8 --- /dev/null +++ b/programs/cmm/lisp/compile.sh @@ -0,0 +1,5 @@ +#!/bin/bash +for file in `find ./ -type f -name "*.c"` +do + cmm $file; +done \ No newline at end of file diff --git a/programs/cmm/lisp/example/str.lisp b/programs/cmm/lisp/example/str.lisp new file mode 100644 index 0000000000..e73507cb7f --- /dev/null +++ b/programs/cmm/lisp/example/str.lisp @@ -0,0 +1 @@ +(print (str 123)) \ No newline at end of file diff --git a/programs/cmm/lisp/lisp.c b/programs/cmm/lisp/lisp.c new file mode 100644 index 0000000000..5eb4ab15bd --- /dev/null +++ b/programs/cmm/lisp/lisp.c @@ -0,0 +1,160 @@ +/* + * Author Pavel Iakovlev +*/ + +#define MEMSIZE 4096*10 + +#include "../lib/io.h" +#include "../lib/obj/console.h" + +byte initConsole = 0; + +#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; + dataArgs = malloc(16*4); + posArgs = dataArgs; + name = malloc(100); + pos = name; + loop() + { + s = DSBYTE[code]; + while(s == ' ') + { + code++; + s = DSBYTE[code]; + } + if (!s) || (s==')') + { + code++; + break; + } + if (!args) + { + if(s>='A') && (s<='z') // name function + { + while (s>='A') && (s<='z') + { + DSBYTE[pos] = s; + code++; + s = DSBYTE[code]; + pos++; + } + code--; + } + } + else + { + if(s == '(') + { + code++; + DSDWORD[posArgs] = evalLisp(); + posArgs += 4; + } + else if (s >= '0') && (s <= '9') + { + tmp = 0; + while (s >= '0') && (s <= '9') + { + tmp *= 10; + tmp += s-'0'; + code++; + s = DSBYTE[code]; + } + code--; + DSDWORD[posArgs] = tmp; + posArgs += 4; + } + } + code++; + args++; + } + args -= 2; + ret = StdCall(args, name, dataArgs); + free(name); + free(dataArgs); + return ret; +} + + +void main() +{ + dword brainFuckCode = 0; + word maxLoop = 1000; + + buffer = malloc(bufferSize); + memory = malloc(memoryBrainfuck); + stack = malloc(stackBrainFuck); + + IF(DSBYTE[I_Param]) + { + IF(io.read(I_Param)) + { + code = EAX; + loop() + { + if(DSBYTE[code]!='(') break; + else code++; + evalLisp(); + if(DSBYTE[code]!=')') break; + code++; + } + } + } + ELSE + { + consoleInit(); + con_printf stdcall ("Lisp interpreter v1.0"); + WHILE(maxLoop) + { + con_printf stdcall ("\r\n\r\nEnter code: "); + con_gets stdcall(buffer, bufferSize); + code = EAX; + con_printf stdcall ("Output: "); + nextLispLine: + if(DSBYTE[code]!='(') continue; + else code++; + evalLisp(); + if(DSBYTE[code]!=')') continue; + code++; + goto nextLispLine; + maxLoop--; + } + } + + IF(initConsole) con_exit stdcall (0); + ExitProcess(); +} + diff --git a/programs/cmm/lisp/stdcall.h b/programs/cmm/lisp/stdcall.h new file mode 100644 index 0000000000..261d579ade --- /dev/null +++ b/programs/cmm/lisp/stdcall.h @@ -0,0 +1,24 @@ +dword StdCall(dword count, name, args) +{ + dword tmp = 0; + if(!strcmp(name, "print")) + { + consoleInit(); + con_printf stdcall (DSDWORD[args]); + } + else if(!strcmp(name, "input")) + { + + } + else if(!strcmp(name, "str")) + { + tmp = malloc(15); + itoa_(tmp,DSDWORD[args]); + return tmp; + } + else if(!strcmp(name, "exit")) + { + ExitProcess(); + } + return 0; +} \ No newline at end of file