ungureanuvladvictor

Untitled

Apr 23rd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1.  
  2. #include <sstream>
  3. #include <map>
  4. #include "../include/Parser.hh"
  5.  
  6. using namespace std;
  7.  
  8. int Parser::parseFile(Brain* brain, ifstream &brainFile, int arguments)
  9. {
  10. map<string,int> labels;
  11. vector<InstructionIF> instructions;
  12. string line;
  13. int counter=-1;
  14. int brk_ind=-1;
  15. int ebk_ind=-1;
  16.  
  17. int ret;
  18.  
  19. while(getline(brainFile,line))
  20. {
  21. counter++;
  22. istringstream iss(line);
  23. if(line[0]=='#')
  24. {
  25. string label;
  26. iss>>label;
  27. cout<<label<<endl;
  28. if(labels.find(label)!=labels.end())
  29. {
  30. cerr<<"Label already exists"<<endl;
  31. return 1;
  32. }
  33. labels[label]=counter;
  34. }
  35. string instr;
  36. iss>>instr;
  37. if(instr=="add")
  38. {
  39. string res,first,second;
  40. iss>>res>>first>>second;
  41. vector<string> args;
  42. args.push_back(res);
  43. args.push_back(first);
  44. args.push_back(second);
  45. instructions.push_back(Instruction_add(&ret,args));
  46. }
  47. else if(instr=="sub")
  48. {
  49. cout<<"SUB"<<endl;
  50. string res,first,second;
  51. iss>>res>>first>>second;
  52. vector<string> args;
  53. args.push_back(res);
  54. args.push_back(first);
  55. args.push_back(second);
  56. instructions.push_back(Instruction_sub(&ret,args));
  57. }
  58. else if(instr=="mul")
  59. {
  60. string res,first,second;
  61. iss>>res>>first>>second;
  62. vector<string> args;
  63. args.push_back(res);
  64. args.push_back(first);
  65. args.push_back(second);
  66. instructions.push_back(Instruction_mul(&ret,args));
  67. }
  68. else if(instr=="div")
  69. {
  70. string res,first,second;
  71. iss>>res>>first>>second;
  72. vector<string> args;
  73. args.push_back(res);
  74. args.push_back(first);
  75. args.push_back(second);
  76. instructions.push_back(Instruction_div(&ret,args));
  77. }
  78. /*else if(instr=="ifL")
  79. {
  80. string first,second,location;
  81. iss>>first>>second>>location;
  82. vector<string> args;
  83. args.push_back(first);
  84. args.push_back(second);
  85. args.push_back(location);
  86. instructions.push_back(Instruction_ifL(&ret,args));
  87. }
  88. else if(instr=="ieL")
  89. {
  90. string first,second,im1,im2;
  91. iss>>first>>second>>im1>>im2;
  92. vector<string> args;
  93. args.push_back(first);
  94. args.push_back(second);
  95. args.push_back(im1);
  96. args.push_back(im2);
  97. //instructions.push_back(Instruction_ieL(&ret,args));
  98. }*/
  99. else if(instr=="ifl")
  100. {
  101. cout<<"IFL"<<endl;
  102. string first,second,location;
  103. iss>>first>>second>>location;
  104. vector<string> args;
  105. args.push_back(first);
  106. args.push_back(second);
  107. args.push_back(location);
  108. instructions.push_back(Instruction_ifl(&ret,args));
  109. }
  110. else if(instr=="iel")
  111. {
  112. string first,second,im1,im2;
  113. iss>>first>>second>>im1>>im2;
  114. vector<string> args;
  115. args.push_back(first);
  116. args.push_back(second);
  117. args.push_back(im1);
  118. args.push_back(im2);
  119. instructions.push_back(Instruction_iel(&ret,args));
  120. }
  121. else if(instr=="ife")
  122. {
  123. cout<<"IFE"<<endl;
  124. string first,second,location;
  125. iss>>first>>second>>location;
  126. vector<string> args;
  127. args.push_back(first);
  128. args.push_back(second);
  129. args.push_back(location);
  130. instructions.push_back(Instruction_ife(&ret,args));
  131. }
  132. else if(instr=="iee")
  133. {
  134. cout<<"IEE"<<endl;
  135. string first,second,im1,im2;
  136. iss>>first>>second>>im1>>im2;
  137. vector<string> args;
  138. args.push_back(first);
  139. args.push_back(second);
  140. args.push_back(im1);
  141. args.push_back(im2);
  142. instructions.push_back(Instruction_iee(&ret,args));
  143. }
  144. else if(instr=="jmp")
  145. {
  146. cout<<"JMP"<<endl;
  147. string im;
  148. iss>>im;
  149. vector<string> args;
  150. args.push_back(im);
  151. instructions.push_back(Instruction_jmp(&ret,args));
  152. }
  153. else if(instr=="put")
  154. {
  155. cout<<"PUT"<<endl;
  156. string first,second;
  157. iss>>first>>second;
  158. vector<string> args;
  159. args.push_back(first);
  160. args.push_back(second);
  161. instructions.push_back(Instruction_put(&ret,args));
  162. }
  163. else if(instr=="crt")
  164. {
  165. cout<<"CRT"<<endl;
  166. string first,second;
  167. iss>>first>>second;
  168. vector<string> args;
  169. args.push_back(first);
  170. args.push_back(second);
  171. instructions.push_back(Instruction_crt(&ret,args));
  172. }
  173. else if(instr=="cra")
  174. {
  175. string im1,im2,im3;
  176. iss>>im1>>im2>>im3;
  177. vector<string> args;
  178. args.push_back(im1);
  179. args.push_back(im2);
  180. args.push_back(im3);
  181. instructions.push_back(Instruction_cra(&ret,args));
  182. }
  183. else if(instr=="del")
  184. {
  185. cout<<"DEL"<<endl;
  186. string im;
  187. iss>>im;
  188. vector<string> args;
  189. args.push_back(im);
  190. instructions.push_back(Instruction_del(&ret,args));
  191. }
  192. else if(instr=="ret")
  193. {
  194. cout<<"RET"<<endl;
  195. string mes;
  196. iss>>mes;
  197. vector<string> args;
  198. args.push_back(mes);
  199. instructions.push_back(Instruction_ret(&ret,args));
  200. }
  201. else if(instr=="wat")
  202. {
  203. cout<<"WAT"<<endl;
  204. vector<string> args;
  205. instructions.push_back(Instruction_wat(&ret,args));
  206. }
  207. else if(instr=="brk")
  208. {
  209. cout<<"BRK"<<endl;
  210. if(brk_ind!=-1)
  211. {
  212. cerr<<"Instruction brk used more than once."<<endl;
  213. return 1;
  214. }
  215. brk_ind=counter;
  216. vector<string> args;
  217. instructions.push_back(Instruction_brk(&ret,args));
  218. }
  219. else if(instr=="ebk")
  220. {
  221. cout<<"EBK"<<endl;
  222. if(ebk_ind!=-1)
  223. {
  224. cerr<<"Instruction ebk used more than once."<<endl;
  225. return 1;
  226. }
  227. ebk_ind=counter;
  228. vector<string> args;
  229. instructions.push_back(Instruction_ebk(&ret,args));
  230. }
  231. else if(instr=="cln")
  232. {
  233. vector<string> args;
  234. instructions.push_back(Instruction_cln(&ret,args));
  235. }
  236. else if(instr=="DST")
  237. {
  238. string res,first,second;
  239. iss>>res>>first>>second;
  240. vector<string> args;
  241. args.push_back(res);
  242. args.push_back(first);
  243. args.push_back(second);
  244. instructions.push_back(Instruction_DST(&ret,args));
  245. }
  246. else if(instr=="SIM")
  247. {
  248. string pos,first,second;
  249. iss>>pos>>first>>second;
  250. vector<string> args;
  251. args.push_back(pos);
  252. args.push_back(first);
  253. args.push_back(second);
  254. instructions.push_back(Instruction_SIM(&ret,args));
  255. }
  256. else if(instr=="GTA")
  257. {
  258. string res,first;
  259. iss>>res>>first;
  260. vector<string> args;
  261. args.push_back(res);
  262. args.push_back(first);
  263. instructions.push_back(Instruction_GTA(&ret,args));
  264. }
  265. else if(instr=="GAP")
  266. {
  267. string im1,im2,im3;
  268. iss>>im1>>im2>>im3;
  269. vector<string> args;
  270. args.push_back(im1);
  271. args.push_back(im2);
  272. args.push_back(im3);
  273. instructions.push_back(Instruction_GAP(&ret,args));
  274. }
  275. else
  276. {
  277. cerr<<"Unrecognized command in brainFile."<<endl;
  278. return 1;
  279. }
  280. if(ret)
  281. {
  282. cerr<<"Error while constructing the instructions"<<endl;
  283. return 1;
  284. }
  285. }
  286. brain= new Brain(instructions);
  287. brain->setInstructionBreakPointer(brk_ind);
  288. brain->setErrorBreakPointer(ebk_ind);
  289. brain->labels=labels;
  290. return 0;
  291. }
  292.  
  293. int Parser::parseImmediate(void* value, ArgumentType* type, string imed)
  294. {
  295. if(imed[0]=='*')
  296. {
  297. *type=STRING;
  298. *(string*)(value)=imed.substr(1);
  299. return 0;
  300. }
  301. istringstream iss(imed);
  302. int k;
  303. double d;
  304. iss>>k;
  305. if(iss.fail())
  306. {
  307. cerr<<"Non-numerical character found in non-string immediate."<<endl;
  308. return 1;
  309. }
  310. else
  311. {
  312. iss>>d;
  313. if(iss.fail())
  314. {
  315. string tmp;
  316. iss>>tmp;
  317. if(iss.fail())
  318. {
  319. *type=INT;
  320. *(int*)value=k;
  321. return 0;
  322. }
  323. else
  324. {
  325. cerr<<"Non-numerical character found in non-string immediate."<<endl;
  326. return 1;
  327. }
  328. }
  329. string tmp;
  330. iss>>tmp;
  331. if(iss.fail())
  332. {
  333. *type=DOUBLE;
  334. *(double*)value=k+d;
  335. return 0;
  336. }
  337. else
  338. {
  339. cerr<<"Non-numerical character found in non-string immediate."<<endl;
  340. return 1;
  341. }
  342. }
  343. return 0;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment