public ExprBinary(ExprOp op, Expr left, Expr right) { Contract.Requires(null != left); Contract.Requires(null != right); Op = op; Left = left; Right = right; }
public void ExprParser_Parse_HandlesMultExpression() { string expr = "2*3"; InputTextStream inStream = new InputTextStream(expr); ExprParser parser = new ExprParser(); ExprOp result = parser.Parse(inStream); Assert.IsNotNull(result); Assert.AreEqual(OpKindEnum.O_MUL, result.Kind); Assert.AreEqual(2, result.Left.AsValue.AsLong); Assert.AreEqual(3, result.Right.AsValue.AsLong); }
/// <summary> /// Ported from void symbol_scope_t::define /// </summary> public override void Define(SymbolKindEnum kind, string name, ExprOp exprOp) { Logger.Current.Debug("scope.symbols", () => String.Format("Defining '{0}' = {1} in {2}", name, exprOp, this)); if (Symbols == null) { Symbols = new Dictionary <Symbol, ExprOp>(); } Symbol symbol = new Symbol(kind, name, exprOp); Symbols[symbol] = exprOp; }
public ExprFunc LookForCommand(Scope scope, string verb) { ExprOp def = scope.Lookup(SymbolKindEnum.COMMAND, verb); if (def != null) { return(def.AsFunction); } else { return(Expr.EmptyFunc); } }
public void ExprOp_Calc_O_GTE_ReturnsTrueIfLeftEqualsToRight() { ExprOp exprOp1 = new ExprOp(OpKindEnum.O_GTE); exprOp1.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(2) }; exprOp1.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(2) }; Assert.Equal(2 >= 2, exprOp1.Calc(null).Bool); }
public void BindScope_Lookup_CallsParentLookupNext() { MockScope mockScope1 = new MockScope(); MockScope mockScope2 = new MockScope("grand-child"); mockScope2.LookupResult = null; BindScope bindScope = new BindScope(mockScope1, mockScope2); ExprOp lookupResult = bindScope.Lookup(SymbolKindEnum.FUNCTION, "dummy"); Assert.Equal(mockScope1.LookupResult, lookupResult); Assert.Equal(1, mockScope1.LookupCalls.Count); Assert.Equal(1, mockScope2.LookupCalls.Count); }
public override ExprOp Lookup(SymbolKindEnum kind, string name) { if (kind != SymbolKindEnum.FUNCTION) { return(null); } if (name == "value") { return(ExprOp.WrapFunctor(GetValue)); } return(base.Lookup(kind, name)); }
public void ExprOp_Calc_O_GTE_ReturnsFalseIfLeftLessThanRight() { ExprOp exprOp1 = new ExprOp(OpKindEnum.O_GTE); exprOp1.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(1) }; exprOp1.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(2) }; Assert.AreEqual(1 >= 2, exprOp1.Calc(null).Bool); }
public void ExprOp_Calc_O_AND_Considers_Left_Int_0_ToBe_False() { ExprOp exprOp1 = new ExprOp(OpKindEnum.O_AND); exprOp1.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(0) }; exprOp1.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.True }; Assert.IsFalse(exprOp1.Calc(null).Bool); }
public void ExprOp_Calc_O_OR_Considers_Left_Int_10_ToBe_True() { ExprOp exprOp1 = new ExprOp(OpKindEnum.O_OR); exprOp1.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(10) }; exprOp1.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.False }; Assert.True(exprOp1.Calc(null).Bool); }
public void CallScope_Resolve_CallsAnyValuesAsExpr() { CallScope callScope = new CallScope(new MockScope()); ExprOp testExprOp = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(55) }; callScope.PushFront(Value.Get(testExprOp)); Value result = callScope.Resolve(0, ValueTypeEnum.Integer, true); Assert.Equal(55, result.AsLong); }
private static Value FnAll(CallScope args) { Account account = args.Context <Account>(); ExprOp expr = args.Get <ExprOp>(0); foreach (Post p in account.Posts) { BindScope boundScope = new BindScope(args, p); if (!expr.Calc(boundScope, args.Locus, args.Depth).AsBoolean) { return(Value.False); } } return(Value.True); }
private static Value FnAny(CallScope args) { Post post = args.Context <Post>(); ExprOp expr = args.Get <ExprOp>(0); foreach (Post p in post.Xact.Posts) { BindScope boundScope = new BindScope(args, p); if (expr.Calc(boundScope, args.Locus, args.Depth).AsBoolean) { return(Value.True); } } return(Value.False); }
public void CallScope_Value_ThisIsEqualToResolve() { CallScope callScope = new CallScope(new MockScope()); ExprOp testExprOp = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(55) }; callScope.PushFront(Value.Get(testExprOp)); Value result = callScope[0]; Assert.Equal(55, result.AsLong); }
public void CallScope_Value_MakesSureThatAllArgumentsAreResolved() { CallScope callScope = new CallScope(new MockScope()); ExprOp testExprOp = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(55) }; callScope.PushFront(Value.Get(testExprOp)); Value result = callScope.Value(); Assert.Equal(55, callScope.Args.AsSequence[0].AsLong); }
public void AutoXact_PostPred_Checks_VALUE() { ExprOp op = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(true) }; Assert.True(AutoXact.PostPred(op, null)); ExprOp op1 = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(false) }; Assert.False(AutoXact.PostPred(op1, null)); }
public void Session_Lookup_LooksForHandlers() { Session session = new Session(); ExprOp checkPayeesOp = session.Lookup(SymbolKindEnum.OPTION, "check-payees"); Assert.IsNotNull(checkPayeesOp); Assert.IsTrue(checkPayeesOp.IsFunction); Assert.AreEqual(OpKindEnum.FUNCTION, checkPayeesOp.Kind); Assert.IsNotNull(checkPayeesOp.AsFunction); // Check that the function is callable CallScope callScope = new CallScope(new EmptyScope()); callScope.PushBack(Value.Get("str")); checkPayeesOp.AsFunction(callScope); Assert.IsTrue(session.CheckPayeesHandler.Handled); }
public static bool PostPred(ExprOp op, Post post) { switch (op.Kind) { case OpKindEnum.VALUE: return(op.AsValue.AsBoolean); case OpKindEnum.O_MATCH: if (op.Left.Kind == OpKindEnum.IDENT && op.Left.AsIdent == "account" && op.Right.Kind == OpKindEnum.VALUE && op.Right.AsValue.Type == ValueTypeEnum.Mask) { return(op.Right.AsValue.AsMask.Match(post.ReportedAccount.FullName)); } else { break; } case OpKindEnum.O_EQ: return(PostPred(op.Left, post) == PostPred(op.Right, post)); case OpKindEnum.O_NOT: return(!PostPred(op.Left, post)); case OpKindEnum.O_AND: return(PostPred(op.Left, post) && PostPred(op.Right, post)); case OpKindEnum.O_OR: return(PostPred(op.Left, post) || PostPred(op.Right, post)); case OpKindEnum.O_QUERY: if (PostPred(op.Left, post)) { return(PostPred(op.Right.Left, post)); } else { return(PostPred(op.Right.Right, post)); } default: break; } throw new CalcError(CalcError.ErrorMessageUnhandledOperator); }
public void SymbolScope_Define_AddsSymbolToSymbols() { MockScope mockScope = new MockScope(); SymbolScope symbolScope = new SymbolScope(mockScope); SymbolKindEnum kind = SymbolKindEnum.FUNCTION; string name = "the-name"; ExprOp exprOp = new ExprOp(OpKindEnum.CONSTANTS); symbolScope.Define(kind, name, exprOp); Assert.Equal(1, symbolScope.Symbols.Count); Assert.Equal(kind, symbolScope.Symbols.First().Key.Kind); Assert.Equal(name, symbolScope.Symbols.First().Key.Name); Assert.Equal(exprOp, symbolScope.Symbols.First().Key.Definition); Assert.Equal(exprOp, symbolScope.Symbols[symbolScope.Symbols.First().Key]); }
public void ExprOp_Calc_O_DIV_DoesNotModifyOriginalLeftArgument() { ExprOp exprOp1 = new ExprOp(OpKindEnum.O_DIV); exprOp1.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(10) }; exprOp1.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(5) }; Value result = exprOp1.Calc(null); Assert.Equal(10, exprOp1.Left.AsValue.AsLong); Assert.Equal(2, result.AsLong); }
/// <summary> /// Ported from: op_bool_tuple find_option(scope_t& scope, const string& name) /// </summary> public static Tuple <ExprOp, bool> FindOption(Scope scope, string name) { if (name != null && name.Length > 127) { throw new OptionError(String.Format(OptionError.ErrorMessage_IllegalOption, name)); } name = name.Replace('-', '_') + '_'; ExprOp exprOp = scope.Lookup(SymbolKindEnum.OPTION, name); if (exprOp != null) { return(new Tuple <ExprOp, bool>(exprOp, true)); } name = name.Remove(name.Length - 1); return(new Tuple <ExprOp, bool>(scope.Lookup(SymbolKindEnum.OPTION, name), false)); }
public void Session_Lookup_LooksForFunctors() { Session session = new Session(); ExprOp minFunc = session.Lookup(SymbolKindEnum.FUNCTION, "min"); Assert.IsNotNull(minFunc); Assert.IsTrue(minFunc.IsFunction); Assert.AreEqual(OpKindEnum.FUNCTION, minFunc.Kind); Assert.IsNotNull(minFunc.AsFunction); // Check that the function is callable CallScope callScope = new CallScope(new EmptyScope()); callScope.PushBack(Value.Get("1")); callScope.PushBack(Value.Get("2")); string result = minFunc.AsFunction(callScope).AsString; Assert.AreEqual("1", result); }
public void ExprOp_Calc_O_OR_ReturnsLeftValueInCaseItIsConsideredAsTrue() { Value leftValue = Value.StringValue("some-left-string-value"); Value rightValue = Value.False; ExprOp exprOp1 = new ExprOp(OpKindEnum.O_OR); exprOp1.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = leftValue }; exprOp1.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = rightValue }; Value result = exprOp1.Calc(null); Assert.Equal("some-left-string-value", result.AsString); }
public void ExprParser_Parse_HandlesAddExpression() { string expr = "2+3"; InputTextStream inStream = new InputTextStream(expr); ExprParser parser = new ExprParser(); ExprOp result = parser.Parse(inStream); Assert.IsNotNull(result); Assert.AreEqual(OpKindEnum.O_ADD, result.Kind); Assert.AreEqual(2, result.Left.AsValue.AsLong); Assert.AreEqual(3, result.Right.AsValue.AsLong); // Just in case... EmptyScope scope = new EmptyScope(); Value val = result.Calc(scope); Assert.AreEqual(5, val.AsLong); }
public void SymbolScope_Lookup_TriesGetAValueFromSymbols() { MockScope mockScope = new MockScope(); SymbolScope symbolScope = new SymbolScope(mockScope); SymbolKindEnum kind = SymbolKindEnum.FUNCTION; string name = "the-name"; ExprOp exprOp = new ExprOp(OpKindEnum.CONSTANTS); symbolScope.Define(kind, name, exprOp); ExprOp result = symbolScope.Lookup(kind, name); Assert.Equal(exprOp, result); ExprOp result2 = symbolScope.Lookup(SymbolKindEnum.OPTION, "dummy"); Assert.Equal(mockScope.LookupResult, result2); }
private static Value FnAll(CallScope args) { Post post = args.Context <Post>(); ExprOp expr = args.Get <ExprOp>(0); foreach (Post p in post.Xact.Posts) { BindScope boundScope = new BindScope(args, p); if (p == post && args.Has <ExprOp>(1) && !args.Get <ExprOp>(1).Calc(boundScope, args.Locus, args.Depth).AsBoolean) { // If the user specifies any(EXPR, false), and the context is a // posting, then that posting isn't considered by the test. ; // skip it } else if (expr.Calc(boundScope, args.Locus, args.Depth).AsBoolean) { return(Value.False); } } return(Value.True); }
public void ExprParser_Parse_HandlesExpressionsWithParenthesises() { string expr = "2+(3*(4 - 1)/5)"; InputTextStream inStream = new InputTextStream(expr); ExprParser parser = new ExprParser(); ExprOp result = parser.Parse(inStream); Assert.IsNotNull(result); Assert.AreEqual(OpKindEnum.O_ADD, result.Kind); Assert.AreEqual(2, result.Left.AsValue.AsLong); Assert.AreEqual(OpKindEnum.O_DIV, result.Right.Kind); Assert.AreEqual(5, result.Right.Right.AsValue.AsLong); Assert.AreEqual(OpKindEnum.O_MUL, result.Right.Left.Kind); Assert.AreEqual(3, result.Right.Left.Left.AsValue.AsLong); Assert.AreEqual(OpKindEnum.O_SUB, result.Right.Left.Right.Kind); Assert.AreEqual(4, result.Right.Left.Right.Left.AsValue.AsLong); Assert.AreEqual(1, result.Right.Left.Right.Right.AsValue.AsLong); }
public void ExprOp_Dump_ReturnsContentOfExprOp() { Value val = Value.StringValue("some-left-string-value"); ExprOp exprOp1 = new ExprOp(OpKindEnum.VALUE) { AsValue = val }; Assert.Equal("VALUE: \"some-left-string-value\" (0)", exprOp1.Dump().TrimEnd()); ExprOp exprOp2 = new ExprOp(OpKindEnum.O_OR); exprOp2.Left = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.Get(10) }; exprOp2.Right = new ExprOp(OpKindEnum.VALUE) { AsValue = Value.False }; Assert.Equal("O_OR (0)\n VALUE: 10 (0)\n VALUE: false (0)", exprOp2.Dump().TrimEnd().RemoveCarriageReturns()); }
public static string OpContext(ExprOp op, ExprOp locus = null) { ExprOpContext context = new ExprOpContext(op, locus); StringBuilder sb = new StringBuilder(" "); string buf = null; if (op.Print(ref buf, context)) { sb.AppendLine(buf); for (int i = 0; i < context.EndPos; i++) { if (i > context.StartPos) { sb.Append('^'); } else { sb.Append(' '); } } } return(sb.ToString()); }
public Symbol(SymbolKindEnum kind, string name, ExprOp definition) : this() { Kind = kind; Name = name; Definition = definition; }
public Predicate(ExprOp op, AnnotationKeepDetails whatToKeep, Scope context = null) : base(op, context) { WhatToKeep = whatToKeep; }
public virtual int _intOnExpr( Context ctx, ExprOp op ) { Variable rv = null; switch( op ) { case ExprOp.Add: rv = OnAdd( ctx.StackItem( 0 ), ctx.StackItem( 1 ) ); break; case ExprOp.Sub: rv = OnSub( ctx.StackItem( 0 ), ctx.StackItem( 1 ) ); break; case ExprOp.Mul: rv = OnMul( ctx.StackItem( 0 ), ctx.StackItem( 1 ) ); break; case ExprOp.Div: rv = OnDiv( ctx.StackItem( 0 ), ctx.StackItem( 1 ) ); break; case ExprOp.Mod: rv = OnMod( ctx.StackItem( 0 ), ctx.StackItem( 1 ) ); break; case ExprOp.Compare: rv = OnCompare( ctx.StackItem( 0 ), ctx.StackItem( 1 ) ); break; case ExprOp.Negate: rv = OnNegate(); break; } if( rv != null ) { ctx.Push( rv ); return RC.SUCCESS; } return RC.ENOTSUP; }