Example #1
0
        public void EqualExpressions()
        {
            var expr1 = new IntegerExpression(1);
            var expr2 = new IntegerExpression(2);
            var expr3 = new IntegerExpression(1);
            var expr4 = new DoubleExpression(3.14159);
            var expr5 = new DoubleExpression(1.2);
            var expr6 = new DoubleExpression(3.14159);

            Assert.IsFalse(expr1.Equals(expr2));
            Assert.IsFalse(expr1.Equals(null));
            Assert.IsFalse(expr2.Equals(expr1));
            Assert.IsFalse(expr1.Equals(expr4));
            Assert.IsFalse(expr1.Equals(expr5));
            Assert.IsFalse(expr4.Equals(expr1));
            Assert.IsFalse(expr5.Equals(expr1));
            Assert.IsFalse(expr4.Equals(expr5));
            Assert.IsFalse(expr5.Equals(expr4));

            Assert.IsTrue(expr1.Equals(expr3));
            Assert.IsTrue(expr3.Equals(expr1));
            Assert.IsTrue(expr4.Equals(expr6));
            Assert.IsTrue(expr6.Equals(expr4));

            Assert.AreEqual(expr1.GetHashCode(), expr3.GetHashCode());
            Assert.AreEqual(expr4.GetHashCode(), expr6.GetHashCode());
        }
Example #2
0
        public void GetIntegerConstant()
        {
            var expr = new IntegerExpression(42);

            Assert.AreEqual(42, expr.Value);
            Assert.AreSame(IntegerType.Instance, expr.Type);
            Assert.AreSame(expr, expr.Reduce());
        }
Example #3
0
        public void MatchUnboundNameExpression()
        {
            var expr = new NameExpression("a");
            var ctx = new Context<IExpression>();
            var cexpr = new IntegerExpression(42);

            Assert.IsTrue(expr.Match(cexpr, ctx));

            Assert.AreSame(cexpr, ctx.GetValue("a"));
        }
        public void MatchVariableInFunctionalExpression()
        {
            var one = new IntegerExpression(1);
            var two = new IntegerExpression(2);
            var expr = new FunctionalExpression(new AddIntegersFunction(), new IExpression[] { new NameExpression("a"), new NameExpression("b") });
            var expr1 = new FunctionalExpression(new AddIntegersFunction(), new IExpression[] { one, two });

            Context<IExpression> ctx = new Context<IExpression>();

            Assert.IsTrue(expr.Match(expr1, ctx));

            Assert.AreSame(one, ctx.GetValue("a"));
            Assert.AreSame(two, ctx.GetValue("b"));
        }
Example #5
0
        public void MatchDoubleConstant()
        {
            var expr = new DoubleExpression(1.2);
            var expr1 = new DoubleExpression(1.2);
            var expr2 = new DoubleExpression(3.4);
            var expr3 = new IntegerExpression(42);

            Assert.IsTrue(expr.Match(expr, null));
            Assert.IsTrue(expr.Match(expr1, null));

            Assert.IsFalse(expr.Match(null, null));
            Assert.IsFalse(expr.Match(expr2, null));
            Assert.IsFalse(expr.Match(expr3, null));
        }
Example #6
0
        public void MatchStringConstant()
        {
            var expr = new StringExpression("foo");
            var expr1 = new StringExpression("foo");
            var expr2 = new StringExpression("bar");
            var expr3 = new IntegerExpression(42);

            Assert.IsTrue(expr.Match(expr, null));
            Assert.IsTrue(expr.Match(expr1, null));

            Assert.IsFalse(expr.Match(null, null));
            Assert.IsFalse(expr.Match(expr2, null));
            Assert.IsFalse(expr.Match(expr3, null));
        }
Example #7
0
 void PrintExpression(IntegerExpression e, int d)
 {
     Say("IntegerExpression(");
     Say(e.Value);
     Say(")");
 }
 public SimplifiedExpression Visit(IntegerExpression expression)
 {
     return(new SimplifiedExpression(expression));
 }
Example #9
0
 public TypeDenoter VisitIntegerExpression(IntegerExpression ast, Void arg)
 {
     ast.Type = StandardEnvironment.IntegerType;
     return(ast.Type);
 }
Example #10
0
 public virtual void Accept(IntegerExpression integer)
 {
 }
Example #11
0
 public void visit(IntegerExpression that)
 {
     that.Type = new IntegerType(that.Position);
 }
Example #12
0
        private Declaration[] CreateStandardEnvironment(bool insert = false)
        {
            List <Declaration> result = new List <Declaration>();

            // signal that the following symbols are part of the standard library
            Position    position = new Position("(library)", 0, 0);
            Declaration declaration;
            Expression  expression;

            // enter the predefined constant 'maxint' into the symbol table
            expression  = new IntegerExpression(position, int.MaxValue);
            declaration = new ConstantDeclaration(position, "maxint", new IntegerType(position), expression);
            result.Add(declaration);

            // enter the predefined constants 'false' and 'true' into the symbol table
            expression  = new BooleanExpression(position, false);
            declaration = new ConstantDeclaration(position, "false", new BooleanType(position), expression);
            result.Add(declaration);

            expression  = new BooleanExpression(position, true);
            declaration = new ConstantDeclaration(position, "true", new BooleanType(position), expression);
            result.Add(declaration);

            // enter the predefined operators into the symbol table
            // ... the \ operator
            declaration = new FunctionDeclaration(
                position,
                "\\",
                new BooleanType(position),
                new ParameterDeclaration[] { new ParameterDeclaration(position, "value", new BooleanType(position)) },
                null        // note: the code generator must handle these predefined functions so no body is defined
                );
            result.Add(declaration);

            // ... all Triangle operators of the form Boolean x Boolean -> Boolean
            string[] boolean_and_boolean_to_boolean_operators = { "/\\", "\\/", "=", "\\=" };
            foreach (string @operator in boolean_and_boolean_to_boolean_operators)
            {
                declaration = new FunctionDeclaration(
                    position,
                    @operator,
                    new BooleanType(position),
                    new ParameterDeclaration[]
                {
                    new ParameterDeclaration(position, "first", new BooleanType(position)),
                    new ParameterDeclaration(position, "other", new BooleanType(position))
                },
                    null    // note: the code generator must handle these predefined functions so no body is defined
                    );
                result.Add(declaration);
            }

            // ... all Triangle operators of the form Integer x Integer -> Integer
            string[] integer_and_integer_to_integer_operators = { "+", "-", "*", "/", "//" };
            foreach (string @operator in integer_and_integer_to_integer_operators)
            {
                declaration = new FunctionDeclaration(
                    position,
                    @operator,
                    new IntegerType(position),
                    new ParameterDeclaration[]
                {
                    new ParameterDeclaration(position, "first", new IntegerType(position)),
                    new ParameterDeclaration(position, "other", new IntegerType(position))
                },
                    null    // note: the code generator must handle these predefined functions so no body is defined
                    );
                result.Add(declaration);
            }

            // ... all Triangle operators of the form Integer x Integer -> Boolean
            string[] integer_and_integer_to_boolean_operators = { "<", "<=", ">", ">=", "=", "\\=" };
            foreach (string @operator in integer_and_integer_to_boolean_operators)
            {
                declaration = new FunctionDeclaration(
                    position,
                    @operator,
                    new BooleanType(position),
                    new ParameterDeclaration[]
                {
                    new ParameterDeclaration(position, "first", new IntegerType(position)),
                    new ParameterDeclaration(position, "other", new IntegerType(position))
                },
                    null    // note: the code generator must handle these predefined functions so no body is defined
                    );
                result.Add(declaration);
            }

            // enter the predefined functions (getint and putint) into the symbol table
            declaration = new FunctionDeclaration(
                position,
                "getint",
                new IntegerType(position),
#if false
                new ParameterDeclaration[] { new ParameterDeclaration(position, "value", new IntegerType(position)) },
#else
                new ParameterDeclaration[0],
#endif
                null        // note: the code generator must handle these predefined functions so no body is defined
                );
            result.Add(declaration);

            declaration = new FunctionDeclaration(
                position,
                "putint",
                new IntegerType(position),
                new ParameterDeclaration[] { new ParameterDeclaration(position, "value", new IntegerType(position)) },
                null        // note: the code generator must handle these predefined functions so no body is defined
                );
            result.Add(declaration);

            return(result.ToArray());
        }
Example #13
0
        private void MarkExpressionsUsed(List<Type> allExpressions, IntegerExpression expr)
        {
            var type = expr.GetType();
            allExpressions.Remove(type);

            var children = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                .Where(x => typeof(IntegerExpression).IsAssignableFrom(x.FieldType))
                .Select(x => (IntegerExpression)x.GetValue(expr))
                .Where(x => x != null)
                .ToList();

            foreach (var childExpr in children)
                MarkExpressionsUsed(allExpressions, childExpr);
        }
Example #14
0
        private void VerifyPartialResult(MpirRandom rnd, IntegerExpression expr, long expected)
        {
            rnd.Seed(123);

            using (var r = new HugeInt())
            {
                r.Value = expr;
                Assert.AreEqual(expected.ToString(), r.ToString());
            }
        }
Example #15
0
	private Expression DoEval(IntegerExpression lhs, IntegerExpression rhs)
	{
		long result;
		
		switch (Operator)
		{
			case "+":
				result = lhs.Value + rhs.Value;
				break;
			
			case "-":
				result = lhs.Value - rhs.Value;
				break;
			
			case "*":
				result = lhs.Value * rhs.Value;
				break;
			
			case "/":
				result = lhs.Value / rhs.Value;
				break;
			
			default:
				throw new InvalidOperationException("Bad binary operator: " + Operator);
		}
		
		return new IntegerExpression(result);
	}
 public void Visit(IntegerExpression expression)
 {
 }
 private bool _AreEqual(IntegerExpression a, IntegerExpression b)
 {
     return(a.Value == b.Value);
 }
Example #18
0
 private BoundExpression Bind(IntegerExpression integerExpression)
 {
     return(new BoundIntegerExpression(integerExpression.Value));
 }