1
1

parse.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "parse.h"
  2. #include <QObject>
  3. #include <QException>
  4. #include <QVector>
  5. int32_t process_param(const QString& param, bool& isSepecial)
  6. {
  7. QRegExp regMatchNumber("\\d*");
  8. isSepecial = true;
  9. if(param.compare("#1", Qt::CaseInsensitive) == 0)
  10. {
  11. return Local_1;
  12. }
  13. else if(param.compare("#2", Qt::CaseInsensitive) == 0)
  14. {
  15. return Local_2;
  16. }
  17. else if(param.compare("#3", Qt::CaseInsensitive) == 0)
  18. {
  19. return Local_3;
  20. }
  21. else if(param.compare("#4", Qt::CaseInsensitive) == 0)
  22. {
  23. return Local_4;
  24. }
  25. else if(param.compare("#5", Qt::CaseInsensitive) == 0)
  26. {
  27. return Local_5;
  28. }
  29. else if(param.compare("#6", Qt::CaseInsensitive) == 0)
  30. {
  31. return Local_6;
  32. }
  33. else if(param.compare("#7", Qt::CaseInsensitive) == 0)
  34. {
  35. return Local_7;
  36. }
  37. else if(param.compare("#8", Qt::CaseInsensitive) == 0)
  38. {
  39. return Local_8;
  40. }
  41. else if(param.compare("#9", Qt::CaseInsensitive) == 0)
  42. {
  43. return Local_9;
  44. }
  45. else if(param.compare("#10", Qt::CaseInsensitive) == 0)
  46. {
  47. return Local_10;
  48. }
  49. else if(param.compare("#11", Qt::CaseInsensitive) == 0)
  50. {
  51. return Local_11;
  52. }
  53. else if(param.compare("#12", Qt::CaseInsensitive) == 0)
  54. {
  55. return Local_12;
  56. }
  57. else if(param.compare("#13", Qt::CaseInsensitive) == 0)
  58. {
  59. return Local_13;
  60. }
  61. else if(param.compare("#14", Qt::CaseInsensitive) == 0)
  62. {
  63. return Local_14;
  64. }
  65. else if(param.compare("#15", Qt::CaseInsensitive) == 0)
  66. {
  67. return Local_15;
  68. }
  69. else if(param.compare("#16", Qt::CaseInsensitive) == 0)
  70. {
  71. return Local_16;
  72. }
  73. else if(param.compare("#17", Qt::CaseInsensitive) == 0)
  74. {
  75. return Local_17;
  76. }
  77. else if(param.compare("#18", Qt::CaseInsensitive) == 0)
  78. {
  79. return Local_18;
  80. }
  81. else if(param.compare("#19", Qt::CaseInsensitive) == 0)
  82. {
  83. return Local_19;
  84. }
  85. else if(param.compare("#20", Qt::CaseInsensitive) == 0)
  86. {
  87. return Local_20;
  88. }
  89. else if(param.compare("#Active", Qt::CaseInsensitive) == 0)
  90. {
  91. return LocalActive;
  92. }
  93. else if(param.compare("$Banks", Qt::CaseInsensitive) == 0)
  94. {
  95. return LocalBanks;
  96. }
  97. else if(param.compare("$InstrSet", Qt::CaseInsensitive) == 0)
  98. {
  99. return LocalInstrSet;
  100. }
  101. else if(param.compare("$Mobile", Qt::CaseInsensitive) == 0)
  102. {
  103. return LocalMobile;
  104. }
  105. else if(param.compare("$Age", Qt::CaseInsensitive) == 0)
  106. {
  107. return LocalAge;
  108. }
  109. else if(param.compare("$Tasks", Qt::CaseInsensitive) == 0)
  110. {
  111. return LocalTasks;
  112. }
  113. else if(param.compare("$Generation", Qt::CaseInsensitive) == 0)
  114. {
  115. return LocalGeneration;
  116. }
  117. else if(param.compare("$ID", Qt::CaseInsensitive) == 0)
  118. {
  119. return LocalId;
  120. }
  121. else if(param.compare("%Active", Qt::CaseInsensitive) == 0)
  122. {
  123. return RemoteActive;
  124. }
  125. else if(param.compare("%Banks", Qt::CaseInsensitive) == 0)
  126. {
  127. return RemoteBanks;
  128. }
  129. else if(param.compare("%InstrSet", Qt::CaseInsensitive) == 0)
  130. {
  131. return RemoteInstrSet;
  132. }
  133. else if(param.compare("%Mobile", Qt::CaseInsensitive) == 0)
  134. {
  135. return RemoteMobile;
  136. }
  137. else if(param.compare("%Age", Qt::CaseInsensitive) == 0)
  138. {
  139. return RemoteAge;
  140. }
  141. else if(param.compare("%Tasks", Qt::CaseInsensitive) == 0)
  142. {
  143. return RemoteTasks;
  144. }
  145. else if(param.compare("%Generation", Qt::CaseInsensitive) == 0)
  146. {
  147. return RemoteGeneration;
  148. }
  149. else if(param.compare("#Pub", Qt::CaseInsensitive) == 0)
  150. {
  151. return GlobalPub;
  152. }
  153. else if(param.compare("#Team", Qt::CaseInsensitive) == 0)
  154. {
  155. return GlobalTeam;
  156. }
  157. else if(param.compare("$Own", Qt::CaseInsensitive) == 0)
  158. {
  159. return GlobalOwn;
  160. }
  161. else if(param.compare("$Others", Qt::CaseInsensitive) == 0)
  162. {
  163. return GlobalOthers;
  164. }
  165. else if(param.compare("$Fields", Qt::CaseInsensitive) == 0)
  166. {
  167. return GlobalFields;
  168. }
  169. else if(param.compare("$Time", Qt::CaseInsensitive) == 0)
  170. {
  171. return GlobalTime;
  172. }
  173. else if(param.compare("$Timeout", Qt::CaseInsensitive) == 0)
  174. {
  175. return GlobalTimeout;
  176. }
  177. else if(regMatchNumber.exactMatch(param))
  178. {
  179. isSepecial = false;
  180. return param.toInt();
  181. }
  182. else
  183. {
  184. throw QException();
  185. }
  186. }
  187. Instruction process_0(const QString& cmd)
  188. {
  189. if(cmd.compare("MOVE", Qt::CaseInsensitive) == 0)
  190. {
  191. return {MOVE, N, 0, 0, 0};
  192. }
  193. else if(cmd.compare("DIE", Qt::CaseInsensitive) == 0)
  194. {
  195. return {DIE, N, 0, 0, 0};
  196. }
  197. else if(cmd.compare("BREAK", Qt::CaseInsensitive) == 0)
  198. {
  199. return {BREAK, N, 0, 0, 0};
  200. }
  201. else if(cmd.compare("RESUME", Qt::CaseInsensitive) == 0)
  202. {
  203. return {RESUME, N, 0, 0, 0};
  204. }
  205. else if(cmd.compare("SEIZE", Qt::CaseInsensitive) == 0)
  206. {
  207. return {SEIZE, N, 0, 0, 0};
  208. }
  209. else if(cmd.compare("QUIT", Qt::CaseInsensitive) == 0)
  210. {
  211. return {QUIT, N, 0, 0, 0};
  212. }
  213. throw QException();
  214. }
  215. Instruction process_1(const QString& cmd, const QString& a)
  216. {
  217. bool isSpecialA;
  218. auto vA = process_param(a, isSpecialA);
  219. Params params = isSpecialA ? V : L;
  220. if(cmd.compare("TURN", Qt::CaseInsensitive) == 0)
  221. {
  222. return {TURN, params, vA};
  223. }
  224. else if(cmd.compare("JUMP", Qt::CaseInsensitive) == 0)
  225. {
  226. return {JUMP, params, vA};
  227. }
  228. else if(cmd.compare("AJUMP", Qt::CaseInsensitive) == 0)
  229. {
  230. return {AJUMP, params, vA};
  231. }
  232. else if(cmd.compare("SCAN", Qt::CaseInsensitive) == 0)
  233. {
  234. return {SCAN, params, vA};
  235. }
  236. else if(cmd.compare("SLEEP", Qt::CaseInsensitive) == 0)
  237. {
  238. return {SLEEP, params, vA};
  239. }
  240. throw QException();
  241. }
  242. Instruction process_2(const QString& cmd, const QString& a, const QString& b)
  243. {
  244. bool isSpecialA;
  245. bool isSpecialB;
  246. auto vA = process_param(a, isSpecialA);
  247. auto vB = process_param(b, isSpecialB);
  248. Params params = isSpecialA ? ( isSpecialB ? VV : VL) : ( isSpecialB ? LV : LL);
  249. if(cmd.compare("TRANS", Qt::CaseInsensitive) == 0)
  250. {
  251. return {TRANS, params, vA, vB};
  252. }
  253. else if(cmd.compare("RTRANS", Qt::CaseInsensitive) == 0)
  254. {
  255. return {RTRANS, params, vA, vB};
  256. }
  257. else if(cmd.compare("BJUMP", Qt::CaseInsensitive) == 0)
  258. {
  259. return {BJUMP, params, vA, vB};
  260. }
  261. else if(cmd.compare("SET", Qt::CaseInsensitive) == 0)
  262. {
  263. return {SET, params, vA, vB};
  264. }
  265. else if(cmd.compare("ADD", Qt::CaseInsensitive) == 0)
  266. {
  267. return {ADD, params, vA, vB};
  268. }
  269. else if(cmd.compare("SUB", Qt::CaseInsensitive) == 0)
  270. {
  271. return {SUB, params, vA, vB};
  272. }
  273. else if(cmd.compare("MUL", Qt::CaseInsensitive) == 0)
  274. {
  275. return {MUL, params, vA, vB};
  276. }
  277. else if(cmd.compare("DIV", Qt::CaseInsensitive) == 0)
  278. {
  279. return {DIV, params, vA, vB};
  280. }
  281. else if(cmd.compare("MOD", Qt::CaseInsensitive) == 0)
  282. {
  283. return {MOD, params, vA, vB};
  284. }
  285. else if(cmd.compare("MIN", Qt::CaseInsensitive) == 0)
  286. {
  287. return {MIN, params, vA, vB};
  288. }
  289. else if(cmd.compare("MAX", Qt::CaseInsensitive) == 0)
  290. {
  291. return {MAX, params, vA, vB};
  292. }
  293. else if(cmd.compare("IF", Qt::CaseInsensitive) == 0)
  294. {
  295. return {IF, params, vA, vB};
  296. }
  297. else if(cmd.compare("IFN", Qt::CaseInsensitive) == 0)
  298. {
  299. return {IFN, params, vA, vB};
  300. }
  301. else if(cmd.compare("IFG", Qt::CaseInsensitive) == 0)
  302. {
  303. return {IFG, params, vA, vB};
  304. }
  305. else if(cmd.compare("IFL", Qt::CaseInsensitive) == 0)
  306. {
  307. return {IFL, params, vA, vB};
  308. }
  309. else if(cmd.compare("IFGE", Qt::CaseInsensitive) == 0)
  310. {
  311. return {IFGE, params, vA, vB};
  312. }
  313. else if(cmd.compare("IFLE", Qt::CaseInsensitive) == 0)
  314. {
  315. return {IFLE, params, vA, vB};
  316. }
  317. else if(cmd.compare("INIT", Qt::CaseInsensitive) == 0)
  318. {
  319. return {INIT, params, vA, vB};
  320. }
  321. throw QException();
  322. }
  323. Instruction process_3(const QString& cmd, const QString& a, const QString& b, const QString& c)
  324. {
  325. bool isSpecialA;
  326. bool isSpecialB;
  327. bool isSpecialC;
  328. auto vA = process_param(a, isSpecialA);
  329. auto vB = process_param(b, isSpecialB);
  330. auto vC = process_param(c, isSpecialC);
  331. Params params = isSpecialA ? ( isSpecialB ? ( isSpecialC ? VVV: VVL) : ( isSpecialC ? VLV: VLL)) : ( isSpecialB ? ( isSpecialC ? LVV : LVL) : ( isSpecialC ? LLV : LLL));
  332. if(cmd.compare("CREATE", Qt::CaseInsensitive) == 0)
  333. {
  334. return {CREATE, params, vA, vB, vC};
  335. }
  336. else if(cmd.compare("FARSCAN", Qt::CaseInsensitive) == 0)
  337. {
  338. return {FARSCAN, params, vA, vB, vC};
  339. }
  340. else if(cmd.compare("RANDOM", Qt::CaseInsensitive) == 0)
  341. {
  342. return {RANDOM, params, vA, vB, vC};
  343. }
  344. throw QException();
  345. }
  346. Instruction process_parts(const QStringList& parts)
  347. {
  348. if(parts.size() == 1)
  349. {
  350. return process_0(parts[0]);
  351. }
  352. else if(parts.size() == 2)
  353. {
  354. return process_1(parts[0], parts[1]);
  355. }
  356. else if(parts.size() == 3)
  357. {
  358. return process_2(parts[0], parts[1], parts[2]);
  359. }
  360. else if(parts.size() == 4)
  361. {
  362. return process_3(parts[0], parts[1], parts[2], parts[3]);
  363. }
  364. throw QException();
  365. }
  366. void process_line(QString line, QVector<QVector<Instruction>>& banks)
  367. {
  368. line = line.mid(0, line.indexOf("//"));
  369. if(line.isEmpty())
  370. {
  371. return;
  372. }
  373. if(line.startsWith("BANK ", Qt::CaseInsensitive))
  374. {
  375. banks.resize(banks.size() + 1);
  376. return;
  377. }
  378. auto parts = line.split(QRegExp("[\\s,]"), QString::SplitBehavior::SkipEmptyParts);
  379. banks.back() += process_parts(parts);
  380. }