From 03104b5bad544f5aa55765f04b983e0df4f15ec4 Mon Sep 17 00:00:00 2001 From: pavelyakov Date: Mon, 14 Jan 2019 09:08:52 +0000 Subject: [PATCH] Math interpreter allow calc git-svn-id: svn://kolibrios.org@7574 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/math/compile.sh | 5 ++ programs/cmm/math/math.c | 138 +++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100755 programs/cmm/math/compile.sh create mode 100644 programs/cmm/math/math.c diff --git a/programs/cmm/math/compile.sh b/programs/cmm/math/compile.sh new file mode 100755 index 0000000000..ddd47b53e8 --- /dev/null +++ b/programs/cmm/math/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/math/math.c b/programs/cmm/math/math.c new file mode 100644 index 0000000000..893f8a8afd --- /dev/null +++ b/programs/cmm/math/math.c @@ -0,0 +1,138 @@ +/* + * Author Pavel Iakovlev +*/ + +#define MEMSIZE 4096*10 + +#include "../lib/io.h" +#include "../lib/obj/console.h" +#include "../lib/array.h" + +byte initConsole = 0; +Dictionary functions = {0}; +Dictionary variables = {0}; + + +#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, "Math interpreter"); + initConsole = 0xFF; + } +} + +:dword getInteger() +{ + dword i = 0; + byte z = 0; + if (DSBYTE[code] == ' ') code++; + if (DSBYTE[code] == '-') + { + z = 0xFF; + code++; + } + if (DSBYTE[code] >= '0') && (DSBYTE[code] <= '9') + { + while (DSBYTE[code] >= '0') && (DSBYTE[code] <= '9') + { + i *= 10; + i += DSBYTE[code] - '0'; + code++; + } + + if (z) return -i; + return i; + } + return 0; +} + +:dword mathEval(dword i) +{ + while (DSBYTE[code] == ' ') code++; + code++; + switch (DSBYTE[code-1]) + { + case '+': + return i + mathEval(getInteger()); + break; + case '-': + return i - mathEval(getInteger()); + break; + case '/': + return i / mathEval(getInteger()); + break; + case '*': + return i * mathEval(getInteger()); + break; + case '(': + return mathEval(mathEval(getInteger())); + break; + case ')': + return i; + break; + case 0: + return 0; + break; + } + return i; +} + +:dword evalMath() +{ + return mathEval(getInteger()); +} + +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; + evalMath(); + } + } + else + { + consoleInit(); + con_printf stdcall ("Math interpreter v1.0"); + while(maxLoop) + { + con_printf stdcall ("\r\n\r\n: "); + con_gets stdcall(buffer, bufferSize); + code = EAX; + //code = txt; + con_printf stdcall ("Result: "); + evalMath(); + con_printf stdcall (itoa(EAX)); + maxLoop--; + } + } + + IF(initConsole) con_exit stdcall (0); + ExitProcess(); +} +