public static async Task <NodeExpr> Eval(NodeExpr e, IAlgebra <N> a, CancellationToken t, IVars v = null) { var vv = (v != null) ? a.Session.Vars.CreateAmbito(v) : a.Session.Vars; var visit = new EvaluateVisitor <N>(a, vv); var r = await visit.Visit(e, t); t.ThrowIfCancellationRequested(); return(r); }
private void EvalOperation(Stack <NodeExpr> se, Stack <ETypeBinary> so) { var pType = so.Pop(); if (se.Count < 2) { throw new InvalidOperationException(); } var r = se.Pop(); var l = se.Pop(); se.Push(NodeExpr.Binary(pType, l, r)); }
private async Task <ParseTermResult> ParseTerm() { await ReadToken(); Tokenizer.EType pType; string pValue; string pToken; lock (mTokenizer) { pType = mTokenizer.TypeToken; pValue = mTokenizer.Value; pToken = mTokenizer.Token; } switch (pType) { case Tokenizer.EType.Number: return(new ParseTermResult { TypeTerm = ParseTermResult.EType.Expression, Expr = NodeExpr.Number(mAlg.ParseNumber(pValue)) }); case Tokenizer.EType.Sign: return(new ParseTermResult { TypeTerm = ParseTermResult.EType.UnaryOperation, TypeUnary = MathExpr.StrToUnary[pToken] }); case Tokenizer.EType.Operation: return(new ParseTermResult { TypeTerm = ParseTermResult.EType.Operation, TypeBinary = MathExpr.StrToBinaries[pToken] }); case Tokenizer.EType.EOF: return(new ParseTermResult { TypeTerm = ParseTermResult.EType.EOF }); default: return(new ParseTermResult { TypeTerm = ParseTermResult.EType.Token }); } }
private async Task <NodeExprInstruction> ParseInstruction() { for (; ;) { mTokenCancel.ThrowIfCancellationRequested(); var pExpr = await ParseExpr(); Tokenizer.EType pType; lock (this) { pType = mTokenizer.TypeToken; switch (mTokenizer.TypeToken) { case Tokenizer.EType.TerminateDolar: case Tokenizer.EType.TerminateSemiColon: return(NodeExpr.Instruction(pExpr, pType == Tokenizer.EType.TerminateSemiColon)); } } } }
public override Task <NodeExpr> Visit(NodeExprBinary e, CancellationToken t) { return(Task.Run ( () => { var lt = Visit(e.Left, t); var rt = Visit(e.Right, t); Task.WaitAll(new[] { lt, rt }, t); t.ThrowIfCancellationRequested(); var l = lt.Result; var r = rt.Result; return ((l is NodeExprNumber <N> ln) && (r is NodeExprNumber <N> rn)) ? (NodeExpr)NodeExpr.Number(mAlg.EvalBinaryOperator(e.TypeBinary, ln.Value, rn.Value)) : NodeExpr.Binary(e.TypeBinary, l, r); } )); }
private async Task <NodeExpr> ParseExprOperations() { var pStackExpr = new Stack <NodeExpr>(); var pStackOps = new Stack <ETypeBinary>(); var pExit = false; do { mTokenCancel.ThrowIfCancellationRequested(); var pTerm = await ParseTerm(); switch (pTerm.TypeTerm) { case ParseTermResult.EType.Expression: pStackExpr.Push(pTerm.Expr); break; case ParseTermResult.EType.UnaryOperation: var pExpr = await ParseExpr(); pStackExpr.Push(NodeExpr.Unary(pTerm.TypeUnary, pExpr)); break; case ParseTermResult.EType.Operation: if (pStackOps.Count <= 0) { pStackOps.Push(pTerm.TypeBinary); } else { var t = pStackOps.Peek(); if (MathExpr.TypeBinariesPriorities[pTerm.TypeBinary] > MathExpr.TypeBinariesPriorities[t]) { EvalOperation(pStackExpr, pStackOps); } pStackOps.Push(pTerm.TypeBinary); } break; case ParseTermResult.EType.EOF: await Yield(new ParseResult { Finished = false }); break; default: pExit = true; break; } } while (!pExit); while (pStackOps.Count > 0) { mTokenCancel.ThrowIfCancellationRequested(); EvalOperation(pStackExpr, pStackOps); } if (pStackExpr.Count != 1) { throw new InvalidOperationException(); } return(pStackExpr.Pop()); }
public static string ToString(NodeExpr e) => AsyncHelper.RunSync(() => new NodeExprStringBuilderVisitor().Visit(e, new CancellationTokenSource().Token));
public static Task <string> ToStringAsync(NodeExpr e, CancellationToken t) => new NodeExprStringBuilderVisitor().Visit(e, t);
public NodeExprUnary(ETypeUnary t, NodeExpr e) : this() { TypeUnary = t; Expr = e; }
public static NodeExprInstruction Instruction(NodeExpr e, bool isshowresult) => new NodeExprInstruction(e, isshowresult);
public static NodeExprBinary Binary(ETypeBinary t, NodeExpr l, NodeExpr r) => new NodeExprBinary(t, l, r);
public static NodeExprUnary Unary(ETypeUnary t, NodeExpr e) => new NodeExprUnary(t, e);
protected NodeExpr(NodeExpr e) : this(e.TypeExpr) { }
public NodeExprInstruction(NodeExpr e, bool isshowresult) : this() { Expr = e; IsShowResult = isshowresult; }
public NodeExprBinary(ETypeBinary type, NodeExpr left, NodeExpr right) : this() { TypeBinary = type; Left = left; Right = right; }
public virtual T Visit(NodeExpr e) => e.Accept(this);
public virtual Task <T> Visit(NodeExpr e, CancellationToken t) => e.Accept(this, t);
public static string ToString(NodeExpr e) => new NodeExprStringBuilderVisitor().Visit(e, new CancellationTokenSource().Token).WaitAndResult();