/// <summary> /// Scan and Parse the string expression, return a syntax tree. /// </summary> /// <returns></returns> public ExpressionData Parse(string expr) { _expressionData = new ExpressionData(); _expressionData.Expression = expr; // ExprDecoder -> ExprParser // build the configuration data (operators...) depending on the definition _operatorsBuilder.BuildOperators(_exprEvalConfig); // the parser need them to compact/group tokens after the first raw parse/split of tokens _scanner.SetConfiguration(_exprEvalConfig); _exprTokensParser.SetConfiguration(_exprEvalConfig); // split the raw string expression into list of tokens List <ExprToken> list = _scanner.SplitExpr(expr); // group/compact tokens if necessary, like operators comparators (having 2 char length) List <ExprToken> listCompacted = _scanner.GroupTokens(expr, list); // parse the tokens found by the scanner in the string expression _expressionData.ExprParseResult = _exprTokensParser.Parse(expr, listCompacted); // analyze the tree of expressions result: get variables, functionCall, define the result data type of the expression _exprSyntaxTreeAnalyzer.Analyze(_expressionData.ExprParseResult); return(_expressionData); }
/// <summary> /// Scan and Parse the string expression, return a syntax tree. /// </summary> /// <returns></returns> public ParseResult Parse(string expr) { // clear the config errors (define var, attach func) _exprExecConfigurator.ClearAllErrorExprConfig(); // parse/decode the expression _expressionData = _exprScannerParser.Parse(expr); // provide the expression data to the execution managers _exprExecConfigurator.SetExpressionData(_expressionData); _exprExecutor.SetExpressionData(_expressionData); return(_expressionData.ExprParseResult); }
/// <summary> /// Start the execution of the last parsed expression. /// Should have any error to execute it. /// </summary> /// <returns></returns> public ExpressionData Exec(ExpressionData expressionData) { // err, parse not executed before _expressionData = expressionData; if (_expressionData == null) { _expressionData = new ExpressionData(); } if (_expressionData.ExprExecResult == null) { // create a new execution/evaluation result, remove the previous if it exists _expressionData.CreateExprExecResult(); // propagate the parse result to the exec result _expressionData.ExprExecResult.ParseResult = _expressionData.ExprParseResult; } // check if the parse is present without any error if (!CheckParse(_expressionData)) { return(_expressionData); } // push variables definition to the result exec GetVarDefinitionToExecResult(); // check that all used variables are defined with a value if (!CheckVariables()) { return(_expressionData); } // check that all functionCall are linked/attached to a function body if (!CheckFunctionCalls()) { return(_expressionData); } // start the execution, analyze and return the list of variable to define ExpressionExecBase exprExecBase; ExecExpression(_expressionData.ExprExecResult, _expressionData.ExprParseResult.RootExpr, out exprExecBase); _expressionData.ExprExecResult.ExprExec = exprExecBase; return(_expressionData); }
/// <summary> /// Default constructor. /// </summary> public ExpressionEval() { _exprEvalConfig = new ExpressionEvalConfig(); _exprScannerParser = new ExprScannerParser(_exprEvalConfig); _exprExecConfigurator = new ExprExecConfigurator(); _exprExecutor = new ExprExecutor(); // todo: passer directement dans le constructeur comme pour le scannerParser? _exprExecConfigurator.SetConfiguration(_exprEvalConfig); _exprExecutor.SetConfiguration(_exprEvalConfig); // default language is en/english SetLang(Language.En); // default double decimal separator _exprEvalConfig.SetDecimalAndFunctionSeparators(DecimalAndFunctionSeparators.Standard); _expressionData = null; }
/// <summary> /// check if the parse is present without any error. /// </summary> /// <param name="expressionData"></param> /// <returns></returns> private bool CheckParse(ExpressionData expressionData) { // parse result should exists if (expressionData.ExprParseResult == null) { expressionData.ExprExecResult.AddErrorExec(ErrorCode.ParsedExpressionMissing); return(false); } // parse result exists, has error? if (expressionData.ExprParseResult.HasError) { // push parse errors to execResult foreach (ExprError err in expressionData.ExprParseResult.ListError) { expressionData.ExprExecResult.AddError(err); } //expressionData.ExprExecResult.AddErrorExec(ErrorCode.ParsedExpressionHasError); return(false); } // ok the parse result is present without any error return(true); }
/// <summary> /// Start the execution of the last parsed expression. /// Evaluate the expression, generate a result value. /// /// Should have any error before (parse stage) to execute it. /// </summary> /// <returns></returns> public ExecResult Exec() { _expressionData = _exprExecutor.Exec(_expressionData); return(_expressionData.ExprExecResult); }
public void SetExpressionData(ExpressionData expressionData) { _expressionData = expressionData; }