about summary refs log tree commit diff stats
path: root/082scenario_screen.cc
blob: ad61d3792e224a2241d11c1f766240c49d6015fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
" SubX syntax file
" Language:    SubX
" Maintainer:  Kartik Agaram <mu@akkartik.com>
" URL:         https://github.com/akkartik/mu
" License:     public domain
"
" Copy this into your ftplugin directory, and add the following to your vimrc
" or to .vim/ftdetect/subx.vim:
"   autocmd BufReadPost,BufNewFile *.subx set filetype=subx

let s:save_cpo = &cpo
set cpo&vim

" setlocal iskeyword=@,48-57,?,!,_,$,-
setlocal formatoptions-=t  " allow long lines
setlocal formatoptions+=c  " but comments should still wrap

setlocal iskeyword+=-,?

" blue tones
" comment colors for dark terminal: 14, 39, 27, 19
" comment colors for light terminal: 19, 27, 39, 6
"? syntax match subxH1Comment /# - .*/ | highlight subxH1Comment cterm=underline ctermfg=27
"? syntax match subxComment /#[^ ].*\|# [^.-].*\|# \?$/ | highlight subxComment ctermfg=27
"? syntax match subxS1Comment /# \..*/ | highlight subxS1Comment ctermfg=19
"? syntax match subxS2Comment /# \. \..*/ | highlight subxS2Comment ctermfg=245

" blue-green tones
syntax match subxH1Comment /# - .*/ | highlight subxH1Comment cterm=underline ctermfg=25
syntax match subxComment /#\( \.\| - \|? \)\@!.*/ | highlight subxComment ctermfg=25
syntax match subxS1Comment /# \..*/ | highlight subxS1Comment ctermfg=19
syntax match subxS2Comment /# \. \..*/ | highlight subxS2Comment ctermfg=245

" grey tones
"? syntax match subxH1Comment /# - .*/ | highlight subxH1Comment cterm=bold,underline
"? syntax match subxComment /#[^ ].*\|# [^.-].*\|# \?$/ | highlight subxComment cterm=bold ctermfg=236
"? hi Normal ctermfg=236
"? syntax match subxS1Comment /# \..*/ | highlight subxS1Comment cterm=bold ctermfg=242
"? syntax match subxS2Comment /# \. \..*/ | highlight subxS2Comment ctermfg=242

set comments-=:#
set comments+=n:#
syntax match subxCommentedCode "#? .*"  | highlight link subxCommentedCode CommentedCode
let b:cmt_head = "#? "

" comment token
syntax match subxDelimiter / \. /  | highlight link subxDelimiter Normal

syntax match subxString %"[^"]*"% | highlight link subxString Constant

"" definitions
" match globals but not registers like 'EAX'
" don't match capitalized words in metadata
" don't match inside strings
syntax match subxGlobal %\(/\)\@<!\<[A-Z][a-z0-9_-]*\>% | highlight link subxGlobal SpecialChar
" tweak the red color from the colorscheme just a tad to improve contrast
highlight SpecialChar ctermfg=160

" functions but not tests, globals or internal functions
syntax match subxFunction "^\(test_\)\@<![a-z][^ ]*\(:\)\@=" | highlight subxFunction cterm=underline ctermfg=130
" tests starting with 'test-'; dark:34 light:64
syntax match subxTest "^test-[^ ]*\(:\)\@=" | highlight subxTest ctermfg=64
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: Clean syntax to manipulate and check the screen in scenarios.
//: Instructions 'assume-screen' and 'screen-should-contain' implicitly create
//: a variable called 'screen' that is accessible to later instructions in the
//: scenario. 'screen-should-contain' can check unicode characters in the fake
//: screen

//: first make sure we don't mangle these functions in other transforms
:(before "End initialize_transform_rewrite_literal_string_to_text()")
recipes_taking_literal_strings.insert("screen-should-contain");
recipes_taking_literal_strings.insert("screen-should-contain-in-color");

:(scenarios run_mu_scenario)
:(scenario screen_in_scenario)
scenario screen-in-scenario [
  local-scope
  assume-screen 5/width, 3/height
  run [
    a:char <- copy 97/a
    screen:&:screen <- print screen:&:screen, a
  ]
  screen-should-contain [
  #  01234
    .a    .
    .     .
    .     .
  ]
]
# checks are inside scenario

:(scenario screen_in_scenario_unicode)
# screen-should-contain can check unicode characters in the fake screen
scenario screen-in-scenario-unicode [
  local-scope
  assume-screen 5/width, 3/height
  run [
    lambda:char <- copy 955/greek-small-lambda
    screen:&:screen <- print screen:&:screen, lambda
    a:char <- copy 97/a
    screen:&:screen <- print screen:&:screen, a
  ]
  screen-should-contain [
  #  01234
    .λa   .
    .     .
    .     .
  ]
]
# checks are inside scenario

:(scenario screen_in_scenario_color)
scenario screen-in-scenario-color [
  local-scope
  assume-screen 5/width, 3/height
  run [
    lambda:char <- copy 955/greek-small-lambda
    screen:&:screen <- print screen:&:screen, lambda, 1/red
    a:char <- copy 97/a
    screen:&:screen <- print screen:&:screen, a, 7/white
  ]
  # screen-should-contain shows everything
  screen-should-contain [
  #  01234
    .λa   .
    .     .
    .     .
  ]
  # screen-should-contain-in-color filters out everything except the given
  # color, all you see is the 'a' in white.
  screen-should-contain-in-color 7/white, [
  #  01234
    . a   .
    .     .
    .     .
  ]
  # ..and the λ in red.
  screen-should-contain-in-color 1/red, [
  #  01234
    .λ    .
    .     .
    .     .
  ]
]
# checks are inside scenario

:(scenario screen_in_scenario_error)
% Scenario_testing_scenario = true;
% Hide_errors = true;
scenario screen-in-scenario-error [
  local-scope
  assume-screen 5/width, 3/height
  run [
    a:char <- copy 97/a
    screen:&:screen <- print screen:&:screen, a
  ]
  screen-should-contain [
  #  01234
    .b    .
    .     .
    .     .
  ]
]
+error: F - screen-in-scenario-error: expected screen location (0, 0) to contain 98 ('b') instead of 97 ('a')

:(scenario screen_in_scenario_color_error)
% Scenario_testing_scenario = true;
% Hide_errors = true;
# screen-should-contain can check unicode characters in the fake screen
scenario screen-in-scenario-color-error [
  local-scope
  assume-screen 5/width, 3/height
  run [
    a:char <- copy 97/a
    screen:&:screen <- print screen:&:screen, a, 1/red
  ]
  screen-should-contain-in-color 2/green, [
  #  01234
    .a    .
    .     .
    .     .
  ]
]
+error: F - screen-in-scenario-color-error: expected screen location (0, 0) to contain 'a' in color 2 instead of 1

:(scenarios run)
:(scenario convert_names_does_not_fail_when_mixing_special_names_and_numeric_locations)
% Scenario_testing_scenario = true;
def main [
  screen:num <- copy 1:num
]
-error: mixing variable names and numeric addresses in main
$error: 0
:(scenarios run_mu_scenario)

//: It's easier to implement assume-screen and other similar scenario-only
//: primitives if they always write to a fixed location. So we'll assign a
//: single fixed location for the per-scenario screen, keyboard, file system,
//: etc. Carve space for these fixed locations out of the reserved-for-test
//: locations.

:(before "End Globals")
extern const int Max_variables_in_scenarios = Reserved_for_tests-100;
int Next_predefined_global_for_scenarios = Max_variables_in_scenarios;
:(before "End Reset")
assert(Next_predefined_global_for_scenarios < Reserved_for_tests);

:(before "End Globals")
// Scenario Globals.
extern const int SCREEN = Next_predefined_global_for_scenarios++;
// End Scenario Globals.

//: give 'screen' a fixed location in scenarios
:(before "End Special Scenario Variable Names(r)")
Name[r]["screen"] = SCREEN;
//: make 'screen' always a raw location in scenarios
:(before "End is_special_name Special-cases")
if (s == "screen") return true;

:(before "End Rewrite Instruction(curr, recipe result)")
// rewrite 'assume-screen width, height' to
// 'screen:&:screen <- new-fake-screen width, height'
if (curr.name == "assume-screen") {
  curr.name = "new-fake-screen";
  if (!curr.products.empty()) {
    raise << result.name << ": 'assume-screen' has no products\n" << end();
  }
  else if (!starts_with(result.name, "scenario_")) {
    raise << result.name << ": 'assume-screen' can't be called here, only in scenarios\n" << end();
  }
  else {
    assert(curr.products.empty());
    curr.products.push_back(reagent("screen:&:screen/raw"));
    curr.products.at(0).set_value(SCREEN);
  }
}

:(scenario assume_screen_shows_up_in_errors)
% Hide_errors = true;
scenario assume-screen-shows-up-in-errors [
  assume-screen width, 5
]
+error: assume-screen-shows-up-in-errors: missing type for 'width' in 'assume-screen width, 5'

//: screen-should-contain is a regular instruction
:(before "End Primitive Recipe Declarations")
SCREEN_SHOULD_CONTAIN,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "screen-should-contain", SCREEN_SHOULD_CONTAIN);
:(before "End Primitive Recipe Checks")
case SCREEN_SHOULD_CONTAIN: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'screen-should-contain' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_literal_text(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'screen-should-contain' should be a literal string, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case SCREEN_SHOULD_CONTAIN: {
  if (!Passed) break;
  assert(scalar(ingredients.at(0)));
  check_screen(current_instruction().ingredients.at(0).name, -1);
  break;
}

:(before "End Primitive Recipe Declarations")
SCREEN_SHOULD_CONTAIN_IN_COLOR,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "screen-should-contain-in-color", SCREEN_SHOULD_CONTAIN_IN_COLOR);
:(before "End Primitive Recipe Checks")
case SCREEN_SHOULD_CONTAIN_IN_COLOR: {
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(get(Recipe, r).name) << "'screen-should-contain-in-color' requires exactly two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'screen-should-contain-in-color' should be a number (color code), but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  if (!is_literal_text(inst.ingredients.at(1))) {
    raise << maybe(get(Recipe, r).name) << "second ingredient of 'screen-should-contain-in-color' should be a literal string, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case SCREEN_SHOULD_CONTAIN_IN_COLOR: {
  if (!Passed) break;
  assert(scalar(ingredients.at(0)));
  assert(scalar(ingredients.at(1)));
  check_screen(current_instruction().ingredients.at(1).name, ingredients.at(0).at(0));
  break;
}

:(before "End Types")
// scan an array of characters in a unicode-aware, bounds-checked manner
struct raw_string_stream {
  int index;
  const int max;
  const char* buf;

  raw_string_stream(const string&);
  uint32_t get();  // unicode codepoint
  uint32_t peek();  // unicode codepoint
  bool at_end() const;
  void skip_whitespace_and_comments();
};

:(code)
void check_screen(const string& expected_contents, const int color) {
  int screen_location = get_or_insert(Memory, SCREEN)+/*skip refcount*/1;
  int data_offset = find_element_name(get(Type_ordinal, "screen"), "data", "");
  assert(data_offset >= 0);
  int screen_data_location = screen_location+data_offset;  // type: address:array:character
  int screen_data_start = get_or_insert(Memory, screen_data_location) + /*skip refcount*/1;  // type: array:character
  int width_offset = find_element_name(get(Type_ordinal, "screen"), "num-columns", "");
  int screen_width = get_or_insert(Memory, screen_location+width_offset);
  int height_offset = find_element_name(get(Type_ordinal, "screen"), "num-rows", "");
  int screen_height = get_or_insert(Memory, screen_location+height_offset);
  raw_string_stream cursor(expected_contents);
  // todo: too-long expected_contents should fail
  int top_index_offset = find_element_name(get(Type_ordinal, "screen"), "top-idx", "");
  int top_index = get_or_insert(Memory, screen_location+top_index_offset);
  for (int i=0, row=top_index/screen_width;  i < screen_height;  ++i, row=(row+1)%screen_height) {
    cursor.skip_whitespace_and_comments();
    if (cursor.at_end()) break;
    if (cursor.get() != '.') {
      raise << maybe(current_recipe_name()) << "each row of the expected screen should start with a '.'\n" << end();
      if (!Scenario_testing_scenario) Passed = false;
      return;
    }
    int addr = screen_data_start+/*length*/1+row*screen_width* /*size of screen-cell*/2;
    for (int column = 0;  column < screen_width;  ++column, addr+= /*size of screen-cell*/2) {
      const int cell_color_offset = 1;
      uint32_t curr = cursor.get();
      if (get_or_insert(Memory, addr) == 0 && isspace(curr)) continue;
      if (curr == ' ' && color != -1 && color != get_or_insert(Memory, addr+cell_color_offset)) {
        // filter out other colors
        continue;
      }
      if (get_or_insert(Memory, addr) != 0 && get_or_insert(Memory, addr) == curr) {
        if (color == -1 || color == get_or_insert(Memory, addr+cell_color_offset)) continue;
        // contents match but color is off
        if (!Hide_errors) cerr << '\n';
        raise << "F - " << maybe(current_recipe_name()) << "expected screen location (" << row << ", " << column << ") to contain '" << unicode_character_at(addr) << "' in color " << color << " instead of " << no_scientific(get_or_insert(Memory, addr+cell_color_offset)) << "\n" << end();
        if (!Hide_errors) dump_screen();
        if (!Scenario_testing_scenario) Passed = false;
        return;
      }

      // really a mismatch
      // can't print multi-byte unicode characters in errors just yet. not very useful for debugging anyway.
      char expected_pretty[10] = {0};
      if (curr < 256 && !iscntrl(curr)) {
        // " ('<curr>')"
        expected_pretty[0] = ' ', expected_pretty[1] = '(', expected_pretty[2] = '\'', expected_pretty[3] = static_cast<unsigned char>(curr), expected_pretty[4] = '\'', expected_pretty[5] = ')', expected_pretty[6] = '\0';
      }
      char actual_pretty[10] = {0};
      if (get_or_insert(Memory, addr) < 256 && !iscntrl(get_or_insert(Memory, addr))) {
        // " ('<curr>')"
        actual_pretty[0] = ' ', actual_pretty[1] = '(', actual_pretty[2] = '\'', actual_pretty[3] = static_cast<unsigned char>(get_or_insert(Memory, addr)), actual_pretty[4] = '\'', actual_pretty[5] = ')', actual_pretty[6] = '\0';
      }

      ostringstream color_phrase;
      if (color != -1) color_phrase << " in color " << color;
      if (!Hide_errors) cerr << '\n';
      raise << "F - " << maybe(current_recipe_name()) << "expected screen location (" << row << ", " << column << ") to contain " << curr << expected_pretty << color_phrase.str() << " instead of " << no_scientific(get_or_insert(Memory, addr)) << actual_pretty << '\n' << end();
      if (!Hide_errors) dump_screen();
      if (!Scenario_testing_scenario) Passed = false;
      return;
    }
    if (cursor.get() != '.') {
      raise << maybe(current_recipe_name()) << "row " << row << " of the expected screen is too long\n" << end();
      if (!Scenario_testing_scenario) Passed = false;
      return;
    }
  }
  cursor.skip_whitespace_and_comments();
  if (!cursor.at_end()) {
    raise << maybe(current_recipe_name()) << "expected screen has too many rows\n" << end();
    Passed = false;
  }
}

const char* unicode_character_at(int addr) {
  int unicode_code_point = static_cast<int>(get_or_insert(Memory, addr));
  return to_unicode(unicode_code_point);
}

raw_string_stream::raw_string_stream(const string& backing) :index(0), max(SIZE(backing)), buf(backing.c_str()) {}

bool raw_string_stream::at_end() const {
  if (index >= max) return true;
  if (tb_utf8_char_length(buf[index]) > max-index) {
    raise << "unicode string seems corrupted at index "<< index << " character " << static_cast<int>(buf[index]) << '\n' << end();
    return true;
  }
  return false;
}

uint32_t raw_string_stream::get() {
  assert(index < max);  // caller must check bounds before calling 'get'
  uint32_t result = 0;
  int length = tb_utf8_char_to_unicode(&result, &buf[index]);
  assert(length != TB_EOF);
  index += length;
  return result;
}

uint32_t raw_string_stream::peek() {
  assert(index < max);  // caller must check bounds before calling 'get'
  uint32_t result = 0;
  int length = tb_utf8_char_to_unicode(&result, &buf[index]);
  assert(length != TB_EOF);
  return result;
}

void raw_string_stream::skip_whitespace_and_comments() {
  while (!at_end()) {
    if (isspace(peek())) get();
    else if (peek() == '#') {
      // skip comment
      get();
      while (peek() != '\n') get();  // implicitly also handles CRLF
    }
    else break;
  }
}

:(before "End Primitive Recipe Declarations")
_DUMP_SCREEN,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump-screen", _DUMP_SCREEN);
:(before "End Primitive Recipe Checks")
case _DUMP_SCREEN: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _DUMP_SCREEN: {
  dump_screen();
  break;
}

:(code)
void dump_screen() {
  int screen_location = get_or_insert(Memory, SCREEN) + /*skip refcount*/1;
  int width_offset = find_element_name(get(Type_ordinal, "screen"), "num-columns", "");
  int screen_width = get_or_insert(Memory, screen_location+width_offset);
  int height_offset = find_element_name(get(Type_ordinal, "screen"), "num-rows", "");
  int screen_height = get_or_insert(Memory, screen_location+height_offset);
  int data_offset = find_element_name(get(Type_ordinal, "screen"), "data", "");
  assert(data_offset >= 0);
  int screen_data_location = screen_location+data_offset;  // type: address:array:character
  int screen_data_start = get_or_insert(Memory, screen_data_location) + /*skip refcount*/1;  // type: array:character
  assert(get_or_insert(Memory, screen_data_start) == screen_width*screen_height);
  int top_index_offset = find_element_name(get(Type_ordinal, "screen"), "top-idx", "");
  int top_index = get_or_insert(Memory, screen_location+top_index_offset);
  for (int i=0, row=top_index/screen_width;  i < screen_height;  ++i, row=(row+1)%screen_height) {
    cerr << '.';
    int curr = screen_data_start+/*length*/1+row*screen_width* /*size of screen-cell*/2;
    for (int col = 0;  col < screen_width;  ++col) {
      if (get_or_insert(Memory, curr))
        cerr << to_unicode(static_cast<uint32_t>(get_or_insert(Memory, curr)));
      else
        cerr << ' ';
      curr += /*size of screen-cell*/2;
    }
    cerr << ".\n";
  }
}
committer Kartik K. Agaram <vc@akkartik.com> 2015-06-12 22:34:45 -0700 1556' href='/akkartik/mu/commit/html/081run_interactive.cc.html?h=hlt&id=d44123cabaa730c778a0e2644c75dbfba6a7ab30'>d44123ca ^
d44123ca ^

d44123ca ^


a654e4ec ^
d44123ca ^

76755b28 ^
9542bb11 ^
d5d908dd ^

a654e4ec ^
4690ce81 ^
76755b28 ^
4690ce81 ^

76755b28 ^
4690ce81 ^
76755b28 ^

4690ce81 ^
76755b28 ^



a654e4ec ^
4690ce81 ^
d5d908dd ^
4690ce81 ^

d5d908dd ^
4690ce81 ^
d5d908dd ^
0e4a335e ^
4690ce81 ^
0e4a335e ^



4690ce81 ^
a654e4ec ^

0e4a335e ^



d5d908dd ^
d44123ca ^
d5d908dd ^
dcc060c7 ^
76755b28 ^
d44123ca ^
d5d908dd ^
d44123ca ^
76755b28 ^

4690ce81 ^

a654e4ec ^
9542bb11 ^

4690ce81 ^

9542bb11 ^

76755b28 ^


4690ce81 ^
a654e4ec ^










90560d71 ^
a654e4ec ^
76755b28 ^

90560d71 ^
d5d908dd ^
9542bb11 ^
76755b28 ^
d5d908dd ^
dcc060c7 ^
76755b28 ^
d44123ca ^

76755b28 ^

4690ce81 ^
76755b28 ^
32b8fac2 ^
76755b28 ^





67db19a0 ^
a654e4ec ^



32b8fac2 ^
a654e4ec ^















d44123ca ^


a654e4ec ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545



                                                                                          
                                         

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            

                                      
                             
                                  
                            



                                                                           

        





                               

       
                         

                                                                                                       
 
                                                               
          
                                                                                                                    

                                                                                                                                                                                                                                                  
                                                                                                                                                            
 
                                                                 

                                                                
          

                                                                                                                                                                    

                                             
                                                                
 
                                                                                                 
                                                                                           
                                                         
                                                                      
                                                                   


                                                                                      

                                                                                                                                                                                                                            

                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

                                                                         

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

                                                                         


                                                                                         


                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                  
                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                  
                                                  

                                                                                                                                     
                                                                   

                                                                                                                                                                        

                                
                                                                
                                                                                                                                 


                                                                                                    
                                                              
                                                                                                
                                      
                                                                                                        
                                           
                                                                                                               
                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

                                                                        
                                                                                                                                                   
                                                                                                                                                                                                                                                                     
                                                                                                                   
                                  
                                                                                                                                                                                             
                                                                                                                                                                                                                                                      

                                                                                                                                                                                                                                                          


                                                                                      
                                                                             
                                                                                                                                                                                                                                           
                                                                                                                               
                                                                                                                                                               
                                                                                                                                            
                                                                                                                              
                                                                                                                                           
                                                                                                                                 
                                                                                                                                                 
                                                 
                                                                        
                                                                                                                                                                                                                                                                                                                                                                           

                                                                                                        
                                                                                                                                                   




                                                                                                                                                                                                                                                                                                                                                                                                                             


                                                                                                           










                                                                                                                                                                                                       
                                                                        
                                                                                  
                                                                                              

                                                                                                                       


                                                                                         
                                                                                             


                                                                                                           
                                                                                                               
                                                                                   
                                                                                               
                                                                                 



                                                                                        
                                                                                                                                                                                                                                                                                                                                 





















                                                                                                                                                                                                  

                                





                                                                                                                                                                                                                                                               
                                                                                                                                                             
                                                                                                                                                                       
                                                                                                                                             


                                                                                                                                                                     


                                                                                                                                                                      
                                                                                                                                    
                                                                                                                                                              
                                                                                                                                          
 
                                                             
                                                                          
                                                                                                                                                                                                                                                                             
 
                                                                   
          
                                                                                                                                                               
                                                                                                    

                                                                                                                                                                    


                                                                 


                                                                                      

                                                                                                                                                                                                                                              
                                                                                           

                                                                       
                                                                                         
                                                                                           






                                                                                                 

                                                                                                                                                                                                                                            
                                                                                          

                                                                       
                                                                                         
                                                                                          






                                                                                                  

                                                                                                                                                                                                                                        
                                                                                        

                                                                       
                                                                                         
                                                                                        





                                                                                                                                                                                                                                                                                        
                                           
                                                                                 
                                                                                                                                                                                                                    
                                                                                
                                                                              

                                                                       
                                                                                         
                                                                              
                                                                                                                                                
                                                                                                                                                                                                                                            



                                                                                      
                                              
                                                                                 

                                                                                                                                                                                                                          
                                                                                 

                                                                       
                                                                                         
                                                                                 

                                                                                                                                                                                                                                          


                                                                       




                                                                                                                                                                                                                                              
                                                                                           


                                                                                         
                                                                                           

                                                                       

                                
                                                                                              
          
                                                                 


                                                                                                                                                                                                                                  



                                                                                  
                                                                                   
          
                                                                 



                                                                                                                                                                                       
   

                                                                                                                                                                    




                                                                  
                                                                                     
          
                                                                      
                                                                                                                                                                                                               
                                                        

                                                                                                                                                                                                                                                            
 
                                                                                                  



                                                                   


                                                                       
          
                                                                               
                                                                                                                                                                                                                                               
                                                                                                            
 
                                                                                                                                                                                                                                                            


                                                                

                                                                
                                                           
                                                              
                                                                                                 
                                                                       
                                                                                                                                                          
                                                                                                                                                           

                                      
                                                                                                                                                                                                                                                                                                                                                                          
                                                   
                                                                                                                                                                                                                                                                                                                               
                                          


                                                                                                                                                                                                                                                                                                          
                                                       
                                                                                                                                                                                                        
                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                  

                                                                                          
                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                             



                                                                                
                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                           

                                                                                   
                                                                                                 

                                
                                      

                                                                                                                               
                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                              

                                                                                                                            

                                                                                                                                                                                                                                                                                                                                                                                                            
                                           

                                    


                                                                                                                    
                                                                                                                                                                                                     

                                                   
                                                                                                                                                                                         
                                                                                                                                                               

                                
                                                                                                                      
                                                                                                                                                                                                                     
                                                   

                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                              
                                                                                                                                                                                                                                                                                                                                             

                                                                                          
                                                                                                                                                                                                                                                     



                                                                                                                             
                                                                                                                    
                                                                                                                                                                                                                     
                                                   

                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                              
                                                                                                                                                                                                                                                                                                                                             
                                  
                                                                                          
                                                                                                                                                                                                                                                     



                                                                                                                             
                                                                                                                                                       

                                                                                                                                                                                                                                               



                                                                                                                                                     
                                
 
                                                                                                       
                                                            
 
                                                                                      
                                      
                                                                                 

                                                                                                                                                                                                          

                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

                                                                         

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

                                                                         


                                                                                         
                                                                         










                                                                                                           
                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                     

                                                                                      
                                                                                                                           
                                                 
                                                                                                                                        
                                                                        
                                                                                                                                                
                                                                                                                                                                                                                                            
                                                                                                                               

                                                                       

                                                                      
          
             
                                                                                                                                        





                                                                                                                     
 



                                                                                                              
                                                                                                                            















                                                                                                                                                                                  


       
                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 091run_interactive.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.traceContains { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Helper for various programming environments: run arbitrary mu code and</span>
<span class="Comment">//: return some result in string form.</span>

<span class="Delimiter">:(scenario run_interactive_code)</span>
def main [
  <span class="Constant">1</span>:number/<span class="Special">raw &lt;- </span>copy <span class="Constant">0</span>
  <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [<span class="Constant">1</span>:number/<span class="Special">raw &lt;- </span>copy <span class="Constant">34</span>]
  run-interactive <span class="Constant">2</span>:address:array:character
  <span class="Constant">3</span>:number/<span class="Special">raw &lt;- </span>copy <span class="Constant">1</span>:number/<span class="Special">raw</span>
]
<span class="traceContains">+mem: storing 34 in location 3</span>

<span class="Delimiter">:(scenario run_interactive_empty)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>/unsafe
  <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>run-interactive <span class="Constant">1</span>:address:array:character
]
<span class="Comment"># result is null</span>
<span class="traceContains">+mem: storing 0 in location 2</span>

<span class="Comment">//: run code in 'interactive mode', i.e. with errors off and return:</span>
<span class="Comment">//:   stringified output in case we want to print it to screen</span>
<span class="Comment">//:   any errors encountered</span>
<span class="Comment">//:   simulated screen any prints went to</span>
<span class="Comment">//:   any 'app' layer traces generated</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
RUN_INTERACTIVE<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;run-interactive&quot;</span><span class="Delimiter">,</span> RUN_INTERACTIVE<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> RUN_INTERACTIVE: <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'run-interactive' requires exactly one ingredient, but got &quot;</span> &lt;&lt; to_original_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!is_mu_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;first ingredient of 'run-interactive' should be a string, but got &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> RUN_INTERACTIVE: <span class="Delimiter">{</span>
  <span class="Normal">bool</span> 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>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!new_code_pushed_to_stack<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">5</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>trace_error_contents<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>
    products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">3</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>trace_app_contents<span class="Delimiter">());</span>
    products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">4</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>  <span class="Comment">// completed</span>
    run_code_end<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>
  <span class="Normal">else</span> <span class="Delimiter">{</span>
    <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// not done with caller; don't increment current_step_index()</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
<span class="Normal">bool</span> Track_most_recent_products = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Tracing&quot;)</span>
trace_stream* Save_trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
string Save_trace_file<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Setup&quot;)</span>
Track_most_recent_products = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Comment">// reads a string, tries to call it as code (treating it as a test), saving</span>
<span class="Comment">// all errors.</span>
<span class="Comment">// returns true if successfully called (no errors found during load and transform)</span>
<span class="Normal">bool</span> run_interactive<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;interactive&quot;</span><span class="Delimiter">)</span> &amp;&amp; get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;interactive&quot;</span><span class="Delimiter">)</span> != <span class="Constant">0</span><span class="Delimiter">);</span>
  <span class="Comment">// try to sandbox the run as best you can</span>
  <span class="Comment">// todo: test this</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!Current_scenario<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">1</span><span class="Delimiter">;</span> i &lt; 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>
  <span class="Delimiter">}</span>
  string command = trim<span class="Delimiter">(</span>strip_comments<span class="Delimiter">(</span>read_mu_string<span class="Delimiter">(</span>address<span class="Delimiter">)));</span>
  Name[get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;interactive&quot;</span><span class="Delimiter">)</span>]<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  run_code_begin<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">should_stash_snapshots</span><span class="Comment">*/</span><span class="Constant">true</span><span class="Delimiter">);</span>
  <span class="Normal">if</span> <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="Comment">// don't kill the current routine on parse errors</span>
  routine* save_current_routine = Current_routine<span class="Delimiter">;</span>
  Current_routine = <span class="Constant">NULL</span><span class="Delimiter">;</span>
  <span class="Comment">// call run(string) but without the scheduling</span>
  load<span class="Delimiter">(</span>string<span class="Delimiter">(</span><span class="Constant">&quot;recipe! interactive [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">)</span> +
          <span class="Constant">&quot;local-scope</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
          <span class="Constant">&quot;screen:address:screen &lt;- next-ingredient</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
          <span class="Constant">&quot;$start-tracking-products</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
          command + <span class="Constant">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
          <span class="Constant">&quot;$stop-tracking-products</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
          <span class="Constant">&quot;return screen</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
       <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">);</span>
  transform_all<span class="Delimiter">();</span>
  Current_routine = save_current_routine<span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>trace_count<span class="Delimiter">(</span><span class="Constant">&quot;error&quot;</span><span class="Delimiter">)</span> &gt; <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Comment">// now call 'sandbox' which will run 'interactive' in a separate routine,</span>
  <span class="Comment">// and wait for it</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>Save_trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    ++Save_trace_stream<span class="Delimiter">-&gt;</span>callstack_depth<span class="Delimiter">;</span>
    trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;trace&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;run-interactive: incrementing callstack depth to &quot;</span> &lt;&lt; Save_trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt;&lt; end<span class="Delimiter">();</span>
    assert<span class="Delimiter">(</span>Save_trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt; <span class="Constant">9000</span><span class="Delimiter">);</span>  <span class="Comment">// 9998-101 plus cushion</span>
  <span class="Delimiter">}</span>
  Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>call<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;sandbox&quot;</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="Comment">//: Carefully update all state to exactly how it was -- including snapshots.</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
map&lt;string<span class="Delimiter">,</span> recipe_ordinal&gt; Recipe_ordinal_snapshot_stash<span class="Delimiter">;</span>
map&lt;recipe_ordinal<span class="Delimiter">,</span> recipe&gt; Recipe_snapshot_stash<span class="Delimiter">;</span>
map&lt;string<span class="Delimiter">,</span> type_ordinal&gt; Type_ordinal_snapshot_stash<span class="Delimiter">;</span>
map&lt;type_ordinal<span class="Delimiter">,</span> type_info&gt; Type_snapshot_stash<span class="Delimiter">;</span>
map&lt;recipe_ordinal<span class="Delimiter">,</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">int</span>&gt; &gt; Name_snapshot_stash<span class="Delimiter">;</span>
map&lt;string<span class="Delimiter">,</span> vector&lt;recipe_ordinal&gt; &gt; Recipe_variants_snapshot_stash<span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> run_code_begin<span class="Delimiter">(</span><span class="Normal">bool</span> should_stash_snapshots<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// stuff to undo later, in run_code_end()</span>
  Hide_errors = <span class="Constant">true</span><span class="Delimiter">;</span>
  Disable_redefine_checks = <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>should_stash_snapshots<span class="Delimiter">)</span>
    stash_snapshots<span class="Delimiter">();</span>
  Save_trace_stream = Trace_stream<span class="Delimiter">;</span>
  Save_trace_file = Trace_file<span class="Delimiter">;</span>
  Trace_file = <span class="Constant">&quot;&quot;</span><span class="Delimiter">;</span>
  Trace_stream = <span class="Normal">new</span> trace_stream<span class="Delimiter">;</span>
  Trace_stream<span class="Delimiter">-&gt;</span>collect_depth = App_depth<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> run_code_end<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  Hide_errors = <span class="Constant">false</span><span class="Delimiter">;</span>
  Disable_redefine_checks = <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">delete</span> Trace_stream<span class="Delimiter">;</span>
  Trace_stream = Save_trace_stream<span class="Delimiter">;</span>
  Save_trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
  Trace_file = Save_trace_file<span class="Delimiter">;</span>
  Save_trace_file<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  Recipe<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;interactive&quot;</span><span class="Delimiter">));</span>  <span class="Comment">// keep past sandboxes from inserting errors</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!Recipe_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
    unstash_snapshots<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Comment">// keep sync'd with save_snapshots and restore_snapshots</span>
<span class="Normal">void</span> stash_snapshots<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  Recipe_ordinal_snapshot_stash = Recipe_ordinal_snapshot<span class="Delimiter">;</span>
  Recipe_snapshot_stash = Recipe_snapshot<span class="Delimiter">;</span>
  Type_ordinal_snapshot_stash = Type_ordinal_snapshot<span class="Delimiter">;</span>
  Type_snapshot_stash = Type_snapshot<span class="Delimiter">;</span>
  Name_snapshot_stash = Name_snapshot<span class="Delimiter">;</span>
  Recipe_variants_snapshot_stash = Recipe_variants_snapshot<span class="Delimiter">;</span>
  save_snapshots<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> unstash_snapshots<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  restore_snapshots<span class="Delimiter">();</span>
  Recipe_ordinal_snapshot = Recipe_ordinal_snapshot_stash<span class="Delimiter">;</span>  Recipe_ordinal_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  Recipe_snapshot = Recipe_snapshot_stash<span class="Delimiter">;</span>  Recipe_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  Type_ordinal_snapshot = Type_ordinal_snapshot_stash<span class="Delimiter">;</span>  Type_ordinal_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  Type_snapshot = Type_snapshot_stash<span class="Delimiter">;</span>  Type_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  Name_snapshot = Name_snapshot_stash<span class="Delimiter">;</span>  Name_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  Recipe_variants_snapshot = Recipe_variants_snapshot_stash<span class="Delimiter">;</span>  Recipe_variants_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Load Recipes&quot;)</span>
load<span class="Delimiter">(</span>string<span class="Delimiter">(</span>
<span class="Constant">&quot;recipe interactive [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">)</span> +  <span class="Comment">// just a dummy version to initialize the Recipe_ordinal and so on</span>
<span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
<span class="Constant">&quot;recipe sandbox [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;local-scope</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;screen:address:screen &lt;- new-fake-screen 30, 5</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;r:number/routine_id &lt;- start-running interactive, screen</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;limit-time r, 100000/instructions</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;wait-for-routine r</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;sandbox-state:number &lt;- routine-state r/routine_id</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;completed?:boolean &lt;- equal sandbox-state, 1/completed</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;output:address:array:character &lt;- $most-recent-products</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;errors:address:array:character &lt;- save-errors</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;stashes:address:array:character &lt;- save-app-trace</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;$cleanup-run-interactive</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
  <span class="Constant">&quot;return output, errors, screen, stashes, completed?</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> +
<span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">);</span>

<span class="Comment">//: adjust errors in the sandbox</span>
<span class="Delimiter">:(after &quot;string maybe(string s)&quot;)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;interactive&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">&quot;&quot;</span><span class="Delimiter">;</span>

<span class="Delimiter">:(scenario run_interactive_comments)</span>
def main [
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [<span class="Comment"># ab</span>
add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>]
  <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>run-interactive <span class="Constant">1</span>:address:array:character
  <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
]
<span class="traceContains">+mem: storing 52 in location 4</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
_START_TRACKING_PRODUCTS<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;$start-tracking-products&quot;</span><span class="Delimiter">,</span> _START_TRACKING_PRODUCTS<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> _START_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> _START_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
  Track_most_recent_products = <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
_STOP_TRACKING_PRODUCTS<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;$stop-tracking-products&quot;</span><span class="Delimiter">,</span> _STOP_TRACKING_PRODUCTS<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> _STOP_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> _STOP_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
  Track_most_recent_products = <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
_MOST_RECENT_PRODUCTS<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;$most-recent-products&quot;</span><span class="Delimiter">,</span> _MOST_RECENT_PRODUCTS<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> _MOST_RECENT_PRODUCTS: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> _MOST_RECENT_PRODUCTS: <span class="Delimiter">{</span>
  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</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>new_mu_string<span class="Delimiter">(</span>Most_recent_products<span class="Delimiter">));</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
SAVE_ERRORS<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;save-errors&quot;</span><span class="Delimiter">,</span> SAVE_ERRORS<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> SAVE_ERRORS: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> SAVE_ERRORS: <span class="Delimiter">{</span>
  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</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>trace_error_contents<span class="Delimiter">());</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
SAVE_APP_TRACE<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;save-app-trace&quot;</span><span class="Delimiter">,</span> SAVE_APP_TRACE<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> SAVE_APP_TRACE: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> SAVE_APP_TRACE: <span class="Delimiter">{</span>
  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</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>trace_app_contents<span class="Delimiter">());</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
_CLEANUP_RUN_INTERACTIVE<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;$cleanup-run-interactive&quot;</span><span class="Delimiter">,</span> _CLEANUP_RUN_INTERACTIVE<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> _CLEANUP_RUN_INTERACTIVE: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> _CLEANUP_RUN_INTERACTIVE: <span class="Delimiter">{</span>
  run_code_end<span class="Delimiter">();</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario &quot;run_interactive_converts_result_to_text&quot;)</span>
def main [
  <span class="Comment"># try to interactively add 2 and 2</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>]
  <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>run-interactive <span class="Constant">1</span>:address:array:character
  <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy <span class="Constant">2</span>:address:array:character/lookup
]
<span class="Comment"># first letter in the output should be '4' in unicode</span>
<span class="traceContains">+mem: storing 52 in location 11</span>

<span class="Delimiter">:(scenario &quot;run_interactive_returns_text&quot;)</span>
def main [
  <span class="Comment"># try to interactively add 2 and 2</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [
    <span class="Normal">x</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [a]
    <span class="Normal">y</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [b]
    <span class="Normal">z</span>:address:array:character<span class="Special"> &lt;- </span>append x:address:array:character<span class="Delimiter">,</span> y:address:array:character
  ]
  <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>run-interactive <span class="Constant">1</span>:address:array:character
  <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy <span class="Constant">2</span>:address:array:character/lookup
]
<span class="Comment"># output contains &quot;ab&quot;</span>
<span class="traceContains">+mem: storing 97 in location 11</span>
<span class="traceContains">+mem: storing 98 in location 12</span>

<span class="Delimiter">:(scenario &quot;run_interactive_returns_errors&quot;)</span>
def main [
  <span class="Comment"># run a command that generates an error</span>
  <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [x:number<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
get x:number<span class="Delimiter">,</span> foo:offset]
  <span class="Constant">2</span>:address:array:character<span class="Delimiter">,</span> <span class="Constant">3</span>:address:array:character<span class="Special"> &lt;- </span>run-interactive <span class="Constant">1</span>:address:array:character
  <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy <span class="Constant">3</span>:address:array:character/lookup
]
<span class="Comment"># error should be &quot;unknown element foo in container number&quot;</span>
<span class="traceContains">+mem: storing 117 in location 11</span>
<span class="traceContains">+mem: storing 110 in location 12</span>
<span class="traceContains">+mem: storing 107 in location 13</span>
<span class="traceContains">+mem: storing 110 in location 14</span>
<span class="Comment"># ...</span>

<span class="Delimiter">:(scenario run_interactive_with_comment)</span>
def main [
  <span class="Comment"># 2 instructions, with a comment after the first</span>
  <span class="Constant">1</span>:address:array:number<span class="Special"> &lt;- </span><span class="Normal">new</span> [a:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># abc</span>
<span class="Normal">b</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
  <span class="Constant">2</span>:address:array:character<span class="Delimiter">,</span> <span class="Constant">3</span>:address:array:character<span class="Special"> &lt;- </span>run-interactive <span class="Constant">1</span>:address:array:character
]
<span class="Comment"># no errors</span>
<span class="traceContains">+mem: storing 0 in location 3</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
string Most_recent_products<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Setup&quot;)</span>
Most_recent_products = <span class="Constant">&quot;&quot;</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End of Instruction&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Track_most_recent_products<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  track_most_recent_products<span class="Delimiter">(</span>current_instruction<span class="Delimiter">(),</span> products<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> track_most_recent_products<span class="Delimiter">(</span><span class="Normal">const</span> instruction&amp; instruction<span class="Delimiter">,</span> <span class="Normal">const</span> vector&lt;vector&lt;<span class="Normal">double</span>&gt; &gt;&amp; products<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  ostringstream out<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Comment">// string</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>i &lt; SIZE<span class="Delimiter">(</span>instruction<span class="Delimiter">.</span>products<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_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="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Delimiter">{</span>
          tb_shutdown<span class="Delimiter">();</span>
          cerr &lt;&lt; read_mu_string<span class="Delimiter">(</span>trace_error_contents<span class="Delimiter">())</span> &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
          cerr &lt;&lt; SIZE<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> &lt;&lt; <span class="Constant">&quot;: &quot;</span><span class="Delimiter">;</span>
          <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span> j &lt; 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>
            cerr &lt;&lt; no_scientific<span class="Delimiter">(</span>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> &lt;&lt; <span class="Constant">' '</span><span class="Delimiter">;</span>
          cerr &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
        <span class="Delimiter">}</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 &lt;&lt; read_mu_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> &lt;&lt; <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>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span> j &lt; 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 &lt;&lt; no_scientific<span class="Delimiter">(</span>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> &lt;&lt; <span class="Constant">' '</span><span class="Delimiter">;</span>
    out &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  Most_recent_products = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
string strip_comments<span class="Delimiter">(</span>string in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  ostringstream result<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>in<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> != <span class="Constant">'#'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      result &lt;&lt; in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
    <span class="Normal">else</span> <span class="Delimiter">{</span>
      <span class="Normal">while</span> <span class="Delimiter">(</span>i+<span class="Constant">1</span> &lt; SIZE<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i+<span class="Constant">1</span><span class="Delimiter">)</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span>
        ++i<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> result<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> stringified_value_of_location<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// convert to string</span>
  ostringstream out<span class="Delimiter">;</span>
  out &lt;&lt; no_scientific<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">));</span>
  <span class="Identifier">return</span> new_mu_string<span class="Delimiter">(</span>out<span class="Delimiter">.</span>str<span class="Delimiter">());</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> trace_error_contents<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!Trace_stream<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  ostringstream out<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>vector&lt;trace_line&gt;::iterator p = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-&gt;</span>label != <span class="Constant">&quot;error&quot;</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    out &lt;&lt; p<span class="Delimiter">-&gt;</span>contents<span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>*--p<span class="Delimiter">-&gt;</span>contents<span class="Delimiter">.</span>end<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> out &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  string result = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  truncate<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
  <span class="Identifier">return</span> new_mu_string<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> trace_app_contents<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!Trace_stream<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  ostringstream out<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>vector&lt;trace_line&gt;::iterator p = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-&gt;</span>depth != App_depth<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    out &lt;&lt; p<span class="Delimiter">-&gt;</span>contents<span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>*--p<span class="Delimiter">-&gt;</span>contents<span class="Delimiter">.</span>end<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> out &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  string result = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  truncate<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
  <span class="Identifier">return</span> new_mu_string<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> truncate<span class="Delimiter">(</span>string&amp; x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>x<span class="Delimiter">)</span> &gt; <span class="Constant">1024</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    x<span class="Delimiter">.</span>erase<span class="Delimiter">(</span><span class="Constant">1024</span><span class="Delimiter">);</span>
    *x<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> = <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
    *++x<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> = <span class="Constant">'.'</span><span class="Delimiter">;</span>
    *++++x<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> = <span class="Constant">'.'</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Comment">//: simpler version of run-interactive: doesn't do any running, just loads</span>
<span class="Comment">//: recipes and reports errors.</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
RELOAD<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;reload&quot;</span><span class="Delimiter">,</span> RELOAD<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> RELOAD: <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'reload' requires exactly one ingredient, but got &quot;</span> &lt;&lt; to_original_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!is_mu_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;first ingredient of 'reload' should be a string, but got &quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> RELOAD: <span class="Delimiter">{</span>
  <span class="Comment">// conundrum: need to support repeated reloads of the same code, but not</span>
  <span class="Comment">// wipe out state for the current test</span>
  <span class="Comment">// hacky solution: subset of restore_snapshots() without restoring recipes {</span>
  <span class="Comment">// can't yet define containers in a test that runs 'reload'</span>
  Type_ordinal = Type_ordinal_snapshot<span class="Delimiter">;</span>
  Type = Type_snapshot<span class="Delimiter">;</span>
  <span class="Comment">// can't yet create new specializations of shape-shifting recipes in a test</span>
  <span class="Comment">// that runs 'reload'</span>
  Recipe_variants = Recipe_variants_snapshot<span class="Delimiter">;</span>
  Name = Name_snapshot<span class="Delimiter">;</span>
  <span class="Comment">// }</span>
  string code = read_mu_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>
  run_code_begin<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">should_stash_snapshots</span><span class="Comment">*/</span><span class="Constant">false</span><span class="Delimiter">);</span>
  routine* save_current_routine = Current_routine<span class="Delimiter">;</span>
  Current_routine = <span class="Constant">NULL</span><span class="Delimiter">;</span>
  vector&lt;recipe_ordinal&gt; recipes_reloaded = load<span class="Delimiter">(</span>code<span class="Delimiter">);</span>
  transform_all<span class="Delimiter">();</span>
  Trace_stream<span class="Delimiter">-&gt;</span>newline<span class="Delimiter">();</span>  <span class="Comment">// flush trace</span>
  Current_routine = save_current_routine<span class="Delimiter">;</span>
  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</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>trace_error_contents<span class="Delimiter">());</span>
  run_code_end<span class="Delimiter">();</span>  <span class="Comment">// wait until we're done with the trace contents</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario reload_continues_past_error)</span>
def main [
  local-scope
  <span class="Normal">x</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [recipe foo [
  get <span class="Constant">1234</span>:number<span class="Delimiter">,</span> foo:offset
]]
  reload x
  <span class="Constant">1</span>:number/<span class="Special">raw &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+mem: storing 34 in location 1</span>

<span class="Delimiter">:(scenario reload_can_repeatedly_load_container_definitions)</span>
<span class="Comment"># define a container and try to create it (merge requires knowing container size)</span>
def main [
  local-scope
  <span class="Normal">x</span>:address:array:character<span class="Special"> &lt;- </span><span class="Normal">new</span> [
    container foo [
      <span class="Normal">x</span>:number
      <span class="Normal">y</span>:number
    ]
    recipe bar [
      local-scope
      <span class="Normal">x</span>:foo<span class="Special"> &lt;- </span>merge <span class="Constant">34</span><span class="Delimiter">,</span> <span class="Constant">35</span>
    ]
  ]
  <span class="Comment"># save warning addresses in locations of type 'number' to avoid spurious changes to them due to 'abandon'</span>
  <span class="Constant">1</span>:number/<span class="Special">raw &lt;- </span>reload x
  <span class="Constant">2</span>:number/<span class="Special">raw &lt;- </span>reload x
]
<span class="Comment"># no errors on either load</span>
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->