Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Author Pavel Iakovlev
  3. */
  4.  
  5. #define MEMSIZE 4096*10
  6.  
  7. #include "../lib/io.h"
  8. #include "../lib/obj/console.h"
  9. #include "../lib/array.h"
  10.  
  11. byte initConsole = 0;
  12. Dictionary functions = {0};
  13. Dictionary variables = {0};
  14.  
  15.  
  16. #define bufferSize 10000;
  17. #define memoryBrainfuck 30000*4
  18. #define memoryByteBF 1
  19. #define stackBrainFuck 4*1024
  20.  
  21. dword buffer = 0;
  22. word bufferSymbol = 0;
  23. dword memory = 0;
  24.  
  25. dword stack = 0;
  26. dword code = 0;
  27. byte tempBuffer[100] = {0};
  28.  
  29. void consoleInit()
  30. {
  31.         IF(!initConsole)
  32.         {
  33.                 load_dll(libConsole, #con_init, 0);
  34.                 con_init stdcall (-1, -1, -1, -1, "Math interpreter");
  35.                 initConsole = 0xFF;
  36.         }
  37. }
  38.  
  39. :dword getInteger()
  40. {
  41.         dword i = 0;
  42.         byte z = 0;
  43.         if (DSBYTE[code] == ' ') code++;
  44.         if (DSBYTE[code] == '-')
  45.         {
  46.                 z = 0xFF;
  47.                 code++;
  48.         }
  49.         if (DSBYTE[code] >= '0') && (DSBYTE[code] <= '9')
  50.         {
  51.                 while (DSBYTE[code] >= '0') && (DSBYTE[code] <= '9')
  52.                 {
  53.                         i *= 10;
  54.                         i += DSBYTE[code] - '0';
  55.                         code++;
  56.                 }
  57.                
  58.                 if (z) return -i;
  59.                 return i;
  60.         }
  61.         return 0;
  62. }
  63.  
  64. :dword mathEval(dword i)
  65. {
  66.         while (DSBYTE[code] == ' ') code++;
  67.         code++;
  68.         switch (DSBYTE[code-1])
  69.         {
  70.                 case '+':
  71.                         return i + mathEval(getInteger());
  72.                 break;
  73.                 case '-':
  74.                         return i - mathEval(getInteger());
  75.                 break;
  76.                 case '/':
  77.                         return i / mathEval(getInteger());
  78.                 break;
  79.                 case '*':
  80.                         return i * mathEval(getInteger());
  81.                 break;
  82.                 case '(':
  83.                         return mathEval(mathEval(getInteger()));
  84.                 break;
  85.                 case ')':
  86.                         return i;
  87.                 break;
  88.                 case 0:
  89.                         return 0;
  90.                 break;
  91.         }
  92.         return i;
  93. }
  94.  
  95. :dword evalMath()
  96. {
  97.         return mathEval(getInteger());
  98. }
  99.  
  100. void main()
  101. {
  102.         dword brainFuckCode = 0;
  103.         word maxLoop = 1000;
  104.  
  105.         buffer = malloc(bufferSize);
  106.         memory = malloc(memoryBrainfuck);
  107.         stack = malloc(stackBrainFuck);
  108.        
  109.        
  110.         IF(DSBYTE[I_Param])
  111.         {
  112.                 IF(io.read(I_Param))
  113.                 {
  114.                         code = EAX;
  115.                         evalMath();
  116.                 }
  117.         }
  118.         else
  119.         {
  120.                 consoleInit();
  121.                 con_printf stdcall ("Math interpreter v1.0");
  122.                 while(maxLoop)
  123.                 {
  124.                         con_printf stdcall ("\r\n\r\n: ");
  125.                         con_gets stdcall(buffer, bufferSize);
  126.                         code = EAX;
  127.                         //code = txt;
  128.                         con_printf stdcall ("Result: ");
  129.                         evalMath();
  130.                         con_printf stdcall (itoa(EAX));
  131.                         maxLoop--;
  132.                 }
  133.         }
  134.        
  135.         IF(initConsole) con_exit stdcall (0);
  136.         ExitProcess();
  137. }
  138.  
  139.