]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Add support for 'for'
authorCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 30 Jun 2009 20:46:40 +0000 (21:46 +0100)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 30 Jun 2009 20:46:40 +0000 (21:46 +0100)
Matches the use in C as "for (first, condition, last)" where first is executed
once, last is executed at the end of each iteration (even when continue is
executed) and condition is tested at the start of every cycle.

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

index 3320208eb6ba830e08d8481c1bee83329448d344..0dc056439ed12c8c5d0510d6e48d506f52a4a24e 100644 (file)
@@ -874,6 +874,7 @@ script_return script_execute (script_state* state, script_op* op)
         break;
         }
     case SCRIPT_OP_TYPE_WHILE:
+    case SCRIPT_OP_TYPE_FOR:
         {
         script_obj* obj;
         while (1){
@@ -889,9 +890,9 @@ script_return script_execute (script_state* state, script_op* op)
                     case SCRIPT_RETURN_TYPE_BREAK:
                         return (script_return){SCRIPT_RETURN_TYPE_NORMAL, NULL};
                     case SCRIPT_RETURN_TYPE_CONTINUE:
-                        reply = (script_return){SCRIPT_RETURN_TYPE_NORMAL, NULL};
                         break;
                     }
+                script_execute (state, op->data.cond_op.op2);
                 }
             else {
                 script_obj_unref(obj);
index 7ee20f6008f1012425f8ccbf3751a38fa2c38204..08d884ef4d00375a475ff77011b20c4cac8872be 100644 (file)
@@ -533,8 +533,8 @@ static script_op* script_parse_if_while (ply_scan_t* scan)
     script_parse_error (curtoken, "Expected a ')' at the end of a condition block");
     return NULL;
     }
- curtoken = ply_scan_get_next_token(scan);
  
+ ply_scan_get_next_token(scan);
  script_op* cond_op = script_parse_op(scan);
  script_op* else_op = NULL;
  
@@ -542,7 +542,7 @@ static script_op* script_parse_if_while (ply_scan_t* scan)
  if (type == SCRIPT_OP_TYPE_IF &&
         curtoken->type == PLY_SCAN_TOKEN_TYPE_IDENTIFIER &&
         !strcmp(curtoken->data.string, "else")){
-    curtoken = ply_scan_get_next_token(scan);
+    ply_scan_get_next_token(scan);
     else_op = script_parse_op(scan);
     }
  
@@ -556,6 +556,87 @@ static script_op* script_parse_if_while (ply_scan_t* scan)
 }
 
 
+static script_op* script_parse_for (ply_scan_t* scan)
+{
+ ply_scan_token_t* curtoken = ply_scan_get_current_token(scan);
+ if (curtoken->type != PLY_SCAN_TOKEN_TYPE_IDENTIFIER)
+    return NULL;
+ if (strcmp(curtoken->data.string, "for")) return NULL;
+ curtoken = ply_scan_get_next_token(scan);
+ if (curtoken->type != PLY_SCAN_TOKEN_TYPE_SYMBOL || curtoken->data.symbol != '('){
+    script_parse_error (curtoken, "Expected a '(' at the start of a condition block");
+    return NULL;
+    }
+ curtoken = ply_scan_get_next_token(scan);
+ script_exp* first = script_parse_exp (scan);
+ if (!first){
+    script_parse_error (curtoken, "Expected a valid first expression");
+    return NULL;
+    }
+ curtoken = ply_scan_get_current_token(scan);
+ if (curtoken->type != PLY_SCAN_TOKEN_TYPE_SYMBOL || curtoken->data.symbol != ';'){
+    script_parse_error (curtoken, "Expected a ';' after the first 'for' expression");
+    return NULL;
+    }
+ ply_scan_get_next_token(scan);
+ script_exp* cond = script_parse_exp (scan);
+ if (!cond){
+    script_parse_error (curtoken, "Expected a valid condition expression");
+    return NULL;
+    }
+ curtoken = ply_scan_get_current_token(scan);
+ if (curtoken->type != PLY_SCAN_TOKEN_TYPE_SYMBOL || curtoken->data.symbol != ';'){
+    script_parse_error (curtoken, "Expected a ';' after the 'for' condition");
+    return NULL;
+    }
+ ply_scan_get_next_token(scan);
+    
+ script_exp* last = script_parse_exp (scan);
+ if (!last){
+    script_parse_error (curtoken, "Expected a valid last expression");
+    return NULL;
+    }
+    
+ curtoken = ply_scan_get_current_token(scan);
+ if (curtoken->type != PLY_SCAN_TOKEN_TYPE_SYMBOL || curtoken->data.symbol != ')'){
+    script_parse_error (curtoken, "Expected a ')' at the end of a for block");
+    return NULL;
+    }
+ ply_scan_get_next_token(scan);
+ script_op* op_body = script_parse_op(scan);
+ script_op* op_first = malloc(sizeof(script_op));
+ op_first->type = SCRIPT_OP_TYPE_EXPRESSION;
+ op_first->data.exp = first;
+ script_op* op_last = malloc(sizeof(script_op));
+ op_last->type = SCRIPT_OP_TYPE_EXPRESSION;
+ op_last->data.exp = last;
+ script_op* op_for = malloc(sizeof(script_op));
+ op_for->type = SCRIPT_OP_TYPE_FOR;
+ op_for->data.cond_op.cond = cond;
+ op_for->data.cond_op.op1 = op_body;
+ op_for->data.cond_op.op2 = op_last;
+ script_op* op_block = malloc(sizeof(script_op));
+ op_block->type = SCRIPT_OP_TYPE_OP_BLOCK;
+ op_block->data.list = ply_list_new();
+ ply_list_append_data(op_block->data.list, op_first);
+ ply_list_append_data(op_block->data.list, op_for);
+ return op_block;
+}
+
+
+
 
 
 
@@ -679,6 +760,9 @@ static script_op* script_parse_op (ply_scan_t* scan)
  reply = script_parse_if_while (scan);
  if (reply) return reply;
 
+ reply = script_parse_for (scan);
+ if (reply) return reply;
+
  reply = script_parse_return (scan);
  if (reply) return reply;
 
@@ -798,6 +882,7 @@ void script_parse_op_free (script_op* op)
         }
     case SCRIPT_OP_TYPE_IF:
     case SCRIPT_OP_TYPE_WHILE:
+    case SCRIPT_OP_TYPE_FOR:
         {
         script_parse_exp_free (op->data.cond_op.cond);
         script_parse_op_free  (op->data.cond_op.op1);
index 30d6144d4aaa641588fefeaec868a005f2eacd53..93e34e5dd3a34e7471a5c685333e64af4cbc75d3 100644 (file)
@@ -162,6 +162,7 @@ typedef enum
  SCRIPT_OP_TYPE_OP_BLOCK,
  SCRIPT_OP_TYPE_IF,
  SCRIPT_OP_TYPE_WHILE,
+ SCRIPT_OP_TYPE_FOR,
  SCRIPT_OP_TYPE_FUNCTION_DEF,
  SCRIPT_OP_TYPE_RETURN,
  SCRIPT_OP_TYPE_BREAK,
index dccea015af30f4cfd71a330b67d8f8326d9fd5da..41bdc55f7a7aa9c0b109b3c563cfd3e16a8aedb7 100644 (file)
@@ -28,7 +28,7 @@ fun refresh (){
         SpriteSetY (logo.sprite, 300 - ImageGetHeight(logo.image));
         SpriteSetOpacity (logo.sprite, 1);
         }
-    if (status != "normal"){
+    else{
         SpriteSetOpacity (throbber_sprite, 0);
         SpriteSetX (logo.sprite, 0);
         SpriteSetY (logo.sprite, 0);
@@ -98,9 +98,8 @@ fun dialogue_opacity(opacity){
     SpriteSetOpacity (lock.sprite, opacity);
     SpriteSetOpacity (entry.sprite, opacity);
     index = 0;
-    while (bullet[index]){
+    for (index = 0; bullet[index]; index++){
         SpriteSetOpacity(bullet[index].sprite, opacity);
-        index++;
         }
     }
 
@@ -117,8 +116,7 @@ fun display_password (prompt, bullets){
     global.status = "password";
     if (!global.dialogue) dialogue_setup();
     dialogue_opacity (1);
-    index = 0;
-    while (dialogue.bullet[index] || index < bullets){
+    for (index = 0; dialogue.bullet[index] || index < bullets; index++){
         if (!dialogue.bullet[index]){
             dialogue.bullet[index].sprite = SpriteNew();
             SpriteSetImage(dialogue.bullet[index].sprite, dialogue.bullet_image);
@@ -133,8 +131,6 @@ fun display_password (prompt, bullets){
         if (index < bullets){
             SpriteSetOpacity(dialogue.bullet[index].sprite, 1);
             }
-        
-        index++;
         }
     }
 PlymouthSetDisplayPasswordFunction(display_password);