uoj#98 未来程序·改

硬写就行了

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <set>
  10. #include <map>
  11. #include <regex>
  12. #include <cstdarg>
  13. #include <cassert>
  14. #include <algorithm>
  15. using namespace std;
  16.  
  17. [[noreturn]] void err(const char *fmt, ...){
  18. 	va_list argptr;
  19. 	va_start(argptr,fmt);
  20. 	vprintf(fmt,argptr);
  21.  	va_end(argptr);
  22. 	exit(0);
  23. }
  24.  
  25. const string Keywords[]={"if","else","while","for","switch","case","return","new","delete","inline","do",
  26. "true","false","class","struct","contiune","break","try","catch","const","enum","using","namespace","auto",
  27. "static","public","private","protected","friend","operator","sizeof","template","typename","goto","typedef","register","long","int"};
  28.  
  29. const string Operators[]={"+","-","*","/","%","=",";","<",">","<<",">>","&","|","&&","||","(",")","[","]","{","}",">=","<=","==","!=",".","+=","-=","*=","/=",",","!"};
  30. map<string, int> Keywords_;
  31. map<string, int> Operators_;
  32.  
  33. vector<string> id_str;
  34. map<string, int> str_id;
  35. void append_id(const string &s){
  36. 	str_id[s]=(int)id_str.size();
  37. 	id_str.push_back(s);
  38. }
  39.  
  40. int inputs[2000010], inputs_p, inputs_size;
  41.  
  42. void init(){
  43. 	cin>>inputs_size;
  44. 	for (int i=0;i<inputs_size;i++) cin>>inputs[i]; 
  45. 	int c=0;
  46. 	for (auto &s:Keywords) Keywords_[s]=c++;
  47. 	c=0;
  48. 	for (auto &s:Operators) Operators_[s]=c++;
  49. 	append_id("cin");
  50. 	append_id("cout");
  51. 	append_id("endl");
  52. }
  53.  
  54. enum TokenType{INT,FLOAT,STRING,CHAR,KEYWORD,ID,OPERATOR};
  55. struct Token{
  56. 	TokenType type;
  57. 	int val;
  58. 	bool operator==(Token &tok){return type==tok.type && val==tok.val;}
  59. 	Token(TokenType type, int val):type(type), val(val){}
  60. 	void debug(){
  61. 		switch (type){
  62. 			case INT: cout<<val; break;
  63. 			case KEYWORD: cout<<Keywords[val]; break;
  64. 			case OPERATOR: cout<<Operators[val]; break;
  65. 			case ID: cout<<id_str[val]; break;
  66. 			default: ;
  67. 		}
  68. 	}
  69. 	bool equal(int tp, const string &str){
  70. 		if (tp!=type) return 0;
  71. 		if (type==OPERATOR) return Operators[val]==str;
  72. 		if (type==KEYWORD) return Keywords[val]==str;
  73. 		if (type==ID) return id_str[val]==str;
  74. 		err("Error %s\n",__LINE__);
  75. 	};
  76. };
  77. vector<Token> tok;
  78.  
  79. void tokenlize(char *s){
  80. 	char *cur=s;
  81. 	char tmp[128];
  82. 	if (s[0]=='#') return;
  83. 	while (*cur){
  84. 		if (isdigit(*cur)){
  85. 			int t=0;
  86. 			while (isdigit(*cur)) t=t*10+*cur-'0',cur++;
  87. 			tok.emplace_back(INT, t);
  88. 		}
  89. 		else if (isalpha(*cur) || *cur=='_'){
  90. 			char *t=tmp;
  91. 			while (isalpha(*cur) || *cur=='_' || isdigit(*cur)) *t=*cur, t++, cur++;
  92. 			*t=0;
  93. 			string s1(tmp);
  94. 			if (Keywords_.count(s1)){
  95. 				tok.emplace_back(KEYWORD, Keywords_[s1]);
  96. 			}
  97. 			else{
  98. 				if (!str_id.count(s1)) 
  99. 					append_id(s1);
  100. 				tok.emplace_back(ID, str_id[s1]);
  101. 			}
  102. 		}
  103. 		else if (*cur=='\''){
  104. 			cur++;
  105. 			while (*cur!='\'') cur++;
  106. 			cur++;
  107. 		}
  108. 		else if (*cur=='\"'){
  109. 			cur++;
  110. 			while (*cur!='\"') cur++;
  111. 			cur++;
  112. 		}
  113. 		else if (isspace(*cur)){
  114. 			cur++;
  115. 		}
  116. 		else{
  117. 			tmp[0]=*cur; cur++;
  118. 			tmp[1]=*cur; cur++;
  119. 			tmp[2]=0;
  120. 			string s1(tmp);
  121. 			if (tmp[1] && Operators_.count(s1))
  122. 				tok.emplace_back(OPERATOR, Operators_[s1]);
  123. 			else{
  124. 				tmp[1]=0;
  125. 				cur--;
  126. 				s1=string(tmp);
  127. 				if (Operators_.count(s1))
  128. 					tok.emplace_back(OPERATOR, Operators_[s1]);
  129. 				else
  130. 					err("Unkonwn operator %s\n",s1.c_str());
  131. 			}
  132. 		} 
  133. 	}
  134. }
  135.  
  136. char line[1000];
  137.  
  138. int tokp;
  139.  
  140. void eat(TokenType type, const string &s){
  141. 	if (!tok[tokp].equal(type, s)){
  142. 		puts("[Error] Read ");
  143. 		tok[tokp].debug();
  144. 		err("expect %s\n", s.c_str());
  145. 	}
  146. 	tokp++;
  147. }
  148.  
  149. struct ASTNode{
  150. 	enum Type{
  151. 		FUNC,
  152. 		STMTS,
  153. 		STMT,
  154. 		UNIT10,UNIT9,UNIT8,UNIT7,UNIT6,UNIT5,UNIT4,UNIT3,UNIT2,UNIT1,UNIT0,
  155. 		INT,
  156. 		ID,
  157. 		VAR,
  158. 		DEFINES
  159. 	};
  160. 	const static string name[18];
  161. 	void debug(){
  162. 		cout<<name[type]<<' ';
  163. 		switch(type){
  164. 			case VAR:
  165. 			case DEFINES:
  166. 			case STMT:
  167. 			case STMTS:
  168. 			case UNIT0:
  169. 			case FUNC: break;
  170. 			case INT: cout<<val; break;
  171. 			case ID: cout<<id_str[val]; break;
  172. 			default: cout<<Operators[val]; break;
  173. 		}
  174. 		cout<<'\n';
  175. 	}
  176. 	void debugdfs(int deep=0){
  177. 		for (int i=0;i<deep;i++) cout<<"  ";
  178. 		debug();
  179. 		for (auto p:ch) p->debugdfs(deep+1);
  180. 	}
  181. 	Type type;
  182. 	int val;
  183. 	ASTNode(Type type):type(type){}
  184. 	ASTNode(Type type, int val):type(type),val(val){}
  185. 	vector<ASTNode *> ch;
  186. };
  187. const string ASTNode::name[18]={"FUNC","STMTS","STMT","UNIT10","UNIT9","UNIT8","UNIT7","UNIT6","UNIT5","UNIT4","UNIT3","UNIT2","UNIT1","UNIT0","INT","ID","VAR","DEFINES"};
  188. map<int, ASTNode*> funcs;
  189. vector<ASTNode*> global_vars;
  190.  
  191. ASTNode* parse_expr();
  192.  
  193. ASTNode* parse_id(){
  194. 	return new ASTNode(ASTNode::ID, tok[tokp++].val);
  195. }
  196. ASTNode* parse_int(){
  197. 	return new ASTNode(ASTNode::INT, tok[tokp++].val);
  198. }
  199.  
  200. ASTNode* parse_var(){
  201. 	auto node=new ASTNode(ASTNode::VAR);
  202. 	node->ch.push_back(parse_id());
  203. 	while (tok[tokp].equal(OPERATOR, "[")){
  204. 		tokp++; //[ 
  205. 		node->ch.push_back(parse_expr());
  206. 		eat(OPERATOR, "]"); //]
  207. 	}
  208. 	return node;
  209. }
  210.  
  211. ASTNode* parse_unit0(){
  212. 	ASTNode *node;
  213. 	if (tok[tokp].equal(OPERATOR,"(")){
  214. 		tokp++; //(
  215. 		node=parse_expr();
  216. 		eat(OPERATOR, ")"); //)
  217. 	}
  218. 	else if (tok[tokp+1].equal(OPERATOR,"(")){ //func
  219. 		node=new ASTNode(ASTNode::UNIT0, 0);
  220. 		node->ch.push_back(parse_id());
  221. 		while (tok[tokp].equal(OPERATOR, "(") || tok[tokp].equal(OPERATOR, ",") ){
  222. 			tokp++; //( or ,
  223. 			if (tok[tokp].equal(OPERATOR,")")) break;
  224. 			node->ch.push_back(parse_expr());
  225. 		}
  226. 		eat(OPERATOR, ")"); //)
  227. 		return node;
  228. 	}
  229. 	else if (tok[tokp].type==INT){
  230. 		node=new ASTNode(ASTNode::UNIT0, 2);
  231. 		node->ch.push_back(parse_int());
  232. 	}
  233. 	else{ //var
  234. 		node=new ASTNode(ASTNode::UNIT0, 1);
  235. 		node->ch.push_back(parse_var());
  236. 	}
  237. 	return node;
  238. }
  239. ASTNode* parse_unit1(){
  240. 	if (tok[tokp].equal(OPERATOR,"+") || tok[tokp].equal(OPERATOR,"-") 
  241. 		|| tok[tokp].equal(OPERATOR,"!")){
  242. 		auto node=new ASTNode(ASTNode::UNIT1, tok[tokp].val);
  243. 		tokp++;
  244. 		auto left=parse_unit1();
  245. 		node->ch.push_back(left);
  246. 		return node;
  247. 	}
  248. 	else return parse_unit0();
  249. }
  250. ASTNode* parse_unit2(){
  251. 	auto left=parse_unit1();
  252. 	while (tok[tokp].equal(OPERATOR,"*") || tok[tokp].equal(OPERATOR,"/") || tok[tokp].equal(OPERATOR,"%")){
  253. 		auto node=new ASTNode(ASTNode::UNIT2, tok[tokp].val);
  254. 		node->val=tok[tokp].val; tokp++;
  255. 		node->ch.push_back(left);
  256. 		node->ch.push_back(parse_unit1());
  257. 		left=node;
  258. 	}
  259. 	return left;
  260. }
  261. ASTNode* parse_unit3(){
  262. 	auto left=parse_unit2();
  263. 	while (tok[tokp].equal(OPERATOR,"+") || tok[tokp].equal(OPERATOR,"-")){
  264. 		auto node=new ASTNode(ASTNode::UNIT3, tok[tokp].val);
  265. 		node->val=tok[tokp].val; tokp++;
  266. 		node->ch.push_back(left);
  267. 		node->ch.push_back(parse_unit2());
  268. 		left=node;
  269. 	}
  270. 	return left;
  271. }
  272. ASTNode* parse_unit4(){
  273. 	auto left=parse_unit3();
  274. 	while (tok[tokp].equal(OPERATOR,">") || tok[tokp].equal(OPERATOR,">=") ||
  275. 		   tok[tokp].equal(OPERATOR,"<") || tok[tokp].equal(OPERATOR,"<=") ){
  276. 		auto node=new ASTNode(ASTNode::UNIT4, tok[tokp].val);
  277. 		node->val=tok[tokp].val; tokp++;
  278. 		node->ch.push_back(left);
  279. 		node->ch.push_back(parse_unit3());
  280. 		left=node;
  281. 	}
  282. 	return left;
  283. }
  284. ASTNode* parse_unit5(){
  285. 	auto left=parse_unit4();
  286. 	while (tok[tokp].equal(OPERATOR,"==") || tok[tokp].equal(OPERATOR,"!=")){
  287. 		auto node=new ASTNode(ASTNode::UNIT5, tok[tokp].val);
  288. 		node->val=tok[tokp].val; tokp++;
  289. 		node->ch.push_back(left);
  290. 		node->ch.push_back(parse_unit4());
  291. 		left=node;
  292. 	}
  293. 	return left;
  294. }
  295. ASTNode* parse_unit6(){
  296. 	auto left=parse_unit5();
  297. 	while (tok[tokp].equal(OPERATOR,"^")){
  298. 		auto node=new ASTNode(ASTNode::UNIT6, tok[tokp].val);
  299. 		node->val=tok[tokp].val; tokp++;
  300. 		node->ch.push_back(left);
  301. 		node->ch.push_back(parse_unit5());
  302. 		left=node;
  303. 	}
  304. 	return left;
  305. }
  306. ASTNode* parse_unit7(){
  307. 	auto left=parse_unit6();
  308. 	while (tok[tokp].equal(OPERATOR,"&&")){
  309. 		auto node=new ASTNode(ASTNode::UNIT7, tok[tokp].val);
  310. 		node->val=tok[tokp].val; tokp++;
  311. 		node->ch.push_back(left);
  312. 		node->ch.push_back(parse_unit6());
  313. 		left=node;
  314. 	}
  315. 	return left;
  316. }
  317. ASTNode* parse_unit8(){
  318. 	auto left=parse_unit7();
  319. 	while (tok[tokp].equal(OPERATOR,"||")){
  320. 		auto node=new ASTNode(ASTNode::UNIT8, tok[tokp].val);
  321. 		node->val=tok[tokp].val; tokp++;
  322. 		node->ch.push_back(left);
  323. 		node->ch.push_back(parse_unit7());
  324. 		left=node;
  325. 	}
  326. 	return left;
  327. }
  328. ASTNode* parse_unit9(){
  329. 	auto left=parse_unit8();
  330. 	if (tok[tokp].equal(OPERATOR,"=")){
  331. 		auto node=new ASTNode(ASTNode::UNIT9, tok[tokp].val);
  332. 		node->val=tok[tokp].val; tokp++;
  333. 		node->ch.push_back(left);
  334. 		node->ch.push_back(parse_unit9());
  335. 		return node;
  336. 	}
  337. 	else return left;
  338. }
  339. ASTNode* parse_unit10(){
  340. 	auto left=parse_unit9();
  341. 	while (tok[tokp].equal(OPERATOR,">>") || tok[tokp].equal(OPERATOR,"<<")){
  342. 		auto node=new ASTNode(ASTNode::UNIT10, tok[tokp].val);
  343. 		node->val=tok[tokp].val; tokp++;
  344. 		node->ch.push_back(left);
  345. 		node->ch.push_back(parse_unit9());
  346. 		left=node;
  347. 	}
  348. 	return left;
  349. }
  350.  
  351. ASTNode* parse_expr(){
  352. 	return parse_unit10();
  353. }
  354.  
  355. ASTNode* parse_defines(){
  356. 	tokp++; // <type-id>
  357. 	auto node=new ASTNode(ASTNode::DEFINES);
  358. 	node->ch.push_back(parse_var());
  359. 	while (!tok[tokp].equal(OPERATOR, ";")){
  360. 		eat(OPERATOR, ",");
  361. 		node->ch.push_back(parse_var());
  362. 	}
  363. 	return node;
  364. }
  365.  
  366. ASTNode* parse_for_stmt(){
  367. 	auto node=new ASTNode(ASTNode::STMT);
  368. 	auto &ch=node->ch;
  369. 	if (tok[tokp].equal(OPERATOR,";") || tok[tokp].equal(OPERATOR,")")){ //empty
  370. 		node->val=7;
  371. 	}
  372. 	else if (tok[tokp].equal(KEYWORD,"int")){
  373. 		node->val=5;
  374. 		tokp++;
  375. 		ch.push_back(parse_defines());
  376. 	}
  377. 	else{
  378. 		node->val=6;
  379. 		ch.push_back(parse_expr());
  380. 	}
  381. 	return node;
  382. }
  383.  
  384. ASTNode* parse_stmts();
  385.  
  386. ASTNode* parse_stmt(){
  387. 	auto node=new ASTNode(ASTNode::STMT);
  388. 	auto &ch=node->ch;
  389. 	if (tok[tokp].equal(KEYWORD,"if")){
  390. 		tokp++;
  391. 		node->val=0;
  392. 		eat(OPERATOR, "(");
  393. 		ch.push_back(parse_expr());
  394. 		eat(OPERATOR, ")");
  395. 		ch.push_back(parse_stmt());
  396. 		if (tok[tokp].equal(KEYWORD, "else")){
  397. 			tokp++;
  398. 			ch.push_back(parse_stmt());
  399. 		}
  400. 	}
  401. 	else if (tok[tokp].equal(KEYWORD,"while")){
  402. 		tokp++;
  403. 		node->val=1;
  404. 		eat(OPERATOR, "(");
  405. 		ch.push_back(parse_expr());
  406. 		eat(OPERATOR, ")");
  407. 		ch.push_back(parse_stmt());
  408. 	}
  409. 	else if (tok[tokp].equal(KEYWORD,"for")){
  410. 		tokp++;
  411. 		node->val=2;
  412. 		eat(OPERATOR, "(");
  413. 		ch.push_back(parse_for_stmt());
  414. 		eat(OPERATOR, ";");
  415. 		ch.push_back(parse_for_stmt());
  416. 		eat(OPERATOR, ";");
  417. 		ch.push_back(parse_for_stmt());
  418. 		eat(OPERATOR, ")");
  419. 		ch.push_back(parse_stmt());
  420. 	}
  421. 	else if (tok[tokp].equal(KEYWORD,"return")){
  422. 		node->val=3;
  423. 		tokp++;
  424. 		ch.push_back(parse_expr());
  425. 		eat(OPERATOR, ";");
  426. 	}
  427. 	else if (tok[tokp].equal(OPERATOR,"{")){
  428. 		tokp++; //{
  429. 		node->val=4;
  430. 		ch.push_back(parse_stmts());
  431. 		eat(OPERATOR, "}");
  432. 	}
  433. 	else if (tok[tokp].equal(OPERATOR,";")){ //empty
  434. 		tokp++;
  435. 		node->val=7;
  436. 	}
  437. 	else if (tok[tokp].equal(KEYWORD,"using")){ //empty
  438. 		while (!tok[tokp].equal(OPERATOR, ";")) tokp++;
  439. 		tokp++;
  440. 		node->val=7;
  441. 	}
  442. 	else if (tok[tokp].equal(KEYWORD,"int")){
  443. 		node->val=5;
  444. 		ch.push_back(parse_defines());
  445. 		eat(OPERATOR, ";");
  446. 	}
  447. 	else{
  448. 		node->val=6;
  449. 		ch.push_back(parse_expr());
  450. 		eat(OPERATOR, ";");
  451. 	}
  452. 	return node;
  453. }
  454.  
  455. ASTNode* parse_stmts(){
  456. 	auto node=new ASTNode(ASTNode::STMTS);
  457. 	while (!tok[tokp].equal(OPERATOR,"}")){
  458. 		node->ch.push_back(parse_stmt());
  459. 	}
  460. 	return node;
  461. }
  462.  
  463. ASTNode* parse_func(){
  464. 	tokp++; //<type>
  465. 	auto node=new ASTNode(ASTNode::FUNC, tok[tokp].val);
  466. 	node->ch.push_back(parse_id());
  467. 	tokp++; //(
  468. 	while(!tok[tokp].equal(OPERATOR, ")")){
  469. 		tokp++; //<type>
  470. 		node->ch.push_back(parse_id());
  471. 		if (!tok[tokp].equal(OPERATOR, ")")) 
  472. 			eat(OPERATOR,",");
  473. 	}
  474. 	tokp++; //)
  475. 	eat(OPERATOR, "{");
  476. 	node->ch.push_back(parse_stmts());
  477. 	eat(OPERATOR, "}");
  478. 	return node;
  479. }
  480.  
  481. void parse_root(){
  482. 	while (tokp<(int)tok.size()){
  483. 		if (tok[tokp+2].equal(OPERATOR, "(")){ //func
  484. 			int fun_id=tok[tokp+1].val;
  485. 			auto ret=parse_func();
  486. 			//ret->debugdfs();
  487. 			funcs[fun_id]=ret;
  488. 		}
  489. 		else{ //define vars
  490. 			global_vars.push_back(parse_stmt());
  491. 		}
  492. 	}
  493. }
  494.  
  495. int mem[4200000];
  496.  
  497. struct VarInfo{
  498. 	int pos; 
  499. 	bool isarray;
  500. 	vector<int> dim;
  501. 	int repos(const vector<int> &v){
  502. 		assert(v.size()==dim.size());
  503. 		int b=1,sum=0;
  504. 		for (int i=(int)dim.size()-1;i>=0;i--){
  505. 			sum+=v[i]*b;
  506. 			b*=dim[i];
  507. 		}
  508. 		return pos+sum;
  509. 	}
  510. };
  511.  
  512. int cur_pos=1;
  513. int func_dep;
  514. stack<int> base_pos;
  515. vector<map<int,VarInfo>> id_info;
  516. vector<int> pre_scope;
  517. bool return_flag;
  518. #define ENDL 3
  519.  
  520. void in_scope(int _pre_scope){
  521. 	base_pos.push(cur_pos);
  522. 	pre_scope.push_back(_pre_scope);
  523. 	id_info.emplace_back();
  524. }
  525. void out_scope(){
  526. 	cur_pos=base_pos.top();
  527. 	base_pos.pop();
  528. 	pre_scope.pop_back();
  529. 	id_info.pop_back();
  530. }
  531. void alloc(int id, const vector<int> &dim, int init_val=0){
  532. 	if (dim.size()==0){
  533. 		id_info.back()[id]=VarInfo({cur_pos, 0, vector<int> ()});
  534. 		mem[cur_pos]=init_val;
  535. 		cur_pos++;
  536. 	}
  537. 	else{
  538. 		id_info.back()[id]=VarInfo({cur_pos, 0, vector<int> (dim)});
  539. 		int len=accumulate(begin(dim),end(dim),1,[](int x, int y){return x*y;});
  540. 		fill(mem+cur_pos,mem+cur_pos+len,0);
  541. 		cur_pos+=len;
  542. 	}
  543. }
  544. int read_var(int id, const vector<int> &dim){
  545. 	for (int i=(int)id_info.size()-1;i>=0;i=pre_scope[i]) 
  546. 		if(id_info[i].count(id)){
  547. 			if (dim.size()==0) return id_info[i][id].pos;
  548. 			return id_info[i][id].repos(dim);
  549. 		}
  550. 	err("var %s not defined\n", id_str[id].c_str());
  551. }
  552.  
  553. void run_stmts(ASTNode *node);
  554. int run_expr(ASTNode *node);
  555.  
  556. int run_var(ASTNode *node){
  557. 	int id=(node->ch[0])->val;
  558.  
  559. 	vector<int> dim;
  560. 	for (int i=1;i<(int)node->ch.size();i++){
  561. 		int ret=run_expr(node->ch[i]);
  562. 		dim.push_back(mem[ret]);
  563. 	}
  564. 	return read_var(id, dim);
  565. }
  566.  
  567. void run_stmts(ASTNode *node);
  568.  
  569. void run_func(ASTNode *node, const vector<int> &args){
  570. 	in_scope(0);
  571. 	func_dep++;
  572. 	return_flag=0;
  573. 	auto &ch=node->ch;
  574. 	if (ch.size()!=args.size()+2)
  575. 		err("arg list mismatch\n");
  576. 	for (int i=1;i<(int)ch.size()-1;i++){
  577. 		alloc(ch[i]->val, vector<int> (), args[i-1]);
  578. 	}
  579. 	run_stmts(ch.back());
  580. 	if (return_flag==0) *mem=0;
  581. 	return_flag=0;
  582. 	func_dep--;
  583. 	out_scope();
  584. }
  585.  
  586. void run_funccall(ASTNode *node){
  587. 	int id=node->ch[0]->val;
  588. 	vector<int> args;
  589. 	for (int i=1;i<(int)node->ch.size();i++){
  590. 		int ret=run_expr(node->ch[i]);
  591. 		args.push_back(mem[ret]);
  592. 	}
  593. 	if (id_str[id]==string("putchar")){
  594. 		*mem=putchar(args[0]);
  595. 		return;
  596. 	}
  597. 	//for (int i=0;i<func_dep;i++) cout<<"  "; cout<<"Calling func "<<id_str[id]<<", args: "; for (auto x:args) cout<<x<<' '; cout<<'\n';
  598. 	if (!funcs.count(id)) err("[Error] no such function to call %s", id_str[id].c_str());
  599. 	run_func(funcs[id], args);	
  600. }
  601.  
  602. int run_expr(ASTNode *node){
  603. 	if (node->type==ASTNode::UNIT0){
  604. 		if (node->val==1)
  605. 			return run_var(node->ch[0]);
  606. 		else if (node->val==2)
  607. 			*mem=node->ch[0]->val;
  608. 		else 
  609. 			run_funccall(node);
  610. 		return 0;
  611. 	}
  612. 	int op=node->val;
  613. 	int vall,valr=-1;
  614.  
  615. 	vall=run_expr(node->ch[0]);
  616. 	int tmp=mem[vall];
  617.  
  618. 	if (node->ch.size()>1) 
  619. 		valr=run_expr(node->ch[1]);
  620. 	if (Operators[op]=="="){
  621. 		if (vall==0) err("rvalue can't be assigned\n");
  622. 		mem[vall]=mem[valr];
  623. 		return vall;
  624. 	}
  625. 	else if (Operators[op]=="+"){
  626. 		if (valr==-1) *mem=tmp;
  627. 		else *mem=tmp+mem[valr];
  628. 		return 0;
  629. 	}
  630. 	else if (Operators[op]=="-"){
  631. 		if (valr==-1) *mem=-tmp;
  632. 		else *mem=tmp-mem[valr];
  633. 		return 0;
  634. 	}
  635. 	else if (Operators[op]=="!"){
  636. 		*mem=!tmp;
  637. 		return 0;
  638. 	}
  639. 	else if (Operators[op]=="*"){
  640. 		*mem=tmp*mem[valr];
  641. 		return 0;
  642. 	}
  643. 	else if (Operators[op]=="/"){
  644. 		*mem=tmp/mem[valr];
  645. 		return 0;
  646. 	}
  647. 	else if (Operators[op]=="=="){
  648. 		*mem=tmp==mem[valr];
  649. 		return 0;
  650. 	}
  651. 	else if (Operators[op]=="!="){
  652. 		*mem=tmp!=mem[valr];
  653. 		return 0;
  654. 	}
  655. 	else if (Operators[op]==">="){
  656. 		*mem=tmp>=mem[valr];
  657. 		return 0;
  658. 	}
  659. 	else if (Operators[op]=="<="){
  660. 		*mem=tmp<=mem[valr];
  661. 		return 0;
  662. 	}
  663. 	else if (Operators[op]==">"){
  664. 		*mem=tmp>mem[valr];
  665. 		return 0;
  666. 	}
  667. 	else if (Operators[op]=="<"){
  668. 		*mem=tmp<mem[valr];
  669. 		return 0;
  670. 	}
  671. 	else if (Operators[op]=="%"){
  672. 		*mem=tmp%mem[valr];
  673. 		return 0;
  674. 	}
  675. 	else if (Operators[op]=="^"){
  676. 		*mem=(!!tmp)^(!!mem[valr]);
  677. 		return 0;
  678. 	}
  679. 	else if (Operators[op]=="&&"){ //no short
  680. 		*mem=tmp&&mem[valr];
  681. 		return 0;
  682. 	}
  683. 	else if (Operators[op]=="||"){
  684. 		*mem=tmp||mem[valr];
  685. 		return 0;
  686. 	}
  687. 	else if (Operators[op]=="<<"){
  688. 		int ret;
  689. 		if (valr==ENDL) ret=bool(cout<<endl);
  690. 		else ret=bool(cout<<mem[valr]);
  691. 		*mem=ret;
  692. 		return 0;
  693. 	}
  694. 	else if (Operators[op]==">>"){
  695. 		if (valr==0) err("cin: rvalue can't be assigned");
  696. 		if (inputs_p==inputs_size) *mem=0;
  697. 		else mem[valr]=inputs[inputs_p++],*mem=1;
  698. 		return 0;
  699.  
  700. 	}
  701. 	throw;
  702. }
  703.  
  704. void run_defines(ASTNode *node){
  705. 	for (auto ch:node->ch){
  706. 		vector<int> dim;
  707. 		for (int i=1;i<(int)ch->ch.size();i++){
  708. 			int ret=run_expr(ch->ch[i]);
  709. 			dim.push_back(mem[ret]);
  710. 		}
  711. 		alloc(ch->ch[0]->val, dim);
  712. 	}
  713. }
  714.  
  715. void run_stmt(ASTNode *node){
  716. 	if (return_flag) return;
  717. 	if (node->val==0){ //if
  718. 		if (mem[run_expr(node->ch[0])])
  719. 			run_stmt(node->ch[1]);
  720. 		else if (node->ch.size()>2){
  721. 			run_stmt(node->ch[2]);
  722. 		}
  723. 	}
  724. 	else if (node->val==1){ //while
  725. 		while (1){
  726. 			if (mem[run_expr(node->ch[0])] == 0) break;
  727. 			run_stmt(node->ch[1]);
  728. 			if (return_flag) return; 
  729. 		}	
  730. 	}
  731. 	else if (node->val==2){ //for
  732. 		run_stmt(node->ch[0]);
  733. 		while (1){
  734. 			*mem=1;
  735. 			run_stmt(node->ch[1]);
  736. 			if (*mem == 0) break;
  737. 			run_stmt(node->ch[3]);
  738. 			if (return_flag) return;
  739. 			run_stmt(node->ch[2]);
  740. 		}
  741. 	}
  742. 	else if (node->val==3){ //return
  743. 		int ret=run_expr(node->ch[0]);
  744. 		*mem=mem[ret];
  745. 		return_flag=1;
  746. 	}
  747. 	else if (node->val==4){ //stmts
  748. 		in_scope(pre_scope.size()-1);
  749. 		run_stmts(node->ch[0]);
  750. 		out_scope();
  751. 	}
  752. 	else if (node->val==5){ //define
  753. 		run_defines(node->ch[0]);
  754. 	}
  755. 	else if (node->val==6){ //expr
  756. 		*mem=mem[run_expr(node->ch[0])];
  757. 	}
  758. }
  759.  
  760. void run_stmts(ASTNode *node){
  761. 	for (auto p:node->ch)
  762. 		run_stmt(p);
  763. }
  764.  
  765. void run_root(){
  766. 	in_scope(-1);
  767. 	alloc(str_id["cin"], vector<int>());
  768. 	alloc(str_id["cout"], vector<int>());
  769. 	alloc(str_id["endl"], vector<int>());
  770. 	for (auto p:global_vars)
  771. 		run_stmt(p);
  772. 	run_func(funcs[str_id["main"]], vector<int> ());
  773. }
  774.  
  775. int main(){
  776. 	freopen("1.cpp","r",stdin);
  777. 	init();
  778. 	while (fgets(line,1000,stdin)){
  779. 		//printf("%s",line);
  780. 		tokenlize(line);
  781. 	}
  782. 	/*
  783. 	int f=0;
  784. 	for (int i=0;i<tok.size();i++){
  785. 		if (tok[i].equal(ID, "function61") || tok[i].equal(ID,"function6") ||  tok[i].equal(ID,"Test6")) f=1;
  786. 		if (f)
  787. 		tok[i].debug(); 
  788. 	}*/
  789. 	parse_root();
  790. 	run_root();
  791. 	return 0;
  792. }

de了一堆bug才过

#bug现象原因及解决
8表达式计算错误‘%’符号未被正确计算
9未判断for结束条件空的情况
10for循环内return导致程序无法终止运行完一次内部stmt后未检查return_flag
11被调用函数中未定义的变量会引用外层函数变量修改作用域规则
12运行错误变量内存太小
13for循环无法结束运行for结束条件stmt时,未将单个expr左值转为右值
14未将if等语句的expr左值转为右值
15增加putchar函数的返回值
16WA10-Test6未考虑嵌套作用域

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注