2121#include < vector>
2222
2323#define DBG (x ) \
24- std::cerr << " [+] LINE " << __LINE__ \
25- << " : " << (x) << std::endl
24+ std::cerr << " [debug]" << (x) << std::endl
25+ #define DBG_WITH_LINE (x ) \
26+ std::cerr << " [debug] (LINE " << __LINE__ << " ): " << (x) << std::endl
2627// TODO: add DBG everywhere and use a macro to control all of them
2728
2829namespace utils {
@@ -226,8 +227,8 @@ namespace syntax {
226227 if (!hasDigit) {
227228 utils::panic (" lexer" , " incomplete integer literal" , startsl);
228229 }
229- // string literal
230230 }
231+ // string literal
231232 else if (ss.peekNext () == ' "' ) {
232233 text += ss.popNext ();
233234 bool complete = false ;
@@ -252,26 +253,26 @@ namespace syntax {
252253 if (!complete) {
253254 utils::panic (" lexer" , " incomplete string literal" , startsl);
254255 }
255- // variable / keyword
256256 }
257+ // variable / keyword
257258 else if (std::isalpha (ss.peekNext ()) || ss.peekNext () == ' _' ) {
258259 while (ss.hasNext () && (
259260 std::isalpha (ss.peekNext ()) || std::isdigit (ss.peekNext ()) ||
260261 ss.peekNext () == ' _' )) {
261262 text += ss.popNext ();
262263 }
263- // intrinsic
264264 }
265+ // intrinsic
265266 else if (ss.peekNext () == ' .' ) {
266267 while (ss.hasNext () && !(std::isspace (ss.peekNext ()) || ss.peekNext () == ' )' )) {
267268 text += ss.popNext ();
268269 }
269- // special symbol
270270 }
271+ // special symbol
271272 else if (std::string (" (){}@" ).find (ss.peekNext ()) != std::string::npos) {
272273 text += ss.popNext ();
273- // comment
274274 }
275+ // comment
275276 else if (ss.peekNext () == ' #' ) {
276277 while (ss.hasNext () && ss.peekNext () != ' \n ' ) {
277278 ss.popNext ();
@@ -1039,8 +1040,8 @@ namespace syntax {
10391040 }
10401041 else if (tokens[0 ].text == " if" ) {
10411042 return parseIf ();
1042- // check keywords before var to avoid recognizing keywords as vars
10431043 }
1044+ // check keywords before var to avoid recognizing keywords as vars
10441045 else if (isVariableToken (tokens[0 ])) {
10451046 return parseVariable ();
10461047 }
@@ -1789,6 +1790,9 @@ class State {
17891790 }
17901791public:
17911792 State (std::string origin) {
1793+ #ifdef DEBUG
1794+ DBG (" constructor" );
1795+ #endif
17921796 if (origin.size () == 0 ) {
17931797 utils::panic (" state constructor" , " empty origin string" );
17941798 }
@@ -1956,9 +1960,15 @@ class State {
19561960 heap(state.heap),
19571961 numLiterals(state.numLiterals),
19581962 resultLoc(state.resultLoc) {
1963+ #ifdef DEBUG
1964+ DBG (" copy constructor" );
1965+ #endif
19591966 _migrateAST (state, *this );
19601967 }
19611968 State& operator =(const State& state) {
1969+ #ifdef DEBUG
1970+ DBG (" copy assignment operator" );
1971+ #endif
19621972 if (this != &state) {
19631973 source = state.source ;
19641974 delete expr;
@@ -1978,9 +1988,15 @@ class State {
19781988 heap (std::move(state.heap)),
19791989 numLiterals (state.numLiterals),
19801990 resultLoc (state.resultLoc) {
1991+ #ifdef DEBUG
1992+ DBG (" move constructor" );
1993+ #endif
19811994 state.expr = nullptr ;
19821995 }
19831996 State& operator =(State&& state) {
1997+ #ifdef DEBUG
1998+ DBG (" move assignment operator" );
1999+ #endif
19842000 if (this != &state) {
19852001 source = std::move (state.source );
19862002 delete expr;
@@ -1994,6 +2010,9 @@ class State {
19942010 return *this ;
19952011 }
19962012 ~State () {
2013+ #ifdef DEBUG
2014+ DBG (" destructor" );
2015+ #endif
19972016 if (expr != nullptr ) {
19982017 delete expr;
19992018 }
@@ -2740,18 +2759,18 @@ int main(int argc, char** argv) {
27402759 << serialization::join (
27412760 serialization::valueToSentence (state.getResult (), state.getExpr ())) << std::endl;
27422761#else
2743- // test the ( copy & move) (constructors & assignment operators) for State
2762+ // test the copy control operators for State
27442763 std::string source = readSource (argv[1 ]);
2745- State state1 (source);
2746- State state2 (state1); // copy constructor
2764+ State state1 (source); // constructor
2765+ State state2 (state1); // copy constructor
27472766 state1.clear ();
27482767 State state3 (std::move (state2)); // move constructor
27492768 state2.clear ();
2750- State state4 (source);
2751- state4 = state3; // copy assignment operator
2769+ State state4 (source); // constructor
2770+ state4 = state3; // copy assignment operator
27522771 state3.clear ();
2753- State state5 (source);
2754- state5 = std::move (state4); // move assignment operator
2772+ State state5 (source); // constructor
2773+ state5 = std::move (state4); // move assignment operator
27552774 state4.clear ();
27562775 state5.execute ();
27572776 std::cout << " <end-of-stdout>\n "
0 commit comments