2323#define DBG (x ) \
2424 std::cerr << " [+] LINE " << __LINE__ \
2525 << " : " << (x) << std::endl
26- // TODO: add DBG everywhere and use a macro to control it
26+ // TODO: add DBG everywhere and use a macro to control all of them
2727
2828namespace utils {
2929
@@ -89,6 +89,8 @@ namespace syntax {
8989 // ------------------------------
9090
9191 std::string quote (std::string s) {
92+ // Note: quote will not change newlines back to
93+ // the form "\\n" and thus can produce multi-line strings
9294 std::string r;
9395 r += ' \" ' ;
9496 for (char c : s) {
@@ -1174,6 +1176,12 @@ namespace runtime {
11741176
11751177namespace serialization {
11761178
1179+ // ------------------------------
1180+ // serialization and de-serialization utilities
1181+ // TODO: functions in this namespace do not generally
1182+ // have safety checks (e.g. vector out-of-bound)
1183+ // ------------------------------
1184+
11771185 // Ser is the type of serialized program states (excluding the source code)
11781186 using Ser = std::deque<std::string>;
11791187 // every segment inside a Ser starts with ("{" | "<" | "[" | "(") and "<label>"
@@ -1729,6 +1737,7 @@ class State {
17291737 while (sin >> word) {
17301738 ser.push_back (word);
17311739 // special treatment of string values
1740+ // TODO: change this to a better implementation
17321741 if (word == " (" ) {
17331742 sin >> word;
17341743 ser.push_back (word);
@@ -1780,7 +1789,8 @@ class State {
17801789 resultLoc = std::stoi (ser.front ());
17811790 ser.pop_front (); // <rLoc>
17821791 ser.pop_front (); // "}"; this isn't really necessary here
1783- // don't do preAlloc because heap will be reconstructed
1792+ // _populateStaticAnalysisResults don't do preAlloc
1793+ // because heap will be reconstructed
17841794 // instead, do findPreAlloc to find out previously allocated locations
17851795 // this has to come after (5) literal boundary initialization
17861796 // because it needs numLiterals
@@ -2114,6 +2124,10 @@ class State {
21142124 }
21152125 void execute () {
21162126 // can choose different initial values here
2127+ // Note: when the program state is recovered
2128+ // from de-serialization, the value of gc_threshold
2129+ // is not preserved and will re-start from this initial value
2130+ // when calling execute().
21172131 int gc_threshold = numLiterals + 64 ;
21182132 while (step ()) {
21192133 int total = heap.size ();
@@ -2204,6 +2218,10 @@ class State {
22042218 expr->traverse (syntax::TraversalMode::TOP_DOWN, preAllocate);
22052219 }
22062220 else {
2221+ // it's ok to find any location with the same value
2222+ // and this is the only known (unobservable) difference
2223+ // between the original state and serialized-and-de-serialized state
2224+ // TODO: change to unique values
22072225 std::function<void (syntax::ExprNode*)> findPreAllocate =
22082226 [this ](syntax::ExprNode* e) -> void {
22092227 if (auto inode = dynamic_cast <syntax::IntegerNode*>(e)) {
@@ -2214,7 +2232,7 @@ class State {
22142232 std::get<runtime::Integer>(heap[i]).value == std::stoi (inode->val )
22152233 ) {
22162234 inode->loc = i;
2217- break ; // it's ok to find any location with the same value
2235+ break ;
22182236 }
22192237 }
22202238 }
@@ -2225,7 +2243,7 @@ class State {
22252243 std::get<runtime::String>(heap[i]).value == syntax::unquote (snode->val )
22262244 ) {
22272245 snode->loc = i;
2228- break ; // it's ok to find any location with the same value
2246+ break ;
22292247 }
22302248 }
22312249 }
@@ -2480,6 +2498,9 @@ class State {
24802498 return runtime::Integer (label);
24812499 }
24822500 else if (name == " .eval" ) {
2501+ // Note: evaluating a string as a program is one "step"
2502+ // and thus currently cannot be divided by the serialization
2503+ // in a fine-grained way.
24832504 _typecheck<runtime::String>(sl, args);
24842505 State state (std::get<runtime::String>(heap[args[0 ]]).value );
24852506 state.execute ();
@@ -2613,7 +2634,7 @@ class State {
26132634 if (relocation.contains (loc)) {
26142635 loc = relocation.at (loc);
26152636 }
2616- };
2637+ };
26172638 // traverse the stack
26182639 for (auto & layer : stack) {
26192640 // only frames "own" the environments
0 commit comments