Example #1
0
        public void TestParens()
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res =
                Parser.Expression("  1 + 2 * 3  ");

            Assert.AreEqual(new Right(new Tuple <string, ExpressionBuilder>("",
                                                                            new ExpressionBuilder.Binary(
                                                                                ExpressionBuilder.Op.Add,
                                                                                new ExpressionBuilder.Value(Utils.Integer(1)),
                                                                                new ExpressionBuilder.Binary(
                                                                                    ExpressionBuilder.Op.Mul,
                                                                                    new ExpressionBuilder.Value(Utils.Integer(2)),
                                                                                    new ExpressionBuilder.Value(Utils.Integer(3))
                                                                                    )
                                                                                )
                                                                            )),
                            res);

            ExpressionBuilder e = res.Right.Item2;
            SymbolTable       s = new SymbolTable();

            Biscuit.Datalog.Expressions.Expression ex = e.Convert(s);

            Assert.IsTrue(
                Arrays.AsList <Biscuit.Datalog.Expressions.Op>(
                    new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(1)),
                    new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(2)),
                    new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(3)),
                    new Biscuit.Datalog.Expressions.Op.Binary(Biscuit.Datalog.Expressions.Op.BinaryOp.Mul),
                    new Biscuit.Datalog.Expressions.Op.Binary(Biscuit.Datalog.Expressions.Op.BinaryOp.Add)
                    ).SequenceEqual(
                    ex.GetOps())
                );

            Dictionary <ulong, ID> variables = new Dictionary <ulong, ID>();
            Option <ID>            value     = ex.Evaluate(variables);

            Assert.AreEqual(Option.Some(new ID.Integer(7)), value);
            Assert.AreEqual("1 + 2 * 3", ex.Print(s).Get());


            Either <Error, Tuple <string, ExpressionBuilder> > res2 =
                Parser.Expression("  (1 + 2) * 3  ");

            Assert.AreEqual(new ExpressionBuilder.Binary(
                                ExpressionBuilder.Op.Mul,
                                new ExpressionBuilder.Unary(
                                    ExpressionBuilder.Op.Parens,
                                    new ExpressionBuilder.Binary(
                                        ExpressionBuilder.Op.Add,
                                        new ExpressionBuilder.Value(Utils.Integer(1)),
                                        new ExpressionBuilder.Value(Utils.Integer(2))
                                        ))
                                ,
                                new ExpressionBuilder.Value(Utils.Integer(3))
                                )
                            ,
                            res2.Right.Item2);

            ExpressionBuilder e2 = res2.Right.Item2;
            SymbolTable       s2 = new SymbolTable();

            Biscuit.Datalog.Expressions.Expression ex2 = e2.Convert(s2);

            Assert.IsTrue(
                Arrays.AsList <Biscuit.Datalog.Expressions.Op>(
                    new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(1)),
                    new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(2)),
                    new Biscuit.Datalog.Expressions.Op.Binary(Biscuit.Datalog.Expressions.Op.BinaryOp.Add),
                    new Biscuit.Datalog.Expressions.Op.Unary(Biscuit.Datalog.Expressions.Op.UnaryOp.Parens),
                    new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(3)),
                    new Biscuit.Datalog.Expressions.Op.Binary(Biscuit.Datalog.Expressions.Op.BinaryOp.Mul)
                    ).SequenceEqual(
                    ex2.GetOps())
                );

            Dictionary <ulong, ID> variables2 = new Dictionary <ulong, ID>();
            Option <ID>            value2     = ex2.Evaluate(variables2);

            Assert.AreEqual(Option.Some(new ID.Integer(9)), value2);
            Assert.AreEqual("(1 + 2) * 3", ex2.Print(s2).Get());
        }
Example #2
0
        public void TestExpression()
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res =
                Parser.Expression(" -1 ");

            Assert.AreEqual(
                new ExpressionBuilder.Value(Utils.Integer(-1)),
                res.Right.Item2);

            Either <Error, Tuple <string, ExpressionBuilder> > res2 =
                Parser.Expression(" $0 <= 2019-12-04T09:46:41+00:00");

            Assert.AreEqual(
                new ExpressionBuilder.Binary(
                    ExpressionBuilder.Op.LessOrEqual,
                    new ExpressionBuilder.Value(Utils.Var("0")),
                    new ExpressionBuilder.Value(new Term.Date(1575452801))),
                res2.Right.Item2);

            Either <Error, Tuple <string, ExpressionBuilder> > res3 =
                Parser.Expression(" 1 < $test + 2 ");

            Assert.AreEqual(
                new ExpressionBuilder.Binary(
                    ExpressionBuilder.Op.LessThan,
                    new ExpressionBuilder.Value(Utils.Integer(1)),
                    new ExpressionBuilder.Binary(
                        ExpressionBuilder.Op.Add,
                        new ExpressionBuilder.Value(Utils.Var("test")),
                        new ExpressionBuilder.Value(Utils.Integer(2))
                        )
                    ),
                res3.Right.Item2);

            SymbolTable s3       = new SymbolTable();
            ulong       test     = s3.Insert("test");
            var         expected = Arrays.AsList <Biscuit.Datalog.Expressions.Op>(
                new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(1)),
                new Biscuit.Datalog.Expressions.Op.Value(new ID.Variable(test)),
                new Biscuit.Datalog.Expressions.Op.Value(new ID.Integer(2)),
                new Biscuit.Datalog.Expressions.Op.Binary(Biscuit.Datalog.Expressions.Op.BinaryOp.Add),
                new Biscuit.Datalog.Expressions.Op.Binary(Biscuit.Datalog.Expressions.Op.BinaryOp.LessThan)
                );

            Assert.IsTrue(expected.SequenceEqual(
                              res3.Right.Item2.Convert(s3).GetOps())
                          );

            Either <Error, Tuple <string, ExpressionBuilder> > res4 =
                Parser.Expression("  2 < $test && $var2.starts_with(\"test\") && true ");

            Assert.IsTrue(res4.IsRight);
            Assert.AreEqual(new ExpressionBuilder.Binary(
                                ExpressionBuilder.Op.And,
                                new ExpressionBuilder.Binary(
                                    ExpressionBuilder.Op.And,
                                    new ExpressionBuilder.Binary(
                                        ExpressionBuilder.Op.LessThan,
                                        new ExpressionBuilder.Value(Utils.Integer(2)),
                                        new ExpressionBuilder.Value(Utils.Var("test"))
                                        ),
                                    new ExpressionBuilder.Binary(
                                        ExpressionBuilder.Op.Prefix,
                                        new ExpressionBuilder.Value(Utils.Var("var2")),
                                        new ExpressionBuilder.Value(Utils.Strings("test"))
                                        )
                                    ),
                                new ExpressionBuilder.Value(new Term.Bool(true))
                                ),
                            res4.Right.Item2);

            Either <Error, Tuple <string, ExpressionBuilder> > res5 =
                Parser.Expression("  [ #abc, #def ].contains($operation) ");

            HashSet <Term> s = new HashSet <Term>
            {
                Utils.Symbol("abc"),
                Utils.Symbol("def")
            };

            Assert.IsTrue(res5.IsRight);
            Assert.AreEqual(new Tuple <string, ExpressionBuilder>("",
                                                                  new ExpressionBuilder.Binary(
                                                                      ExpressionBuilder.Op.Contains,
                                                                      new ExpressionBuilder.Value(Utils.Set(s)),
                                                                      new ExpressionBuilder.Value(Utils.Var("operation"))
                                                                      )
                                                                  ),
                            res5.Right);
        }