Inheritance: IExpression
コード例 #1
0
ファイル: ListExpressionTests.cs プロジェクト: ajlopez/AjErl
        public void CreateListWithVariableAsTail()
        {
            Context context = new Context();
            var expr = new ListExpression(new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) }, new VariableExpression(new Variable("Tail")));

            var result = expr.Evaluate(context, true);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(List));
            Assert.AreEqual("[1,2|Tail]", result.ToString());
        }
コード例 #2
0
ファイル: ListExpressionTests.cs プロジェクト: ajlopez/AjErl
        public void CreateListWithTail()
        {
            Context context = new Context();
            var list = List.MakeList(new object[] { 3, 4 });
            context.SetValue("Tail", list);
            var expr = new ListExpression(new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) }, new VariableExpression(new Variable("Tail")));

            var result = expr.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(List));
            Assert.AreEqual("[1,2,3,4]", result.ToString());
        }
コード例 #3
0
ファイル: ListExpressionTests.cs プロジェクト: ajlopez/AjErl
        public void CreateSimpleList()
        {
            Context context = new Context();
            var expr = new ListExpression(new IExpression[] { new ConstantExpression(1), new AtomExpression(new Atom("x")), new AtomExpression(new Atom("y")) });

            Assert.IsFalse(expr.HasVariable());

            var result = expr.Evaluate(context, true);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(List));

            var list = (List)result;

            Assert.IsNotNull(list.Head);
            Assert.IsNotNull(list.Tail);

            Assert.AreEqual("[1,x,y]", list.ToString());
        }
コード例 #4
0
ファイル: Parser.cs プロジェクト: ajlopez/AjErl
        private IExpression ParseTerm()
        {
            Token token = this.NextToken();
            IExpression expression = null;

            if (token == null)
                return null;

            if (token.Type == TokenType.Variable)
            {
                expression = new VariableExpression(new Variable(token.Value));

                if (this.TryParseToken(TokenType.Separator, "("))
                {
                    var list = this.ParseExpressionList();
                    this.ParseToken(TokenType.Separator, ")");
                    expression = new CallExpression(expression, list);
                }
            }
            else if (token.Type == TokenType.Atom)
            {
                if (token.Value == "false")
                    return new ConstantExpression(false);
                if (token.Value == "true")
                    return new ConstantExpression(true);

                if (token.Value == "fun")
                    return this.ParseFunExpression();

                if (token.Value == "receive")
                    return this.ParseReceiveExpression();

                expression = new AtomExpression(new Atom(token.Value));

                if (this.TryParseToken(TokenType.Separator, "("))
                {
                    var list = this.ParseExpressionList();
                    this.ParseToken(TokenType.Separator, ")");
                    expression = new CallExpression(expression, list);
                }
                else if (this.TryParseToken(TokenType.Separator, ":"))
                {
                    var nexpression = new AtomExpression(new Atom(this.ParseAtom()));
                    this.ParseToken(TokenType.Separator, "(");
                    var list = this.ParseExpressionList();
                    this.ParseToken(TokenType.Separator, ")");
                    expression = new QualifiedCallExpression(expression, nexpression, list);
                }
            }
            else if (token.Type == TokenType.Integer)
                expression = new ConstantExpression(int.Parse(token.Value, CultureInfo.InvariantCulture));
            else if (token.Type == TokenType.Real)
                expression = new ConstantExpression(double.Parse(token.Value, CultureInfo.InvariantCulture));
            else if (token.Type == TokenType.String)
                expression = new ConstantExpression(token.Value);
            else if (token.Type == TokenType.Separator && token.Value == "(")
            {
                expression = this.ParseSimpleExpression();
                this.ParseToken(TokenType.Separator, ")");
            }
            else if (token.Type == TokenType.Separator && token.Value == "{")
            {
                var expressions = this.ParseExpressionList();
                this.ParseToken(TokenType.Separator, "}");
                expression = new TupleExpression(expressions);
            }
            else if (token.Type == TokenType.Separator && token.Value == "[")
            {
                var expressions = this.ParseExpressionList();
                IExpression tailexpression = null;

                if (this.TryParseToken(TokenType.Separator, "|"))
                    tailexpression = this.ParseSimpleExpression();

                this.ParseToken(TokenType.Separator, "]");
                expression = new ListExpression(expressions, tailexpression);
            }
            else
                this.PushToken(token);

            return expression;
        }
コード例 #5
0
ファイル: ListExpressionTests.cs プロジェクト: ajlopez/AjErl
        public void RaiseWhenUnboundVariable()
        {
            Context context = new Context();
            var expr = new ListExpression(new IExpression[] { new ConstantExpression(1), new VariableExpression(new Variable("X")), new AtomExpression(new Atom("y")) });

            Assert.IsTrue(expr.HasVariable());

            try
            {
                expr.Evaluate(context, false);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("variable 'X' is unbound", ex.Message);
            }
        }
コード例 #6
0
ファイル: ListExpressionTests.cs プロジェクト: ajlopez/AjErl
        public void RaiseIfUnboundVariableAsTail()
        {
            Context context = new Context();
            var expr = new ListExpression(new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) }, new VariableExpression(new Variable("Tail")));

            try
            {
                expr.Evaluate(context, false);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("variable 'Tail' is unbound", ex.Message);
            }
        }