From a0989cf97c7f2461ac507fbd996e3022e8713ee9 Mon Sep 17 00:00:00 2001 From: Charlie Brej Date: Tue, 1 Sep 2009 20:25:38 +0100 Subject: [PATCH] [script] Move function definitions from operations to expressions This removes some of the duplication due to the two methods of creating functions. Now function definitions like: fun name (foo) {bar} are treated as name = fun (foo) {bar}; No script code needs to change as both are still valid. --- src/plugins/splash/script/script-execute.c | 10 -------- src/plugins/splash/script/script-parse.c | 28 ++++------------------ src/plugins/splash/script/script.h | 6 ----- 3 files changed, 5 insertions(+), 39 deletions(-) diff --git a/src/plugins/splash/script/script-execute.c b/src/plugins/splash/script/script-execute.c index 66cc6783..37a027a5 100644 --- a/src/plugins/splash/script/script-execute.c +++ b/src/plugins/splash/script/script-execute.c @@ -621,16 +621,6 @@ script_return_t script_execute (script_state_t *state, break; } - case SCRIPT_OP_TYPE_FUNCTION_DEF: - { - script_obj_t *obj = script_evaluate (state, op->data.function_def.name); - script_obj_t *function_obj = script_obj_new_function(op->data.function_def.function); - script_obj_assign (obj, function_obj); - script_obj_unref (function_obj); - script_obj_unref (obj); - break; - } - case SCRIPT_OP_TYPE_RETURN: { script_obj_t *obj; diff --git a/src/plugins/splash/script/script-parse.c b/src/plugins/splash/script/script-parse.c index ef9a79d5..ec8c665c 100644 --- a/src/plugins/splash/script/script-parse.c +++ b/src/plugins/splash/script/script-parse.c @@ -666,6 +666,7 @@ static script_op_t *script_parse_function (script_scan_t *scan) script_scan_token_t *curtoken = script_scan_get_current_token (scan); if (!script_scan_token_is_identifier_of_value (curtoken, "fun")) return NULL; + script_debug_location_t location = curtoken->location; curtoken = script_scan_get_next_token (scan); if (!script_scan_token_is_identifier (curtoken)) { @@ -679,11 +680,10 @@ static script_op_t *script_parse_function (script_scan_t *scan) script_function_t *function = script_parse_function_def (scan); if (!function) return NULL; - - script_op_t *op = malloc (sizeof (script_op_t)); - op->type = SCRIPT_OP_TYPE_FUNCTION_DEF; - op->data.function_def.name = name; - op->data.function_def.function = function; + script_exp_t *func_exp = script_parse_new_exp_function_def (function, &location); + script_exp_t *func_def = script_parse_new_exp_dual (SCRIPT_EXP_TYPE_ASSIGN, name, func_exp, &location); + + script_op_t *op = script_parse_new_op_exp (func_def, &location); return op; } @@ -882,24 +882,6 @@ void script_parse_op_free (script_op_t *op) break; } - case SCRIPT_OP_TYPE_FUNCTION_DEF: - { - if (op->data.function_def.function->type == SCRIPT_FUNCTION_TYPE_SCRIPT) - script_parse_op_free (op->data.function_def.function->data.script); - ply_list_node_t *node; - for (node = ply_list_get_first_node (op->data.function_def.function->parameters); - node; - node = ply_list_get_next_node (op->data.function_def.function->parameters, node)) - { - char *arg = ply_list_node_get_data (node); - free (arg); - } - ply_list_free (op->data.function_def.function->parameters); - script_parse_exp_free (op->data.function_def.name); - free (op->data.function_def.function); - break; - } - case SCRIPT_OP_TYPE_RETURN: { if (op->data.exp) script_parse_exp_free (op->data.exp); diff --git a/src/plugins/splash/script/script.h b/src/plugins/splash/script/script.h index 96ad203b..342dab80 100644 --- a/src/plugins/splash/script/script.h +++ b/src/plugins/splash/script/script.h @@ -180,7 +180,6 @@ typedef enum 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, SCRIPT_OP_TYPE_CONTINUE, @@ -199,11 +198,6 @@ typedef struct script_op_t struct script_op_t *op1; struct script_op_t *op2; } cond_op; - struct - { - script_exp_t *name; - script_function_t *function; - } function_def; } data; } script_op_t; -- 2.47.3