Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- stringstream convert;
- //Type
- //1 func or return value (0-1)
- //2 minimum number of required values (>=0)
- //3 max number of required values (>=0)
- //4 takes in string
- //5 takes in number
- //This probably belongs in some kind of header
- extern const std:: string globclist[] = { "PRINT", "CLS", "END" };
- extern const int globcnum = 3;
- extern const char globmlist[] = { '%', '(', '(', ')', '*', '+', '-', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- extern const int globmnum = 18;
- extern int mathCache[];
- extern std::string gPassString = "";
- // % 37
- // ( 40
- // ) 41
- // * 42
- // + 43
- // - 45
- // / 47
- // 0-9 48-57
- // = 61
- int const SBarrSize = 10;
- std::string filePath = "run.txt";
- int line;
- int ret;
- bool stopr;
- int pLength;
- //0 End of program
- //1 END command
- //2 Syntax error
- std::string us;
- int u;
- char uc;
- extern int ug;
- bool synerr;
- std::string SBarr[SBarrSize];
- //Function prototypes
- void printPRG(std::string[], int);
- bool isLetter(char);
- bool isNumber(char);
- bool isFunc(std::string);
- bool isNumeric(std::string);
- int getElem(std::string);
- std::string caps(std::string);
- std::string numToString(double);
- int stringToNum(std::string);
- void PRINT(std::string);
- void CLS();
- double solve(std::string);
- extern std::string gFormulaToParse;
- extern int gFormulaToParsep = 0;
- std::string getParenthesis(std::string);
- double getNu(std::string, int &);
- void flushCache(double &, double &, int &, int &);
- int main()
- {
- //cout << solve("7*7*7+7") << endl;
- //system("pause");
- //Setting the array use to be it's own function, but arr[] didn't like being referenced with "reader >>".
- ifstream reader(filePath);
- if (!reader.is_open()){
- ofstream writer(filePath);
- writer << "end";
- writer.close();
- ifstream reader(filePath);
- }
- us = "";
- do{
- reader >> us;
- us = caps(us);
- SBarr[pLength] = us;
- if (SBarr[pLength] == "END"){ stopr = 1; }
- else{ pLength += 1; }
- } while (!stopr);
- reader.close();
- //Running the SmileBASIC code (code messy as I don't have (or know of) an alternative to switch, which doesn't work with strings)
- //"synerr = 0; "
- for (line = 0; line <= pLength; line++){
- synerr = 1; //If synerr == 0 by the end nothing was triggered which means a typo was made
- //console
- if (SBarr[line] == "CLS"){ CLS(); synerr = 0; }
- if (SBarr[line] == "PRINT"){
- if (getElem(SBarr[line + 1]) == 0){ synerr = 0; PRINT(""); }
- if (getElem(SBarr[line + 1]) == 1){
- synerr = 0;
- PRINT(numToString(solve(SBarr[line + 1])));
- line++;
- }
- }
- //system
- if (SBarr[line] == "END"){ ret = 1; synerr = 0; break; }
- //syntax error check
- if (synerr){ ret = 2; break; }
- }
- switch (ret){
- case 0:
- cout << "End of program" << endl;
- break;
- case 1:
- cout << "END at element " << line+1 << endl;
- if (line == 0){ cout << "Open 'run.txt' to edit your program, save it and try again.\nSee 'Functionality (ReadMe).txt' for usage." << endl; }
- break;
- case 2:
- cout << "Syntax error on element " << line+1 << endl;
- break;
- }
- // << endl << "Break in element " << line << endl;
- system("PAUSE");
- return 0;
- }
- void printPRG(std::string arr[], int size){
- for (int u = 0; u<size; u++){
- cout << "LINE " << u << " " << arr[u] << endl;
- }
- return;
- }
- std::string caps(std::string us){
- //97-122 lowercase
- //65-90 uppercase
- std::string usr = "";
- for (int u = 0; u < us.size(); u++){
- if (static_cast<int>(us[u]) > 96 && static_cast<int>(us[u]) < 123){ us[u] -= 32; }
- }
- return us;
- }
- //Returns the global string "gPassString" along with it's own integer
- //Returns;
- //0 a function
- //1 number
- //2 string
- int getElem(std::string str){
- gPassString = "";
- if (isFunc(caps(str))){ return 0; };
- if (isNumeric(caps(str))){ gPassString = numToString(solve(caps(str))); return 1; }
- gPassString = "0";
- return 0;
- }
- bool isFunc(std::string us){
- bool ret = 0;
- for (int u = 0; u < globcnum; u++){
- if (us == globclist[u]){ ret = 1; break; }
- }
- return ret;
- }
- bool isNumeric(std::string us){
- int u = 0;
- int mathchars = 0;
- int u1 = 0;
- //Check to make sure the string is a numeric expression
- for (u = 0; u < globmnum; u++){ //Going through the valid characters
- for (u1 = 0; u1 < us.size(); u1++){ //Going through the string
- if (us[u1] == globmlist[u]){ mathchars += 1; }
- }
- }
- return (mathchars==us.size());
- }
- bool isLetter(char uc){
- bool ret = 0;
- if ((static_cast<int>(uc) > 96 && static_cast<int>(uc) < 123) || (static_cast<int>(uc) > 64 && static_cast<int>(uc) < 91)){
- ret = 1;
- }
- else{
- ret = 0;
- }
- return ret;
- }
- bool isNumber(char uc){
- bool ret = 0;
- if (static_cast<int>(uc) > 47 && static_cast<int>(uc) < 58){
- ret = 1;
- }
- else{
- ret = 0;
- }
- return ret;
- }
- std::string numToString(double Number){
- std::string Result;
- ostringstream convert;
- convert << Number;
- return convert.str();
- }
- int stringToNum(std::string myString){
- return atoi(myString.c_str());
- }
- //Decimals bugged somewhat
- double getNu(std::string str, int &strPos){
- double result = 0;
- double decimalCount = 1;
- int go = 1;
- //char strc = str[3]; Uhhh
- while (go){
- int strv = str[strPos];
- strv -= '0';
- if (isNumber(str[strPos])){ result *= 10; result += strv; strPos++; }
- else { break; }
- }
- if (str[strPos] == '.'){
- strPos++;
- go = 1;
- while (go){
- int strv = str[strPos];
- strv -= '0';
- if (isNumber(str[strPos])){ decimalCount *= 0.1; result += decimalCount*strv; strPos++; }
- else { go = 0; }
- }
- }
- return result;
- }
- void flushCache(double &addArr, double &cache, int &positive, int &addArrPos){
- }
- //This section of the code converts a math string (something like "(4+97)*7") and returns a single value. It will
- //go through the code and create an array called addArr[], which will contain all numbers to be added together.
- //Multiplication and division along with parenthesis content will be simplified before being added to the array,
- //keeping the order of operations intact.
- double solve(std::string str){
- double result = 0;
- double cache = 0;
- double temp = 0;
- double addArr[50];
- for (int i = 0; i < 50; i++){addArr[i] = 0;}
- //IMPORTANT, a better way to calculate the size needed for this array needs to be implemented
- int addArrPos = 0;
- int positive = 1;
- int strPos = 0;
- int go = 1;
- //flushCache(addArr[addArrPos], cache, positive, addArrPos);
- while (go){
- 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
- if (str[strPos] == '-'){ addArr[addArrPos] = cache*positive; cache = 0; positive = -1; /*cout << "-" << endl;*/ addArrPos++; strPos++; } //if it gets a symbol it flushes the cache
- if (str[strPos] == '+'){ addArr[addArrPos] = cache*positive; cache = 0; positive = 1; /* cout << "+" << endl;*/ addArrPos++; strPos++; }
- if (str[strPos] == '*'){ strPos++; cache = cache*getNu(str, strPos); }
- if (str[strPos] == '/'){}
- if (strPos >= str.size()){ result += cache*positive; go = 0; }
- }
- //cout << "pass" << endl;
- //system("pause");
- for (int i = 0; i <= addArrPos; i++){result += addArr[i];}
- return result;
- }
- //This function was designed to return the contents of the first detected parenthesis, to be solved by recursion.
- std::string getParenthesis(std::string str){
- int firstParenthesis = -1;
- int closingParenthesis = -1;
- int closingParenthesisNeeded = 0;
- int i;
- for (i = 0; i < str.size(); i++){
- if (static_cast<int>(str[i]) == 40){ firstParenthesis = i; closingParenthesisNeeded = 1; break; }
- }
- while (closingParenthesisNeeded){
- i++;
- if (str[i] == '('){ closingParenthesisNeeded++; }
- if (str[i] == ')'){ closingParenthesisNeeded--; }
- }
- if (firstParenthesis > -1) {
- closingParenthesis = i;
- if (firstParenthesis != -1){
- std::string str2 = str.replace(closingParenthesis, str.size(), "");
- std::string str1 = str.replace(0, firstParenthesis+1, "");
- }
- }
- return str;
- }
- //SmileBASIC functions
- //Graphics related functions will be updated to run on openGL when it's implemented
- //Could probably go in their own file for organization ¯\_(ツ)_/¯
- void CLS(){
- system("CLS");
- }
- void PRINT(std::string str){
- cout << str << endl;
- }
- void PRINT(double str){
- cout << str << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement