about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-12-15 09:33:56 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-12-15 10:20:41 -0800
commit62a390ca0a27daa80ce4b6b17934d8d067db8631 (patch)
tree8e7131d8770e2c150f8124fa449c38f39c9a9dbc
parent601ff75bc76b5cdc76d54a399dd764cf399822e3 (diff)
downloadmu-62a390ca0a27daa80ce4b6b17934d8d067db8631.tar.gz
support immutability checks in sandbox/ app
-rw-r--r--060immutable.cc29
-rw-r--r--sandbox/001-editor.mu2
-rw-r--r--sandbox/002-typing.mu8
-rw-r--r--sandbox/003-shortcuts.mu14
-rw-r--r--sandbox/004-programming-environment.mu6
-rw-r--r--sandbox/005-sandbox.mu2
-rw-r--r--sandbox/006-sandbox-edit.mu2
-rw-r--r--sandbox/007-sandbox-delete.mu2
8 files changed, 47 insertions, 18 deletions
diff --git a/060immutable.cc b/060immutable.cc
index f57d7c23..1ba5aa24 100644
--- a/060immutable.cc
+++ b/060immutable.cc
@@ -123,6 +123,26 @@ recipe bar x:address:test-list -> y:address:test-list [
 ]
 $warn: 0
 
+:(scenario handle_optional_ingredients_in_immutability_checks)
+% Hide_warnings = true;
+recipe main [
+  k:address:number <- new number:type
+  test k
+]
+# recipe taking an immutable address ingredient
+recipe test k:address:number [
+  local-scope
+  load-ingredients
+  foo k
+]
+# ..calling a recipe with an optional address ingredient
+recipe foo -> [
+  local-scope
+  load-ingredients
+  k:address:number, found?:boolean <- next-ingredient
+]
+$warn: 0
+
 :(before "End Transforms")
 Transform.push_back(check_immutable_ingredients);  // idempotent
 
@@ -132,9 +152,11 @@ void check_immutable_ingredients(recipe_ordinal r) {
   // call get-address or index-address with it, and that any non-primitive
   // recipe calls in the body aren't returning it as a product.
   const recipe& caller = get(Recipe, r);
+//?   cerr << caller.name << '\n';
   if (!caller.has_header) return;  // skip check for old-style recipes calling next-ingredient directly
   for (long long int i = 0; i < SIZE(caller.ingredients); ++i) {
     const reagent& current_ingredient = caller.ingredients.at(i);
+//?     cerr << "  " << current_ingredient.original_string << '\n';
     if (!is_mu_address(current_ingredient)) continue;  // will be copied
     if (is_present_in_products(caller, current_ingredient.name)) continue;  // not expected to be immutable
     // End Immutable Ingredients Special-cases
@@ -142,6 +164,7 @@ void check_immutable_ingredients(recipe_ordinal r) {
     immutable_vars.insert(current_ingredient.name);
     for (long long int i = 0; i < SIZE(caller.steps); ++i) {
       const instruction& inst = caller.steps.at(i);
+//?       cerr << "    " << inst.to_string() << '\n';
       check_immutable_ingredient_in_instruction(inst, immutable_vars, current_ingredient.name, caller);
       update_aliases(inst, immutable_vars);
     }
@@ -171,8 +194,10 @@ void update_aliases(const instruction& inst, set<string>& current_ingredient_and
 set<long long int> scan_contained_in_product_indices(const instruction& inst, set<long long int>& ingredient_indices) {
   set<string> selected_ingredient_names;
   const recipe& callee = get(Recipe, inst.operation);
-  for (set<long long int>::iterator p = ingredient_indices.begin(); p != ingredient_indices.end(); ++p)
+  for (set<long long int>::iterator p = ingredient_indices.begin(); p != ingredient_indices.end(); ++p) {
+    if (*p >= SIZE(callee.ingredients)) continue;  // optional immutable ingredient
     selected_ingredient_names.insert(callee.ingredients.at(*p).name);
+  }
   set<long long int> result;
   for (long long int i = 0; i < SIZE(callee.products); ++i) {
     const reagent& current_product = callee.products.at(i);
@@ -213,6 +238,7 @@ void check_immutable_ingredient_in_instruction(const instruction& inst, const se
   if (current_ingredient_indices.empty()) return;  // ingredient not found in call
   for (set<long long int>::iterator p = current_ingredient_indices.begin(); p != current_ingredient_indices.end(); ++p) {
     const long long int current_ingredient_index = *p;
+//?     cerr << "      ingredient index: " << *p << '\n';
     reagent current_ingredient = inst.ingredients.at(current_ingredient_index);
     canonize_type(current_ingredient);
     const string& current_ingredient_name = current_ingredient.name;
@@ -244,6 +270,7 @@ bool is_modified_in_recipe(recipe_ordinal r, long long int ingredient_index, con
     raise << maybe(caller.name) << "can't check mutability of ingredients in " << callee.name << " because it uses 'next-ingredient' directly, rather than a recipe header.\n" << end();
     return true;
   }
+  if (ingredient_index >= SIZE(callee.ingredients)) return false;  // optional immutable ingredient
   return is_present_in_products(callee, callee.ingredients.at(ingredient_index).name);
 }
 
diff --git a/sandbox/001-editor.mu b/sandbox/001-editor.mu
index 56c98d81..4ef9be36 100644
--- a/sandbox/001-editor.mu
+++ b/sandbox/001-editor.mu
@@ -252,7 +252,7 @@ recipe clear-screen-from screen:address:screen, row:number, column:number, left:
   reply screen/same-as-ingredient:0
 ]
 
-recipe clear-rest-of-screen screen:address:screen, row:number, left:number, right:number [
+recipe clear-rest-of-screen screen:address:screen, row:number, left:number, right:number -> screen:address:screen [
   local-scope
   load-ingredients
   row <- add row, 1
diff --git a/sandbox/002-typing.mu b/sandbox/002-typing.mu
index 2b446101..037c222b 100644
--- a/sandbox/002-typing.mu
+++ b/sandbox/002-typing.mu
@@ -11,7 +11,7 @@ recipe! main text:address:array:character [
   close-console
 ]
 
-recipe editor-event-loop screen:address:screen, console:address:console, editor:address:editor-data [
+recipe editor-event-loop screen:address:screen, console:address:console, editor:address:editor-data -> screen:address:screen, console:address:console, editor:address:editor-data [
   local-scope
   load-ingredients
   {
@@ -45,7 +45,7 @@ recipe editor-event-loop screen:address:screen, console:address:console, editor:
 ]
 
 # process click, return if it was on current editor
-recipe move-cursor-in-editor screen:address:screen, editor:address:editor-data, t:touch-event -> in-focus?:boolean [
+recipe move-cursor-in-editor screen:address:screen, editor:address:editor-data, t:touch-event -> in-focus?:boolean, editor:address:editor-data [
   local-scope
   load-ingredients
   reply-unless editor, 0/false
@@ -259,7 +259,7 @@ recipe insert-at-cursor editor:address:editor-data, c:character, screen:address:
 ]
 
 # helper for tests
-recipe editor-render screen:address:screen, editor:address:editor-data -> screen:address:screen [
+recipe editor-render screen:address:screen, editor:address:editor-data -> screen:address:screen, editor:address:editor-data [
   local-scope
   load-ingredients
   left:number <- get *editor, left:offset
@@ -1012,7 +1012,7 @@ after <handle-special-key> [
 
 ## helpers
 
-recipe draw-horizontal screen:address:screen, row:number, x:number, right:number [
+recipe draw-horizontal screen:address:screen, row:number, x:number, right:number -> screen:address:screen [
   local-scope
   load-ingredients
   style:character, style-found?:boolean <- next-ingredient
diff --git a/sandbox/003-shortcuts.mu b/sandbox/003-shortcuts.mu
index b3676efb..79d53b2a 100644
--- a/sandbox/003-shortcuts.mu
+++ b/sandbox/003-shortcuts.mu
@@ -84,6 +84,7 @@ recipe delete-before-cursor editor:address:editor-data, screen:address:screen ->
   local-scope
   load-ingredients
   before-cursor:address:address:duplex-list:character <- get-address *editor, before-cursor:offset
+  data:address:duplex-list:character <- get *editor, data:offset
   # if at start of text (before-cursor at § sentinel), return
   prev:address:duplex-list:character <- prev *before-cursor
   go-render?, backspaced-cell <- copy 0/no-more-render, 0/nothing-deleted
@@ -92,7 +93,7 @@ recipe delete-before-cursor editor:address:editor-data, screen:address:screen ->
   original-row:number <- get *editor, cursor-row:offset
   editor, scroll?:boolean <- move-cursor-coordinates-left editor
   backspaced-cell:address:duplex-list:character <- copy *before-cursor
-  remove *before-cursor  # will also neatly trim next/prev pointers in backspaced-cell/*before-cursor
+  data <- remove *before-cursor, data  # will also neatly trim next/prev pointers in backspaced-cell/*before-cursor
   *before-cursor <- copy prev
   go-render? <- copy 1/true
   reply-if scroll?
@@ -333,11 +334,12 @@ recipe delete-at-cursor editor:address:editor-data, screen:address:screen -> edi
   local-scope
   load-ingredients
   before-cursor:address:address:duplex-list:character <- get-address *editor, before-cursor:offset
+  data:address:duplex-list:character <- get *editor, data:offset
   deleted-cell:address:duplex-list:character <- next *before-cursor
   go-render? <- copy 0/false
   reply-unless deleted-cell
   currc:character <- get *deleted-cell, value:offset
-  remove deleted-cell
+  data <- remove deleted-cell, data
   deleted-newline?:boolean <- equal currc, 10/newline
   go-render? <- copy 1/true
   reply-if deleted-newline?
@@ -1244,7 +1246,7 @@ after <handle-special-key> [
   }
 ]
 
-recipe move-to-start-of-line editor:address:editor-data [
+recipe move-to-start-of-line editor:address:editor-data -> editor:address:editor-data [
   local-scope
   load-ingredients
   # update cursor column
@@ -1415,7 +1417,7 @@ after <handle-special-key> [
   }
 ]
 
-recipe move-to-end-of-line editor:address:editor-data [
+recipe move-to-end-of-line editor:address:editor-data -> editor:address:editor-data [
   local-scope
   load-ingredients
   before-cursor:address:address:duplex-list:character <- get-address *editor, before-cursor:offset
@@ -1545,7 +1547,7 @@ after <handle-special-character> [
   }
 ]
 
-recipe delete-to-start-of-line editor:address:editor-data -> result:address:duplex-list:character [
+recipe delete-to-start-of-line editor:address:editor-data -> result:address:duplex-list:character, editor:address:editor-data [
   local-scope
   load-ingredients
   # compute range to delete
@@ -1679,7 +1681,7 @@ after <handle-special-character> [
   }
 ]
 
-recipe delete-to-end-of-line editor:address:editor-data -> result:address:duplex-list:character [
+recipe delete-to-end-of-line editor:address:editor-data -> result:address:duplex-list:character, editor:address:editor-data [
   local-scope
   load-ingredients
   # compute range to delete
diff --git a/sandbox/004-programming-environment.mu b/sandbox/004-programming-environment.mu
index c6742397..1535e75c 100644
--- a/sandbox/004-programming-environment.mu
+++ b/sandbox/004-programming-environment.mu
@@ -19,7 +19,7 @@ container programming-environment-data [
   current-sandbox:address:editor-data
 ]
 
-recipe new-programming-environment screen:address:screen, initial-sandbox-contents:address:array:character -> result:address:programming-environment-data [
+recipe new-programming-environment screen:address:screen, initial-sandbox-contents:address:array:character -> result:address:programming-environment-data, screen:address:screen [
   local-scope
   load-ingredients
   width:number <- screen-width screen
@@ -38,7 +38,7 @@ recipe new-programming-environment screen:address:screen, initial-sandbox-conten
   *current-sandbox <- new-editor initial-sandbox-contents, screen, 0, width/right
 ]
 
-recipe event-loop screen:address:screen, console:address:console, env:address:programming-environment-data [
+recipe event-loop screen:address:screen, console:address:console, env:address:programming-environment-data -> screen:address:screen, console:address:console, env:address:programming-environment-data [
   local-scope
   load-ingredients
   current-sandbox:address:editor-data <- get *env, current-sandbox:offset
@@ -135,7 +135,7 @@ recipe event-loop screen:address:screen, console:address:console, env:address:pr
   }
 ]
 
-recipe resize screen:address:screen, env:address:programming-environment-data -> env:address:programming-environment-data [
+recipe resize screen:address:screen, env:address:programming-environment-data -> env:address:programming-environment-data, screen:address:screen [
   local-scope
   load-ingredients
   clear-screen screen  # update screen dimensions
diff --git a/sandbox/005-sandbox.mu b/sandbox/005-sandbox.mu
index da5bd54c..db0a195b 100644
--- a/sandbox/005-sandbox.mu
+++ b/sandbox/005-sandbox.mu
@@ -222,7 +222,7 @@ recipe! render-sandbox-side screen:address:screen, env:address:programming-envir
   clear-rest-of-screen screen, row, left, left, right
 ]
 
-recipe render-sandboxes screen:address:screen, sandbox:address:sandbox-data, left:number, right:number, row:number -> row:number, screen:address:screen [
+recipe render-sandboxes screen:address:screen, sandbox:address:sandbox-data, left:number, right:number, row:number -> row:number, screen:address:screen, sandbox:address:sandbox-data [
   local-scope
   load-ingredients
   env:address:programming-environment-data, _/optional <- next-ingredient
diff --git a/sandbox/006-sandbox-edit.mu b/sandbox/006-sandbox-edit.mu
index e021b961..83257df0 100644
--- a/sandbox/006-sandbox-edit.mu
+++ b/sandbox/006-sandbox-edit.mu
@@ -98,7 +98,7 @@ recipe empty-editor? editor:address:editor-data -> result:boolean [
   result <- not first
 ]
 
-recipe extract-sandbox env:address:programming-environment-data, click-row:number -> result:address:sandbox-data [
+recipe extract-sandbox env:address:programming-environment-data, click-row:number -> result:address:sandbox-data, env:address:programming-environment-data [
   local-scope
   load-ingredients
   # assert click-row >= sandbox.starting-row-on-screen
diff --git a/sandbox/007-sandbox-delete.mu b/sandbox/007-sandbox-delete.mu
index 85d7af28..b0b6f4ee 100644
--- a/sandbox/007-sandbox-delete.mu
+++ b/sandbox/007-sandbox-delete.mu
@@ -76,7 +76,7 @@ after <global-touch> [
   }
 ]
 
-recipe delete-sandbox t:touch-event, env:address:programming-environment-data -> was-delete?:boolean [
+recipe delete-sandbox t:touch-event, env:address:programming-environment-data -> was-delete?:boolean, env:address:programming-environment-data [
   local-scope
   load-ingredients
   click-column:number <- get t, column:offset