Snuvi.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "server/snuviscript/Snuvi.h"
  2. #include "Compiler.h"
  3. #include "libraries/Math.h"
  4. #include "libraries/Time.h"
  5. #include "tokenizer/Tokenizer.h"
  6. #include "utils/HashMap.h"
  7. #include "utils/StringBuffer.h"
  8. #include "vm/Script.h"
  9. static const char* unicode(int32 c) {
  10. static char buffer[5];
  11. int index = 0;
  12. if(c > 0xFFFF) {
  13. buffer[index++] = 0xF0 | ((c >> 18) & 0x07);
  14. buffer[index++] = 0x80 | ((c >> 12) & 0x3F);
  15. buffer[index++] = 0x80 | ((c >> 6) & 0x3F);
  16. buffer[index++] = 0x80 | (c & 0x3F);
  17. } else if(c > 0x7FF) {
  18. buffer[index++] = 0xE0 | ((c >> 12) & 0x0F);
  19. buffer[index++] = 0x80 | ((c >> 6) & 0x3F);
  20. buffer[index++] = 0x80 | (c & 0x3F);
  21. } else if(c > 0x7F) {
  22. buffer[index++] = 0xC0 | ((c >> 6) & 0x1F);
  23. buffer[index++] = 0x80 | (c & 0x3F);
  24. } else {
  25. buffer[index++] = c;
  26. }
  27. buffer[index++] = '\0';
  28. return buffer;
  29. }
  30. static void printString(Script* sc) {
  31. int length;
  32. Pointer p;
  33. if(!sPopPointer(sc, &p) || sGetPointerLength(sc, &p, &length)) {
  34. return;
  35. }
  36. for(int i = 0; i < length; i++) {
  37. const void* data = sCheckAddress(sc, &p, sizeof(int));
  38. if(data != nullptr) {
  39. int c;
  40. memcpy(&c, data, sizeof(int));
  41. printf(unicode(c));
  42. }
  43. p.offset += sizeof(int);
  44. }
  45. }
  46. static void printInt32(Script* sc) {
  47. int32 i;
  48. if(sPopInt32(sc, &i)) {
  49. printf("%d", i);
  50. }
  51. }
  52. static void wait(Script* sc) {
  53. sError(sc, "w");
  54. }
  55. static Snuvi::Event event = Snuvi::Event::NONE;
  56. static void getEvent(Script* sc) {
  57. sPushInt32(sc, event);
  58. }
  59. static void initPrinter() {
  60. Snuvi::initFunction("print", dtVoid(), printString);
  61. Snuvi::addArgument(dtConst(dtText()));
  62. Snuvi::addFunction();
  63. Snuvi::initFunction("print", dtVoid(), printInt32);
  64. Snuvi::addArgument(dtInt32());
  65. Snuvi::addFunction();
  66. }
  67. struct Scripts {
  68. int scriptId = 0;
  69. HashMap<int, Script*> scripts;
  70. Scripts() {
  71. gfsInit();
  72. gstsInit();
  73. lTimeRegister();
  74. lMathRegister();
  75. initPrinter();
  76. Snuvi::initFunction("wait", dtVoid(), wait);
  77. Snuvi::addFunction();
  78. Snuvi::initFunction("getEvent", dtInt32(), getEvent);
  79. Snuvi::addFunction();
  80. }
  81. ~Scripts() {
  82. gfsDelete();
  83. gstsDelete();
  84. Snuvi::termAll();
  85. }
  86. };
  87. static Scripts scripts;
  88. static Function function;
  89. void Snuvi::initFunction(const char* name, DataType returnType,
  90. ScriptFunction sf) {
  91. gfInit(&function, name, returnType, sf);
  92. }
  93. void Snuvi::addArgument(DataType type) {
  94. gfAddArgument(&function, type);
  95. }
  96. void Snuvi::addFunction() {
  97. gfsAdd(&function);
  98. }
  99. static void logError(const char* msg, int line) {
  100. puts(msg);
  101. printf("line: %d\n", line);
  102. }
  103. static bool runScript(Script* script) {
  104. sRun(script);
  105. if(script->error[0] == 'w' && script->error[1] == '\0') {
  106. script->error[0] = '\0';
  107. return false;
  108. } else if(script->error[0] != '\0') {
  109. logError(script->error, script->line);
  110. return true;
  111. }
  112. return script->readIndex >= script->code->length;
  113. }
  114. int Snuvi::start(const char* path) {
  115. StringBuffer<256> s("resources/scripts/");
  116. s.append(path).append(".snuvi");
  117. if(tTokenize(s)) {
  118. logError(tGetError(), tGetLine());
  119. return -1;
  120. }
  121. ByteCode* code = cCompile();
  122. if(code == nullptr) {
  123. logError(cGetError(), cGetLine());
  124. return -1;
  125. }
  126. Script* script = sInit(code);
  127. if(runScript(script)) {
  128. sDelete(script);
  129. return -1;
  130. }
  131. int id = scripts.scriptId++;
  132. scripts.scripts.tryEmplace(id, script);
  133. return id;
  134. }
  135. void Snuvi::termAll() {
  136. for(auto& entry : scripts.scripts) {
  137. sDelete(entry.value);
  138. }
  139. scripts.scripts.clear();
  140. }
  141. void Snuvi::callEvent(Event e) {
  142. event = e;
  143. for(auto& entry : scripts.scripts) {
  144. if(runScript(entry.value)) {
  145. sDelete(entry.value);
  146. scripts.scripts.remove(entry.getKey());
  147. }
  148. }
  149. }
  150. static int32 readChar(int& index, const char* s) {
  151. if(s[index] == '\0') {
  152. return '\0';
  153. }
  154. return s[index++];
  155. }
  156. Pointer Snuvi::toString(Script* sc, const char* s) {
  157. List<int> data;
  158. int index = 0;
  159. while(s[index] != '\0') {
  160. int32 c = readChar(index, s);
  161. if((c & 0xE0) == 0xC0) {
  162. c = ((c & 0x1F) << 6) | (readChar(index, s) & 0x3F);
  163. } else if((c & 0xF0) == 0xE0) {
  164. c = ((c & 0xF) << 12) | ((readChar(index, s) & 0x3F) << 6);
  165. c |= readChar(index, s) & 0x3F;
  166. } else if((c & 0xF8) == 0xF0) {
  167. c = ((c & 0x7) << 18) | ((readChar(index, s) & 0x3F) << 12);
  168. c |= (readChar(index, s) & 0x3F) << 6;
  169. c |= readChar(index, s) & 0x3F;
  170. }
  171. data.add(c);
  172. }
  173. Pointer p;
  174. p.offset = 0;
  175. p.array = asAllocate(&sc->arrays, sizeof(int32), data.getLength());
  176. printf("%d wsdfdg\n", data.getLength());
  177. SnuviArray* array = asGet(&sc->arrays, p.array);
  178. if(array == nullptr) {
  179. sError(sc, "cannot allocate string memory");
  180. p.offset = -1;
  181. p.array = -1;
  182. } else {
  183. memcpy(array->data, data.begin(), sizeof(int32) * data.getLength());
  184. }
  185. return p;
  186. }