From: Charlie Brej Date: Tue, 30 Jun 2009 20:46:40 +0000 (+0100) Subject: [script] Add support for 'for' X-Git-Tag: 0.7.0~124 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb4ed684b84872f2f41da3018186c6a5659f8744;p=thirdparty%2Fplymouth.git [script] Add support for 'for' 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. --- diff --git a/src/plugins/splash/script/script-execute.c b/src/plugins/splash/script/script-execute.c index 3320208e..0dc05643 100644 --- a/src/plugins/splash/script/script-execute.c +++ b/src/plugins/splash/script/script-execute.c @@ -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); diff --git a/src/plugins/splash/script/script-parse.c b/src/plugins/splash/script/script-parse.c index 7ee20f60..08d884ef 100644 --- a/src/plugins/splash/script/script-parse.c +++ b/src/plugins/splash/script/script-parse.c @@ -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); diff --git a/src/plugins/splash/script/script.h b/src/plugins/splash/script/script.h index 30d6144d..93e34e5d 100644 --- a/src/plugins/splash/script/script.h +++ b/src/plugins/splash/script/script.h @@ -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, diff --git a/themes/script/script.script b/themes/script/script.script index dccea015..41bdc55f 100644 --- a/themes/script/script.script +++ b/themes/script/script.script @@ -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);