about summary refs log blame commit diff stats
path: root/062rewrite_stash.cc
blob: 5c6f28ca2f571e7d2b3eb236cc592dca2cdc3d0a (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                       

                                                                                      






                                                                            
                                              

          

                                                                                                                
 







                                             








                                                                                                    
                                                 








                                                        



                                             

                                                           

                                                              
                        
                                                                    
                                          
                                                       






                                                            
                                      
                                                                                   











                                                                
 
                                                       
                                              



                                      
                                        



                                 























                                                                          
                                                                                                                                         

          
                        







                                                                                                                
//: when encountering other types, try to convert them to strings using
//: 'to-text'

:(scenarios transform)
:(scenario rewrite_stashes_to_text)
recipe main [
  local-scope
  n:number <- copy 34
  stash n
]
+transform: {stash_2_0: ("address" "array" "character")} <- to-text-line {n: "number"}
+transform: stash {stash_2_0: ("address" "array" "character")}

//: special case: rewrite attempts to stash contents of most arrays to avoid
//: passing addresses around

:(scenario rewrite_stashes_of_arrays)
recipe main [
  local-scope
  n:address:array:number <- new number:type, 3
  stash *n
]
+transform: {stash_2_0: ("address" "array" "character")} <- array-to-text-line {n: ("address" "array" "number")}
+transform: stash {stash_2_0: ("address" "array" "character")}

:(scenario ignore_stashes_of_static_arrays)
recipe main [
  local-scope
  n:array:number:3 <- create-array
  stash n
]
+transform: stash {n: ("array" "number" "3")}

:(before "End Instruction Inserting/Deleting Transforms")
Transform.push_back(rewrite_stashes_to_text);

:(code)
void rewrite_stashes_to_text(recipe_ordinal r) {
  recipe& caller = get(Recipe, r);
  trace(9991, "transform") << "--- rewrite 'stash' instructions in recipe " << caller.name << end();
  // in recipes without named locations, 'stash' is still not extensible
  if (contains_numeric_locations(caller)) return;
  check_or_set_types_by_name(r);  // prerequisite
  rewrite_stashes_to_text(caller);
}

void rewrite_stashes_to_text(recipe& caller) {
  vector<instruction> new_instructions;
  for (int i = 0; i < SIZE(caller.steps); ++i) {
    instruction& inst = caller.steps.at(i);
    if (inst.name == "stash") {
      for (int j = 0; j < SIZE(inst.ingredients); ++j) {
        if (!inst.ingredients.at(j).type) {
          // error; will be handled elsewhere
          continue;
        }
        if (is_literal(inst.ingredients.at(j))) continue;
        if (is_mu_string(inst.ingredients.at(j))) continue;
        // don't try to extend static arrays
        if (is_static_array(inst.ingredients.at(j))) continue;
        instruction def;
        if (is_lookup_of_address_of_array(inst.ingredients.at(j))) {
          def.name = "array-to-text-line";
          reagent/*copy*/ tmp = inst.ingredients.at(j);
          drop_one_lookup(tmp);
          def.ingredients.push_back(tmp);
        }
        else {
          def.name = "to-text-line";
          def.ingredients.push_back(inst.ingredients.at(j));
        }
        ostringstream ingredient_name;
        ingredient_name << "stash_" << i << '_' << j << ":address:array:character";
        def.products.push_back(reagent(ingredient_name.str()));
        trace(9993, "transform") << to_string(def) << end();
        new_instructions.push_back(def);
        inst.ingredients.at(j).clear();  // reclaim old memory
        inst.ingredients.at(j) = reagent(ingredient_name.str());
      }
    }
    trace(9993, "transform") << to_string(inst) << end();
    new_instructions.push_back(inst);
  }
  caller.steps.swap(new_instructions);
}

bool is_lookup_of_address_of_array(reagent/*copy*/ x) {
  if (x.type->name != "address") return false;
  if (!canonize_type(x)) return false;
  return x.type->name == "array";
}

bool is_static_array(const reagent& x) {
  // no canonize_type()
  return x.type->name == "array";
}

//: Make sure that the new system is strictly better than just the 'stash'
//: primitive by itself.

:(scenarios run)
:(scenario rewrite_stash_continues_to_fall_back_to_default_implementation)
# type without a to-text implementation
container foo [
  x:number
  y:number
]
recipe main [
  local-scope
  x:foo <- merge 34, 35
  stash x
]
+app: 34 35

:(before "End Primitive Recipe Declarations")
TO_TEXT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "to-text", TO_TEXT);
:(before "End Primitive Recipe Checks")
case TO_TEXT: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'to-text' requires a single ingredient, but got '" << inst.original_string << "'\n" << end();
    break;
  }
  // can handle any type
  break;
}
:(before "End Primitive Recipe Implementations")
case TO_TEXT: {
  products.resize(1);
  products.at(0).push_back(new_mu_string(print_mu(current_instruction().ingredients.at(0), ingredients.at(0))));
  break;
}
s='alt'>




                                    





                                                          

 

                                                   
                                                  

                                                                   
                                               

                                                                                  
                                                                  

                                                                                    
                                         


                                                                                     
                                                                               




                                                    
                                                     
                                                                                    





                          
                                                                                                                             
             
                  
                                                      
                                                          




                                                                                   
                                                                          








                                                                                      
                                                                   
                                                                                 

                                                                

 
                                                                                           
             
                  
                                                                                     

                                        





                                                           
                                                                     
                                                                
   





                                                                             
                                                                         
                                                                                       
                                                                   
                                                                              

                                            
                                                                                  


                                                
                                                                                    



                                  

                                          
                                                                     
 
## clicking on sandbox results to 'fix' them and turn sandboxes into tests

scenario sandbox-click-on-result-toggles-color-to-green [
  trace-until 100/app  # trace too long
  assume-screen 100/width, 10/height
  # basic recipe
  1:address:array:character <- new [ 
recipe foo [
  reply 4
]]
  # run it
  2:address:array:character <- new [foo]
  assume-console [
    press F4
  ]
  3:address:programming-environment-data <- new-programming-environment screen:address:screen, 1:address:array:character, 2:address:array:character
  event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .recipe foo [                                      ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━.
    .  reply 4                                         0   edit          copy            delete         .
    .]                                                 foo                                              .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊4                                                .
    .                                                  ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━.
    .                                                                                                   .
  ]
  # click on the '4' in the result
  assume-console [
    left-click 5, 51
  ]
  run [
    event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data
  ]
  # color toggles to green
  screen-should-contain-in-color 2/green, [
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                   4                                                .
    .                                                                                                    .
    .                                                                                                    .
  ]
  # cursor should remain unmoved
  run [
    4:character/cursor <- copy 9251/    print screen:address:screen, 4:character/cursor
  ]
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                  .
    .recipe foo [                                      ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━.
    .  reply 4                                         0   edit          copy            delete         .
    .]                                                 foo                                              .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊4                                                .
    .                                                  ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━.
    .                                                                                                   .
    .                                                                                                   .
    .                                                                                                   .
  ]
  # now change the result
  # then rerun
  assume-console [
    left-click 3, 11  # cursor to end of line
    press backspace
    type [3]
    press F4
  ]
  run [
    event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data
  ]
  # result turns red
  screen-should-contain-in-color 1/red, [
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                   3                                                .
    .                                                                                                    .
    .                                                                                                    .
  ]
]

# this requires tracking a couple more things
container sandbox-data [
  response-starting-row-on-screen:number
  expected-response:address:array:character
]

# include expected response when saving or restoring a sandbox
before <end-save-sandbox> [
  {
    expected-response:address:array:character <- get *curr, expected-response:offset
    break-unless expected-response
    filename <- append filename, [.out]
    save filename, expected-response
  }
]

before <end-restore-sandbox> [
  {
    filename <- append filename, [.out]
    contents <- restore filename
    break-unless contents
    *curr <- put *curr, expected-response:offset, contents
  }
]

# clicks on sandbox responses save it as 'expected'
after <global-touch> [
  # check if it's inside the output of any sandbox
  {
    sandbox-left-margin:number <- get *current-sandbox, left:offset
    click-column:number <- get t, column:offset
    on-sandbox-side?:boolean <- greater-or-equal click-column, sandbox-left-margin
    break-unless on-sandbox-side?
    first-sandbox:address:sandbox-data <- get *env, sandbox:offset
    break-unless first-sandbox
    first-sandbox-begins:number <- get *first-sandbox, starting-row-on-screen:offset
    click-row:number <- get t, row:offset
    below-sandbox-editor?:boolean <- greater-or-equal click-row, first-sandbox-begins
    break-unless below-sandbox-editor?
    # identify the sandbox whose output is being clicked on
    sandbox:address:sandbox-data <- find-click-in-sandbox-output env, click-row
    break-unless sandbox
    # toggle its expected-response, and save session
    sandbox <- toggle-expected-response sandbox
    save-sandboxes env
    hide-screen screen
    screen <- render-sandbox-side screen, env, render
    screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
    # no change in cursor
    show-screen screen
    loop +next-event:label
  }
]

def find-click-in-sandbox-output env:address:programming-environment-data, click-row:number -> sandbox:address:sandbox-data [
  local-scope
  load-ingredients
  # assert click-row >= sandbox.starting-row-on-screen
  sandbox:address:sandbox-data <- get *env, sandbox:offset
  start:number <- get *sandbox, starting-row-on-screen:offset
  clicked-on-sandboxes?:boolean <- greater-or-equal click-row, start
  assert clicked-on-sandboxes?, [extract-sandbox called on click to sandbox editor]
  # while click-row < sandbox.next-sandbox.starting-row-on-screen
  {
    next-sandbox:address:sandbox-data <- get *sandbox, next-sandbox:offset
    break-unless next-sandbox
    next-start:number <- get *next-sandbox, starting-row-on-screen:offset
    found?:boolean <- lesser-than click-row, next-start
    break-if found?
    sandbox <- copy next-sandbox
    loop
  }
  # return sandbox if click is in its output region
  response-starting-row:number <- get *sandbox, response-starting-row-on-screen:offset
  return-unless response-starting-row, 0/no-click-in-sandbox-output
  click-in-response?:boolean <- greater-or-equal click-row, response-starting-row
  return-unless click-in-response?, 0/no-click-in-sandbox-output
  return sandbox
]

def toggle-expected-response sandbox:address:sandbox-data -> sandbox:address:sandbox-data [
  local-scope
  load-ingredients
  expected-response:address:array:character <- get *sandbox, expected-response:offset
  {
    # if expected-response is set, reset
    break-unless expected-response
    *sandbox <- put *sandbox, expected-response:offset, 0
  }
  {
    # if not, set expected response to the current response
    break-if expected-response
    response:address:array:character <- get *sandbox, response:offset
    *sandbox <- put *sandbox, expected-response:offset, response
  }
]

# when rendering a sandbox, color it in red/green if expected response exists
after <render-sandbox-response> [
  {
    break-unless sandbox-response
    *sandbox <- put *sandbox, response-starting-row-on-screen:offset, row
    expected-response:address:array:character <- get *sandbox, expected-response:offset
    break-unless expected-response  # fall-through to print in grey
    response-is-expected?:boolean <- equal expected-response, sandbox-response
    {
      break-if response-is-expected?:boolean
      row, screen <- render-text screen, sandbox-response, left, right, 1/red, row
    }
    {
      break-unless response-is-expected?:boolean
      row, screen <- render-text screen, sandbox-response, left, right, 2/green, row
    }
    jump +render-sandbox-end:label
  }
]

before <end-render-sandbox-reset-hidden> [
  *sandbox <- put *sandbox, response-starting-row-on-screen:offset, 0
]