📄 parser.c
字号:
} //if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: finished per frame equation evaluation (LINE %d)\n", line_count); /* Create a new per frame equation */ if ((per_frame_eqn = new_per_frame_eqn(index, param, gen_expr)) == NULL) { //if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: failed to create a new per frame eqn, out of memory?\n"); free_gen_expr(gen_expr); return NULL; } //if (PARSE_DEBUG) printf("parse_implicit_per_frame_eqn: per_frame eqn parsed succesfully\n"); return per_frame_eqn;}/* Parses an initial condition */init_cond_t * parse_init_cond(FILE * fs, char * name, struct PRESET_T * preset) { param_t * param; value_t init_val; init_cond_t * init_cond; if (name == NULL) return NULL; if (preset == NULL) return NULL; /* Search for the paramater in the database, creating it if necessary */ if ((param = find_param(name, preset, P_CREATE)) == NULL) { return NULL; } //if (PARSE_DEBUG) printf("parse_init_cond: parameter = \"%s\" (LINE %d)\n", param->name, line_count); if (param->flags & P_FLAG_READONLY) { //if (PARSE_DEBUG) printf("parse_init_cond: builtin parameter \"%s\" marked as read only!\n", param->name); return NULL; } /* At this point, a parameter has been created or was found in the database. */ //if (PARSE_DEBUG) printf("parse_init_cond: parsing initial condition value... (LINE %d)\n", line_count); /* integer value (boolean is an integer in C) */ if ((param->type == P_TYPE_INT) || (param->type == P_TYPE_BOOL)) { if ((parse_int(fs, (int*)&init_val.int_val)) == PARSE_ERROR) { //if (PARSE_DEBUG) printf("parse_init_cond: error parsing integer!\n"); return NULL; } } /* double value */ else if (param->type == P_TYPE_DOUBLE) { if ((parse_float(fs, (double*)&init_val.double_val)) == PARSE_ERROR) { //if (PARSE_DEBUG) printf("parse_init_cond: error parsing double!\n"); return NULL; } } /* Unknown value */ else { //if (PARSE_DEBUG) printf("parse_init_cond: unknown parameter type!\n"); return NULL; } /* Create new initial condition */ if ((init_cond = new_init_cond(param, init_val)) == NULL) { //if (PARSE_DEBUG) printf("parse_init_cond: new_init_cond failed!\n"); return NULL; } /* Finished */ return init_cond;}/* Parses a per frame init equation, not sure if this works right now */init_cond_t * parse_per_frame_init_eqn(FILE * fs, struct PRESET_T * preset, splaytree_t * database) { char name[MAX_TOKEN_SIZE]; param_t * param = NULL; value_t init_val; init_cond_t * init_cond; gen_expr_t * gen_expr; double val; token_t token; if (preset == NULL) return NULL; if (fs == NULL) return NULL; if ((token = parseToken(fs, name)) != tEq) return NULL; /* If a database was specified,then use find_param_db instead */ if ((database != NULL) && ((param = find_param_db(name, database, TRUE)) == NULL)) { return NULL; } /* Otherwise use the builtin parameter and user databases. This is confusing. Sorry. */ if ((param == NULL) && ((param = find_param(name, preset, P_CREATE)) == NULL)) { return NULL; } //if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: parameter = \"%s\" (LINE %d)\n", param->name, line_count); if (param->flags & P_FLAG_READONLY) { //if (PARSE_DEBUG) printf("pars_per_frame_init_eqn: builtin parameter \"%s\" marked as read only!\n", param->name); return NULL; } /* At this point, a parameter has been created or was found in the database. */ //if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: parsing right hand side of per frame init equation.. (LINE %d)\n", line_count); if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL) { //if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: failed to parse general expresion!\n"); return NULL; } /* Compute initial condition value */ val = eval_gen_expr(gen_expr); /* Free the general expression now that we are done with it */ free_gen_expr(gen_expr); /* integer value (boolean is an integer in C) */ if ((param->type == P_TYPE_INT) || (param->type == P_TYPE_BOOL)) { init_val.int_val = (int)val; } /* double value */ else if (param->type == P_TYPE_DOUBLE) { init_val.double_val = val; } /* Unknown value */ else { //if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: unknown parameter type!\n"); return NULL; } /* Create new initial condition */ if ((init_cond = new_init_cond(param, init_val)) == NULL) { //if (PARSE_DEBUG) printf("parse_per_frame_init_eqn: new_init_cond failed!\n"); return NULL; } /* Finished */ return init_cond;}int parse_wavecode(char * token, FILE * fs, preset_t * preset) { char * var_string; init_cond_t * init_cond; custom_wave_t * custom_wave; int id; value_t init_val; param_t * param; /* Null argument checks */ if (preset == NULL) return FAILURE; if (fs == NULL) return FAILURE; if (token == NULL) return FAILURE; /* token should be in the form wavecode_N_var, such as wavecode_1_samples */ /* Get id and variable name from token string */ if (parse_wavecode_prefix(token, &id, &var_string) < 0) return PARSE_ERROR; //if (PARSE_DEBUG) printf("parse_wavecode: wavecode id = %d, parameter = \"%s\"\n", id, var_string); /* Retrieve custom wave information from preset. The 3rd argument if true creates a custom wave if one does not exist */ if ((custom_wave = find_custom_wave(id, preset, TRUE)) == NULL) { //if (PARSE_DEBUG) printf("parse_wavecode: failed to load (or create) custom wave (id = %d)!\n", id); return FAILURE; } //if (PARSE_DEBUG) printf("parse_wavecode: custom wave found (id = %d)\n", custom_wave->id); /* Retrieve parameter from this custom waves parameter db */ if ((param = find_param_db(var_string, custom_wave->param_tree, TRUE)) == NULL) return FAILURE; //if (PARSE_DEBUG) printf("parse_wavecode: custom wave parameter found (name = %s)\n", param->name); /* integer value (boolean is an integer in C) */ if ((param->type == P_TYPE_INT) || (param->type == P_TYPE_BOOL)) { if ((parse_int(fs, (int*)&init_val.int_val)) == PARSE_ERROR) { //if (PARSE_DEBUG) printf("parse_wavecode: error parsing integer!\n"); return PARSE_ERROR; } } /* double value */ else if (param->type == P_TYPE_DOUBLE) { if ((parse_float(fs, (double*)&init_val.double_val)) == PARSE_ERROR) { //if (PARSE_DEBUG) printf("parse_wavecode: error parsing double!\n"); return PARSE_ERROR; } } /* Unknown value */ else { //if (PARSE_DEBUG) printf("parse_wavecode: unknown parameter type!\n"); return PARSE_ERROR; } /* Create new initial condition */ if ((init_cond = new_init_cond(param, init_val)) == NULL) { //if (PARSE_DEBUG) printf("parse_wavecode: new_init_cond failed!\n"); return FAILURE; } if (splay_insert(init_cond, param->name, custom_wave->init_cond_tree) < 0) { free_init_cond(init_cond); return PARSE_ERROR; } //if (PARSE_DEBUG) printf("parse_wavecode: [success]\n"); return SUCCESS;}int parse_shapecode(char * token, FILE * fs, preset_t * preset) { char * var_string; init_cond_t * init_cond; custom_shape_t * custom_shape; int id; value_t init_val; param_t * param; /* Null argument checks */ if (preset == NULL) return FAILURE; if (fs == NULL) return FAILURE; if (token == NULL) return FAILURE; /* token should be in the form shapecode_N_var, such as shapecode_1_samples */ /* Get id and variable name from token string */ if (parse_shapecode_prefix(token, &id, &var_string) < 0) return PARSE_ERROR; //if (PARSE_DEBUG) printf("parse_shapecode: shapecode id = %d, parameter = \"%s\"\n", id, var_string); /* Retrieve custom shape information from preset. The 3rd argument if true creates a custom shape if one does not exist */ if ((custom_shape = find_custom_shape(id, preset, TRUE)) == NULL) { //if (PARSE_DEBUG) printf("parse_shapecode: failed to load (or create) custom shape (id = %d)!\n", id); return FAILURE; } //if (PARSE_DEBUG) printf("parse_shapecode: custom shape found (id = %d)\n", custom_shape->id); /* Retrieve parameter from this custom shapes parameter db */ if ((param = find_param_db(var_string, custom_shape->param_tree, TRUE)) == NULL) { //if (PARSE_DEBUG) printf("parse_shapecode: failed to create parameter.\n"); return FAILURE; } //if (PARSE_DEBUG) printf("parse_shapecode: custom shape parameter found (name = %s)\n", param->name); /* integer value (boolean is an integer in C) */ if ((param->type == P_TYPE_INT) || (param->type == P_TYPE_BOOL)) { if ((parse_int(fs, (int*)&init_val.int_val)) == PARSE_ERROR) { //if (PARSE_DEBUG) printf("parse_shapecode: error parsing integer!\n"); return PARSE_ERROR; } } /* double value */ else if (param->type == P_TYPE_DOUBLE) { if ((parse_float(fs, (double*)&init_val.double_val)) == PARSE_ERROR) { //if (PARSE_DEBUG) printf("parse_shapecode: error parsing double!\n"); return PARSE_ERROR; } } /* Unknown value */ else { //if (PARSE_DEBUG) printf("parse_shapecode: unknown parameter type!\n"); return PARSE_ERROR; } /* Create new initial condition */ if ((init_cond = new_init_cond(param, init_val)) == NULL) { //if (PARSE_DEBUG) printf("parse_shapecode: new_init_cond failed!\n"); return FAILURE; } if (splay_insert(init_cond, param->name, custom_shape->init_cond_tree) < 0) { free_init_cond(init_cond); //if (PARSE_DEBUG) printf("parse_shapecode: initial condition already set, not reinserting it (param = \"%s\")\n", param->name); return PARSE_ERROR; } //if (PARSE_DEBUG) printf("parse_shapecode: [success]\n"); return SUCCESS;}int parse_wavecode_prefix(char * token, int * id, char ** var_string) { int len, i, j; if (token == NULL) return FAILURE; if (*var_string == NULL) return FAILURE; if (id == NULL) return FAILURE; len = strlen(token); /* Move pointer passed "wavecode_" prefix */ if (len <= WAVECODE_STRING_LENGTH) return FAILURE; i = WAVECODE_STRING_LENGTH; j = 0; (*id) = 0; /* This loop grabs the integer id for this custom wave */ while ((i < len) && (token[i] >= 48) && (token[i] <= 57)) { if (j >= MAX_TOKEN_SIZE) return FAILURE; (*id) = 10*(*id) + (token[i]-48); j++; i++; } if (i > (len - 2)) return FAILURE; *var_string = token + i + 1; return SUCCESS;}int parse_shapecode_prefix(char * token, int * id, char ** var_string) { int len, i, j; if (token == NULL) return FAILURE; if (*var_string == NULL) return FAILURE; if (id == NULL) return FAILURE; len = strlen(token); /* Move pointer passed "shapecode_" prefix */ if (len <= SHAPECODE_STRING_LENGTH) return FAILURE; i = SHAPECODE_STRING_LENGTH; j = 0; (*id) = 0; /* This loop grabs the integer id for this custom shape */ while ((i < len) && (token[i] >= 48) && (token[i] <= 57)) { if (j >= MAX_TOKEN_SIZE) return FAILURE; (*id) = 10*(*id) + (token[i]-48); j++; i++; } if (i > (len - 2)) return FAILURE; *var_string = token + i + 1; return SUCCESS;}int parse_wave_prefix(char * token, int * id, char ** eqn_string) { int len, i, j; if (token == NULL) return FAILURE; if (eqn_string == NULL) return FAILURE; if (id == NULL) return FAILURE; len = strlen(token); if (len <= WAVE_STRING_LENGTH) return FAILURE; i = WAVE_STRING_LENGTH; j = 0; (*id) = 0; /* This loop grabs the integer id for this custom wave */ while ((i < len) && (token[i] >= 48) && (token[i] <= 57)) { if (j >= MAX_TOKEN_SIZE) return FAILURE; (*id) = 10*(*id) + (token[i]-48); j++; i++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -