44#include < iostream>
55#include < string>
66#include < ctype.h>
7+ #include < time.h>
8+
79#include " stringop.h"
810using namespace std ;
911
@@ -20,13 +22,15 @@ int* moveArrayLeft(int* arr, int start, int end) {
2022}
2123
2224int evalString (std::string o, const int * vars, bool dev) {
25+ srand (time (NULL ));
2326 /* the vars array is the array where variables are stored.
2427 This has been made for being able to make arithmetics with
2528 variables of the BASIC Interpreter. If you don't need that,
2629 just put an empty array.*/
2730
2831 int * numbers = new int [100 ];
2932 int * ops = new int [99 ]; // 0 = Nothing | 1 = + | 2 = - | 3 = * | 4 = /
33+ int * parentheses = new int [100 ]; // 0 = None | 1 = ( | 2 = )
3034 // Supported operators : * / + -
3135 // Parentheses aren't supported.
3236 char c;
@@ -41,7 +45,7 @@ int evalString(std::string o, const int* vars, bool dev) {
4145 return 0 ;
4246 }
4347 c = o.at (i);
44- if (previousType == 0 ) {
48+ if (previousType == 0 || previousType == 2 ) {
4549 if (isoperator (c)) {
4650 cout << " [stringop.cpp] Syntax Error! (Misplaced operator)" << endl;
4751 return 0 ;
@@ -50,17 +54,27 @@ int evalString(std::string o, const int* vars, bool dev) {
5054 numbers[ptr] = c - ' 0' ;
5155 continue ;
5256 } else if (isalpha (c)) {
53- previousType = 3 ;
54- numbers[ptr] = vars[toupper (c) - ' A' ];
55- continue ;
57+ if (isupper (c)) { // User defined variables
58+ previousType = 3 ;
59+ numbers[ptr] = vars[toupper (c) - ' A' ];
60+ continue ;
61+ } else if (islower (c)) { // System variables
62+ previousType = 3 ;
63+ if (c == ' r' )
64+ numbers[ptr] = rand () % 1000 ;
65+ else {
66+ cout << " [stringop.cpp] Syntax Error! (Invalid system variable)" << endl;
67+ return 0 ;
68+ }
69+ }
5670 } else if (c == ' ' )
5771 continue ;
5872 else {
5973 cout << " [stringop.cpp] Syntax Error! (Unknown character)" << endl;
6074 return 0 ;
6175 }
6276
63- } else if (previousType == 1 ) {
77+ } else if (previousType == 1 || previousType == 3 ) {
6478 if (isoperator (c)) {
6579 previousType = 2 ;
6680 if (c == ' +' )
@@ -86,47 +100,6 @@ int evalString(std::string o, const int* vars, bool dev) {
86100 cout << " [stringop.cpp] Syntax Error! (Unknown character)" << endl;
87101 return 0 ;
88102 }
89- } else if (previousType == 2 ) {
90- if (isoperator (c)) {
91- cout << " [stringop.cpp] Syntax Error! (Misplaced operator)" << endl;
92- return 0 ;
93- } else if (isdigit (c)) {
94- previousType = 1 ;
95- numbers[ptr] = c - ' 0' ;
96- continue ;
97- } else if (isalpha (c)) {
98- previousType = 3 ;
99- numbers[ptr] = vars[toupper (c) - ' A' ];
100- continue ;
101- } else if (c == ' ' )
102- continue ;
103- else {
104- cout << " [stringop.cpp] Syntax Error! (Unknown character)" << endl;
105- return 0 ;
106- }
107- } else if (previousType == 3 ) {
108- if (isoperator (c)) {
109- previousType = 2 ;
110- if (c == ' +' )
111- ops[ptr] = 1 ;
112- if (c == ' -' )
113- ops[ptr] = 2 ;
114- if (c == ' *' )
115- ops[ptr] = 3 ;
116- if (c == ' /' )
117- ops[ptr] = 4 ;
118-
119- ++ptr;
120- continue ;
121- } else if (isalpha (c)) {
122- cout << " [stringop.cpp] Syntax Error! (Misplaced variable)" << endl;
123- return 0 ;
124- } else if (c == ' ' )
125- continue ;
126- else {
127- cout << " [stringop.cpp] Syntax Error! (Unknown character)" << endl;
128- return 0 ;
129- }
130103 }
131104 }
132105
0 commit comments