diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-07-18 15:22:49 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-07-18 15:22:49 -0700 |
commit | 762b099ef6c6ad5b6b61d29e473baa6df8d64ab9 (patch) | |
tree | 8075befbcc8ef2784616c4937793ebed9d21123d /html/081run_interactive.cc.html | |
parent | 614ea44bc2708a687ba10b162a6dc70e48dfac59 (diff) | |
download | mu-762b099ef6c6ad5b6b61d29e473baa6df8d64ab9.tar.gz |
1818
Diffstat (limited to 'html/081run_interactive.cc.html')
-rw-r--r-- | html/081run_interactive.cc.html | 112 |
1 files changed, 66 insertions, 46 deletions
diff --git a/html/081run_interactive.cc.html b/html/081run_interactive.cc.html index 5183cee6..54b1456c 100644 --- a/html/081run_interactive.cc.html +++ b/html/081run_interactive.cc.html @@ -49,8 +49,10 @@ recipe main [ <span class="Comment"># result is null</span> <span class="traceContains">+mem: storing 0 in location 1</span> -<span class="Comment">//: run code in 'interactive mode', i.e. with warnings off, and recording</span> -<span class="Comment">//: output in case we want to print it to screen</span> +<span class="Comment">//: run code in 'interactive mode', i.e. with warnings off and return:</span> +<span class="Comment">//: stringified output in case we want to print it to screen</span> +<span class="Comment">//: any warnings encountered</span> +<span class="Comment">//: simulated screen any prints went to</span> <span class="Delimiter">:(before "End Primitive Recipe Declarations")</span> RUN_INTERACTIVE<span class="Delimiter">,</span> <span class="Delimiter">:(before "End Primitive Recipe Numbers")</span> @@ -59,11 +61,13 @@ Recipe_ordinal[<span class="Constant">"run-interactive"</span>] = RUN_ <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> case RUN_INTERACTIVE: <span class="Delimiter">{</span> assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span> - products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span> + products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">3</span><span class="Delimiter">);</span> bool new_code_pushed_to_stack = run_interactive<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span> if <span class="Delimiter">(</span>!new_code_pushed_to_stack<span class="Delimiter">)</span> <span class="Delimiter">{</span> products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>warnings_from_trace<span class="Delimiter">());</span> + products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> + clean_up_interactive<span class="Delimiter">();</span> <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Comment">// done with this instruction</span> <span class="Delimiter">}</span> else <span class="Delimiter">{</span> @@ -73,49 +77,53 @@ case RUN_INTERACTIVE: <span class="Delimiter">{</span> <span class="Delimiter">:(before "End Globals")</span> bool Running_interactive = <span class="Constant">false</span><span class="Delimiter">;</span> +long long int Old_screen = <span class="Constant">0</span><span class="Delimiter">;</span> <span class="Comment">// we can support one iteration of screen inside screen</span> <span class="Delimiter">:(before "End Setup")</span> Running_interactive = <span class="Constant">false</span><span class="Delimiter">;</span> +Old_screen = <span class="Constant">0</span><span class="Delimiter">;</span> <span class="Delimiter">:(code)</span> -<span class="Comment">// reads a string, tries to call it as code, saving all warnings.</span> +<span class="Comment">// reads a string, tries to call it as code (treating it as a test), saving</span> +<span class="Comment">// all warnings.</span> <span class="Comment">// returns true if successfully called (no errors found during load and transform)</span> bool run_interactive<span class="Delimiter">(</span>long long int address<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">"interactive"</span><span class="Delimiter">)</span> == Recipe_ordinal<span class="Delimiter">.</span>end<span class="Delimiter">())</span> Recipe_ordinal[<span class="Constant">"interactive"</span>] = Next_recipe_ordinal++<span class="Delimiter">;</span> + Old_screen = Memory[SCREEN]<span class="Delimiter">;</span> +<span class="CommentedCode">//? cerr << "save screen: " << Old_screen << '\n'; //? 2</span> + <span class="Comment">// try to sandbox the run as best you can</span> + <span class="Comment">// todo: test this</span> + if <span class="Delimiter">(</span>!Current_scenario<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Comment">// not already sandboxed</span> + for <span class="Delimiter">(</span>long long int i = <span class="Constant">1</span><span class="Delimiter">;</span> i < Reserved_for_tests<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> + Memory<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>i<span class="Delimiter">);</span> + Name[Recipe_ordinal[<span class="Constant">"interactive"</span>]]<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> + <span class="Delimiter">}</span> +<span class="CommentedCode">//? cerr << "screen was at " << Name[Recipe_ordinal["interactive"]]["screen"] << '\n'; //? 1</span> + Name[Recipe_ordinal[<span class="Constant">"interactive"</span>]][<span class="Constant">"screen"</span>] = SCREEN<span class="Delimiter">;</span> +<span class="CommentedCode">//? cerr << "screen now at " << Name[Recipe_ordinal["interactive"]]["screen"] << '\n'; //? 1</span> string command = trim<span class="Delimiter">(</span>strip_comments<span class="Delimiter">(</span>to_string<span class="Delimiter">(</span>address<span class="Delimiter">)));</span> if <span class="Delimiter">(</span>command<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> -<span class="CommentedCode">//? tb_shutdown(); //? 1</span> -<span class="CommentedCode">//? cerr << command << '\n'; //? 2</span> Recipe<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>Recipe_ordinal[<span class="Constant">"interactive"</span>]<span class="Delimiter">);</span> Hide_warnings = <span class="Constant">true</span><span class="Delimiter">;</span> if <span class="Delimiter">(</span>!Trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span> Trace_file = <span class="Constant">""</span><span class="Delimiter">;</span> <span class="Comment">// if there wasn't already a stream we don't want to save it</span> Trace_stream = new trace_stream<span class="Delimiter">;</span> + Trace_stream<span class="Delimiter">-></span>collect_layer = <span class="Constant">"warn"</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Comment">// call run(string) but without the scheduling</span> - load<span class="Delimiter">(</span><span class="Constant">"recipe interactive [</span><span class="cSpecial">\n</span><span class="Constant">"</span>+command+<span class="Constant">"</span><span class="cSpecial">\n</span><span class="Constant">]</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">);</span> + <span class="Comment">// we won't create a local scope so that we can get to the new screen after</span> + <span class="Comment">// we return from 'interactive'.</span> + load<span class="Delimiter">(</span>string<span class="Delimiter">(</span><span class="Constant">"recipe interactive [</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">)</span> + + <span class="Constant">"screen:address <- new-fake-screen 5, 5</span><span class="cSpecial">\n</span><span class="Constant">"</span> + + command + <span class="Constant">"</span><span class="cSpecial">\n</span><span class="Constant">"</span> + + <span class="Constant">"]</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">);</span> transform_all<span class="Delimiter">();</span> - if <span class="Delimiter">(</span>trace_count<span class="Delimiter">(</span><span class="Constant">"warn"</span><span class="Delimiter">)</span> > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> - Hide_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> - <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> - <span class="Delimiter">}</span> + if <span class="Delimiter">(</span>trace_count<span class="Delimiter">(</span><span class="Constant">"warn"</span><span class="Delimiter">)</span> > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> Running_interactive = <span class="Constant">true</span><span class="Delimiter">;</span> Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>call<span class="Delimiter">(</span>Recipe_ordinal[<span class="Constant">"interactive"</span>]<span class="Delimiter">));</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> -<span class="Delimiter">:(after "Starting Reply")</span> -if <span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">()</span> == <span class="Constant">"interactive"</span><span class="Delimiter">)</span> clean_up_interactive<span class="Delimiter">();</span> -<span class="Delimiter">:(after "Falling Through End Of Recipe")</span> -if <span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">()</span> == <span class="Constant">"interactive"</span><span class="Delimiter">)</span> clean_up_interactive<span class="Delimiter">();</span> -<span class="Delimiter">:(code)</span> -void clean_up_interactive<span class="Delimiter">()</span> <span class="Delimiter">{</span> -<span class="CommentedCode">//? static int foo = 0; //? 1</span> - Hide_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> - Running_interactive = <span class="Constant">false</span><span class="Delimiter">;</span> -<span class="CommentedCode">//? ++foo; //? 1</span> -<span class="CommentedCode">//? if (foo == 1) tb_init(); //? 1</span> -<span class="Delimiter">}</span> - <span class="Delimiter">:(scenario "run_interactive_returns_stringified_result")</span> recipe main [ <span class="Comment"># try to interactively add 2 and 2</span> @@ -165,42 +173,65 @@ if <span class="Delimiter">(</span>Running_interactive<span class="Delimiter">)< <span class="Delimiter">:(code)</span> void record_products<span class="Delimiter">(</span>const instruction& instruction<span class="Delimiter">,</span> const vector<vector<double> >& products<span class="Delimiter">)</span> <span class="Delimiter">{</span> ostringstream out<span class="Delimiter">;</span> -<span class="CommentedCode">//? cerr << current_instruction().to_string() << '\n'; //? 1</span> for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Comment">// string</span> if <span class="Delimiter">(</span>i < SIZE<span class="Delimiter">(</span>instruction<span class="Delimiter">.</span>products<span class="Delimiter">))</span> <span class="Delimiter">{</span> -<span class="CommentedCode">//? cerr << "AA\n"; //? 1</span> -<span class="CommentedCode">//? cerr << instruction.products.size() << " vs " << i << '\n'; //? 1</span> if <span class="Delimiter">(</span>is_string<span class="Delimiter">(</span>instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Delimiter">{</span> -<span class="CommentedCode">//? cerr << "BB\n"; //? 1</span> assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)));</span> out << to_string<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Comment">// End Record Product Special-cases</span> <span class="Delimiter">}</span> - for <span class="Delimiter">(</span>long long int j = <span class="Constant">0</span><span class="Delimiter">;</span> j < SIZE<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span> ++j<span class="Delimiter">)</span> <span class="Delimiter">{</span> -<span class="CommentedCode">//? cerr << "aa: " << i << ", " << j << ": " << products.at(i).at(j) << '\n'; //? 1</span> - out << products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span>j<span class="Delimiter">)</span> << <span class="Constant">' '</span><span class="Delimiter">;</span> - <span class="Delimiter">}</span> + for <span class="Delimiter">(</span>long long int j = <span class="Constant">0</span><span class="Delimiter">;</span> j < SIZE<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span> ++j<span class="Delimiter">)</span> + out << products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span>j<span class="Delimiter">)</span> << <span class="Constant">' '</span><span class="Delimiter">;</span> out << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> -<span class="CommentedCode">//? cerr << "aa: {\n" << out.str() << "}\n"; //? 2</span> Most_recent_results = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span> <span class="Delimiter">}</span> <span class="Delimiter">:(before "Complete Call Fallthrough")</span> if <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>operation == RUN_INTERACTIVE && !current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span> - assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">)</span> <= <span class="Constant">2</span><span class="Delimiter">);</span> + assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">)</span> <= <span class="Constant">3</span><span class="Delimiter">);</span> <span class="Comment">// Send the results of the most recently executed instruction, regardless of</span> <span class="Comment">// call depth, to be converted to string and potentially printed to string.</span> vector<double> result<span class="Delimiter">;</span> result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>new_string<span class="Delimiter">(</span>Most_recent_results<span class="Delimiter">));</span> write_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">),</span> result<span class="Delimiter">);</span> - if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">)</span> == <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> + if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">)</span> >= <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> vector<double> warnings<span class="Delimiter">;</span> warnings<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>warnings_from_trace<span class="Delimiter">());</span> write_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">),</span> warnings<span class="Delimiter">);</span> <span class="Delimiter">}</span> + if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">)</span> >= <span class="Constant">3</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> + vector<double> screen<span class="Delimiter">;</span> +<span class="CommentedCode">//? cerr << "returning screen " << Memory[SCREEN] << " to " << current_instruction().products.at(2).to_string() << " value " << current_instruction().products.at(2).value << '\n'; //? 1</span> + screen<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>Memory[SCREEN]<span class="Delimiter">);</span> + write_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">),</span> screen<span class="Delimiter">);</span> + <span class="Delimiter">}</span> +<span class="Delimiter">}</span> + +<span class="Comment">//: clean up reply after we've popped it off the call-stack</span> +<span class="Comment">//: however, we need what was on the stack to decide whether to clean up</span> +<span class="Delimiter">:(after "Starting Reply")</span> +bool must_clean_up_interactive = <span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">()</span> == <span class="Constant">"interactive"</span><span class="Delimiter">);</span> +<span class="Delimiter">:(after "Falling Through End Of Recipe")</span> +bool must_clean_up_interactive = <span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">()</span> == <span class="Constant">"interactive"</span><span class="Delimiter">);</span> +<span class="Delimiter">:(before "End Reply")</span> +if <span class="Delimiter">(</span>must_clean_up_interactive<span class="Delimiter">)</span> clean_up_interactive<span class="Delimiter">();</span> +<span class="Delimiter">:(before "Complete Call Fallthrough")</span> +if <span class="Delimiter">(</span>must_clean_up_interactive<span class="Delimiter">)</span> clean_up_interactive<span class="Delimiter">();</span> +<span class="Delimiter">:(code)</span> +void clean_up_interactive<span class="Delimiter">()</span> <span class="Delimiter">{</span> + Hide_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> + Running_interactive = <span class="Constant">false</span><span class="Delimiter">;</span> + <span class="Comment">// hack: assume collect_layer isn't set anywhere else</span> + if <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-></span>collect_layer == <span class="Constant">"warn"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> + delete Trace_stream<span class="Delimiter">;</span> + Trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span> + <span class="Delimiter">}</span> +<span class="CommentedCode">//? cerr << "restore screen: " << Memory[SCREEN] << " to " << Old_screen << '\n'; //? 1</span> + Memory[SCREEN] = Old_screen<span class="Delimiter">;</span> + Old_screen = <span class="Constant">0</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Delimiter">:(code)</span> @@ -227,17 +258,13 @@ string to_string<span class="Delimiter">(</span>long long int address<span class <span class="Comment">// todo: unicode</span> tmp << <span class="Delimiter">(</span>char<span class="Delimiter">)(</span>int<span class="Delimiter">)</span>Memory[curr]<span class="Delimiter">;</span> <span class="Delimiter">}</span> -<span class="CommentedCode">//? tb_shutdown(); //? 1</span> -<span class="CommentedCode">//? cerr << tmp.str() << '\n'; //? 1</span> <span class="Identifier">return</span> tmp<span class="Delimiter">.</span>str<span class="Delimiter">();</span> <span class="Delimiter">}</span> long long int stringified_value_of_location<span class="Delimiter">(</span>long long int address<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Comment">// convert to string</span> ostringstream out<span class="Delimiter">;</span> -<span class="CommentedCode">//? trace(1, "foo") << "a: " << address; //? 1</span> out << Memory[address]<span class="Delimiter">;</span> -<span class="CommentedCode">//? trace(1, "foo") << "b: " << Memory[address]; //? 1</span> <span class="Identifier">return</span> new_string<span class="Delimiter">(</span>out<span class="Delimiter">.</span>str<span class="Delimiter">());</span> <span class="Delimiter">}</span> @@ -277,13 +304,6 @@ case RELOAD: <span class="Delimiter">{</span> Loading_interactive = <span class="Constant">true</span><span class="Delimiter">;</span> Hide_warnings = <span class="Constant">true</span><span class="Delimiter">;</span> load<span class="Delimiter">(</span>to_string<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span> -<span class="CommentedCode">//? static int foo = 0;</span> -<span class="CommentedCode">//? if (++foo == 2) {</span> -<span class="CommentedCode">//? tb_shutdown();</span> -<span class="CommentedCode">//? cerr << Recipe_ordinal["new-add"] << '\n';</span> -<span class="CommentedCode">//? cerr << Recipe[Recipe_ordinal["new-add"]].steps[2].to_string() << '\n';</span> -<span class="CommentedCode">//? exit(0);</span> -<span class="CommentedCode">//? }</span> transform_all<span class="Delimiter">();</span> Hide_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> Loading_interactive = <span class="Constant">false</span><span class="Delimiter">;</span> |