Advertisement
jamieyello

Main SBInterpret

Feb 4th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. using namespace std;
  7. stringstream convert;
  8.  
  9. //Type
  10. //1 func or return value (0-1)
  11. //2 minimum number of required values (>=0)
  12. //3 max number of required values (>=0)
  13. //4 takes in string
  14. //5 takes in number
  15.  
  16. //This probably belongs in some kind of header
  17. extern const std:: string globclist[] = { "PRINT", "CLS", "END" };
  18. extern const int globcnum = 3;
  19. extern const char globmlist[] = { '%', '(', '(', ')', '*', '+', '-', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  20. extern const int globmnum = 18;
  21. extern int mathCache[];
  22. extern std::string gPassString = "";
  23. // % 37
  24. // ( 40
  25. // ) 41
  26. // * 42
  27. // + 43
  28. // - 45
  29. // / 47
  30. // 0-9 48-57
  31. // = 61
  32.  
  33. int const SBarrSize = 10;
  34. std::string filePath = "run.txt";
  35. int line;
  36. int ret;
  37. bool stopr;
  38. int pLength;
  39. //0 End of program
  40. //1 END command
  41. //2 Syntax error
  42. std::string us;
  43. int u;
  44. char uc;
  45. extern int ug;
  46. bool synerr;
  47. std::string SBarr[SBarrSize];
  48.  
  49. //Function prototypes
  50. void printPRG(std::string[], int);
  51. bool isLetter(char);
  52. bool isNumber(char);
  53. bool isFunc(std::string);
  54. bool isNumeric(std::string);
  55. int getElem(std::string);
  56. std::string caps(std::string);
  57. std::string numToString(double);
  58. int stringToNum(std::string);
  59.  
  60. void PRINT(std::string);
  61. void CLS();
  62.  
  63. double solve(std::string);
  64. extern std::string gFormulaToParse;
  65. extern int gFormulaToParsep = 0;
  66. std::string getParenthesis(std::string);
  67. double getNu(std::string, int &);
  68.  
  69. void flushCache(double &, double &, int &, int &);
  70.  
  71.  
  72. int main()
  73. {
  74. //cout << solve("7*7*7+7") << endl;
  75. //system("pause");
  76. //Setting the array use to be it's own function, but arr[] didn't like being referenced with "reader >>".
  77. ifstream reader(filePath);
  78. if (!reader.is_open()){
  79. ofstream writer(filePath);
  80. writer << "end";
  81. writer.close();
  82. ifstream reader(filePath);
  83. }
  84.  
  85.  
  86. us = "";
  87. do{
  88. reader >> us;
  89. us = caps(us);
  90. SBarr[pLength] = us;
  91. if (SBarr[pLength] == "END"){ stopr = 1; }
  92. else{ pLength += 1; }
  93. } while (!stopr);
  94. reader.close();
  95.  
  96.  
  97.  
  98. //Running the SmileBASIC code (code messy as I don't have (or know of) an alternative to switch, which doesn't work with strings)
  99. //"synerr = 0; "
  100. for (line = 0; line <= pLength; line++){
  101. synerr = 1; //If synerr == 0 by the end nothing was triggered which means a typo was made
  102.  
  103. //console
  104. if (SBarr[line] == "CLS"){ CLS(); synerr = 0; }
  105.  
  106. if (SBarr[line] == "PRINT"){
  107. if (getElem(SBarr[line + 1]) == 0){ synerr = 0; PRINT(""); }
  108. if (getElem(SBarr[line + 1]) == 1){
  109. synerr = 0;
  110. PRINT(numToString(solve(SBarr[line + 1])));
  111. line++;
  112. }
  113.  
  114.  
  115.  
  116. }
  117.  
  118. //system
  119. if (SBarr[line] == "END"){ ret = 1; synerr = 0; break; }
  120.  
  121. //syntax error check
  122. if (synerr){ ret = 2; break; }
  123. }
  124.  
  125.  
  126. switch (ret){
  127. case 0:
  128. cout << "End of program" << endl;
  129. break;
  130.  
  131. case 1:
  132. cout << "END at element " << line+1 << endl;
  133. if (line == 0){ cout << "Open 'run.txt' to edit your program, save it and try again.\nSee 'Functionality (ReadMe).txt' for usage." << endl; }
  134. break;
  135.  
  136. case 2:
  137. cout << "Syntax error on element " << line+1 << endl;
  138. break;
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. // << endl << "Break in element " << line << endl;
  146.  
  147. system("PAUSE");
  148. return 0;
  149. }
  150.  
  151. void printPRG(std::string arr[], int size){
  152. for (int u = 0; u<size; u++){
  153. cout << "LINE " << u << " " << arr[u] << endl;
  154. }
  155. return;
  156. }
  157.  
  158. std::string caps(std::string us){
  159. //97-122 lowercase
  160. //65-90 uppercase
  161. std::string usr = "";
  162. for (int u = 0; u < us.size(); u++){
  163. if (static_cast<int>(us[u]) > 96 && static_cast<int>(us[u]) < 123){ us[u] -= 32; }
  164. }
  165. return us;
  166. }
  167.  
  168. //Returns the global string "gPassString" along with it's own integer
  169. //Returns;
  170. //0 a function
  171. //1 number
  172. //2 string
  173.  
  174.  
  175. int getElem(std::string str){
  176. gPassString = "";
  177. if (isFunc(caps(str))){ return 0; };
  178. if (isNumeric(caps(str))){ gPassString = numToString(solve(caps(str))); return 1; }
  179. gPassString = "0";
  180. return 0;
  181. }
  182.  
  183. bool isFunc(std::string us){
  184. bool ret = 0;
  185. for (int u = 0; u < globcnum; u++){
  186. if (us == globclist[u]){ ret = 1; break; }
  187. }
  188. return ret;
  189. }
  190.  
  191. bool isNumeric(std::string us){
  192. int u = 0;
  193. int mathchars = 0;
  194. int u1 = 0;
  195. //Check to make sure the string is a numeric expression
  196. for (u = 0; u < globmnum; u++){ //Going through the valid characters
  197. for (u1 = 0; u1 < us.size(); u1++){ //Going through the string
  198. if (us[u1] == globmlist[u]){ mathchars += 1; }
  199. }
  200. }
  201. return (mathchars==us.size());
  202. }
  203.  
  204. bool isLetter(char uc){
  205. bool ret = 0;
  206. if ((static_cast<int>(uc) > 96 && static_cast<int>(uc) < 123) || (static_cast<int>(uc) > 64 && static_cast<int>(uc) < 91)){
  207. ret = 1;
  208. }
  209. else{
  210. ret = 0;
  211. }
  212. return ret;
  213. }
  214.  
  215. bool isNumber(char uc){
  216. bool ret = 0;
  217. if (static_cast<int>(uc) > 47 && static_cast<int>(uc) < 58){
  218. ret = 1;
  219. }
  220. else{
  221. ret = 0;
  222. }
  223. return ret;
  224. }
  225.  
  226.  
  227.  
  228. std::string numToString(double Number){
  229. std::string Result;
  230. ostringstream convert;
  231. convert << Number;
  232. return convert.str();
  233. }
  234.  
  235. int stringToNum(std::string myString){
  236. return atoi(myString.c_str());
  237. }
  238.  
  239. //Decimals bugged somewhat
  240. double getNu(std::string str, int &strPos){
  241. double result = 0;
  242. double decimalCount = 1;
  243. int go = 1;
  244. //char strc = str[3]; Uhhh
  245.  
  246. while (go){
  247. int strv = str[strPos];
  248. strv -= '0';
  249. if (isNumber(str[strPos])){ result *= 10; result += strv; strPos++; }
  250. else { break; }
  251. }
  252. if (str[strPos] == '.'){
  253. strPos++;
  254. go = 1;
  255. while (go){
  256. int strv = str[strPos];
  257. strv -= '0';
  258. if (isNumber(str[strPos])){ decimalCount *= 0.1; result += decimalCount*strv; strPos++; }
  259. else { go = 0; }
  260. }
  261. }
  262. return result;
  263. }
  264.  
  265.  
  266. void flushCache(double &addArr, double &cache, int &positive, int &addArrPos){
  267.  
  268. }
  269.  
  270. //This section of the code converts a math string (something like "(4+97)*7") and returns a single value. It will
  271. //go through the code and create an array called addArr[], which will contain all numbers to be added together.
  272. //Multiplication and division along with parenthesis content will be simplified before being added to the array,
  273. //keeping the order of operations intact.
  274.  
  275. double solve(std::string str){
  276. double result = 0;
  277. double cache = 0;
  278. double temp = 0;
  279. double addArr[50];
  280. for (int i = 0; i < 50; i++){addArr[i] = 0;}
  281. //IMPORTANT, a better way to calculate the size needed for this array needs to be implemented
  282. int addArrPos = 0;
  283. int positive = 1;
  284. int strPos = 0;
  285. int go = 1;
  286.  
  287.  
  288. //flushCache(addArr[addArrPos], cache, positive, addArrPos);
  289.  
  290. while (go){
  291. if (isNumber(str[strPos])){ temp = getNu(str, strPos); /*cout << "num = " << temp << endl;*/ addArr[addArrPos] += temp; }; //if it gets a number it adds it to the cache
  292. if (str[strPos] == '-'){ addArr[addArrPos] = cache*positive; cache = 0; positive = -1; /*cout << "-" << endl;*/ addArrPos++; strPos++; } //if it gets a symbol it flushes the cache
  293. if (str[strPos] == '+'){ addArr[addArrPos] = cache*positive; cache = 0; positive = 1; /* cout << "+" << endl;*/ addArrPos++; strPos++; }
  294. if (str[strPos] == '*'){ strPos++; cache = cache*getNu(str, strPos); }
  295. if (str[strPos] == '/'){}
  296.  
  297. if (strPos >= str.size()){ result += cache*positive; go = 0; }
  298. }
  299.  
  300. //cout << "pass" << endl;
  301. //system("pause");
  302.  
  303. for (int i = 0; i <= addArrPos; i++){result += addArr[i];}
  304. return result;
  305. }
  306.  
  307.  
  308. //This function was designed to return the contents of the first detected parenthesis, to be solved by recursion.
  309. std::string getParenthesis(std::string str){
  310. int firstParenthesis = -1;
  311. int closingParenthesis = -1;
  312. int closingParenthesisNeeded = 0;
  313. int i;
  314.  
  315. for (i = 0; i < str.size(); i++){
  316. if (static_cast<int>(str[i]) == 40){ firstParenthesis = i; closingParenthesisNeeded = 1; break; }
  317. }
  318.  
  319. while (closingParenthesisNeeded){
  320. i++;
  321. if (str[i] == '('){ closingParenthesisNeeded++; }
  322. if (str[i] == ')'){ closingParenthesisNeeded--; }
  323.  
  324. }
  325. if (firstParenthesis > -1) {
  326. closingParenthesis = i;
  327.  
  328.  
  329.  
  330. if (firstParenthesis != -1){
  331. std::string str2 = str.replace(closingParenthesis, str.size(), "");
  332. std::string str1 = str.replace(0, firstParenthesis+1, "");
  333. }
  334. }
  335. return str;
  336. }
  337.  
  338.  
  339.  
  340.  
  341.  
  342. //SmileBASIC functions
  343. //Graphics related functions will be updated to run on openGL when it's implemented
  344. //Could probably go in their own file for organization ¯\_(ツ)_/¯
  345.  
  346. void CLS(){
  347. system("CLS");
  348. }
  349.  
  350. void PRINT(std::string str){
  351. cout << str << endl;
  352. }
  353.  
  354. void PRINT(double str){
  355. cout << str << endl;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement