[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

基于CLGO的COOL解释器发布

本帖最后由 happy886rr 于 2017-5-3 12:31 编辑

COOL解释器基于CLGO,但核心架构完全重写了一次,增加按键支持,改进了变量表识别机制,改进了语法体系,总代码量也突破1800行,跟CLGO语法和运行机制不再一样。此次发布的是COOL解释器1.0版。并附上一个测试:希尔波特催眠术。
百度网盘下载:https://pan.baidu.com/s/1i4ZswEL
图片均为外链


希尔波特催眠术脚本
  1. @echo off
  2. mode con cols=100 lines=30
  3. cool "%~f0"
  4. exit
  5. //主函数入口
  6. to main
  7. make :penw=3
  8. for  :a 5 7
  9. (
  10. make :penx=500 :peny=-500
  11. hilbert_curve 1000/(2^:a-1) :a 90
  12. )
  13. for  :a 6 3
  14. (
  15. make :penx=500 :peny=-500
  16. hilbert_curve 1000/(2^:a-1) :a 90
  17. )-1
  18. functions_img 1
  19. exit
  20. //希尔波特曲线
  21. to hilbert_curve :len :recursive_times :angle
  22. if :recursive_times=0
  23. (
  24. stop
  25. )
  26. if :recursive_times/3=1
  27. (
  28. make :penc=rand(15)+1
  29. )
  30. if or :penc=9 :penc=10
  31. (
  32. make :penc=7
  33. )
  34. lt :angle
  35. hilbert_curve :len :recursive_times-1 -:angle
  36. fd :len
  37. rt :angle
  38. hilbert_curve :len :recursive_times-1  :angle
  39. fd :len
  40. hilbert_curve :len :recursive_times-1  :angle
  41. rt :angle  
  42. fd :len  
  43. hilbert_curve :len :recursive_times-1 -:angle
  44. lt :angle
  45. //旋转体系
  46. to functions_img :add
  47. memuse
  48. for :a 1 36000
  49. (
  50. make :penx=0 :peny=0
  51. rote :a
  52. memsee
  53. ):add
  54. memout
复制代码
COOL解释器源码(仅支持gcc编译器):
  1. /*
  2. COOL SCRIPT, COPYRIGHT@2017~2019 BY HAPPY, VERSION 1.0
  3. COOL.EXE
  4. LINK: -L./ -lgdi32 -lgdiplus
  5. */
  6. #include <cmath>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <windows.h>
  12. #include <wincon.h>
  13. #include <io.h>
  14. #include <conio.h>
  15. #include <time.h>
  16. #include <gdiplus\gdiplus.h>
  17. //定义isINF和isNAN
  18. #ifndef Nan__
  19. #define Nan__
  20. #define NAN log(-1.0) //无效值NAN
  21. #define isNAN(x) ((x)!=(x))
  22. #define NINF log(0.0) //负无穷大
  23. #define INF -NINF //无穷大
  24. #define PINF -NINF //正无穷大
  25. #define isINF(x) (((x)==PINF)||((x)==NINF))
  26. #define isPINF(x) ((x)==PINF)
  27. #define isNINF(x) ((x)==NINF)
  28. #endif
  29. //声明函数
  30. extern "C" HWND WINAPI GetConsoleWindow();
  31. //使用GDI+
  32. using namespace Gdiplus;
  33. /***************定义宏变量***************/
  34. //堆栈尺寸
  35. #define STACK_SIZE         1024
  36. //循环体最大层数
  37. #define LOOP_LEVELS        64
  38. //变量名最大长度
  39. #define MAX_WORDS_SIZE     64
  40. //绘图系关键词数
  41. #define KEY_SIZE           23
  42. //循环系关键词数
  43. #define CYC_SIZE           5
  44. //逻辑系关键词
  45. #define LGC_SIZE           3
  46. //全局变量解释数
  47. #define GLOBAL_SIZE        18
  48. /***************全局类变量***************/
  49. //主窗区域
  50. RECT     winRECT= {0};
  51. LPRECT   lrRECT;
  52. double   base_X, base_Y;
  53. //主窗句柄
  54. HWND  hCMD;
  55. HWND  hWND2;
  56. HWND  hWND3;
  57. //当前行行号
  58. int   CUR_LINENUM;
  59. //当前行指针
  60. char* CUR_LINEPOINTER;
  61. //画布句柄
  62. HDC   hDC;
  63. HDC   hDC1;
  64. HDC   hDC2;
  65. HDC   hDC3;
  66. //内存变量
  67. char* tRAM;
  68. //缓存容器
  69. char  CacheTain[1024]= {0};
  70. //辅助容器
  71. char  Auxiliary[1024]= {0};
  72. //画线是否可见
  73. BOOL  penMODE=TRUE;
  74. //启用内存背景
  75. BOOL  memMODE=FALSE;
  76. /***************关键词类目***************/
  77. //运算符栈
  78. char   STACK1[STACK_SIZE]= {0};
  79. //逆波兰栈
  80. char   STACK2[STACK_SIZE]= {0};
  81. //浮点数栈
  82. double STACK3[STACK_SIZE]= {0};
  83. //数学函数关键词
  84. static const char* MATH_WORDS[]= {"TIME", "PI", "SQRT", "LG", "LN", "SIN", "COS", "TAN", "ARCSIN", "ARCCOS", "ARCTAN", "DEG", "ABS", "ROUND", "READ", "CEIL", "EXP", "SINH", "COSH", "TANH", "ARCSINH", "ARCCOSH", "ARCTANH", "RAND", "INT", "FACT", NULL};
  85. //绘图系关键词
  86. static const char* KEY_WORDS[]= {"FD", "BK", "LT", "RT", "PU", "PD", "PE", "CS", "PR", "HM", "STOP", "EXIT", "FILL", "TEXT", "WAIT", "ROTE", "MEMUSE", "MEMSEE", "MEMOUT", "MAKE", "SETXY", "OVAL", "RECT"};
  87. static const char  KEY_LLENS[]= {   2,    2,    2,    2,    2,    2,    2,    2,    2,    2,      4,      4,      4,      4,      4,      4,        4,        4,        4,      4,      5,       4,      4};
  88. //循环系关键词
  89. static const char* CYC_WORDS[]= {"IF", "REPEAT", "WHILE", "FOR", "ELIF"};
  90. static const char  CYC_LLENS[]= {   2,        6,       5,     3,      4};
  91. //逻辑系关键词
  92. static const char* LGC_WORDS[]= {"NOT", "AND", "OR"};
  93. static const char  LGC_LLENS[]= {   3,      3,    2};
  94. //定义解释器全局变量
  95. static const char* GLOBAL_WORDS[]= {":PENX", ":PENY", ":HEADING", ":PENB", ":PENW", ":PENC", ":PENS", ":PENA", ":PEN0", ":PEN1", ":PEN2", ":PEN3", ":PEN4", ":PEN5", ":PEN6", ":PEN7", ":PEN8", ":PEN9"};
  96. double  GLOBAL_VALUE[]= {      0,       0,          0,       0,       1,      15,       0,     255,       0,       0,       0,       0,       0,       0,       0,       0,       0,       0};
  97. /***************申明函数类***************/
  98. int     Identify_KeyWords(char* Line, int SENSITIVE_NUM, char** WORDS, const char* endstring);
  99. double  RevPolishCore(const char* expression, int PRO_NUM, char** WORDS, double* VALUE);
  100. /***************RGB 结构体***************/
  101. typedef struct
  102. {
  103. BYTE R;
  104. BYTE G;
  105. BYTE B;
  106. COLORREF VALUE;
  107. } STRGB;
  108. //十六色系数组
  109. STRGB LOGO_RGB[16]=
  110. {
  111. {  0,  0,  0,RGB(  0,  0,  0)}, //黑色
  112. {  0,  0,255,RGB(  0,  0,255)}, //蓝色
  113. {  0,255,  0,RGB(  0,255,  0)}, //绿色
  114. {  0,255,255,RGB(  0,255,255)}, //青色
  115. {255,  0,  0,RGB(255,  0,  0)}, //红色
  116. {238,130,238,RGB(238,130,238)}, //紫兰
  117. {165, 42, 42,RGB(165, 42, 42)}, //棕色
  118. {211,211,211,RGB(211,211,211)}, //浅灰
  119. {169,169,169,RGB(169,169,169)}, //深灰
  120. {173,216,230,RGB(173,216,230)}, //淡蓝
  121. {144,238,144,RGB(144,238,144)}, //浅绿
  122. {224,255,255,RGB(224,255,255)}, //淡青
  123. {248, 29, 56,RGB(248, 29, 56)}, //亮红
  124. {255,  0,255,RGB(255,  0,255)}, //洋紫
  125. {255,255,  0,RGB(255,255,  0)}, //黄色
  126. {255,255,255,RGB(255,255,255)}  //白色
  127. };
  128. /***************辅助函数群***************/
  129. //获取时间
  130. int getTIME(int i)
  131. {
  132. time_t timep;
  133. struct tm *p;
  134. time(&timep);
  135. p=localtime(&timep);
  136. switch(i)
  137. {
  138. case 0:
  139. return p->tm_year+1900;
  140. case 1:
  141. return p->tm_mon+1;
  142. case 2:
  143. return p->tm_mday;
  144. case 3:
  145. return p->tm_hour;
  146. case 4:
  147. return p->tm_min;
  148. case 5:
  149. return p->tm_sec;
  150. default:
  151. break;
  152. }
  153. }
  154. //按键获取
  155. int getKEY(int N,int T)
  156. {
  157. int i,KEY_V,start=clock();
  158. do
  159. {
  160. if(_kbhit())
  161. {
  162. KEY_V=(int)(_getch());
  163. if(KEY_V<97)
  164. {
  165. KEY_V+=32;
  166. }
  167. return KEY_V;
  168. }
  169. for(i=0; i<=N; i++);
  170. }
  171. while((clock()-start)<T);
  172. return -1;
  173. }
  174. //转码函数
  175. static WCHAR  Tainer[1024]= {0};
  176. WCHAR* AnsiToWCHAR(const CHAR* str)
  177. {
  178. if(!str)
  179. {
  180. return NULL;
  181. }
  182. int wLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, NULL, 0);
  183. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, Tainer, wLen);
  184. Tainer[wLen]='\0';
  185. return Tainer;
  186. }
  187. //阶乘函数
  188. long long fact(long long n)
  189. {
  190. return (n<2) ?1 :n*(fact(n-1));
  191. }
  192. //测空字符
  193. inline BOOL isEND(const char c,const char* endstring)
  194. {
  195. if(c=='\0')
  196. {
  197. return TRUE;
  198. }
  199. while(*endstring!='\0')
  200. {
  201. if(c==*(endstring++))
  202. {
  203. return TRUE;
  204. }
  205. }
  206. return FALSE;
  207. }
  208. //过滤行TAB缩进或前空格
  209. char* passNULL(char* Str)
  210. {
  211. if(Str!=NULL)
  212. {
  213. while(*Str=='\t' ||*Str==' ')
  214. {
  215. Str++;
  216. }
  217. }
  218. return Str;
  219. }
  220. //错误反馈
  221. inline int errorEXIT(char* errorTYPE)
  222. {
  223. fprintf(stderr, "[%d]:%s    '%s\n", CUR_LINENUM, errorTYPE, passNULL(CUR_LINEPOINTER));
  224. exit(1);
  225. }
  226. //识别关键字
  227. BOOL isKEY(char* Line, char* key, const char* endstring)
  228. {
  229. char *op=Line, *kp=key;
  230. while(*kp!='\0')
  231. {
  232. if((('a'<= *op && *op<='z')?*op-32:*op) != (('a'<= *kp && *kp<='z')?*kp-32:*kp))
  233. {
  234. break;
  235. }
  236. op++;
  237. kp++;
  238. }
  239. if(
  240.     *kp=='\0' &&
  241.     isEND(*op, endstring)
  242. )
  243. {
  244. return TRUE;
  245. }
  246. return FALSE;
  247. }
  248. //切分变量名
  249. int ARGS(char* Str, char** WORDS, int PRO_NUM, int LineNUM, char* Line)
  250. {
  251. if(PRO_NUM==0)
  252. {
  253. return 0;
  254. }
  255. int i=0;
  256. char* lp=Str;
  257. char* tp;
  258. strtok(lp, " \t");
  259. WORDS[0]=(char*)calloc(MAX_WORDS_SIZE, sizeof(char));
  260. strcpy(WORDS[0], ":\n");
  261. while((++i)<PRO_NUM)
  262. {
  263. if((tp=strtok(NULL, " \t"))==NULL)
  264. {
  265. break;
  266. }
  267. WORDS[i]=(char*)calloc(MAX_WORDS_SIZE, sizeof(char));
  268. strcpy(WORDS[i], tp);
  269. }
  270. //参数不匹配时则退出
  271. if(i<PRO_NUM)
  272. {
  273. errorEXIT("Missing parameter");
  274. }
  275. return i;
  276. }
  277. //切分参数
  278. int ARGP(char* Str, double* WVALUE, int PRO_NUM, char** PRO_WORDS, double* PRO_VALUE)
  279. {
  280. int i=0;
  281. char* tp;
  282. strtok(Str, " \t");
  283. while((tp=strtok(NULL, " \t"))!=NULL && *tp!='\0')
  284. {
  285. WVALUE[++i]=RevPolishCore(tp, PRO_NUM, PRO_WORDS, PRO_VALUE);
  286. }
  287. return i+1;
  288. }
  289. //关键词解析函数
  290. int Identify_KeyWords(char* Line, int SENSITIVE_NUM, char** WORDS, const char* endstring)
  291. {
  292. int SN;
  293. for(SN=0; SN<SENSITIVE_NUM; SN++)
  294. {
  295. char *op=Line, *kp=WORDS[SN];
  296. while(*kp!='\0')
  297. {
  298. if(
  299.     (('a'<= *op && *op<='z')?*op-32:*op) != (('a'<= *kp && *kp<='z')?*kp-32:*kp)
  300. )
  301. {
  302. break;
  303. }
  304. op++;
  305. kp++;
  306. }
  307. if(
  308.     *kp=='\0' &&
  309.     isEND(*op, endstring)
  310. )
  311. {
  312. return SN;
  313. }
  314. }
  315. return -1;
  316. }
  317. /***************计算函数群***************/
  318. //逆波兰核心
  319. double RevPolishCore(const char* expression, int PRO_NUM, char** WORDS, double* VALUE)
  320. {
  321. STACK3[0]=0;
  322. int si, brackets=0;
  323. char   *op=(char*)expression, *S1=STACK1, *S2=STACK2, **key, *cp, *kp;
  324. double *S3=STACK3, di, ni;
  325. //生成逆波兰
  326. while(*op!='\0' && *op!=' ' && *op!='\t')
  327. {
  328. switch(*op)
  329. {
  330. case ' ' :
  331. case '\t':
  332. //过滤空字符
  333. op++;
  334. continue;
  335. case 'a':
  336. case 'b':
  337. case 'c':
  338. case 'd':
  339. case 'e':
  340. case 'f':
  341. case 'g':
  342. case 'h':
  343. case 'i':
  344. case 'j':
  345. case 'k':
  346. case 'l':
  347. case 'm':
  348. case 'n':
  349. case 'o':
  350. case 'p':
  351. case 'q':
  352. case 'r':
  353. case 's':
  354. case 't':
  355. case 'u':
  356. case 'v':
  357. case 'w':
  358. case 'x':
  359. case 'y':
  360. case 'z':
  361. case 'A':
  362. case 'B':
  363. case 'C':
  364. case 'D':
  365. case 'E':
  366. case 'F':
  367. case 'G':
  368. case 'H':
  369. case 'I':
  370. case 'J':
  371. case 'K':
  372. case 'L':
  373. case 'M':
  374. case 'N':
  375. case 'O':
  376. case 'P':
  377. case 'Q':
  378. case 'R':
  379. case 'S':
  380. case 'T':
  381. case 'U':
  382. case 'V':
  383. case 'W':
  384. case 'X':
  385. case 'Y':
  386. case 'Z':
  387. //识别数学函数关键词
  388. key=(char**)MATH_WORDS;
  389. while(*key !=NULL)
  390. {
  391. cp=op, kp=*key;
  392. //比对关键词字母
  393. while((*cp==*kp||*cp==*kp+32) && *kp!='\0')
  394. {
  395. cp++, kp++;
  396. }
  397. //验证关键词结尾
  398. if( ((*cp<'A')||('Z'<*cp && *cp<'a')||(*cp>'z')) && (*kp=='\0') )
  399. {
  400. op=cp;
  401. break;
  402. }
  403. key++;
  404. }
  405. //构建伪双目
  406. if(*key !=NULL)
  407. {
  408. *(S2++)='.';
  409. *(S2++)=' ';
  410. //伪双目入栈
  411. while( ('A'<=(*S1) && (*S1)<='Z') )
  412. {
  413. *(S2++)=*(S1--);
  414. }
  415. *(++S1)=key-(char**)MATH_WORDS+65;
  416. continue;
  417. }
  418. else
  419. {
  420. //无法识别的数学函数
  421. errorEXIT("Unrecognized math function");
  422. }
  423. break;
  424. case '(':
  425. brackets++;
  426. *(++S1)=*op;
  427. if(*(op+1)=='-' || *(op+1)=='+')
  428. {
  429. *(S2++)='0', *(S2++)=' ';
  430. }
  431. break;
  432. case ')':
  433. brackets--;
  434. while(*S1!='(')
  435. {
  436. *(S2++)=*(S1--);
  437. }
  438. //舍弃'('
  439. S1--;
  440. break;
  441. case '+':
  442. case '-':
  443. while(S1!=STACK1 && *S1!='(')
  444. {
  445. *(S2++)=*(S1--);
  446. }
  447. *(++S1)=*op;
  448. break;
  449. case '^':
  450. //指数符
  451. while( ('A'<=(*S1) && (*S1)<='Z') )
  452. {
  453. *(S2++)=*(S1--);
  454. }
  455. *(++S1)=*op;
  456. break;
  457. case '%':
  458. case '*':
  459. case '/':
  460. while(('A'<=(*S1) && (*S1)<='Z') ||*S1=='%' ||*S1=='*' ||*S1=='/' ||*S1=='^')
  461. {
  462. *(S2++)=*(S1--);
  463. }
  464. *(++S1)=*op;
  465. break;
  466. case ':':
  467. //识别变量 :VAR
  468. for(si=PRO_NUM-1; si>=0-GLOBAL_SIZE; si--)
  469. {
  470. cp=op, kp=(si<0)?(char *)GLOBAL_WORDS[-1-si]:WORDS[si];
  471. //比对变量字母
  472. while((*cp==*kp||*cp==*kp+32) && *kp!='\0')
  473. {
  474. cp++, kp++;
  475. }
  476. //验证变量结尾
  477. if(!(('a'<=*cp && *cp<='z')||('A'<=*cp && *cp<='Z')||('0'<=*cp && *cp<='9')||(*cp=='_' || *cp=='#')) && (*kp=='\0'))
  478. {
  479. op=cp-1;
  480. break;
  481. }
  482. }
  483. //判断变量是否存在
  484. if(si>=0-GLOBAL_SIZE)
  485. {
  486. //变量 :VAR入栈
  487. *(S2++)=':', *(S2++)=si, *(S2++)=' ';
  488. }
  489. else
  490. {
  491. //变量 :VAR无效
  492. errorEXIT("Variable  not exist");
  493. }
  494. break;
  495. default :
  496. if((*op<'0' || *op>'9') && (*op!='.'))
  497. {
  498. //无法识别的数字
  499. errorEXIT("Unrecognized number");
  500. }
  501. //浮点数入栈
  502. while(('0'<=*op && *op<='9') ||*op=='.')
  503. {
  504. *(S2++)=*(op++);
  505. }
  506. op--;
  507. *(S2++)=' ';
  508. break;
  509. }
  510. op++;
  511. }
  512. //验证括号是否闭合
  513. if(brackets)
  514. {
  515. errorEXIT("The brackets '(' or ')' are not closed");
  516. }
  517. //收尾逆波兰
  518. while(S1 !=STACK1)
  519. {
  520. *(S2++)=*(S1--);
  521. }
  522. *S2=' ';
  523. //计算逆波兰
  524. op=STACK2;
  525. while(*op!=' ')
  526. {
  527. switch(*op)
  528. {
  529. case 'A':
  530. if(*S3 <0 || *S3 >5)
  531. {
  532. //time(n)函数需要一个整形自变量介于0到5
  533. errorEXIT("The 'time(n)' requires 'n' between 0 to 5");
  534. }
  535. *(S3-1)=getTIME((int)*S3);
  536. S3--;
  537. break;
  538. case 'B':
  539. *S3=3.1415926535897932;
  540. break;
  541. case 'C':
  542. if(*S3 <0)
  543. {
  544. //负数没有平方根
  545. errorEXIT("Negative numbers have no square root");
  546. }
  547. *(S3-1)=sqrt(*S3);
  548. S3--;
  549. break;
  550. case 'D':
  551. if(*S3 <0)
  552. {
  553. //负数没有对数
  554. errorEXIT("Negative numbers are not logarithmic");
  555. }
  556. *(S3-1)=log10(*S3);
  557. S3--;
  558. break;
  559. case 'E':
  560. if(*S3 <0)
  561. {
  562. //负数没有自然对数
  563. errorEXIT("Negative numbers have no natural logarithms");
  564. }
  565. *(S3-1)=log(*S3);
  566. S3--;
  567. break;
  568. case 'F':
  569. *(S3-1)=sin(*S3);
  570. S3--;
  571. break;
  572. case 'G':
  573. *(S3-1)=cos(*S3);
  574. S3--;
  575. break;
  576. case 'H':
  577. if(*S3==3.1415926535897932/2)
  578. {
  579. //π/2没有正切值
  580. errorEXIT("The pi/2 has no tangent");
  581. }
  582. *(S3-1)=tan(*S3);
  583. S3--;
  584. break;
  585. case 'I':
  586. *(S3-1)=asin(*S3);
  587. S3--;
  588. break;
  589. case 'J':
  590. *(S3-1)=acos(*S3);
  591. S3--;
  592. break;
  593. case 'K':
  594. *(S3-1)=atan(*S3);
  595. S3--;
  596. break;
  597. case 'L':
  598. *(S3-1)=(*S3)*3.1415926535897932/180.0;
  599. S3--;
  600. break;
  601. case 'M':
  602. *(S3-1)=fabs(*S3);
  603. S3--;
  604. break;
  605. case 'N':
  606. *(S3-1)=(*S3<=0)?(int)(*S3-0.5):(int)(*S3+0.5);
  607. S3--;
  608. break;
  609. case 'O':
  610. *(S3-1)=getKEY(50, (int)fabs(*S3));
  611. S3--;
  612. break;
  613. case 'P':
  614. *(S3-1)=ceil(*S3);
  615. S3--;
  616. break;
  617. case 'Q':
  618. *(S3-1)=exp(*S3);
  619. S3--;
  620. break;
  621. case 'R':
  622. *(S3-1)=sinh(*S3);
  623. S3--;
  624. break;
  625. case 'S':
  626. *(S3-1)=cosh(*S3);
  627. S3--;
  628. break;
  629. case 'T':
  630. *(S3-1)=tanh(*S3);
  631. S3--;
  632. break;
  633. case 'U':
  634. *(S3-1)=log(*S3+sqrt((*S3)*(*S3)+1));
  635. S3--;
  636. break;
  637. case 'V':
  638. *(S3-1)=log(*S3+sqrt((*S3)*(*S3)-1));
  639. S3--;
  640. break;
  641. case 'W':
  642. *(S3-1)=log((1+(*S3))/(1-(*S3)))/2;
  643. S3--;
  644. break;
  645. case 'X':
  646. //随机数生成器
  647. if(*S3 <0)
  648. {
  649. //负数不能作为随机数的上限
  650. errorEXIT("A negative number can not be used as a random upper bound");
  651. }
  652. else if(*S3 <2)
  653. {
  654. //小于2的随机数为0~1之间的小数
  655. *(S3-1)=rand() % 1024 /1024.0;
  656. }
  657. else
  658. {
  659. *(S3-1)=rand() % (int)(*S3);
  660. }
  661. S3--;
  662. break;
  663. case 'Y':
  664. *(S3-1)=(int)(*S3);
  665. S3--;
  666. break;
  667. case 'Z':
  668. if(*S3 <0)
  669. {
  670. //负数没有阶乘
  671. errorEXIT("Negative numbers have no factorial");
  672. }
  673. *(S3-1)=fact((*S3));
  674. S3--;
  675. break;
  676. case '+':
  677. *(S3-1)+=*S3;
  678. S3--;
  679. break;
  680. case '-':
  681. *(S3-1)-=*S3;
  682. S3--;
  683. break;
  684. case '*':
  685. *(S3-1)*=*S3;
  686. S3--;
  687. break;
  688. case '%':
  689. case '/':
  690. if(*S3 !=0)
  691. {
  692. if(*op=='%')
  693. {
  694. //取余数
  695. *(S3-1)=(int)*(S3-1) % (int)*S3;
  696. }
  697. else
  698. {
  699. *(S3-1)/=*S3;
  700. }
  701. }
  702. else
  703. {
  704. //除数不能为零
  705. errorEXIT("Divisor is zero error");
  706. }
  707. S3--;
  708. break;
  709. case '^':
  710. if(*(S3-1)==0 && *S3<0)
  711. {
  712. //除数不能为零
  713. errorEXIT("Function pow's divisor is zero error");
  714. }
  715. *(S3-1)=pow(*(S3-1), *S3);
  716. S3--;
  717. break;
  718. case ':':
  719. op++;
  720. *(++S3)=(*op<0)?GLOBAL_VALUE[-1-(*op)]:VALUE[*op];
  721. op++;
  722. break;
  723. default :
  724. //字符串转浮点
  725. di=0, ni=1;
  726. while('0'<=*op && *op<='9')
  727. {
  728. di=10*di+(*op)-'0';
  729. op++;
  730. }
  731. if(*op=='.')
  732. {
  733. op++;
  734. while('0'<=*op && *op<='9')
  735. {
  736. di=10*di+(*op)-'0';
  737. op++, ni*=10;
  738. }
  739. }
  740. *(++S3)=di/ni;
  741. break;
  742. }
  743. op++;
  744. }
  745. if(isINF(*S3)||isNAN(*S3))
  746. {
  747. errorEXIT("Overflow or illegal operation is calculated");
  748. }
  749. //返回计算结果
  750. return *S3;
  751. }
  752. //赋值函数
  753. BOOL AssignMent(const char* exp, int PRO_NUM, char** WORDS, double* VALUE)
  754. {
  755. char *op=(char*)exp, *ep=op, *cp, *kp;
  756. int si, i;
  757. //寻找等号
  758. while(!isEND(*(ep++), "=")) {;}
  759. //查找变量
  760. if(op[0]==':')
  761. {
  762. if(op[1]==':')
  763. {
  764. //循环中的瞬时变量::VAR不可赋值
  765. errorEXIT("The loop variables '::' are not assignable");
  766. }
  767. //识别变量 :VAR
  768. for(si=PRO_NUM-1; si>=0-GLOBAL_SIZE; si--)
  769. {
  770. cp=op, kp=(si<0)?(char *)GLOBAL_WORDS[-1-si]:WORDS[si];
  771. //比对变量字母
  772. while((*cp==*kp||*cp==*kp+32) && *kp!='\0')
  773. {
  774. cp++, kp++;
  775. }
  776. //验证变量结尾
  777. if( ((*cp==' ')||(*cp=='\t')||(*cp=='=')) && (*kp=='\0') )
  778. {
  779. op=cp-1;
  780. break;
  781. }
  782. }
  783. //判断变量是否存在
  784. if(si>=0-GLOBAL_SIZE)
  785. {
  786. //笔属性变量赋值
  787. if(si<0)
  788. {
  789. i=-1-si;
  790. GLOBAL_VALUE[i]=RevPolishCore(ep, PRO_NUM, WORDS, VALUE);
  791. STRGB* bgRGB;
  792. HBRUSH m_brush;
  793. switch(i)
  794. {
  795. case 3:
  796. if(GLOBAL_VALUE[i]<0 ||GLOBAL_VALUE[i]>15)
  797. {
  798. //:PENB变量代表背景色,取值只能在0~15之间
  799. errorEXIT("The ':PENB' represents the background color, and only be between 0 to 15");
  800. }
  801. bgRGB=&LOGO_RGB[(int)GLOBAL_VALUE[i]];
  802. //创建画刷
  803. m_brush=CreateSolidBrush(bgRGB->VALUE);
  804. //填充背景
  805. FillRect(hDC1, lrRECT, m_brush);
  806. //删除画刷
  807. DeleteObject(m_brush);
  808. break;
  809. case 4:
  810. if(GLOBAL_VALUE[i]<0 ||GLOBAL_VALUE[i]>99)
  811. {
  812. //:PENW变量代表笔粗细,取值只能在0~99之间
  813. errorEXIT("The ':PENW' represents the width of the pen, and only be between 0 to 99");
  814. }
  815. break;
  816. case 5:
  817. if(GLOBAL_VALUE[i]<0 ||GLOBAL_VALUE[i]>15)
  818. {
  819. //:PENC变量代表笔颜色,取值只能在0~15之间
  820. errorEXIT("The ':PENC' represents the color of the pen, and only be between 0 to 15");
  821. }
  822. break;
  823. case 6:
  824. if(GLOBAL_VALUE[i]<0 ||GLOBAL_VALUE[i]>5)
  825. {
  826. //:PENS变量代表笔光滑,取值只能在0~5之间
  827. errorEXIT("The ':PENS' represents the smooth of the pen, and only be between 0 to 5");
  828. }
  829. break;
  830. case 7:
  831. if(GLOBAL_VALUE[i]<0 ||GLOBAL_VALUE[i]>255)
  832. {
  833. //:PENA变量代表笔透明,取值只能在0~255之间
  834. errorEXIT("The ':PENA' represents the alpha of the pen, and only be between 0 to 255");
  835. }
  836. break;
  837. default:
  838. break;
  839. }
  840. }
  841. else
  842. {
  843. //普通变量赋值
  844. VALUE[si]=RevPolishCore(ep, PRO_NUM, WORDS, VALUE);
  845. }
  846. }
  847. else
  848. {
  849. //无效变量
  850. errorEXIT("Variable  not exist");
  851. }
  852. }
  853. return TRUE;
  854. }
  855. //批量赋值
  856. BOOL MoreAssignMent(const char* Line, int PRO_NUM, char** WORDS, double* VALUE)
  857. {
  858. if(Line[0] !=':')
  859. {
  860. errorEXIT("Variable  not exist");
  861. }
  862. char *gp=(char*)Line;
  863. while(isEND(*gp, ":"))
  864. {
  865. AssignMent(gp, PRO_NUM, WORDS, VALUE);
  866. gp++;
  867. while(!isEND(*gp, " \t\0"))
  868. {
  869. gp++;
  870. }
  871. while(!isEND(*gp, ":\0"))
  872. {
  873. gp++;
  874. }
  875. if(*gp=='\0')
  876. {
  877. return TRUE;
  878. }
  879. }
  880. return TRUE;
  881. }
  882. //测FOR循环
  883. inline BOOL isForTrue(const char* Line, int PRO_NUM, char** WORDS, double* VALUE, int CYC)
  884. {
  885. if(CYC ==0)
  886. {
  887. return TRUE;
  888. }
  889. char *pCHA1, *pCHA2, *pCHA3;
  890. pCHA1=strtok((char*)Line, " \t");
  891. pCHA2=strtok(       NULL, " \t");
  892. pCHA3=strtok(       NULL, " \t");
  893. if(pCHA3 !=NULL)
  894. {
  895. int i=Identify_KeyWords(pCHA1, PRO_NUM, WORDS, " \t");
  896. if(i==-1)
  897. {
  898. errorEXIT("Variable  not exist");
  899. }
  900. int staNUM=RevPolishCore(pCHA2, PRO_NUM, WORDS, VALUE);
  901. int endNUM=RevPolishCore(pCHA3, PRO_NUM, WORDS, VALUE);
  902. int var   =VALUE[i];
  903. if(staNUM-endNUM<0)
  904. {
  905. return (var<=endNUM)?TRUE:FALSE;
  906. }
  907. return (var>=endNUM)?TRUE:FALSE;
  908. }
  909. else
  910. {
  911. errorEXIT("Needs more parameters");
  912. }
  913. }
  914. //真值条件判断
  915. BOOL JudgMent(const char* exp, int PRO_NUM, char** WORDS, double* VALUE)
  916. {
  917. char* op=CacheTain;
  918. char* cp=(char*)exp;
  919. //过滤行TAB缩进或空格
  920. while(*cp=='\t'|| *cp==' ')
  921. {
  922. cp++;
  923. }
  924. if(*cp =='\0')
  925. {
  926. return TRUE;
  927. }
  928. char* Line_Cache=op;
  929. int   i=-1, flag=0;
  930. while(!isEND(*cp, " \t\0"))
  931. {
  932. switch(*cp)
  933. {
  934. case '!':
  935. flag+=8;
  936. break;
  937. case '>':
  938. if(flag==0||flag==8)
  939. {
  940. *(op++)='-', *(op++)='(';
  941. }
  942. flag+=4;
  943. break;
  944. case '<':
  945. if(flag==0||flag==8)
  946. {
  947. *(op++)='-', *(op++)='(';
  948. }
  949. flag+=2;
  950. break;
  951. case '=':
  952. if(flag==0||flag==8)
  953. {
  954. *(op++)='-', *(op++)='(';
  955. }
  956. flag+=1;
  957. break;
  958. default:
  959. *(op++)=*(cp++);
  960. continue;
  961. }
  962. cp++;
  963. }
  964. //闭合括号
  965. *(op++)=(flag!=8)?')':'\0';
  966. *op='\0' ;
  967. float m=RevPolishCore(Line_Cache, PRO_NUM, WORDS, VALUE);
  968. switch(flag)
  969. {
  970. case 8:
  971. //逻辑非
  972. return (m==0)?TRUE:FALSE;
  973. case 4:
  974. return (m >0)?TRUE:FALSE;
  975. break;
  976. case 2:
  977. return (m <0)?TRUE:FALSE;
  978. break;
  979. case 1:
  980. return (m==0)?TRUE:FALSE;
  981. break;
  982. case 3:
  983. case 12:
  984. return (m<=0)?TRUE:FALSE;
  985. break;
  986. case 5:
  987. case 10:
  988. return (m>=0)?TRUE:FALSE;
  989. break;
  990. case 6:
  991. case 9:
  992. return (m!=0)?TRUE:FALSE;
  993. break;
  994. default:
  995. errorEXIT("Wrong logic");
  996. }
  997. }
  998. //批量真值判断
  999. BOOL MoreJudgMent(const char* Line, int PRO_NUM, char** WORDS, double* VALUE)
  1000. {
  1001. char *gp=(char*)Line;
  1002. int li=Identify_KeyWords(gp, LGC_SIZE, (char**)LGC_WORDS, " \t");
  1003. if(li !=-1)
  1004. {
  1005. gp+=LGC_LLENS[li];
  1006. }
  1007. do
  1008. {
  1009. while(isEND(*gp, " \t"))
  1010. {
  1011. gp++;
  1012. }
  1013. if(JudgMent(gp, PRO_NUM, WORDS, VALUE))
  1014. {
  1015. switch(li)
  1016. {
  1017. case 0:
  1018. return FALSE;
  1019. case 1:
  1020. break;
  1021. case 2:
  1022. return TRUE;
  1023. default:
  1024. return TRUE;
  1025. }
  1026. }
  1027. else
  1028. {
  1029. switch(li)
  1030. {
  1031. case 0:
  1032. break;
  1033. case 1:
  1034. return FALSE;
  1035. case 2:
  1036. break;
  1037. default:
  1038. return FALSE;
  1039. }
  1040. }
  1041. while(!isEND(*gp, " \t\0"))
  1042. {
  1043. gp++;
  1044. }
  1045. if(*gp=='\0')
  1046. {
  1047. switch(li)
  1048. {
  1049. case 0:
  1050. return TRUE;
  1051. case 1:
  1052. return TRUE;
  1053. case 2:
  1054. return FALSE;
  1055. }
  1056. }
  1057. while(isEND(*gp, " \t"))
  1058. {
  1059. gp++;
  1060. }
  1061. }
  1062. while(*gp !='\0');
  1063. }
  1064. //PR输出函数
  1065. char* SuperPrintf(const char* pline, int PRO_NUM, char** WORDS, double* VALUE)
  1066. {
  1067. char* op=(char*)pline;
  1068. char* cp;
  1069. char* sp=Auxiliary;
  1070. while(*op !='\0')
  1071. {
  1072. switch(*op)
  1073. {
  1074. case '\\':
  1075. switch(*(++op))
  1076. {
  1077. case 'b':
  1078. case 'B':
  1079. *(sp++)='\b';
  1080. break;
  1081. case 'e':
  1082. case 'E':
  1083. *(sp++)='\0';
  1084. break;
  1085. case 'r':
  1086. case 'R':
  1087. *(sp++)='\r';
  1088. break;
  1089. case 'n':
  1090. case 'N':
  1091. *(sp++)='\n';
  1092. break;
  1093. case 't':
  1094. case 'T':
  1095. *(sp++)='\t';
  1096. break;
  1097. case '{':
  1098. case '}':
  1099. *(sp++)=*op;
  1100. break;
  1101. case '\\':
  1102. *(sp++)='\\';
  1103. break;
  1104. default:
  1105. errorEXIT("Uninterpreted characters");
  1106. }
  1107. break;
  1108. case '{':
  1109. op++, cp=CacheTain;
  1110. while(*op!='\0' && *op!='}')
  1111. {
  1112. *(cp++)=*(op++);
  1113. }
  1114. if(*op=='\0')
  1115. {
  1116. errorEXIT("Missing '}'");
  1117. }
  1118. *cp='\0';
  1119. sprintf(sp, "%.12g", RevPolishCore(CacheTain, PRO_NUM, WORDS, VALUE));
  1120. while(*(sp++)!='\0') {;}
  1121. sp--;
  1122. break;
  1123. default:
  1124. *(sp++)=*(op);
  1125. break;
  1126. }
  1127. op++;
  1128. }
  1129. *(sp++)='\n', *(sp++)='\0';
  1130. return Auxiliary;
  1131. }
  1132. /***************绘图函数群***************/
  1133. //隐藏光标
  1134. BOOL SizeCursor(int size)
  1135. {
  1136. CONSOLE_CURSOR_INFO cursor_info = {(DWORD)((size==0)?25:size), (size==0)?FALSE:TRUE};
  1137. return SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  1138. }
  1139. //平滑画线函数
  1140. void drawSMOOTHLINE(HDC hDC, float penWith, SmoothingMode mode, BYTE sharpMODE, BYTE alpha, STRGB* penRGB, float x1, float y1, float x2, float y2, float angel)
  1141. {
  1142. Graphics gp(hDC);
  1143. gp.SetSmoothingMode(mode);
  1144. Pen pen(Color(alpha,penRGB->R,penRGB->G,penRGB->B), penWith);
  1145. if(angel !=0)
  1146. {
  1147. Matrix matrix;
  1148. matrix.RotateAt(angel, PointF(x1, y1)); // 旋转angel度
  1149. gp.SetTransform(&matrix);
  1150. }
  1151. switch(sharpMODE)
  1152. {
  1153. case 0:
  1154. gp.DrawLine(&pen, PointF(x1, y1), PointF(x2, y2));
  1155. break;
  1156. case 1:
  1157. gp.DrawEllipse(&pen, RectF(x1-x2,y1-y2,x2*2,y2*2));
  1158. break;
  1159. case 2:
  1160. gp.DrawRectangle(&pen, RectF(x1-x2,y1-y2,x2*2,y2*2));
  1161. break;
  1162. }
  1163. DeleteObject(&pen);
  1164. }
  1165. //画线函数,实现FD
  1166. BOOL drawLINE(HDC hDC, float d)
  1167. {
  1168. float x1=GLOBAL_VALUE[0], y1=GLOBAL_VALUE[1];
  1169. GLOBAL_VALUE[0]+=d * sin(GLOBAL_VALUE[2]*0.017453292519943);
  1170. GLOBAL_VALUE[1]+=d * cos(GLOBAL_VALUE[2]*0.017453292519943);
  1171. //当为PU抬笔时,不做绘图
  1172. if(!penMODE)
  1173. {
  1174. return FALSE;
  1175. }
  1176. drawSMOOTHLINE(hDC, (float)GLOBAL_VALUE[4], (SmoothingMode)(int)GLOBAL_VALUE[6], 0, (BYTE)GLOBAL_VALUE[7], &LOGO_RGB[(int)GLOBAL_VALUE[5]], base_X+x1, base_Y-y1, base_X+GLOBAL_VALUE[0], base_Y-GLOBAL_VALUE[1], 0);
  1177. return TRUE;
  1178. }
  1179. //退步函数,实现BK
  1180. void backWARD(float d)
  1181. {
  1182. GLOBAL_VALUE[0]-=d * sin(GLOBAL_VALUE[2]*0.017453292519943);
  1183. GLOBAL_VALUE[1]-=d * cos(GLOBAL_VALUE[2]*0.017453292519943);
  1184. }
  1185. //图形旋转
  1186. int RotateDC(HDC hDC, double angle)
  1187. {
  1188. if(angle==0)
  1189. {
  1190. return 1;
  1191. }
  1192. XFORM Matrix;
  1193. double rad =-angle * 0.017453292519943, rtmp1=base_X+GLOBAL_VALUE[0], rtmp2=base_Y-GLOBAL_VALUE[1];
  1194. Matrix.eM11 = (float)cos(rad);
  1195. Matrix.eM12 = (float)sin(rad);
  1196. Matrix.eM21 =-(float)sin(rad);
  1197. Matrix.eM22 = (float)cos(rad);
  1198. Matrix.eDx  = (float)(rtmp1-cos(rad)*rtmp1+sin(rad)*rtmp2);
  1199. Matrix.eDy  = (float)(rtmp2-cos(rad)*rtmp2-sin(rad)*rtmp1);
  1200. SetGraphicsMode(hDC, GM_ADVANCED);
  1201. SetWorldTransform(hDC, &Matrix);
  1202. return 0;
  1203. }
  1204. //文字绘制
  1205. void drawTEXT(HDC hDC, int fontSIZE, int fontSTYLE, int fontCOLOR, char* fontNAME, char* pSTRING)
  1206. {
  1207. Graphics graphicsp(hDC), *graph=&graphicsp;
  1208. FontFamily fontFAMILY(AnsiToWCHAR(fontNAME));
  1209. Font fontp(&fontFAMILY, fontSIZE, (FontStyle)fontSTYLE, UnitPixel);
  1210. STRGB* txRGB=&LOGO_RGB[fontCOLOR];
  1211. SolidBrush brushp(Color((int)GLOBAL_VALUE[7], txRGB->R, txRGB->G, txRGB->B));
  1212. graph->DrawString(AnsiToWCHAR(pSTRING), -1, &fontp, PointF(base_X+GLOBAL_VALUE[0],base_Y-GLOBAL_VALUE[1]), &brushp);
  1213. //清理绘图工具
  1214. DeleteObject(&brushp);
  1215. DeleteObject(&fontp);
  1216. DeleteObject(&fontFAMILY);
  1217. DeleteObject(&graphicsp);
  1218. }
  1219. /***************解析函数群***************/
  1220. //脚本解析函数
  1221. BOOL FileScript(char* prossName, int PRO_NUM, double* PRO_VALUE)
  1222. {
  1223. //初始参数,其中KLevel为当前循环层号;
  1224. int i=0, j=0, ki, KLevel=0, SN=-1, Line_LEN=0, fcolor=-1, BASE_VAR_NUM=PRO_NUM;
  1225. float pTMP1, pTMP2, pTMP3, pTMP4, pTMP5, wTIME;
  1226. char *prepos=NULL, *pCHA1, *pCHA2, *pCHA3, *pCHA4, *pCHA5;
  1227. double tpadd;
  1228. //定义过程变量存储容器,即定义 :A型变量,后接 ::A型变量
  1229. char** PRO_WORDS=(char**)calloc(LOOP_LEVELS, sizeof(char*));
  1230. //初始化开关参数,SFLAG[0]==1开始;
  1231. char* SFLAG=(char*)calloc(8, sizeof(char));
  1232. SFLAG[2]=-1;
  1233. //初始化循环参量数组
  1234. int*   FORI_CYCL=(int  *)calloc(LOOP_LEVELS,   sizeof(int  ));
  1235. int*   FORI_LNUM=(int  *)calloc(LOOP_LEVELS,   sizeof(int  ));
  1236. char** FORI_FTEL=(char**)calloc(LOOP_LEVELS,   sizeof(char*));
  1237. char** FORI_FEND=(char**)calloc(LOOP_LEVELS,   sizeof(char*));
  1238. BOOL*  FORI_IFOT=(BOOL *)calloc(LOOP_LEVELS,   sizeof(BOOL ));
  1239. //分配行容器
  1240. char* LCache=(char*)calloc(1025, sizeof(char));
  1241. //用于错误反馈的行指针标记
  1242. CUR_LINEPOINTER=LCache;
  1243. //辅助行指针
  1244. char* Line;
  1245. //辅助流指针
  1246. char* tmpfp=tRAM;
  1247. while(*tmpfp !='\0')
  1248. {
  1249. //记录位置
  1250. char* prepos=tmpfp;
  1251. //指针置换
  1252. Line=LCache;
  1253. while(*tmpfp !='\n' && *tmpfp !='\0')
  1254. {
  1255. *(Line++)=*(tmpfp++);
  1256. }
  1257. if(*tmpfp =='\n')
  1258. {
  1259. tmpfp++;
  1260. }
  1261. *Line='\0';
  1262. //指针回首
  1263. Line=LCache;
  1264. //行计数器
  1265. i++, CUR_LINENUM=i;
  1266. //过滤行TAB缩进或前空格
  1267. while(*Line=='\t'|| *Line==' ')
  1268. {
  1269. Line++;
  1270. }
  1271. //过滤空行或行注释
  1272. if(
  1273.     (Line[0]=='\0')                 ||
  1274.     (Line[0]=='/' && Line[1]=='/')
  1275. )
  1276. {
  1277. continue;
  1278. }
  1279. //过滤段间注释
  1280. if(
  1281.     (Line[0]=='/' &&Line[1]=='*')
  1282. )
  1283. {
  1284. SFLAG[3]=1;
  1285. continue;
  1286. }
  1287. else if(
  1288.     (Line[0]=='*' &&Line[1]=='/')
  1289. )
  1290. {
  1291. SFLAG[3]=0;
  1292. continue;
  1293. }
  1294. //识别子过程名
  1295. if(
  1296.     (isEND(Line[2], " \t\0") )      &&
  1297.     (Line[0]=='T' || Line[0]=='t')  &&
  1298.     (Line[1]=='O' || Line[1]=='o')
  1299. )
  1300. {
  1301. Line+=2;
  1302. //过滤TO后TAB缩进,或TO后空格
  1303. while(*Line=='\t'|| *Line==' ')
  1304. {
  1305. Line++;
  1306. }
  1307. if(isKEY(Line, prossName, " \t\0"))
  1308. {
  1309. SFLAG[0]=1, SFLAG[4]=1;
  1310. ARGS(Line, PRO_WORDS, PRO_NUM, i, Line);
  1311. }
  1312. else
  1313. {
  1314. SFLAG[0]=0;
  1315. }
  1316. continue;
  1317. }
  1318. //开始开关未打开,或遇到段注释,或遇到空行
  1319. if(
  1320.     SFLAG[0]==0        ||
  1321.     SFLAG[3]==1        ||
  1322.     Line[0]=='('
  1323. )
  1324. {
  1325. continue;
  1326. }
  1327. //IF...ELIF连带性分析
  1328. if(
  1329.     (FORI_IFOT[KLevel+1] && SFLAG[1]==0) &&
  1330.     (Line[0]!='E'&&Line[0]!='e')   ||
  1331.     (Line[1]!='L'&&Line[1]!='l')   ||
  1332.     (Line[2]!='I'&&Line[2]!='i')   ||
  1333.     (Line[3]!='F'&&Line[3]!='f')
  1334. )
  1335. {
  1336. FORI_IFOT[KLevel+1]=FALSE;
  1337. }
  1338. //解析循环结束标签,循环加速
  1339. if(Line[0]==')')
  1340. {
  1341. if(KLevel>SFLAG[1])
  1342. {
  1343. if(FORI_CYCL[KLevel] <0)
  1344. {
  1345. tpadd=RevPolishCore(Line+1, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1346. PRO_VALUE[0-FORI_CYCL[KLevel]]+=(tpadd==0)?1:tpadd;
  1347. }
  1348. if(FORI_FEND[KLevel]==NULL)
  1349. {
  1350. FORI_FEND[KLevel]=tmpfp;
  1351. }
  1352. tmpfp=FORI_FTEL[KLevel];
  1353. i=FORI_LNUM[KLevel];
  1354. }
  1355. else
  1356. {
  1357. FORI_FTEL[KLevel]=NULL;
  1358. FORI_FEND[KLevel]=NULL;
  1359. FORI_CYCL[KLevel]=0;
  1360. SFLAG[1]=0;
  1361. }
  1362. KLevel--;
  1363. continue;
  1364. }
  1365. else
  1366. {
  1367. if((ki=Identify_KeyWords(Line, CYC_SIZE, (char**)CYC_WORDS, " \t\0")) !=-1)
  1368. {
  1369. Line=passNULL(Line+CYC_LLENS[ki]);
  1370. KLevel++;
  1371. //解析循环
  1372. if(
  1373.     ki==0 && FORI_CYCL[KLevel]<1 && MoreJudgMent(Line, PRO_NUM, PRO_WORDS, PRO_VALUE)                     ||
  1374.     ki==1 && FORI_CYCL[KLevel]<RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE)                         ||
  1375.     ki==2 && MoreJudgMent(Line, PRO_NUM, PRO_WORDS, PRO_VALUE)                                            ||
  1376.     ki==3 && isForTrue(Line, PRO_NUM, PRO_WORDS, PRO_VALUE,  FORI_CYCL[KLevel])                           ||
  1377.     ki==4 && (FORI_IFOT[KLevel]) && FORI_CYCL[KLevel]<1 && MoreJudgMent(Line, PRO_NUM, PRO_WORDS, PRO_VALUE)
  1378. )
  1379. {
  1380. if( FORI_FTEL[KLevel]==NULL )
  1381. {
  1382. FORI_FTEL[KLevel]=prepos;
  1383. FORI_LNUM[KLevel]=i-1;
  1384. if(ki==3)
  1385. {
  1386. char *tp1, *tp2;
  1387. //提取瞬时变量名
  1388. if((tp1=strtok(Line, " \t"))==NULL)
  1389. {
  1390. errorEXIT("Missing parameter");
  1391. }
  1392. PRO_WORDS[PRO_NUM]=(char*)calloc(MAX_WORDS_SIZE, sizeof(char));
  1393. strcpy(PRO_WORDS[PRO_NUM], tp1);
  1394. //提取始步
  1395. if((tp2=strtok(NULL, " \t"))==NULL)
  1396. {
  1397. errorEXIT("Missing parameter");
  1398. }
  1399. PRO_VALUE[PRO_NUM]=RevPolishCore(tp2, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1400. FORI_CYCL[KLevel] =0-PRO_NUM;
  1401. PRO_NUM++;
  1402. }
  1403. if(ki==0 || ki==4)
  1404. {
  1405. FORI_IFOT[KLevel]=FALSE;
  1406. }
  1407. }
  1408. if(ki !=3)
  1409. {
  1410. FORI_CYCL[KLevel]  ++;
  1411. }
  1412. }
  1413. else
  1414. {
  1415. if((ki==0) && FORI_CYCL[KLevel]==0)
  1416. {
  1417. FORI_IFOT[KLevel]=TRUE;
  1418. }
  1419. if(FORI_FEND[KLevel] !=NULL)
  1420. {
  1421. if(FORI_CYCL[KLevel] <0)
  1422. {
  1423. free(PRO_WORDS[PRO_NUM]);
  1424. PRO_NUM-=1;
  1425. }
  1426. tmpfp=FORI_FEND[KLevel];
  1427. i=FORI_LNUM[KLevel]-1;
  1428. FORI_FTEL[KLevel]=NULL;
  1429. FORI_FEND[KLevel]=NULL;
  1430. FORI_CYCL[KLevel]=0;
  1431. KLevel--;
  1432. }
  1433. else
  1434. {
  1435. SFLAG[1]=KLevel;
  1436. }
  1437. }
  1438. continue;
  1439. }
  1440. }
  1441. //语句块循环开关
  1442. if(SFLAG[1]!=0)
  1443. {
  1444. continue;
  1445. }
  1446. //解析命令行
  1447. if((SN=Identify_KeyWords(Line, KEY_SIZE, (char**)KEY_WORDS, " \t\0")) !=-1)
  1448. {
  1449. Line=(SN==8)?Line+KEY_LLENS[SN]+1:passNULL(Line+KEY_LLENS[SN]);
  1450. }
  1451. else
  1452. {
  1453. //定义过程传参容器
  1454. double* PAR_VALUE=(double*)calloc(LOOP_LEVELS, sizeof(double));
  1455. //调用子过程
  1456. if(! FileScript(Line,    ARGP(Line, PAR_VALUE, PRO_NUM, PRO_WORDS, PRO_VALUE),    PAR_VALUE) )
  1457. {
  1458. fprintf(stderr, "[%d]:Undefined procedure name    '%s\n", i, passNULL(LCache));
  1459. exit(1);
  1460. }
  1461. free(PAR_VALUE);
  1462. continue;
  1463. }
  1464. switch(SN)
  1465. {
  1466. case  0:
  1467. //FD
  1468. drawLINE(hDC, RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE));
  1469. break;
  1470. case  1:
  1471. //BK
  1472. backWARD(RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE));
  1473. break;
  1474. case  2:
  1475. //LT
  1476. GLOBAL_VALUE[2]-=RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1477. break;
  1478. case  3:
  1479. //RT
  1480. GLOBAL_VALUE[2]+=RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1481. break;
  1482. case  4:
  1483. //PU
  1484. penMODE=FALSE;
  1485. break;
  1486. case  5:
  1487. //PD
  1488. if(SFLAG[2] !=-1)
  1489. {
  1490. GLOBAL_VALUE[5]=SFLAG[2];
  1491. SFLAG[2] =-1;
  1492. }
  1493. penMODE=TRUE;
  1494. break;
  1495. case  6:
  1496. //PE
  1497. if(SFLAG[2] ==-1)
  1498. {
  1499. SFLAG[2]=GLOBAL_VALUE[5];
  1500. GLOBAL_VALUE[5]=GLOBAL_VALUE[3];
  1501. }
  1502. break;
  1503. case  7:
  1504. //CS
  1505. InvalidateRect(hCMD, NULL, FALSE);
  1506. GLOBAL_VALUE[0]=GLOBAL_VALUE[1]=GLOBAL_VALUE[2]=0;
  1507. break;
  1508. case  8:
  1509. //PR, 打印输出
  1510. fputs(SuperPrintf(Line, PRO_NUM, PRO_WORDS, PRO_VALUE), stdout);
  1511. break;
  1512. case  9:
  1513. //HM
  1514. GLOBAL_VALUE[0]=GLOBAL_VALUE[1]=GLOBAL_VALUE[2]=0;
  1515. break;
  1516. case 10:
  1517. //STOP
  1518. free(PRO_WORDS);
  1519. free(SFLAG);
  1520. free(FORI_CYCL);
  1521. free(FORI_LNUM);
  1522. free(FORI_FTEL);
  1523. free(FORI_IFOT);
  1524. free(LCache);
  1525. return TRUE;
  1526. case 11:
  1527. //EXIT
  1528. exit(0);
  1529. case 12:
  1530. //FILL
  1531. if((fcolor=RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE)) !=-1)
  1532. {
  1533. //创建画刷
  1534. STRGB* bgRGB=&LOGO_RGB[fcolor];
  1535. HBRUSH m_brush=CreateSolidBrush(bgRGB->VALUE);
  1536. SelectObject(hDC1, m_brush);
  1537. ExtFloodFill(hDC1,  base_X+GLOBAL_VALUE[0], base_Y-GLOBAL_VALUE[1],  GetPixel(hDC1,base_X+GLOBAL_VALUE[0],base_Y-GLOBAL_VALUE[1]),FLOODFILLSURFACE);
  1538. //删除画刷
  1539. DeleteObject(m_brush);
  1540. }
  1541. break;
  1542. case 13:
  1543. //TEXT
  1544. pCHA1=strtok(Line, " \t"), pCHA2=strtok(NULL, " \t"), pCHA3=strtok(NULL, " \t"), pCHA4=strtok(NULL, " \t"), pCHA5=strtok(NULL, " \t");
  1545. if(pCHA5 !=NULL)
  1546. {
  1547. pTMP1=RevPolishCore(pCHA1, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1548. pTMP2=RevPolishCore(pCHA2, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1549. pTMP3=RevPolishCore(pCHA3, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1550. drawTEXT(hDC, (int)fabs(pTMP1), (int)fabs(pTMP2)%5, (int)fabs(pTMP3)%16, pCHA4, SuperPrintf(pCHA5, PRO_NUM, PRO_WORDS, PRO_VALUE));
  1551. }
  1552. else
  1553. {
  1554. errorEXIT("Needs more parameters");
  1555. }
  1556. break;
  1557. case 14:
  1558. //WAIT
  1559. wTIME=RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1560. if(wTIME>0)
  1561. {
  1562. Sleep(wTIME);
  1563. }
  1564. else
  1565. {
  1566. getch();
  1567. }
  1568. break;
  1569. case 15:
  1570. //ROTE
  1571. RotateDC(hDC1, (double)RevPolishCore(Line, PRO_NUM, PRO_WORDS, PRO_VALUE));
  1572. break;
  1573. case 16:
  1574. //MEMUSE,开辟内存画布
  1575. if(!memMODE)
  1576. {
  1577. BitBlt(hDC3,0,0,lrRECT->right,lrRECT->bottom, hDC1,0,0,SRCCOPY);
  1578. memMODE=TRUE;
  1579. }
  1580. BitBlt(hDC2,0,0,lrRECT->right,lrRECT->bottom, hDC3,0,0,SRCCOPY);
  1581. hDC=hDC2;
  1582. break;
  1583. case 17:
  1584. //MEMSEE,展示内存画布
  1585. BitBlt(hDC1,0,0,lrRECT->right,lrRECT->bottom, hDC2,0,0,SRCCOPY);
  1586. BitBlt(hDC2,0,0,lrRECT->right,lrRECT->bottom, hDC3,0,0,SRCCOPY);
  1587. break;
  1588. case 18:
  1589. //MEMOUT,丢弃内存画布
  1590. memMODE=FALSE;
  1591. hDC=hDC1;
  1592. break;
  1593. case 19:
  1594. //MAKE,  赋值
  1595. MoreAssignMent(passNULL(Line), PRO_NUM, PRO_WORDS, PRO_VALUE);
  1596. break;
  1597. case 20:
  1598. //SETXY,画线函数
  1599. pCHA1=strtok(Line, " \t"), pCHA2=strtok(NULL, " \t");
  1600. if(pCHA2 !=NULL)
  1601. {
  1602. pTMP1=RevPolishCore(pCHA1, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1603. pTMP2=RevPolishCore(pCHA2, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1604. if(penMODE)
  1605. {
  1606. drawSMOOTHLINE(hDC, (float)GLOBAL_VALUE[4], (SmoothingMode)(int)GLOBAL_VALUE[6], 0, (BYTE)GLOBAL_VALUE[7], &LOGO_RGB[(int)GLOBAL_VALUE[5]], base_X+GLOBAL_VALUE[0], base_Y-GLOBAL_VALUE[1], base_X+pTMP1, base_Y-pTMP2, 0);
  1607. }
  1608. GLOBAL_VALUE[0]=pTMP1, GLOBAL_VALUE[1]=pTMP2;
  1609. }
  1610. else
  1611. {
  1612. errorEXIT("Needs more parameters");
  1613. }
  1614. break;
  1615. case 21:
  1616. //RECT,画方函数
  1617. pCHA1=strtok(Line, " \t"), pCHA2=strtok(NULL, " \t"), pCHA3=strtok(NULL, " \t");
  1618. if(pCHA3 !=NULL)
  1619. {
  1620. pTMP1=RevPolishCore(pCHA1, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1621. pTMP2=RevPolishCore(pCHA2, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1622. pTMP3=RevPolishCore(pCHA3, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1623. drawSMOOTHLINE(hDC, (float)GLOBAL_VALUE[4], (SmoothingMode)(int)GLOBAL_VALUE[6], 1, (BYTE)GLOBAL_VALUE[7], &LOGO_RGB[(int)GLOBAL_VALUE[5]], base_X+GLOBAL_VALUE[0], base_Y-GLOBAL_VALUE[1], pTMP1, pTMP2, pTMP3);
  1624. }
  1625. else
  1626. {
  1627. errorEXIT("Needs more parameters");
  1628. }
  1629. break;
  1630. case 22:
  1631. //OVAL,画圆函数
  1632. pCHA1=strtok(Line, " \t"), pCHA2=strtok(NULL, " \t"), pCHA3=strtok(NULL, " \t");
  1633. if(pCHA3 !=NULL)
  1634. {
  1635. pTMP1=RevPolishCore(pCHA1, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1636. pTMP2=RevPolishCore(pCHA2, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1637. pTMP3=RevPolishCore(pCHA3, PRO_NUM, PRO_WORDS, PRO_VALUE);
  1638. drawSMOOTHLINE(hDC, (float)GLOBAL_VALUE[4], (SmoothingMode)(int)GLOBAL_VALUE[6], 2, (BYTE)GLOBAL_VALUE[7], &LOGO_RGB[(int)GLOBAL_VALUE[5]], base_X+GLOBAL_VALUE[0], base_Y-GLOBAL_VALUE[1], pTMP1, pTMP2, pTMP3);
  1639. }
  1640. else
  1641. {
  1642. errorEXIT("Needs more parameters");
  1643. }
  1644. break;
  1645. default:
  1646. errorEXIT("Error line");
  1647. }
  1648. }
  1649. if(!(BOOL)SFLAG[4])
  1650. {
  1651. return FALSE;
  1652. }
  1653. free(PRO_WORDS);
  1654. free(SFLAG);
  1655. free(FORI_CYCL);
  1656. free(FORI_LNUM);
  1657. free(FORI_FTEL);
  1658. free(FORI_IFOT);
  1659. free(LCache);
  1660. return TRUE;
  1661. }
  1662. /*************MAIN主函数入口*************/
  1663. int main(int argc, char** argv)
  1664. {
  1665. if(argc!=2)
  1666. {
  1667. //异常则抛出使用说明
  1668. fputs("COPYRIGHT@2017~2019 BY HAPPY, VERSION 1.0\nUSAGE: COOL [FILE]\n", stdout);
  1669. return 1;
  1670. }
  1671. //读文件流
  1672. FILE* fp=fopen(argv[1], "r");
  1673. if(fp==NULL)
  1674. {
  1675. fputs("Failed to read file.", stdout);
  1676. return 2;
  1677. }
  1678. //隐藏光标
  1679. SizeCursor(0);
  1680. //测量尺寸
  1681. int fsize=0;
  1682. fseek(fp, 0, SEEK_END);
  1683. fsize=ftell(fp);
  1684. //指针复原
  1685. fseek(fp, 0, SEEK_SET);
  1686. //超过1M的脚本,拒绝执行
  1687. if(fsize > 1*1024*1024)
  1688. {
  1689. fputs("File size is too large, out of memory.", stdout);
  1690. return 1;
  1691. }
  1692. //读入内存
  1693. tRAM=(char*)malloc((fsize+1)*sizeof(char));
  1694. fread(tRAM, fsize, 1, fp);
  1695. tRAM[fsize]='\0';
  1696. //释放文件
  1697. fclose(fp);
  1698. //初始化随机种子
  1699. srand((unsigned)time(NULL));
  1700. //初始化GdiPlus
  1701. ULONG_PTR gdipludToken;
  1702. GdiplusStartupInput gdiplusInput;
  1703. GdiplusStartup(&gdipludToken,&gdiplusInput,NULL);
  1704. {
  1705. //获取CMD窗口句柄
  1706. hCMD=GetConsoleWindow();
  1707. //获取CMD窗口大小
  1708. lrRECT=&winRECT;
  1709. GetClientRect(hCMD, lrRECT);
  1710. //获取像素坐标基值
  1711. base_X=lrRECT->right/2, base_Y=lrRECT->bottom/2;
  1712. lrRECT->right=base_X*2, lrRECT->bottom=base_Y*2;
  1713. //CMD画布
  1714. hDC1=GetDC(hCMD);
  1715. HBITMAP hBitmap1=CreateCompatibleBitmap(hDC1, lrRECT->right, lrRECT->bottom);
  1716. SelectObject(hDC1, hBitmap1);
  1717. //内存画布
  1718. hWND2=NULL;
  1719. hDC2=CreateCompatibleDC(GetDC(hWND2));
  1720. HBITMAP hBitmap2=CreateCompatibleBitmap(hDC1, lrRECT->right, lrRECT->bottom);
  1721. SelectObject(hDC2, hBitmap2);
  1722. //辅助画布
  1723. hWND3=NULL;
  1724. hDC3=CreateCompatibleDC(GetDC(hWND3));
  1725. HBITMAP hBitmap3=CreateCompatibleBitmap(hDC1, lrRECT->right, lrRECT->bottom);
  1726. SelectObject(hDC3, hBitmap3);
  1727. //运行脚本
  1728. double* PAR_VALUE=(double*)calloc(LOOP_LEVELS, sizeof(double));
  1729. //主DC选择CMD画布
  1730. hDC=hDC1;
  1731. //寻找主函数入口点
  1732. FileScript("MAIN", 1, PAR_VALUE);
  1733. //释放DC
  1734. ReleaseDC(NULL, hDC3);
  1735. ReleaseDC(NULL, hDC2);
  1736. ReleaseDC(hCMD, hDC1);
  1737. DeleteDC(hDC3);
  1738. DeleteDC(hDC2);
  1739. DeleteDC(hDC1);
  1740. DeleteObject(hBitmap3);
  1741. DeleteObject(hBitmap2);
  1742. DeleteObject(hBitmap1);
  1743. //释放内存
  1744. free(PAR_VALUE);
  1745. free(tRAM);
  1746. }
  1747. //关闭GdiPlus
  1748. GdiplusShutdown(gdipludToken);
  1749. return 0;
  1750. }
复制代码
1

评分人数

    • 老刘1号: 沉迷于绘图无法自拔的happy兄技术 + 1

返回列表