1313#endif
1414
1515using namespace std ;
16- void sleep (unsigned msec) {
16+ void sleep (unsigned msec)
17+ {
1718 struct timespec sleepTime;
1819 sleepTime.tv_sec = msec / 1000 ;
1920 sleepTime.tv_nsec = (msec % 1000 ) * 1000000 ;
2021 nanosleep (&sleepTime, NULL );
2122}
2223
2324int main (int argc, char *argv[])
24- { (void )(argc);(void )(argv);
25+ {
26+ (void )(argc);
27+ (void )(argv);
2528 string text = " " ;
2629
2730 const map<std::string, Token> keywords = {
28- {" NUM" , {OC::NUM, 0 , 0 , 0 }}, // {opcode, arity, precedence, value}
29- {" VAR" , {OC::VAR, 0 , 0 , 0 }},
30- {" *" , {OC::MUL, 2 , 3 , 0 }},
31- {" /" , {OC::DIV, 2 , 3 , 0 }},
32- {" +" , {OC::ADD, 2 , 4 , 0 }},
33- {" -" , {OC::SUB, 2 , 4 , 0 }},
34- {" <" , {OC::LT, 2 , 6 , 0 }},
35- {" <=" , {OC::LE, 2 , 6 , 0 }},
36- {" >" , {OC::GT, 2 , 6 , 0 }},
37- {" >=" , {OC::GE, 2 , 6 , 0 }},
38- {" ==" , {OC::EQ, 2 , 7 , 0 }},
39- {" !=" , {OC::NE, 2 , 7 , 0 }},
40- {" ?" , {OC::QU, -1 , 13 , 0 }},
41- {" :" , {OC::COL, 3 , 13 , 0 }},
42- {" =" , {OC::ASS, 2 , 14 , 0 }},
43- {" (" , {OC::PAR_L,0 , 0 , 0 }},
44- {" )" , {OC::PAR_R,0 , 0 , 0 }},
45- {" NIL" , {OC::NIL, 0 , 0 , 0 }}
46- };
31+ {" NUM" , {OC::NUM, 0 , 0 , 0 }}, // {opcode, arity, precedence, value}
32+ {" VAR" , {OC::VAR, 0 , 0 , 0 }},
33+ {" *" , {OC::MUL, 2 , 3 , 0 }},
34+ {" /" , {OC::DIV, 2 , 3 , 0 }},
35+ {" +" , {OC::ADD, 2 , 4 , 0 }},
36+ {" -" , {OC::SUB, 2 , 4 , 0 }},
37+ {" <" , {OC::LT, 2 , 6 , 0 }},
38+ {" <=" , {OC::LE, 2 , 6 , 0 }},
39+ {" >" , {OC::GT, 2 , 6 , 0 }},
40+ {" >=" , {OC::GE, 2 , 6 , 0 }},
41+ {" ==" , {OC::EQ, 2 , 7 , 0 }},
42+ {" !=" , {OC::NE, 2 , 7 , 0 }},
43+ {" ?" , {OC::QU, -1 , 13 , 0 }},
44+ {" :" , {OC::COL, 3 , 13 , 0 }},
45+ {" =" , {OC::ASS, 2 , 14 , 0 }},
46+ {" (" , {OC::PAR_L, 0 , 0 , 0 }},
47+ {" )" , {OC::PAR_R, 0 , 0 , 0 }},
48+ {" NIL" , {OC::NIL, 0 , 0 , 0 }}};
4749
4850 VarTable vvv;
4951
@@ -53,24 +55,26 @@ int main(int argc, char *argv[])
5355
5456 // SPEEDTEST
5557
56- if (1 ==2 ){ // 1==1 if you want it!
58+ if (1 == 2 )
59+ { // 1==1 if you want it!
5760 text = " b=a*a*a/100000000" ;
58- tokenList = makeTokenList (text, & keywords, & vvv);
61+ tokenList = makeTokenList (text, keywords, vvv);
5962 // tokensRPN = p3.makeRPN(tokenList);
6063 int n = 0 ;
61- while (n<500000 ){
64+ while (n < 500000 )
65+ {
6266 std::cout << " \033 [H\033 [2J\033 [3J" ;
6367 vvv.setVar (0 , n);
6468 // calc.calcandprint(tokensRPN, false);
6569 n++;
6670 }
67- sleep (100 ); // without sleep, cout gets in trouble in this final phase
71+ sleep (100 ); // without sleep, cout gets in trouble in this final phase
6872 // calc.calcandprint(tokensRPN, true);
6973 return 0 ;
7074 }
7175 // SPEEDTEST END
7276
73- vector<string> inputs = {" " ," type it yourself" ,
77+ vector<string> inputs = {" " , " type it yourself" ,
7478 " 4/5" ,
7579 " 2<3" ,
7680 " 2>3" ,
@@ -101,40 +105,52 @@ int main(int argc, char *argv[])
101105 cout << " \n choose: (0 to exit)\n "
102106 " \n 1. enter expression" ;
103107
104- for (long unsigned int i = 2 ; i<inputs.size (); i++)
105- cout << " \n " << i << " . " << inputs[i] ;
108+ for (long unsigned int i = 2 ; i < inputs.size (); i++)
109+ cout << " \n "
110+ << i << " . " << inputs[i];
106111 cout << endl;
107112 cout << " \n vartable at start:" ;
108113 vvv.printVarTable ();
109- while (true ) {
114+ while (true )
115+ {
110116 cout << " \n choice (0 to exit, 1 to enter expression manually, r to toggle reporting level) ==> " ;
111- cin >> ch; cin.ignore ();
112- if (ch!=" " ){
113- if (isNumeric (ch) ){
117+ cin >> ch;
118+ cin.ignore ();
119+ if (ch != " " )
120+ {
121+ if (isNumeric (ch))
122+ {
114123 choice = stoi (ch);
115- if (choice < inputs.size ()){
116- if (choice == 0 ) return 0 ;
117- if (choice == 1 ) {
124+ if (choice < inputs.size ())
125+ {
126+ if (choice == 0 )
127+ return 0 ;
128+ if (choice == 1 )
129+ {
118130 getline (cin, text);
119- if (text == " " ) continue ;
131+ if (text == " " )
132+ continue ;
120133 }
121- else text = inputs[choice];
122- cout << " text in:\t\t " << text << " \t\t " ;
134+ else
135+ text = inputs[choice];
136+ cout << " \n text in:\t\t " << text << " \t\t " ;
123137
124- vector<Token> tokenList = makeTokenList (text,
125- &keywords,
126- &vvv);
138+ // directly from expression to RPN:
139+ vector<RPNToken> tokensRPN = makeRPN (text, keywords, vvv);
127140
128- vector<RPNToken> tokensRPN = makeRPN (tokenList);
141+ // separate calls:
142+ // vector<Token> tokenList = makeTokenList(text, keywords, vvv);
143+ // vector<RPNToken> tokensRPN = makeRPN(tokenList);
129144
130145 calcandprint (tokensRPN, &vvv, true );
131-
132- } else cout << " choice not allowed" << endl;
133- }else
134- if (ch==" r" )
135- vvv.errorlevel = 1 - vvv.errorlevel ;
146+ }
136147 else
137- cout << " no number. retry" << endl;
148+ cout << " choice not allowed" << endl;
149+ }
150+ else if (ch == " r" )
151+ vvv.errorlevel = 1 - vvv.errorlevel ;
152+ else
153+ cout << " no number. retry" << endl;
138154 }
139155 };
140156
0 commit comments