⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.c

📁 video linux conference
💻 C
📖 第 1 页 / 共 5 页
字号:
          case tEOL:  /* Empty line */    line_mode = NORMAL_LINE_MODE;    return SUCCESS;      case tEOF: /* End of File */    line_mode = NORMAL_LINE_MODE;    line_count = 1;    return EOF;      case tSemiColon: /* Indicates end of expression */    return SUCCESS;        /* Valid Case, either an initial condition or equation should follow */  case tEq:        /* CASE: PER FRAME INIT EQUATION */	        if (!strncmp(eqn_string, PER_FRAME_INIT_STRING, PER_FRAME_INIT_STRING_LENGTH)) {            //if (PARSE_DEBUG) printf("parse_line: per frame init equation found...(LINE %d)\n", line_count);            /* Set the line mode to normal */      line_mode = NORMAL_LINE_MODE;            /* Parse the per frame equation */      if ((init_cond = parse_per_frame_init_eqn(fs, preset, NULL)) == NULL) {	//if (PARSE_DEBUG) printf("parse_line: per frame init equation parsing failed (LINE %d)\n", line_count);	return PARSE_ERROR;      }	            /* Insert the equation in the per frame equation tree */      if (splay_insert(init_cond, init_cond->param->name, preset->per_frame_init_eqn_tree) < 0) {	//if (PARSE_DEBUG) printf("parse_line: failed to add a perframe equation (ERROR)\n");	free_init_cond(init_cond); /* will free the gen expr too */			return ERROR;      }                 if (update_string_buffer(preset->per_frame_init_eqn_string_buffer, 			       &preset->per_frame_init_eqn_string_index) < 0)	{	return FAILURE;}            return SUCCESS;          }    /* Per frame equation case */	        if (!strncmp(eqn_string, PER_FRAME_STRING, PER_FRAME_STRING_LENGTH)) {            /* Sometimes per frame equations are implicitly defined without the	 per_frame_ prefix. This informs the parser that one could follow */      line_mode = PER_FRAME_LINE_MODE;            //if (PARSE_DEBUG) printf("parse_line: per frame equation found...(LINE %d)\n", line_count);            /* Parse the per frame equation */      if ((per_frame_eqn = parse_per_frame_eqn(fs, ++per_frame_eqn_count, preset)) == NULL) {	if (PARSE_DEBUG) printf("parse_line: per frame equation parsing failed (LINE %d)\n", line_count);	return PARSE_ERROR;      }	            /* Insert the equation in the per frame equation tree */      if (splay_insert(per_frame_eqn, &per_frame_eqn_count, preset->per_frame_eqn_tree) < 0) {	if (PARSE_DEBUG) printf("parse_line: failed to add a perframe equation (ERROR)\n");	free_per_frame_eqn(per_frame_eqn); /* will free the gen expr too */			return ERROR;      }          if (update_string_buffer(preset->per_frame_eqn_string_buffer, 			       &preset->per_frame_eqn_string_index) < 0)	return FAILURE;                        return SUCCESS;          }        /* Wavecode initial condition case */    if (!strncmp(eqn_string, WAVECODE_STRING, WAVECODE_STRING_LENGTH)) {                line_mode = CUSTOM_WAVE_WAVECODE_LINE_MODE;      //if (PARSE_DEBUG)       //      printf("parse_line: wavecode prefix found: \"%s\"\n", eqn_string);	  //      printf("string:%d\n", 5);	  //SUPER MYSTERIO-BUG - Don't Remove	  printf("");	        return parse_wavecode(eqn_string, fs, preset);    }        /* Custom Wave Prefix */    if ((!strncmp(eqn_string, WAVE_STRING, WAVE_STRING_LENGTH)) && 	((eqn_string[5] >= 48) && (eqn_string[5] <= 57))) {            //    if (PARSE_DEBUG) printf("parse_line wave prefix found: \"%s\"\n", eqn_string);            return parse_wave(eqn_string, fs, preset);          }            /* Shapecode initial condition case */    if (!strncmp(eqn_string, SHAPECODE_STRING, SHAPECODE_STRING_LENGTH)) {            line_mode = CUSTOM_SHAPE_SHAPECODE_LINE_MODE;            if (PARSE_DEBUG) printf("parse_line: shapecode prefix found: \"%s\"\n", eqn_string);            return parse_shapecode(eqn_string, fs, preset);    }        /* Custom Shape Prefix */    if ((!strncmp(eqn_string, SHAPE_STRING, SHAPE_STRING_LENGTH)) && 	((eqn_string[6] >= 48) && (eqn_string[6] <= 57))) {            if (PARSE_DEBUG) printf("parse_line shape prefix found: \"%s\"\n", eqn_string);      return parse_shape(eqn_string, fs, preset);          }        /* Per pixel equation case */    if (!strncmp(eqn_string, PER_PIXEL_STRING, PER_PIXEL_STRING_LENGTH)) {      line_mode = PER_PIXEL_LINE_MODE;            if (parse_per_pixel_eqn(fs, preset) < 0)	return PARSE_ERROR;                  if (update_string_buffer(preset->per_pixel_eqn_string_buffer, 			       &preset->per_pixel_eqn_string_index) < 0)	return FAILURE;            if (PARSE_DEBUG) printf("parse_line: finished parsing per pixel equation (LINE %d)\n", line_count);      return SUCCESS;    }         /* Sometimes equations are written implicitly in milkdrop files, in the form           per_frame_1 = p1 = eqn1; p2 = eqn2; p3 = eqn3;..;         which is analagous to:        per_frame_1 = p1 = eqn1; per_frame_2 = p2 = eqn2; per_frame_3 = p3 = eqn3; ...;        The following line mode hack allows such implicit declaration of the     prefix that specifies the equation type. An alternative method    may be to associate each equation line as list of equations separated    by semicolons (and a new line ends the list). Instead, however, a global    variable called "line_mode" specifies the last type of equation found,    and bases any implicitly typed input on this fact        Note added by Carmelo Piccione (cep@andrew.cmu.edu) 10/19/03    */        /* Per frame line mode previously, try to parse the equation implicitly */    if (line_mode == PER_FRAME_LINE_MODE) {      if ((per_frame_eqn = parse_implicit_per_frame_eqn(fs, eqn_string, ++per_frame_eqn_count, preset)) == NULL)	return PARSE_ERROR;            /* Insert the equation in the per frame equation tree */      if (splay_insert(per_frame_eqn, &per_frame_eqn_count, preset->per_frame_eqn_tree) < 0) {	if (PARSE_DEBUG) printf("parse_line: failed to add a perframe equation (ERROR)\n");	free_per_frame_eqn(per_frame_eqn); /* will free the gen expr too */			return ERROR;      }                  if (update_string_buffer(preset->per_frame_eqn_string_buffer, 			       &preset->per_frame_eqn_string_index) < 0)	return FAILURE;                        return SUCCESS;    }        //if (PARSE_DEBUG) printf("parse_line: found initial condition: name = \"%s\" (LINE %d)\n", eqn_string, line_count);        /* Evaluate the initial condition */    if ((init_cond = parse_init_cond(fs, eqn_string, preset)) == NULL) {       if (PARSE_DEBUG) printf("parse_line: failed to parse initial condition (LINE %d)\n", line_count);      return PARSE_ERROR;     }	        /* Add equation to initial condition tree */    if (splay_insert(init_cond, init_cond->param->name, preset->init_cond_tree) < 0) {      if (PARSE_DEBUG) printf("parse_line: failed to add initial condition \"%s\" to equation tree (LINE %d)\n",       		      init_cond->param->name, line_count);      free_init_cond(init_cond);      return FAILURE;    }        /* Finished with initial condition line */    //    if (PARSE_DEBUG) printf("parse_line: initial condition parsed successfully\n");        return SUCCESS;        /* END INITIAL CONDITIONING PARSING */          default: /* an uncaught type or an error has occurred */    if (PARSE_DEBUG) printf("parse_line: uncaught case, token val = %d\n", token);     return PARSE_ERROR;  }    /* Because of the default in the case statement,      control flow should never actually reach here */   return PARSE_ERROR;}/* Parses a general expression, this function is the meat of the parser */gen_expr_t * parse_gen_expr (FILE * fs, tree_expr_t * tree_expr, struct PRESET_T * preset) {    int i;  char string[MAX_TOKEN_SIZE];  token_t token;  gen_expr_t * gen_expr;  double val;  param_t * param = NULL;  func_t * func;  gen_expr_t ** expr_list;  switch (token = parseToken(fs,string)) {  /* Left Parentice Case */  case tLPr:        /* CASE 1 (Left Parentice): See if the previous string before this parentice is a function name */    if ((func = find_func(string)) != NULL) {        if (PARSE_DEBUG) printf("parse_gen_expr: found prefix function (name = %s) (LINE %d)\n", func->name, line_count);            /* Parse the functions arguments */      if ((expr_list = parse_prefix_args(fs, func->num_args, preset)) == NULL) {	if (PARSE_DEBUG) printf("parse_prefix_args: failed to generate an expresion list! (LINE %d) \n", line_count);	free_tree_expr(tree_expr);	return NULL;      }            /* Convert function to expression */      if ((gen_expr = prefun_to_expr((void*)func->func_ptr, expr_list, func->num_args)) == NULL)  { 		  if (PARSE_DEBUG) printf("parse_prefix_args: failed to convert prefix function to general expression (LINE %d) \n", 	  				line_count);	free_tree_expr(tree_expr);	for (i = 0; i < func->num_args;i++)	  free_gen_expr(expr_list[i]);	free(expr_list);	return NULL;      }                      token = parseToken(fs, string);      if (*string != 0) {	if (PARSE_DEBUG) printf("parse_prefix_args: empty string expected, but not found...(LINE %d)\n", line_count);	/* continue anyway for now, could be implicit multiplication */				      }		            return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);    }             /* Case 2: (Left Parentice), a string coupled with a left parentice. Either an error or implicit        multiplication operator. For now treat it as an error */    if (*string != 0) {      if (PARSE_DEBUG) printf("parse_gen_expr: implicit multiplication case unimplemented!\n");      free_tree_expr(tree_expr);      return NULL;    }        /* CASE 3 (Left Parentice): the following is enclosed parentices to change order       of operations. So we create a new expression tree */        if ((gen_expr = parse_gen_expr(fs, NULL, preset)) == NULL) {      //if (PARSE_DEBUG) printf("parse_gen_expr:  found left parentice, but failed to create new expression tree \n");      free_tree_expr(tree_expr);      return NULL;    }        if (PARSE_DEBUG) printf("parse_gen_expr: finished enclosed expression tree...\n");	    token = parseToken(fs, string);    return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);    /* Plus is a prefix operator check */  case tPlus:    if (*string == 0) {            //if (PARSE_DEBUG) printf("parse_gen_expr: plus used as prefix (LINE %d)\n", line_count);	  /* Treat prefix plus as implict 0 preceding operator */      gen_expr = const_to_expr(0);      return parse_infix_op(fs, tPositive, insert_gen_expr(gen_expr, &tree_expr), preset);	    }        /* Minus is a prefix operator check */  case tMinus:    if (*string == 0) {           /* Use the negative infix operator, but first add an implicit zero to the operator tree */      gen_expr = const_to_expr(0);      //return parse_gen_expr(fs, insert_gen_expr(gen_expr, &tree_expr), preset);		return parse_infix_op(fs, tNegative, insert_gen_expr(gen_expr, &tree_expr), preset);    }        /* All the following cases are strings followed by an infix operator or terminal */  case tRPr:  case tEOL:   case tEOF:  case tSemiColon:  case tComma:        /* CASE 1 (terminal): string is empty, but not null. Not sure if this will actually happen       any more. */    if (*string == 0) {      //if (PARSE_DEBUG) printf("parse_gen_expr: empty string coupled with terminal (LINE %d) \n", line_count);      return parse_infix_op(fs, token, tree_expr, preset);          }      default:      /* CASE 0: Empty string, parse error */    if (*string == 0) {      if (PARSE_DEBUG) printf("parse_gen_expr: empty string coupled with infix op (ERROR!) (LINE %d) \n", line_count);      free_tree_expr(tree_expr);      return NULL;    }    /* CASE 1: Check if string is a just a floating point number */    if (string_to_float(string, &val) != PARSE_ERROR) {      if ((gen_expr = const_to_expr(val)) == NULL) {	free_tree_expr(tree_expr);	return NULL;      }            /* Parse the rest of the line */      return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);                  }          /* CASE 4: custom shape variable */    if (current_shape != NULL) {      if ((param = find_param_db(string, current_shape->param_tree, FALSE)) == NULL) {	if ((param = find_builtin_param(string)) == NULL)	  if ((param = find_param_db(string, current_shape->param_tree, TRUE)) == NULL) {	    free_tree_expr(tree_expr);	    return NULL;	  }      }            if (PARSE_DEBUG) {	printf("parse_gen_expr: custom shape parameter (name = %s)... ", param->name);	fflush(stdout);      }              /* Convert parameter to an expression */      if ((gen_expr = param_to_expr(param)) == NULL) {	free_tree_expr(tree_expr);	return NULL;      }            //if (PARSE_DEBUG) printf("converted to expression (LINE %d)\n", line_count);            /* Parse the rest of the line */      return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);    }        /* CASE 5: custom wave variable */    if (current_wave != NULL) {      if ((param = find_param_db(string, current_wave->param_tree, FALSE)) == NULL) {	if ((param = find_builtin_param(string)) == NULL) 	  if ((param = find_param_db(string, current_wave->param_tree, TRUE)) == NULL) {	    free_tree_expr(tree_expr);	    return NULL;	  }              }      if (PARSE_DEBUG) {	printf("parse_gen_expr: custom wave parameter (name = %s)... ", param->name);	fflush(stdout);      }		/* Convert parameter to an expression */	if ((gen_expr = param_to_expr(param)) == NULL) {	  free_tree_expr(tree_expr);	  return NULL;	}		if (PARSE_DEBUG) printf("converted to expression (LINE %d)\n", line_count);		/* Parse the rest of the line */	return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);          }    /* CASE 6: regular parameter. Will be created if necessary and the string has no invalid characters */    if ((param = find_param(string, preset, P_CREATE)) != NULL) {            if (PARSE_DEBUG) {	printf("parse_gen_expr: parameter (name = %s)... ", param->name);	fflush(stdout);      }      		/* Convert parameter to an expression */      if ((gen_expr = param_to_expr(param)) == NULL) {	free_tree_expr(tree_expr);	return NULL;      }            if (PARSE_DEBUG) printf("converted to expression (LINE %d)\n", line_count);            /* Parse the rest of the line */      return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset);              }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -