Browse Source

work in progress....

Igor 2 years ago
parent
commit
86ae07bd74
4 changed files with 131 additions and 14 deletions
  1. 51 14
      js/util/codeParser.js
  2. 39 0
      js/visualUI/functions.js
  3. 20 0
      js/visualUI/globals.js
  4. 21 0
      js/visualUI/variables.js

+ 51 - 14
js/util/codeParser.js

@@ -158,9 +158,17 @@ function parseCommands (commands_block, function_obj) {
       case Models.COMMAND_TYPES.iftrue:
         return parseIfTrue(commands_block, function_obj);
 
-      //case Models.COMMAND_TYPES.repeatNtimes:
-      default:
+      case Models.COMMAND_TYPES.repeatNtimes:
         return parseRepeatNTimes(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.whiletrue:
+        return parseWhileTrue(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.dowhiletrue:
+        return parseDoWhileTrue(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.switch:
+        return parseSwitch(commands_block, function_obj);
     }
     return null;
   }
@@ -216,7 +224,7 @@ function parseAttribution (command, function_obj) {
   );
 }
 
-function parseComment(command, function_obj) {
+function parseComment (command, function_obj) {
   // TODO
   return new Models.Comment(
     null
@@ -267,27 +275,56 @@ function parseRepeatNTimes(command, function_obj) {
   var expression2 = CodeParser.expressionParserCodeVisual(command.condition, function_obj);
   expression2 = expression2[0];
 
-  var expression3 = CodeParser.expressionParserCodeVisual(command.step_expression, function_obj);
-
-  console.log('expression3', expression3);
+  var var_step = CodeParser.expressionParserCodeVisual(command.step_expression, function_obj);
 
   var commands_block = parseCommands(command.commands, function_obj);
 
-  // var_attribution,
-  //   var_incrementation,
-  //   expression1,
-  //   expression2,
-  //   expression3,
-  //   commands_block
+  var operator = command.step_expression[0].value == '+' 
+                  ? Models.ARITHMETIC_TYPES.plus
+                  : Models.ARITHMETIC_TYPES.minus;
+
+  var expression3 = new Models.ExpressionElement(
+    Models.EXPRESSION_ELEMENTS.exp_op_exp, 
+    [ 
+      null,
+      operator, 
+      var_step[1]
+    ]);
 
   return new Models.RepeatNTimes(
     var_attribution,
     new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
     expression1,
     expression2,
-    null, 
+    expression3, 
     commands_block);
 
 }
 
-			
+function parseWhileTrue (command, function_obj) {
+
+  var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
+  var commands = parseCommands(command.commands, function_obj);
+
+  return new Models.WhileTrue(
+    expression, 
+    commands
+  );
+}
+
+function parseDoWhileTrue (command, function_obj) {
+  
+  // TODO
+  console.log(command)
+
+			// return new Models.DoWhileTrue([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)], null);
+}
+
+function parseSwitch (command, function_obj) {
+
+  console.log(command)
+  
+			// var sc = [new Models.SwitchCase(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true))];
+
+			// return new Models.Switch(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true), sc);
+}

+ 39 - 0
js/visualUI/functions.js

@@ -138,6 +138,25 @@ WatchJS.watch(window.program_obj.functions, function(){
 
 function addFunctionHandler () {
 
+  if (window.program_obj.functions) {
+		var in_use = false;
+		do {
+			in_use = false;
+			
+			var temp_name = LocalizedStrings.getUI('new_function') + '_' + counter_new_functions;
+
+			window.program_obj.functions.forEach(function(el) {
+				if (temp_name == el.name) {
+					in_use = true;
+				}
+			})
+
+			if (in_use)
+        counter_new_functions++;
+
+		} while (in_use);
+	}
+
 	const new_function = new Models.Function(LocalizedStrings.getUI("new_function") + "_" + counter_new_functions, Types.VOID, 0, [], false, false, [], new Models.Comment(LocalizedStrings.getUI('text_comment_start')));
 	program.addFunction(new_function);
 
@@ -156,6 +175,26 @@ function addParameter (function_obj, function_container/*, is_from_click = false
   if (function_obj.parameters_list == null) {
     function_obj.parameters_list = [];
   }
+
+  if (function_obj.parameters_list) {
+		var in_use = false;
+		do {
+			in_use = false;
+			
+			var temp_name = LocalizedStrings.getUI('new_parameter') + '_' + counter_new_parameters;
+
+			function_obj.parameters_list.forEach(function(el) {
+				if (temp_name == el.name) {
+					in_use = true;
+				}
+			})
+
+			if (in_use)
+        counter_new_parameters++;
+
+		} while (in_use);
+	}
+
   const new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
   function_obj.parameters_list.push(new_parameter);
   counter_new_parameters ++;

+ 20 - 0
js/visualUI/globals.js

@@ -8,6 +8,26 @@ var counter_new_globals = 0;
 
 export function addGlobal (program, is_from_click = false) {
 
+	// Verify if the next global name is in use:
+	if (program.globals) {
+		var in_use = false;
+		do {
+			in_use = false;
+			
+			var temp_name = LocalizedStrings.getUI('new_global') + '_' + counter_new_globals;
+
+			program.globals.forEach(function(el) {
+				if (temp_name == el.name) {
+					in_use = true;
+				}
+			})
+
+			if (in_use)
+				counter_new_globals++;
+
+		} while (in_use);
+	}
+
 	var new_global = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI('new_global') + '_' + counter_new_globals, 0);
 	counter_new_globals ++;
 

+ 21 - 0
js/visualUI/variables.js

@@ -8,6 +8,27 @@ import { isValidIdentifier } from "./../util/utils";
 var counter_new_variables = 0;	
 
 export function addVariable (function_obj, function_container, is_in_click = false) {
+
+	// Verify if the next variable name is in use:
+	if (function_obj.variables_list) {
+		var in_use = false;
+		do {
+			in_use = false;
+			
+			var temp_name = LocalizedStrings.getUI('new_variable') + '_' + counter_new_variables;
+
+			function_obj.variables_list.forEach(function(el) {
+				if (temp_name == el.name) {
+					in_use = true;
+				}
+			})
+
+			if (in_use)
+				counter_new_variables++;
+
+		} while (in_use);
+	}
+
 	var new_var = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI('new_variable') + '_' + counter_new_variables, 0);
 	if (function_obj.variables_list == null) {
 		function_obj.variables_list = [];