コード例 #1
0
        public void RaiseIfNoFunctionExported()
        {
            Context context = new Context();
            Module  module  = new Module(null);

            context.SetValue("mod", module);
            Function function = new Function(context, new object[] { new Variable("X"), new Variable("Y") }, new AddExpression(new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y"))));

            module.Context.SetValue("add/2", function);
            QualifiedCallExpression expr = new QualifiedCallExpression(new AtomExpression(new Atom("mod")), new AtomExpression(new Atom("add")), new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) });

            try
            {
                expr.Evaluate(context);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("undefined function mod:add/2", ex.Message);
            }
        }
コード例 #2
0
        public void EvaluateQualifiedCallExpression()
        {
            Context context = new Context();
            Module  module  = new Module(null);

            context.SetValue("mod", module);
            Function function = new Function(context, new object[] { new Variable("X"), new Variable("Y") }, new AddExpression(new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y"))));

            module.Context.SetValue("add/2", function);
            module.AddExportNames(new string[] { "add/2" });
            QualifiedCallExpression expr = new QualifiedCallExpression(new AtomExpression(new Atom("mod")), new AtomExpression(new Atom("add")), new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) });

            Assert.IsFalse(expr.HasVariable());
            Assert.IsNotNull(expr.ModuleExpression);
            Assert.IsInstanceOfType(expr.ModuleExpression, typeof(AtomExpression));
            Assert.IsNotNull(expr.NameExpression);
            Assert.IsInstanceOfType(expr.NameExpression, typeof(AtomExpression));
            Assert.IsNotNull(expr.ArgumentExpressions);
            Assert.AreEqual(2, expr.ArgumentExpressions.Count);

            Assert.AreEqual(3, expr.Evaluate(context));
        }
コード例 #3
0
ファイル: Parser.cs プロジェクト: vis2k/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);
        }