Exemplo n.º 1
0
 private Function MakeFunction(string text)
 {
     Context context = new Context();
     Parser parser = new Parser(text);
     var form = parser.ParseForm();
     return (Function)form.Evaluate(context);
 }
Exemplo n.º 2
0
        public void RaiseIfNoPointAtEnd()
        {
            Parser parser = new Parser("-export([foo/3, bar/2]);");

            try
            {
                parser.ParseForm();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.AreEqual("Unexpected ';'", ex.Message);
            }
        }
Exemplo n.º 3
0
        public void RaiseIfNoArity()
        {
            Parser parser = new Parser("-export([foo/bar, bar/2]).");

            try
            {
                parser.ParseForm();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.AreEqual("Expected integer", ex.Message);
            }
        }
Exemplo n.º 4
0
        public void ParseSimpleModuleForm()
        {
            Parser parser = new Parser("-module(mymodule).");

            var result = parser.ParseForm();

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

            var mform = (ModuleForm)result;

            Assert.AreEqual("mymodule", mform.Name);
        }
Exemplo n.º 5
0
        public void ParseSimpleFunctionFormWithVariableParameter()
        {
            Parser parser = new Parser("inc(X) -> X+1.");

            var result = parser.ParseForm();

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

            var fdef = (FunctionForm)result;

            Assert.AreEqual("inc", fdef.Name);
            Assert.IsNotNull(fdef.ParameterExpressions);
            Assert.AreEqual(1, fdef.ParameterExpressions.Count);
            Assert.IsInstanceOfType(fdef.ParameterExpressions[0], typeof(VariableExpression));
            Assert.AreEqual("X", ((VariableExpression)fdef.ParameterExpressions[0]).Variable.Name);
            Assert.IsNotNull(fdef.Body);
            Assert.IsInstanceOfType(fdef.Body, typeof(AddExpression));
        }
Exemplo n.º 6
0
        public void ParseSimpleFunctionForm()
        {
            Parser parser = new Parser("one() -> 1.");

            var result = parser.ParseForm();

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

            var fdef = (FunctionForm)result;

            Assert.AreEqual("one", fdef.Name);
            Assert.IsNotNull(fdef.ParameterExpressions);
            Assert.AreEqual(0, fdef.ParameterExpressions.Count);
            Assert.IsNotNull(fdef.Body);
            Assert.IsInstanceOfType(fdef.Body, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)fdef.Body).Value);
        }
Exemplo n.º 7
0
 private object EvaluateForm(string text)
 {
     Parser parser = new Parser(text);
     IForm form = parser.ParseForm();
     return form.Evaluate(this.context);
 }
Exemplo n.º 8
0
        public void RaiseIfUnkownForm()
        {
            Parser parser = new Parser("-unknown().");

            try
            {
                parser.ParseForm();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Unknown form", ex.Message);
            }
        }
Exemplo n.º 9
0
        public void ParseFunctionWithDelayedCall()
        {
            Parser parser = new Parser("f(X,Y) -> f(X-1, Y+1).");

            var form = parser.ParseForm();

            Assert.IsNotNull(form);
            Assert.IsInstanceOfType(form, typeof(FunctionForm));

            var fform = (FunctionForm)form;

            Assert.IsInstanceOfType(fform.Body, typeof(DelayedCallExpression));
        }
Exemplo n.º 10
0
        public void ParseFunctionWithCompositeBodyAndDelayedCall()
        {
            Parser parser = new Parser("f(X,Y) -> Z=X-1, W=Y+1, f(Z, W).");

            var form = parser.ParseForm();

            Assert.IsNotNull(form);
            Assert.IsInstanceOfType(form, typeof(FunctionForm));

            var fform = (FunctionForm)form;

            Assert.IsInstanceOfType(fform.Body, typeof(CompositeExpression));

            var cexpr = (CompositeExpression)fform.Body;

            Assert.AreEqual(3, cexpr.Expressions.Count);
            Assert.IsInstanceOfType(cexpr.Expressions[2], typeof(DelayedCallExpression));
        }
Exemplo n.º 11
0
        public void ParseFunctionFormWithArgumentsAndExpressionBody()
        {
            Parser parser = new Parser("add(X,Y) -> X+Y.");

            var result = parser.ParseForm();

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

            var fdef = (FunctionForm)result;

            Assert.AreEqual("add", fdef.Name);
            Assert.IsNotNull(fdef.ParameterExpressions);

            Assert.AreEqual(2, fdef.ParameterExpressions.Count);
            Assert.IsInstanceOfType(fdef.ParameterExpressions[0], typeof(VariableExpression));
            Assert.IsInstanceOfType(fdef.ParameterExpressions[1], typeof(VariableExpression));
            Assert.AreEqual("X", ((VariableExpression)fdef.ParameterExpressions[0]).Variable.Name);
            Assert.AreEqual("Y", ((VariableExpression)fdef.ParameterExpressions[1]).Variable.Name);

            Assert.IsNotNull(fdef.Body);
            Assert.IsInstanceOfType(fdef.Body, typeof(AddExpression));

            var addexpr = (AddExpression)fdef.Body;
            Assert.IsInstanceOfType(addexpr.LeftExpression, typeof(VariableExpression));
            Assert.IsInstanceOfType(addexpr.RightExpression, typeof(VariableExpression));
            Assert.AreEqual("X", ((VariableExpression)addexpr.LeftExpression).Variable.Name);
            Assert.AreEqual("Y", ((VariableExpression)addexpr.RightExpression).Variable.Name);
        }
Exemplo n.º 12
0
        public void ParseEmptyStringAsNullForm()
        {
            Parser parser = new Parser(string.Empty);

            Assert.IsNull(parser.ParseForm());
        }
Exemplo n.º 13
0
        public Module LoadModule(string modname)
        {
            Module module = new Module(this.rootcontext);
            StreamReader reader = File.OpenText(modname + ".erl");
            Parser parser = new Parser(reader);

            for (var form = parser.ParseForm(); form != null; form = parser.ParseForm())
                form.Evaluate(module.Context);

            reader.Close();

            return module;
        }
Exemplo n.º 14
0
        public void RaiseIfParseUnclosedMultiFunctionForm()
        {
            Parser parser = new Parser("f(0) -> 1;");

            try
            {
                var result = parser.ParseForm();
                Assert.Fail();
            }
            catch (ParserException ex)
            {
                Assert.AreEqual("expected atom", ex.Message);
            }
        }
Exemplo n.º 15
0
        public void ParseMultiFunctionForm()
        {
            Parser parser = new Parser("f(0) -> 1; f(1) -> 2; f(X) -> f(X-1) + f(X-2).");

            var result = parser.ParseForm();

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

            var fdef = (MultiFunctionForm)result;

            Assert.IsNotNull(fdef.Forms);
            Assert.AreEqual(3, fdef.Forms.Count);
        }
Exemplo n.º 16
0
        public void RaiseIfUnclosedModuleForm()
        {
            Parser parser = new Parser("-module(");

            try
            {
                parser.ParseForm();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Expected atom", ex.Message);
            }
        }
Exemplo n.º 17
0
        public void ParseSimpleExportForm()
        {
            Parser parser = new Parser("-export([foo/1, bar/2]).");

            var result = parser.ParseForm();

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

            var eform = (ExportForm)result;

            Assert.AreEqual(2, eform.Names.Count);
            Assert.IsTrue(eform.Names.Contains("foo/1"));
            Assert.IsTrue(eform.Names.Contains("bar/2"));
        }
Exemplo n.º 18
0
        public void RaiseWhenUnexpectedIntegerParsingForm()
        {
            Parser parser = new Parser("123");

            try
            {
                parser.ParseForm();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("unexpected '123'", ex.Message);
            }
        }
Exemplo n.º 19
0
 private FunctionForm MakeForm(string text)
 {
     Parser parser = new Parser(text);
     return (FunctionForm)parser.ParseForm();
 }