about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-07-18 15:09:13 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-07-18 15:09:13 -0700
commit614ea44bc2708a687ba10b162a6dc70e48dfac59 (patch)
treeff31df45293b44282e91b416c04cde953abd9255
parent11c53a59dc6a6890889347484b73e34dd9bb2636 (diff)
downloadmu-614ea44bc2708a687ba10b162a6dc70e48dfac59.tar.gz
1817 - save for sandboxes
No restore. We'll have to do that manually for the first lesson.

The version control is also super ugly; every save creates a new commit.
But that's ok; version control is just the backup of last resort.
-rw-r--r--082persist.cc26
-rw-r--r--edit.mu18
2 files changed, 34 insertions, 10 deletions
diff --git a/082persist.cc b/082persist.cc
index 63516bb2..8e05981c 100644
--- a/082persist.cc
+++ b/082persist.cc
@@ -1,6 +1,6 @@
 //: Dead simple persistence.
-//:   'restore' - reads string from a hardcoded file
-//:   'save' - writes string to a hardcoded file
+//:   'restore' - reads string from a file
+//:   'save' - writes string to a file
 
 :(before "End Primitive Recipe Declarations")
 RESTORE,
@@ -8,13 +8,16 @@ RESTORE,
 Recipe_ordinal["restore"] = RESTORE;
 :(before "End Primitive Recipe Implementations")
 case RESTORE: {
+  if (!scalar(ingredients.at(0)))
+    raise << "restore: illegal operand " << current_instruction().ingredients.at(0).to_string() << '\n';
   products.resize(1);
-  products.at(0).push_back(new_string(slurp("lesson/recipe.mu")));
+  products.at(0).push_back(new_string(slurp("lesson/"+current_instruction().ingredients.at(0).name)));
   break;
 }
 
 :(code)
 string slurp(const string& filename) {
+//?   cerr << filename << '\n'; //? 1
   ostringstream result;
   ifstream fin(filename.c_str());
   fin.peek();
@@ -27,6 +30,7 @@ string slurp(const string& filename) {
     result << buf;
   }
   fin.close();
+//?   cerr << "=> " << result.str(); //? 1
   return result.str();
 }
 
@@ -37,14 +41,22 @@ Recipe_ordinal["save"] = SAVE;
 :(before "End Primitive Recipe Implementations")
 case SAVE: {
   if (!scalar(ingredients.at(0)))
-    raise << "save: illegal operand " << current_instruction().ingredients.at(0).to_string() << '\n';
-  string contents = to_string(ingredients.at(0).at(0));
-  ofstream fout("lesson/recipe.mu");
+    raise << "save: illegal operand 0 " << current_instruction().ingredients.at(0).to_string() << '\n';
+  string filename = current_instruction().ingredients.at(0).name;
+  if (!is_literal(current_instruction().ingredients.at(0))) {
+    ostringstream tmp;
+    tmp << ingredients.at(0).at(0);
+    filename = tmp.str();
+  }
+  ofstream fout(("lesson/"+filename).c_str());
+  if (!scalar(ingredients.at(1)))
+    raise << "save: illegal operand 1 " << current_instruction().ingredients.at(1).to_string() << '\n';
+  string contents = to_string(ingredients.at(1).at(0));
   fout << contents;
   fout.close();
   if (!exists("lesson/.git")) break;
   // bug in git: git diff -q messes up --exit-code
-  int status = system("cd lesson; git diff --exit-code >/dev/null || git commit -a -m . >/dev/null");
+  int status = system("cd lesson; git add .; git diff HEAD --exit-code >/dev/null || git commit -a -m . >/dev/null");
   if (status != 0)
     raise << "error in commit: contents " << contents << '\n';
   break;
diff --git a/edit.mu b/edit.mu
index e6f4b014..0ab0d174 100644
--- a/edit.mu
+++ b/edit.mu
@@ -2,8 +2,9 @@
 
 recipe main [
   local-scope
-  open-console
-  initial-recipe:address:array:character <- restore
+  open-console #? 1
+  initial-recipe:address:array:character <- restore [recipes.mu]
+#?   $exit #? 1
   initial-sandbox:address:array:character <- new [test 2, 2]
   env:address:programming-environment-data <- new-programming-environment 0:literal/screen, initial-recipe:address:array:character, initial-sandbox:address:array:character
   event-loop 0:literal/screen, 0:literal/console, env:address:programming-environment-data
@@ -2770,7 +2771,7 @@ recipe run-sandboxes [
   current-sandbox:address:editor-data <- get env:address:programming-environment-data/deref, current-sandbox:offset
   # copy code from recipe editor, persist, load into mu, save any warnings
   in:address:array:character <- editor-contents recipes:address:editor-data
-  save in:address:array:character
+  save [recipes.mu], in:address:array:character
   recipe-warnings:address:address:array:character <- get-address env:address:programming-environment-data/deref, recipe-warnings:offset
   recipe-warnings:address:address:array:character/deref <- reload in:address:array:character
   # check contents of right editor (sandbox)
@@ -2791,6 +2792,17 @@ recipe run-sandboxes [
     init:address:address:duplex-list <- get-address current-sandbox:address:editor-data/deref, data:offset
     init:address:address:duplex-list/deref <- push-duplex 167:literal/§, 0:literal/tail
   }
+  # save all sandboxes before running, just in case we die when running
+  curr:address:sandbox-data <- get env:address:programming-environment-data/deref, sandbox:offset
+  filename:number <- copy 0:literal
+  {
+    break-unless curr:address:sandbox-data
+    data:address:address:array:character <- get-address curr:address:sandbox-data/deref, data:offset
+    save filename:number, data:address:address:array:character/deref
+    filename:number <- add filename:number, 1:literal
+    curr:address:sandbox-data <- get curr:address:sandbox-data/deref, next-sandbox:offset
+    loop
+  }
   # run all sandboxes
   curr:address:sandbox-data <- get env:address:programming-environment-data/deref, sandbox:offset
   {