Example #1
0
Variant SglExprLex::eval()
{
  CallBacks cb;
  NumValue pResult;
  int errorpos;
  int errortokenlg;
  Variant result;

  scan();
  cb.str2Val = str2Val;
  cb.appendStr = appendStr;
  cb.convertStr = convertStr;
  cb.addParam = addParam;
  cb.callFunction = callFunction;
  cb.assignVar = assignVar;
  cb.getToken = getToken;
  index = -1;
  if (parseexpr(cb, &pResult, &errorpos, &errortokenlg) == PARSE_OK)
    result = numValue2variant(pResult);
  else 
    result = SyntaxError;
  strlist.clear();
  numlist.clear();
  paramlists.clear();
  tokens.clear();
  return result;
}
Example #2
0
	double MYMICRO::eval(int &err)
	{
		err = -1;
		double ret=0,tp;
		double *val;
		try
		{
			auto tokeniter = cur_tokens.begin();
			while(tokeniter != cur_tokens.end())
			{
				auto titer2= tokeniter+1;
				if(titer2 != cur_tokens.end() &&titer2->tokentype == TOKEN::ASSI && tokeniter->tokentype == TOKEN::VAID)
				{
					val = &this->var_table[tokeniter->tokenval];
					tokeniter = titer2+1;
				}
				else
				{
					val = &this->var_table["ans"];
				}
				if(tokeniter == cur_tokens.end()) return 0.0;
				double t = parseexpr(cur_tokens,tokeniter);
				*val = t;
				ret = *val;
				if(tokeniter != cur_tokens.end()) ++tokeniter;
			}
		}
		catch(MYMICRO::ERRORTYPE e)
		{
			err= e;
		}
		catch(...)
		{
			err = 100;
		}
		return ret;

	}
Example #3
0
File: tagfs.c Project: ericvh/tagfs
static void
fsread(Req* r)
{
	Query*	q;
	char**	toks;
	int	ntoks;
	int	atoks;
	char*	s;
	int	pos;
	File*	f;

	if(r->fid->qid.type&QTDIR){
		respond(r, "bug: write on dir");
		return;
	}
	f = r->fid->file;
	if(f == ctlf){
		ctlread(r);
		return;
	}
	q = f->aux;

	/* The first read process the query.
	 * Further reads just retrieve more data,
	 * if any.
	 */
	if(q->expr == nil){
		atoks = 512;
		toks = emalloc9p(atoks*sizeof(char*));
		do {
			s = estrdup9p(q->text);
			ntoks = tokenize(s, toks, atoks);
			if(ntoks == atoks){
				atoks += 512;
				toks = erealloc9p(toks, atoks*sizeof(char*));
				free(s);
			}
		}while(ntoks == atoks);
		pos = 0;
		if(chatty9p)
			fprint(2, "compiling %s (%d toks)\n", q->text, ntoks);
		q->expr = parseexpr(ntoks, toks, &pos);
		if(q->expr == nil){
			free(s);
			free(toks);
			respond(r, "syntax error");
			return;
		}
		if(chatty9p)
			fprint(2, "evaluating %s (%d toks)\n", q->text, ntoks);
		evalexpr(trie, q->expr);
		free(s);
		free(toks);
		free(q->text);
		/* smprintexprval limits the total output to
		 * at most 64K of text
		 */
		q->text = smprintexprval(q->expr);
		if(chatty9p)
			fprint(2, "result is [%s]\n", q->text);
	}
	/* After the query is process,
	 * q->text holds the reply.
	 */
	readstr(r,  q->text);
	respond(r, nil);
}
Example #4
0
/*函数参数解析及其计算,添加函数到其中就可以退添加函数*/
double MYMICRO::parsefunction(std::vector<TOKEN> & tokens,std::vector<TOKEN>::iterator & tokeniter)
{
	/*参数栈*/
	//stack<double> params;
	vector<double> params;
	//double param=0;
	double ret=0;
	//int leftp=1;/*记录括号数*/
	auto func = *tokeniter;
	++tokeniter;/*读掉当前函数名*/
	++tokeniter;/*读掉一个括号*/
	while(true)
	{
		params.push_back(parseexpr(tokens,tokeniter));
		//params.push();
		if(tokeniter != tokens.end() && tokeniter->tokentype == TOKEN::COMM)
		{
			++tokeniter;
			continue;
		}
		else
		{
				if(tokeniter != tokens.end()) ++tokeniter;
				break;
		}
	}
	if(func.tokenval =="sin")
	{
		if(params.size()==1)
		{
			ret = sin(params[0]);
		}
		else throw(FPERROR3);
	}
	else if(func.tokenval =="cos")
	{
		if(params.size()==1)
		{
			ret = cos(params[0]);
		}
		else throw(FPERROR3);
	}
	else if(func.tokenval =="log")
	{
		if(params.size()==1)
		{
			ret = log(params[0]);
		}
		else if(params.size()==2)
		{
			ret = log(params[1])/log(params[0]);
		}
		else throw(FPERROR3);
	}
	else if(func.tokenval =="global")
	{
		for(auto it:this->var_table)
		{
			cout<<it.first<<":\t"<<it.second<<"\n";
		}
	}
	else if(func.tokenval =="help")
	{
		cout<<"help funciton\n sorry now it very simpleness\n later it will be detailedness! \n";
		cout<<"functions:\nhelp(),global()\nabs(),max(),min(),\nexp(),log(base,index),rand(),sum(),\npi(),sin(),cos()\n";
	}
	else if(func.tokenval =="max")
	{
		double tp = 0.0;
		if(params.size()<1)
		{
			throw(FPERROR3);
		}
		else
		{
			tp = params[0];
		}
		for(int i = 1; i<params.size();++i)
		{
			if(tp<params[i])
			{
				tp = params[i];
			}
		}
		ret = tp;
	}
	else if(func.tokenval =="min")
	{
		double tp = 0.0;
		if(params.size()<1)
		{
			throw(FPERROR3);
		}
		else
		{
			tp = params[0];
		}
		for(int i = 1; i<params.size();++i)
		{
			if(tp>params[i])
			{
				tp = params[i];
			}
		}
		ret = tp;
	}
	else if(func.tokenval =="rand")
	{
		//double tp = 0.0;
		if(params.size()>1)
		{
			throw(FPERROR3);
		}
		ret = calcrnd();
	}
	else if(func.tokenval =="sum")
	{
		double tp = 0.0;
		if(params.size()<1)
		{
			throw(FPERROR3);
		}
		for(int i = 0; i<params.size();++i)
		{
			tp+=params[i];
		}
		ret = tp;
	}
	else if(func.tokenval =="exp")
	{
		if(params.size()==1)
		{
			ret = exp(params[0]);
		}
		else throw(FPERROR3);
	}
	else if(func.tokenval =="abs")
	{
		if(params.size()==1)
		{
			ret = abs(params[0]);
		}
		else throw(FPERROR3);
	}
	else if(func.tokenval =="pi")
	{
		ret = 3.14159265358979;
	}
	else
	{
		throw(FPERROR2);
	}
	return ret;
}