From 6ae27506fa1c9c1bfcc13041a2139f5cd19e2545 Mon Sep 17 00:00:00 2001
From: Vincent Nivoliers <vincent.nivoliers@gmail.com>
Date: Wed, 21 Sep 2016 00:48:17 +0200
Subject: [PATCH] debugging compilation, improved notes

---
 css/style.css          |  3 ++-
 js/notes.js            |  7 ++---
 js/pgm_compiler.js     | 60 +++++++++++++++++++++++++++++-------------
 js/pgm_construction.js |  1 +
 4 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/css/style.css b/css/style.css
index d73b2e6..4b823c9 100644
--- a/css/style.css
+++ b/css/style.css
@@ -373,6 +373,7 @@ script {
 
 #pgm-main {
   text-align : left ;
+  min-height : 35em ;
 }
 
 .pgm > .panel-heading {
@@ -452,7 +453,7 @@ script {
   display : inline-block ;
 }
 
-.pgm-recv .list-group-item {
+#pgm .pgm-recv > .list-group-item {
   border : 1px solid #ddd ;
   border-radius : 5px ;
   vertical-align : middle ;
diff --git a/js/notes.js b/js/notes.js
index 2df541c..3438a7b 100644
--- a/js/notes.js
+++ b/js/notes.js
@@ -112,7 +112,8 @@ Notes.set_variable = function(name, value, global) {
   if(notes_elt.value.match(re)) {
     notes_elt.value = notes_elt.value.replace(re, "$1" + value + "$3") ;
   } else {
-    notes_elt.value = notes_elt.value + "\n[ " + name + " = " + value + " ]" ;
+    var prefix = notes_elt.value.match(/\S+$/) ? "\n" : "" ;
+    notes_elt.value = notes_elt.value + prefix + "[ " + name + " = " + value + " ]" ;
   }
   Notes.save() ;
 }
@@ -138,7 +139,7 @@ Notes.check_variable = function(name, global) {
 
 /* clear a variable locally or globally */
 Notes.clear_variable = function(name, global) {
-  var re = new RegExp("\\[ " + name + " = ([^\\[\\]=]*) \\]", "g") ;
+  var re = new RegExp("\\[ " + name + " = ([^\\[\\]=]*) \\]\n?", "g") ;
   var notes_elt = global ?
     document.getElementById("global_notes") :
     document.getElementById("local_notes") ;
@@ -148,7 +149,7 @@ Notes.clear_variable = function(name, global) {
 
 /* clear local variables */
 Notes.clear_local_variables = function(name) {
-  var re = new RegExp("\\[ " + name + " = ([^\\[\\]=]*) \\]", "g") ;
+  var re = new RegExp("\\[ " + name + " = ([^\\[\\]=]*) \\]\n?", "g") ;
   for(key in localStorage) {
     if(key.match(storage_prefix + ":local_notes:")) {
       localStorage[key] = localStorage[key].replace(re, "") ;
diff --git a/js/pgm_compiler.js b/js/pgm_compiler.js
index 4276ae5..d843c8e 100644
--- a/js/pgm_compiler.js
+++ b/js/pgm_compiler.js
@@ -31,7 +31,7 @@ function block_compile(el) {
 
 function get_integer(str, error_target) {
   var i = parseInt(str) ;
-  if(!str.match(/^\d+$/) || isNaN(i)) {
+  if(!str.match(/^-?\d+$/) || isNaN(i)) {
     return pgm_selector_error(
         error_target,
         "la valeur fournie n\'est pas un entier : " + str
@@ -41,6 +41,10 @@ function get_integer(str, error_target) {
   }
 }
 
+function translate_bool(b) {
+  return b ? '"vrai"' : '"faux"' ;
+}
+
 function get_boolean(data, error_target) {
   if(typeof data === 'boolean') {
     return data ;
@@ -212,8 +216,8 @@ function get_movement(str, error_target) {
   }
 }
 
-function direction_to_number(colorname) {
-  return dir_list.indexOf(colorname) ;
+function direction_to_number(dirname) {
+  return dir_list.indexOf(dirname) ;
 }
 
 function number_to_direction(number) {
@@ -250,7 +254,7 @@ function color_to_direction(color) {
 }
 
 function direction_to_color(direction) {
-  return number_to_color(direction_to_number(color)) ;
+  return number_to_color(direction_to_number(direction)) ;
 }
 
 function next_direction(direction) {
@@ -412,6 +416,9 @@ function get_variable(name, err_target, global) {
 function command_write_local(el) {
   var name = receiver_compile(el.children[1]) ;
   var value = receiver_compile(el.children[2]) ;
+  value = "typeof " + value + " === 'boolean' ? " 
+    + translate_bool(value) + ' : ' 
+    + value ;
   return 'Explosurf.notes.set_variable(' + name + ', ' + value + ')' ;
 }
 
@@ -452,6 +459,9 @@ pgm_elements['cmd-var-local-delete-world'] = command_delete_all_local ;
 function command_write_global(el) {
   var name = receiver_compile(el.children[1]) ;
   var value = receiver_compile(el.children[2]) ;
+  value = "typeof " + value + " === 'boolean' ? " 
+    + translate_bool(value) + ' : ' 
+    + value ;
   return 'Explosurf.notes.set_variable(' + name + ', ' + value + ', true)' ;
 }
 
@@ -587,14 +597,14 @@ pgm_elements['cmd-test-var-exists-global'] = command_exists_global ;
 
 function command_border_dir(el) {
   var direction = receiver_compile(el.children[0]) ;
-  return 'Explosurf.explorer.check_boundary(direction_to_number(' + name + '))' ;
+  return 'Explosurf.explorer.check_boundary(direction_to_number(' + direction + '))' ;
 }
 
 pgm_elements['cmd-test-boundary-dir'] = command_border_dir ;
 
 function command_border_col(el) {
   var color = receiver_compile(el.children[0]) ;
-  return 'Explosurf.explorer.check_boundary(color_to_number(' + name + '))' ;
+  return 'Explosurf.explorer.check_boundary(color_to_number(' + color + '))' ;
 }
 
 pgm_elements['cmd-test-boundary-col'] = command_border_col ;
@@ -640,8 +650,8 @@ pgm_elements['cmd-arith-div'] = command_div ;
 /* {{{ Strings ============================================================== */
 
 function command_concat(el) {
-  var lhs = receiver_compile(el.children[0]) ;
-  var rhs = receiver_compile(el.children[1]) ;
+  var lhs = receiver_compile(el.children[1]) ;
+  var rhs = receiver_compile(el.children[2]) ;
   return lhs + ' + ' + rhs ;
 }
 
@@ -651,20 +661,32 @@ pgm_elements['cmd-str-concat'] = command_concat ;
 
 /* {{{ Compilation ========================================================== */
 
-function run() {
-  pgm_clear_error() ;
+function show_progress() {
   var progress = document.getElementById("progress") ;
   Pgm.removeClass(progress, 'hidden') ;
+}
+
+function hide_progress() {
+  var progress = document.getElementById("progress") ;
+  Pgm.addClass(progress, 'hidden') ;
+}
+
+function run() {
+  pgm_clear_error() ;
   var el = document.getElementById("pgm-main") ;
-  try {
-    var program = 'try{' + block_compile(el) + '}catch(e){pgm_show_error(e);}' ;
-    eval(program) ;
-    Pgm.addClass(progress, 'hidden') ;
-  } catch (e) {
-    pgm_show_error(e) ;
-    Pgm.addClass(progress, 'hidden') ;
-    throw e ;
-  }
+  show_progress() ;
+  //timeout to allow the browser to update and show the bar
+  setTimeout(function () {
+    try {
+      var program = 'try{' + block_compile(el) + '}catch(e){pgm_show_error(e);}' ;
+      eval(program) ;
+      hide_progress() ;
+    } catch (e) {
+      pgm_show_error(e) ;
+      hide_progress() ;
+      throw e ;
+    }
+  }, 100) ;
 }
 
 document.getElementById("pgm-run").addEventListener('click', run) ;
diff --git a/js/pgm_construction.js b/js/pgm_construction.js
index 2b27256..d17e521 100644
--- a/js/pgm_construction.js
+++ b/js/pgm_construction.js
@@ -107,6 +107,7 @@ function local_load(name) {
   var program = localStorage.getItem(prefix) ;
   if(program) {
     main.innerHTML = program ;
+    update_pgm_ui() ;
   }
 }
 
-- 
GitLab