示例#1
0
文件: mp.c 项目: OctalS/math-parser
int	main() {

	char s[INPUT_SIZE];

	printf("\nExample: 2.5 * (3.1 - 2)\n");
	printf("\nenter math expression > ");
	fgets(s, INPUT_SIZE, stdin);
	printf("\nResult: %f\n\n", evaluate_exp(s));

	return 0;
}
示例#2
0
/*
 * Evalutes an expression as a string.
 */
static apr_status_t evaluate_exp_str (render_rec *d, int index) {
	apr_status_t status;

	if ((status = evaluate_exp(d, index, 1)) != APR_SUCCESS) {
		return status;
	}
	if (!lua_isstring(d->L, -1)) {
		lua_pushfstring(d->L, "(%s)", luaL_typename(d->L, -1));
		lua_replace(d->L, -2);
	}

	return APR_SUCCESS;
}
示例#3
0
static int w_evaluate_rpn(struct sip_msg *msg, char *exp, char *result)
{
	pv_elem_p exp_fmt = (pv_elem_p)exp;
	str s;

	if (pv_printf_s(msg, exp_fmt, &s) != 0) {
		LM_ERR("Failed to print the pv format string!\n");
		return -1;
	}

	LM_DBG("Evaluating expression: %.*s\n", s.len, s.s);

	return evaluate_exp(msg, &s, (pv_spec_p)result, 1);
}
示例#4
0
/*
 * Renders a template.
 */
static apr_status_t render_template (render_rec *d) {
        int i, cnt;
        template_node_t *n;
        apr_status_t status;
	const char *str;
	apr_array_header_t *t_save;

	d->depth++;
	if (d->depth > TEMPLATE_MAX_DEPTH) {
		d->err = apr_psprintf(d->pool, "template depth exceeds %d",
				TEMPLATE_MAX_DEPTH);
		return APR_EGENERAL;
	}

	i = 0;
	while (i < d->t->nelts) {
		n = ((template_node_t *) d->t->elts) + i;
		switch (n->type) {
		case TEMPLATE_TJUMP:
			i = n->jump_next;
			break;

		case TEMPLATE_TIF:
			if ((status = evaluate_exp(d, n->if_index, 1))
					!= APR_SUCCESS) {
				return status;
			} 
			if (lua_toboolean(d->L, -1)) {
				i++;
			} else {
				i = n->if_next;
			}
			lua_pop(d->L, 1);
			break;

		case TEMPLATE_TFOR_INIT:
			if ((status = evaluate_exp(d, n->for_init_index, 3))
					!= APR_SUCCESS) {
				return status;
			}
			i++;
			break;

		case TEMPLATE_TFOR_NEXT:
			lua_pushvalue(d->L, -3);
			lua_pushvalue(d->L, -3);
			lua_pushvalue(d->L, -3);
			cnt = n->for_next_names->nelts;
			if (lua_pcall(d->L, 2, cnt, d->errfunc) != 0) {
				return runtime_error(d);
			}
			if (lua_isnil(d->L, -cnt)) {
				lua_pop(d->L, 3 + cnt);
				i = n->for_next_next;
			} else {
				lua_pushvalue(d->L, -cnt);
				lua_replace(d->L, -1 - cnt - 1);
				while (cnt > 0) {
					cnt--;
					lua_setglobal(d->L, ((const char **)
							n->for_next_names
							->elts)[cnt]);
				}
				i++;
			}
			break;

		case TEMPLATE_TSET:
			cnt = n->set_names->nelts;
			if ((status = evaluate_exp(d, n->set_index, cnt))
					!= APR_SUCCESS) {
				return status;
			}
			while (cnt > 0) {
				cnt--;
				lua_setglobal(d->L, ((const char **)
						n->set_names->elts)[cnt]);
			}
			i++;
			break;
 
		case TEMPLATE_TINCLUDE:
			if ((status = evaluate_exp_str(d, n->include_index))
					!= APR_SUCCESS) {
				return status;
			}
			str = lua_tostring(d->L, -1);
			lua_pop(d->L, 1);
			t_save = d->t;
			d->t = (apr_array_header_t *) apr_hash_get(d->templates,
					str, strlen(str));
			if (d->t == NULL) {
				if ((status = lwt_template_parse(str, d->L,
						n->include_flags, d->pool,
						&d->t, &d->err))
						!= APR_SUCCESS) {
					return status;
				}
				apr_hash_set(d->templates, str, strlen(str),
						d->t);
			}
			if ((status = render_template(d)) != APR_SUCCESS) {
				return status;
			}
			d->t = t_save;
			i++;
			break;			

		case TEMPLATE_TSUB:
			lua_rawgeti(d->L, LUA_REGISTRYINDEX, n->sub_index);
			switch (lua_pcall(d->L, 0, 1, d->errfunc)) {
			case 0:
				if (lua_isstring(d->L, -1)) {
					str = lua_tostring(d->L, -1);
				} else if (lua_isnil(d->L, -1) && (n->sub_flags
						& TEMPLATE_FSUPNIL)) {
					str = "";
				} else {
					str = apr_psprintf(d->pool, "(%s)",
							luaL_typename(d->L,
							-1));
				}
				break;

			case LUA_ERRRUN:
				if (n->sub_flags & TEMPLATE_FSUPERR) {
					str = "";
				} else {
					return runtime_error(d);
				}
				break;

			default:
				return runtime_error(d);
			}
			lua_pop(d->L, 1);
			if (n->sub_flags & TEMPLATE_FESCURL) {
				str = lwt_util_escape_uri(d->pool, str);
			}
			if (n->sub_flags & TEMPLATE_FESCXML) {
				str = ap_escape_html(d->pool, str);
			}
			if (n->sub_flags & TEMPLATE_FESCJS) {
				str = lwt_util_escape_js(d->pool, str);
			}
			fputs(str, d->f);
			i++;
			break;

		case TEMPLATE_TRAW:
			fwrite(n->raw_str, n->raw_len, 1, d->f);
			i++;
			break;
		}
	}

	d->depth--;

	return APR_SUCCESS;
}
示例#5
0
tree
evaluate_impl (tree t) {
  //cout << "Really evaluate " << t << LF;
  switch (L(t)) {
  /* Typesetting primitives with side effects */
  case DATOMS:
    return evaluate_formatting (t, ATOM_DECORATIONS);
  case DLINES:
    return evaluate_formatting (t, LINE_DECORATIONS);
  case DPAGES:
    return evaluate_formatting (t, PAGE_DECORATIONS);
  case TFORMAT:
    return evaluate_formatting (t, CELL_FORMAT);
  case TABLE:
    return evaluate_table (t);

  /* Primitives for macro expansion */
  case ASSIGN:
    return evaluate_assign (t);
  case WITH:
    return evaluate_with (t);
  case PROVIDES:
    return evaluate_provides (t);
  case VALUE:
    return evaluate_value (t);
  case QUOTE_VALUE:
    return evaluate_quote_value (t);
  case MACRO:
    return copy (t);
  case DRD_PROPS:
    return evaluate_drd_props (t);
#ifdef CLASSICAL_MACRO_EXPANSION
  case ARG:
    return evaluate_arg (t);
  case QUOTE_ARG:
    return evaluate_quote_arg (t);
#endif
  case COMPOUND:
    return evaluate_compound (t);
  case XMACRO:
    return copy (t);
  case GET_LABEL:
    return evaluate_get_label (t);
  case GET_ARITY:
    return evaluate_get_arity (t);

  /* Primitives for quoting and evaluation */
  case MAP_ARGS:
    return evaluate_rewrite (t);
  case EVAL_ARGS:
    return evaluate_eval_args (t);
  case MARK:
    return tree (MARK, copy (t[0]), evaluate (t[1]));
  case EXPAND_AS:
    return evaluate (t[1]);
  case EVAL:
    return evaluate (evaluate (t[0]));
  case QUOTE:
    return t[0];
  case QUASI:
    return evaluate (evaluate_quasiquote (t[0]));
  case QUASIQUOTE:
    return evaluate_quasiquote (t[0]);
  case UNQUOTE:
  case VAR_UNQUOTE:
    return evaluate (t[0]);
  case COPY:
    return copy (evaluate (t[0]));    

  /* Control structures */
  case IF:
  case VAR_IF:
    return evaluate_if (t);
  case CASE:
    return evaluate_case (t);
  case WHILE:
    return evaluate_while (t);
  case FOR_EACH:
    return evaluate_for_each (t);
  case EXTERN:
    return evaluate_rewrite (t);
  case VAR_INCLUDE:
    return evaluate_include (t);
  case WITH_PACKAGE:
    return evaluate_rewrite (t);
  case USE_PACKAGE:
    return evaluate_use_package (t);
  case USE_MODULE:
    return evaluate_use_module (t);

  /* Computational markup */
  case OR:
    return evaluate_or (t);
  case XOR:
    return evaluate_xor (t);
  case AND:
    return evaluate_and (t);
  case NOT:
    return evaluate_not (t);
  case PLUS:
  case MINUS:
    return evaluate_plus_minus (t);
  case TIMES:
  case OVER:
    return evaluate_times_over (t);
  case DIV:
    return evaluate_divide (t);
  case MOD:
    return evaluate_modulo (t);
  case MATH_SQRT:
    return evaluate_math_sqrt (t);
  case EXP:
    return evaluate_exp (t);
  case LOG:
    return evaluate_log (t);
  case POW:
    return evaluate_pow (t);
  case COS:
    return evaluate_cos (t);
  case SIN:
    return evaluate_sin (t);
  case TAN:
    return evaluate_tan (t);
  case MERGE:
    return evaluate_merge (t);
  case LENGTH:
    return evaluate_length (t);
  case RANGE:
    return evaluate_range (t);
  case NUMBER:
    return evaluate_number (t);
  case _DATE:
    return evaluate_date (t);
  case TRANSLATE:
    return evaluate_translate (t);
  case CHANGE_CASE:
    return evaluate_change_case (t);
  case FIND_FILE:
    return evaluate_find_file (t);
  case IS_TUPLE:
    return evaluate_is_tuple (t);
  case LOOK_UP:
    return evaluate_lookup (t);
  case EQUAL:
    return evaluate_equal (t);
  case UNEQUAL:
    return evaluate_unequal (t);
  case LESS:
    return evaluate_less (t);
  case LESSEQ:
    return evaluate_lesseq (t);
  case GREATER:
    return evaluate_greater (t);
  case GREATEREQ:
    return evaluate_greatereq (t);
  case BLEND:
    return evaluate_blend (t);

  /* Length units */
  case CM_LENGTH:
    return evaluate_cm_length ();
  case MM_LENGTH:
    return evaluate_mm_length ();
  case IN_LENGTH:
    return evaluate_in_length ();
  case PT_LENGTH:
    return evaluate_pt_length ();
  case BP_LENGTH:
    return evaluate_bp_length ();
  case DD_LENGTH:
    return evaluate_dd_length ();
  case PC_LENGTH:
    return evaluate_pc_length ();
  case CC_LENGTH:
    return evaluate_cc_length ();
  case FS_LENGTH:
    return evaluate_fs_length ();
  case FBS_LENGTH:
    return evaluate_fbs_length ();
  case EM_LENGTH:
    return evaluate_em_length ();
  case LN_LENGTH:
    return evaluate_ln_length ();
  case SEP_LENGTH:
    return evaluate_sep_length ();
  case YFRAC_LENGTH:
    return evaluate_yfrac_length ();
  case EX_LENGTH:
    return evaluate_ex_length ();
  case FN_LENGTH:
    return evaluate_fn_length ();
  case FNS_LENGTH:
    return evaluate_fns_length ();
  case BLS_LENGTH:
    return evaluate_bls_length ();
  case FNBOT_LENGTH:
    return evaluate_fnbot_length ();
  case FNTOP_LENGTH:
    return evaluate_fntop_length ();
  case SPC_LENGTH:
    return evaluate_spc_length ();
  case XSPC_LENGTH:
    return evaluate_xspc_length ();
  case PAR_LENGTH:
    return evaluate_par_length ();
  case PAG_LENGTH:
    return evaluate_pag_length ();
  case GW_LENGTH:
    return evaluate_gw_length ();
  case GH_LENGTH:
    return evaluate_gh_length ();
  case GU_LENGTH:
    return evaluate_gu_length ();
  case TMPT_LENGTH:
    return evaluate_tmpt_length ();
  case PX_LENGTH:
    return evaluate_px_length ();
  case MSEC_LENGTH:
    return evaluate_msec_length ();
  case SEC_LENGTH:
    return evaluate_sec_length ();
  case MIN_LENGTH:
    return evaluate_min_length ();
  case HR_LENGTH:
    return evaluate_hr_length ();

  /* Primitives for stylesheet editing */
  case STYLE_WITH:
  case VAR_STYLE_WITH:
    return evaluate (t[N(t)-1]);
  case STYLE_ONLY:
  case VAR_STYLE_ONLY:
  case ACTIVE:
  case VAR_ACTIVE:
  case INACTIVE:
  case VAR_INACTIVE:
    return evaluate_compound (t);
  case REWRITE_INACTIVE:
    return evaluate_rewrite (t);

  /* Linking primitives */
  case HARD_ID:
    return evaluate_hard_id (t[0]);
  case SCRIPT:
    return evaluate_script (t);
  case HLINK:
  case ACTION:
    return evaluate_compound (t);
  case SET_BINDING:
    return evaluate_set_binding (t);
  case GET_BINDING:
    return evaluate_get_binding (t);

  /* Graphical primitives */
  case PATTERN:
    return evaluate_pattern (t);
  case _POINT:
    return evaluate_point (t);
    /*
  case BOX_INFO:
    return evaluate_box_info (t);
  case FRAME_DIRECT:
    return evaluate_frame_direct (t);
  case FRAME_INVERSE:
    return evaluate_frame_inverse (t);
    */

  /* User extensions */
  default:
    if (L(t) < START_EXTENSIONS) {
      int i, n= N(t);
      tree r (t, n);
      for (i=0; i<n; i++)
	r[i]= evaluate (t[i]);
      transfer_ip (t, r);
      return r;
    }
    else {
      tree r= evaluate_compound (t);
      return r;
    }      
  }
}