worker.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "worker.h"
  2. int status = 0; //0: idle, 1: running, 2: failure
  3. char* inst = 0;
  4. int instCount = 0;
  5. int instPtr = 0;
  6. int runOpCount = 0;
  7. char* wOutput = 0;
  8. int wOutputPtr = 0;
  9. char* wInput = 0;
  10. int wInputPtr = 0;
  11. uint8_t* bfStack = 0;
  12. int stackPtr = 0;
  13. int stackSize = BF_STACK_INITIAL_SIZE;
  14. int stackSizeReal = 0;
  15. bool validateInstPtr(){
  16. if(instPtr > instCount || instPtr < 0){
  17. return false;
  18. }
  19. return true;
  20. }
  21. bool validateStackPtr(){
  22. if(stackPtr > stackSize || stackPtr < 0){
  23. return false;
  24. }
  25. return true;
  26. }
  27. char* workerGetOutput(){
  28. return wOutput;
  29. }
  30. int getStackSize(){
  31. return stackSizeReal;
  32. }
  33. int getOpCount(){
  34. return runOpCount;
  35. }
  36. int getStatus(){
  37. return status;
  38. }
  39. void initWorker(BFApp* app){
  40. //rebuild output
  41. if(wOutput){ free(wOutput); }
  42. wOutput = (char*)malloc(BF_OUTPUT_SIZE);
  43. wOutputPtr = 0;
  44. //rebuild stack
  45. if(bfStack){ free(bfStack); }
  46. bfStack = (uint8_t*)malloc(BF_STACK_INITIAL_SIZE);
  47. memset(bfStack, 0x00, BF_STACK_INITIAL_SIZE);
  48. stackSize = BF_STACK_INITIAL_SIZE;
  49. stackSizeReal = 0;
  50. stackPtr = 0;
  51. //set instructions
  52. inst = app->dataBuffer;
  53. instCount = app->dataSize;
  54. instPtr = 0;
  55. runOpCount = 0;
  56. //set input
  57. wInput = app->inputBuffer;
  58. wInputPtr = 0;
  59. //set status
  60. status = 0;
  61. }
  62. void rShift(){
  63. runOpCount++;
  64. stackPtr++;
  65. if(!validateStackPtr()){ status = 2; return; }
  66. while(stackPtr > stackSize){
  67. stackSize += BF_STACK_STEP_SIZE;
  68. void* tmp = realloc(bfStack, stackSize);
  69. if(!tmp){
  70. status = 2;
  71. return;
  72. }
  73. memset((tmp + stackSize) - BF_STACK_STEP_SIZE, 0x00, BF_STACK_STEP_SIZE);
  74. bfStack = (uint8_t*)tmp;
  75. };
  76. if(stackPtr > stackSizeReal){
  77. stackSizeReal = stackPtr;
  78. }
  79. }
  80. void lShift(){
  81. runOpCount++;
  82. stackPtr--;
  83. if(!validateStackPtr()){ status = 2; return; }
  84. }
  85. void inc(){
  86. runOpCount++;
  87. if(!validateStackPtr()){ status = 2; return; }
  88. bfStack[stackPtr]++;
  89. }
  90. void dec(){
  91. runOpCount++;
  92. if(!validateStackPtr()){ status = 2; return; }
  93. bfStack[stackPtr]--;
  94. }
  95. void print(){
  96. runOpCount++;
  97. wOutput[wOutputPtr] = bfStack[stackPtr];
  98. wOutputPtr++;
  99. if(wOutputPtr > (BF_OUTPUT_SIZE - 1)){ wOutputPtr = 0;}
  100. }
  101. void input(){
  102. runOpCount++;
  103. bfStack[stackPtr] = (uint8_t)wInput[wInputPtr];
  104. if(wInput[wInputPtr] == 0x00 || wInputPtr >= 64){
  105. wInputPtr = 0;
  106. }
  107. else{
  108. wInputPtr++;
  109. }
  110. }
  111. void loop() {
  112. runOpCount++;
  113. if (bfStack[stackPtr] == 0) {
  114. int loopCount = 1;
  115. while (loopCount > 0) {
  116. instPtr++;
  117. if(!validateInstPtr()){ status = 2; return; }
  118. if (inst[instPtr] == '[') { loopCount++; }
  119. else if (inst[instPtr] == ']') { loopCount--; }
  120. }
  121. }
  122. }
  123. void endLoop() {
  124. runOpCount++;
  125. if (bfStack[stackPtr] != 0) {
  126. int loopCount = 1;
  127. while (loopCount > 0) {
  128. instPtr--;
  129. if(!validateInstPtr()){ status = 2; return; }
  130. if (inst[instPtr] == ']') { loopCount++; }
  131. else if (inst[instPtr] == '[') { loopCount--; }
  132. }
  133. }
  134. }
  135. void beginWorker(){
  136. status = 1;
  137. while (inst[instPtr] != 0x00) {
  138. if(status == 2){ return; }
  139. switch (inst[instPtr]) {
  140. case '>':
  141. rShift();
  142. break;
  143. case '<':
  144. lShift();
  145. break;
  146. case '+':
  147. inc();
  148. break;
  149. case '-':
  150. dec();
  151. break;
  152. case '.':
  153. print();
  154. break;
  155. case ',':
  156. input();
  157. break;
  158. case '[':
  159. loop();
  160. break;
  161. case ']':
  162. endLoop();
  163. break;
  164. default:
  165. break;
  166. }
  167. instPtr++;
  168. if(!validateInstPtr()){ status = 2; return; }
  169. }
  170. status = 0;
  171. }