]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Allow inline array definitions
authorCharlie Brej <cbrej@cs.man.ac.uk>
Sun, 4 Oct 2009 16:46:59 +0000 (17:46 +0100)
committerroot <cbrej@cs.man.ac.uk>
Sun, 4 Oct 2009 16:46:59 +0000 (17:46 +0100)
This can be used to initialise an array:
arr = [12, 43];    // arr[0] = 12; arr[1] = 43;

Or create an empty object:
obj = [];

src/plugins/splash/script/script-execute.c
src/plugins/splash/script/script-parse.c
src/plugins/splash/script/script.h

index 161186f1ae110c531470c2da675772dc38c0b388..7f16dd4ea3cee0397e7a1b1a9eec10b9530425de 100644 (file)
@@ -125,6 +125,29 @@ static script_obj_t *script_evaluate_var (script_state_t *state,
   return obj;
 }
 
+static script_obj_t *script_evaluate_set (script_state_t *state,
+                                          script_exp_t   *exp)
+{
+
+  ply_list_t *parameter_data = exp->data.parameters;
+  ply_list_node_t *node_data = ply_list_get_first_node (parameter_data);
+  int index = 0;
+  script_obj_t *obj = script_obj_new_hash ();
+  while (node_data)
+    {
+      script_exp_t *data_exp = ply_list_node_get_data (node_data);
+      script_obj_t *data_obj = script_evaluate (state, data_exp);
+      char *name;
+      asprintf (&name, "%d", index);
+      index++;
+      script_obj_hash_add_element (obj, data_obj, name);
+      free(name);
+      
+      node_data = ply_list_get_next_node (parameter_data, node_data);
+    }
+  return obj;
+}
+
 static script_obj_t *script_evaluate_assign (script_state_t *state,
                                              script_exp_t   *exp)
 {
@@ -444,6 +467,11 @@ static script_obj_t *script_evaluate (script_state_t *state,
           return state->this;
         }
 
+      case SCRIPT_EXP_TYPE_TERM_SET:
+        {
+          return script_evaluate_set (state, exp);
+        }
+
       case SCRIPT_EXP_TYPE_TERM_VAR:
         {
           return script_evaluate_var (state, exp);
index 1e02ac5df302a77d7f6f120dcdb2401b4ad81af4..bddba474d2fe8775aec21af2b019e3e0f5cce380 100644 (file)
@@ -122,6 +122,14 @@ static script_exp_t *script_parse_new_exp_function_def (script_function_t
   return exp;
 }
 
+static script_exp_t *script_parse_new_exp_set (ply_list_t              *parameters,
+                                               script_debug_location_t *location)
+{
+  script_exp_t *exp = script_parse_new_exp(SCRIPT_EXP_TYPE_TERM_SET, location);
+  exp->data.parameters = parameters;
+  return exp;
+}
+
 static script_op_t *script_parse_new_op (script_op_type_t         type,
                                          script_debug_location_t *location)
 {
@@ -301,6 +309,33 @@ static script_exp_t *script_parse_exp_tm (script_scan_t *scan)
       script_scan_get_next_token (scan);
       return exp;
     }
+  
+  if (script_scan_token_is_symbol_of_value (curtoken, '['))
+    {
+      ply_list_t *parameters = ply_list_new ();
+      script_debug_location_t location = curtoken->location;
+      script_scan_get_next_token (scan);
+      while (true)
+        {
+          if (script_scan_token_is_symbol_of_value (curtoken, ']')) break;
+          script_exp_t *parameter = script_parse_exp (scan);
+
+          ply_list_append_data (parameters, parameter);
+
+          curtoken = script_scan_get_current_token (scan);
+          if (script_scan_token_is_symbol_of_value (curtoken, ']')) break;
+          if (!script_scan_token_is_symbol_of_value (curtoken, ','))
+            {
+              script_parse_error (&curtoken->location,
+                "Set parameters should be separated with a ',' and terminated with a ']'");
+              return NULL;
+            }
+          curtoken = script_scan_get_next_token (scan);
+        }
+      script_scan_get_next_token (scan);
+      exp = script_parse_new_exp_set (parameters, &location);
+      return exp;
+    }
   if (script_scan_token_is_symbol_of_value (curtoken, '('))
     {
       script_scan_get_next_token (scan);
@@ -877,7 +912,19 @@ static void script_parse_exp_free (script_exp_t *exp)
       case SCRIPT_EXP_TYPE_TERM_GLOBAL:
       case SCRIPT_EXP_TYPE_TERM_THIS:
         break;
-
+      case SCRIPT_EXP_TYPE_TERM_SET:
+        {
+          ply_list_node_t *node;
+          for (node = ply_list_get_first_node (exp->data.parameters);
+               node;
+               node = ply_list_get_next_node (exp->data.parameters, node))
+            {
+              script_exp_t *sub = ply_list_node_get_data (node);
+              script_parse_exp_free (sub);
+            }
+          ply_list_free (exp->data.parameters);
+          break;
+        }
       case SCRIPT_EXP_TYPE_FUNCTION_EXE:
         {
           ply_list_node_t *node;
index e4f6958ba1dfcbec444b57bb55257750e0175798..391deca1f434a3ccfeb7c1786afb4072d49b99d5 100644 (file)
@@ -130,6 +130,7 @@ typedef enum
   SCRIPT_EXP_TYPE_TERM_LOCAL,
   SCRIPT_EXP_TYPE_TERM_GLOBAL,
   SCRIPT_EXP_TYPE_TERM_THIS,
+  SCRIPT_EXP_TYPE_TERM_SET,
   SCRIPT_EXP_TYPE_PLUS,
   SCRIPT_EXP_TYPE_MINUS,
   SCRIPT_EXP_TYPE_MUL,
@@ -181,6 +182,7 @@ typedef struct script_exp_t
       struct script_exp_t *name;
       ply_list_t *parameters;
     } function_exe;
+    ply_list_t *parameters;
     script_function_t *function_def;
   } data;
 } script_exp_t;