public SimpleParser() { var operators = DefaultOperators.Evaluatables; Evaluator = new PostFixEvaluator(operators); Converter = new PostFixConverter(operators); }
public override IValue Eval(IValue[] parameters) { this.Validate(parameters); var set = parameters[0].AsSet; var res = new ListArray(); var i = new Variable(parameters[1].AsString, new DoubleValue(0)); var evaluator = default(PostFixEvaluator); this.Context.VariableManager.Define(i); try { var tokenizer = new Tokenizer(parameters[2].AsString, this.Context.Config); tokenizer.Run(); var parser = new Parser(tokenizer.Tokens, this.Context.Config); evaluator = new PostFixEvaluator(parser.CreatePostFixExpression(), this.Context); } catch (System.Exception) { throw new OperandEvaluationException(); } foreach (var item in set) { i.Value = item; if (evaluator.Run().AsDouble != 0) { res.Add(item); } } return(new ArrayValue(res)); }
public decimal Calculate(string str) { ShuntingYard sy = new ShuntingYard(); PostFixEvaluator pfe = new PostFixEvaluator(); return(Math.Round(pfe.Evaluate(sy.InfixToPostfix(str)), 2)); }
public void TestInvalidInput() { var evaluator = new PostFixEvaluator(new EvaluatableCollection <double>(Ops)); var result = evaluator.Eval("+ 1 +"); Console.WriteLine(result); }
public void TestUnaryOperators() { var evaluator = new PostFixEvaluator(new EvaluatableCollection <double>(Ops)); var result = evaluator.Eval("1 -"); var expected = -1; Assert.AreEqual(expected, result); }
public void TestBasicEval() { var evaluator = new PostFixEvaluator(new EvaluatableCollection <double>(Ops)); var result = evaluator.Eval("1 2 +"); var expected = 3; Assert.AreEqual(expected, result); }
public void TestMultipleArgumentEvaluation() { var operators = new List <IEvaluatable <double> > { new Operator <double>("f", 2, Associativity.L, (a, b) => a * b) }; var evaluator = new PostFixEvaluator(new EvaluatableCollection <double>(operators)); var result = evaluator.Eval("2 3 f"); var expected = 6; Assert.AreEqual(expected, result); }
public LazyValue(string expression, CalculationContext context) { this.context = context; this.lastRequestWasType = false; this.rawExpression = expression; this.dirty = true; var tokenizer = new Tokenizing.Tokenizer(expression, context.Config); tokenizer.Run(); var parser = new Parsing.Parser(tokenizer.Tokens, context.Config); this.evaluator = new PostFixEvaluator(parser.CreatePostFixExpression(), context); this.BindEvents(); }
public override IValue Eval(IValue[] parameters) { this.Validate(parameters); var add = parameters[0].AsString == "+"; var count = (int)parameters[1].AsDouble; var res = 0.0; var evaluator = default(PostFixEvaluator); var counter = new Variable(parameters[2].AsString, new DoubleValue(0)); this.Context.VariableManager.Define(counter); try { var tokenizer = new Tokenizer(parameters[3].AsString, this.Context.Config); tokenizer.Run(); var parser = new Parser(tokenizer.Tokens, this.Context.Config); evaluator = new PostFixEvaluator(parser.CreatePostFixExpression(), this.Context); } catch (System.Exception) { throw new OperandEvaluationException(); } for (var i = 0; i < count; i++) { counter.Value = new DoubleValue(i); var aggregate = evaluator.Run(); if (aggregate.Type != ValueType.Number) { throw new OperandEvaluationException(); } if (add) { res += aggregate.AsDouble; } else { res *= aggregate.AsDouble; } } this.Context.VariableManager.Remove(counter.Name); return(new DoubleValue(res)); }
public LazyValue(IReadOnlyList <IPostFixExpression> expressions, CalculationContext context) { this.context = context; this.lastRequestWasType = false; this.evaluator = new PostFixEvaluator(expressions, context); this.dirty = true; var sb = new StringBuilder(); foreach (var expression in expressions) { sb.Append(expression.ToString()).Append(' '); } this.rawExpression = sb.ToString(); this.BindEvents(); }
public override IValue Eval(IValue[] parameters) { this.Validate(parameters); var set = parameters[0].AsSet; var evaluator = default(PostFixEvaluator); var i = this.Context.VariableManager.CreateTempVariable(); var res = this.Context.VariableManager.CreateTempVariable(); try { var tokenizer = new Tokenizer(parameters[2].AsString, this.Context.Config); tokenizer.Run(); var tokens = new List <Token>(); tokens.Add(new Token(TokenType.FunctionStart, new Functions.DefaultFunctions.Util.Set().Name)); tokens.Add(new Token(TokenType.String, res.Name)); tokens.Add(new Token(TokenType.Seperator, ",")); tokens.Add(new Token(TokenType.Identifier, res.Name)); tokens.AddRange(tokenizer.Tokens); tokens.Add(new Token(TokenType.Identifier, i.Name)); tokens.Add(new Token(TokenType.FunctionEnd)); var parser = new Parser(tokens, this.Context.Config); evaluator = new PostFixEvaluator(parser.CreatePostFixExpression(), this.Context); } catch (System.Exception) { throw new OperandEvaluationException(); } foreach (var item in set) { i.Value = item; evaluator.Run(); } this.Context.VariableManager.Remove(i.Name); this.Context.VariableManager.Remove(res.Name); return(res.Value); }
public double CALCULATE(string[] formula, int ID_NhanVien, DateTime date) { Parse p = new Parse(formula, ID_NhanVien, date); List <Element> e = p.PARSE; InfixToPostfix i = new InfixToPostfix(); e = i.ConvertFromInfixToPostFix(e); PostFixEvaluator pfe = new PostFixEvaluator(); if (e.Count == 2 && (e[1].ToString() == "+" || e[1].ToString() == "-")) { this.Value = (double)Double.Parse(formula[0]); } else { this.Value = pfe.Evaluate(e); } return(Value); }
public IValue Calculate(string expression) { var config = this.context.Config.Clone(); var value = default(IValue); try { var tokenizer = new Tokenizer(expression, config); tokenizer.Run(); var parser = new Parser(tokenizer.Tokens, config); var evaluater = new PostFixEvaluator(parser.CreatePostFixExpression(), this.context); value = evaluater.Run(); return(value); } catch (TokenizerException t) { return(new ErrorValue(t)); } catch (ParserException p) { return(new ErrorValue(p)); } }
public SimpleParser(List <IEvaluatable <double> > ops) { Evaluator = new PostFixEvaluator(new EvaluatableCollection <double>(ops)); Converter = new PostFixConverter(new EvaluatableCollection <double>(ops)); }
public SimpleParser(EvaluatableCollection <double> ops) { Evaluator = new PostFixEvaluator(ops); Converter = new PostFixConverter(ops); }
public void TestAssociativity() { var evaluator = new PostFixEvaluator(new EvaluatableCollection <double>(Ops)); var result = evaluator.Eval("1 2 -"); var expected = -1; }