Пример #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);
 }
Пример #2
0
        public void ParseAddMultiplyWithParens()
        {
            Parser parser = new Parser("(2+3)*4.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(MultiplyExpression));

            MultiplyExpression multexpression = (MultiplyExpression)expression;

            Assert.IsInstanceOfType(multexpression.RightExpression, typeof(ConstantExpression));
            Assert.AreEqual(4, ((ConstantExpression)multexpression.RightExpression).Value);

            Assert.IsInstanceOfType(multexpression.LeftExpression, typeof(AddExpression));

            AddExpression addexpression = (AddExpression)multexpression.LeftExpression;

            Assert.IsInstanceOfType(addexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(addexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(2, ((ConstantExpression)addexpression.LeftExpression).Value);
            Assert.AreEqual(3, ((ConstantExpression)addexpression.RightExpression).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #3
0
        private static void ProcessExpression(Parser parser, Context context)
        {
            IExpression expression = parser.ParseExpression();
            object result = Machine.ExpandDelayedCall(expression.Evaluate(context));

            if (result == null)
                return;

            Console.Write("> ");
            Console.WriteLine(result);
        }
Пример #4
0
        public void ParseAtom()
        {
            Parser parser = new Parser("ok.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(AtomExpression));

            AtomExpression atomexpression = (AtomExpression)expression;
            Assert.AreEqual("ok", atomexpression.Atom.Name);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #5
0
        public static void Main(string[] args)
        {
            Console.WriteLine("AjErl alfa 0.0.1");

            Lexer lexer = new Lexer(Console.In);
            Parser parser = new Parser(lexer);
            Machine machine = new Machine();

            while (true)
                try
                {
                    ProcessExpression(parser, machine.RootContext);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    Console.Error.WriteLine(ex.StackTrace);
                }
        }
Пример #6
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);
            }
        }
Пример #7
0
        public void ParseSimpleRem()
        {
            Parser parser = new Parser("10 rem 20.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(RemExpression));

            RemExpression remexpression = (RemExpression)expression;

            Assert.IsInstanceOfType(remexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(remexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)remexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)remexpression.RightExpression).Value);
        }
Пример #8
0
        public void ParseSimpleMultiply()
        {
            Parser parser = new Parser("10*20.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(MultiplyExpression));

            MultiplyExpression multiplyexpression = (MultiplyExpression)expression;

            Assert.IsInstanceOfType(multiplyexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(multiplyexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)multiplyexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)multiplyexpression.RightExpression).Value);
        }
Пример #9
0
 private object EvaluateForm(string text)
 {
     Parser parser = new Parser(text);
     IForm form = parser.ParseForm();
     return form.Evaluate(this.context);
 }
Пример #10
0
 private FunctionForm MakeForm(string text)
 {
     Parser parser = new Parser(text);
     return (FunctionForm)parser.ParseForm();
 }
Пример #11
0
        public void ThrowIfNoPoint()
        {
            Parser parser = new Parser("1");

            try
            {
                parser.ParseExpression();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Expected '.'", ex.Message);
            }
        }
Пример #12
0
        public void ParseUnaryTuple()
        {
            Parser parser = new Parser("{1}.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(TupleExpression));

            TupleExpression tupleexpression = (TupleExpression)expression;
            Assert.IsNotNull(tupleexpression.Expressions);
            Assert.AreEqual(1, tupleexpression.Expressions.Count);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #13
0
        public void ParseTuple()
        {
            Parser parser = new Parser("{1,2,3}.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(TupleExpression));

            TupleExpression tupleexpression = (TupleExpression)expression;
            Assert.IsNotNull(tupleexpression.Expressions);
            Assert.AreEqual(3, tupleexpression.Expressions.Count);

            foreach (var expr in tupleexpression.Expressions)
                Assert.IsInstanceOfType(expr, typeof(ConstantExpression));

            Assert.IsNull(parser.ParseExpression());
        }
Пример #14
0
        public void ParseString()
        {
            Parser parser = new Parser("\"foo\".");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            ConstantExpression consexpression = (ConstantExpression)expression;
            Assert.IsNotNull(consexpression.Value);
            Assert.AreEqual("foo", consexpression.Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #15
0
        public void ParseStrictEqualExpression()
        {
            Parser parser = new Parser("1 =:= 0.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(StrictEqualExpression));

            var seqexpr = (StrictEqualExpression)expr;

            Assert.IsInstanceOfType(seqexpr.LeftExpression, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)seqexpr.LeftExpression).Value);
            Assert.IsInstanceOfType(seqexpr.RightExpression, typeof(ConstantExpression));
            Assert.AreEqual(0, ((ConstantExpression)seqexpr.RightExpression).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #16
0
        public void ParseSimpleSubtract()
        {
            Parser parser = new Parser("10-20.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(SubtractExpression));

            SubtractExpression subtractexpression = (SubtractExpression)expression;

            Assert.IsInstanceOfType(subtractexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(subtractexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)subtractexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)subtractexpression.RightExpression).Value);
        }
Пример #17
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);
            }
        }
Пример #18
0
        public void ParseVariable()
        {
            Parser parser = new Parser("X.");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(VariableExpression));

            VariableExpression varexpression = (VariableExpression)expression;
            Assert.AreEqual("X", varexpression.Variable.Name);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #19
0
        public void ThrowIfListIsNotClosed()
        {
            Parser parser = new Parser("[1,2");

            try
            {
                parser.ParseExpression();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Expected ']'", ex.Message);
            }
        }
Пример #20
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);
            }
        }
Пример #21
0
        public void ThrowIfTupleHasUnexpectedOperator()
        {
            Parser parser = new Parser("{1,2,=");

            try
            {
                parser.ParseExpression();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Unexpected '='", ex.Message);
            }
        }
Пример #22
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);
            }
        }
Пример #23
0
 private object EvaluateExpression(string text)
 {
     Parser parser = new Parser(text);
     IExpression expression = parser.ParseExpression();
     return expression.Evaluate(this.context);
 }
Пример #24
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);
            }
        }
Пример #25
0
 private FunExpression MakeExpression(string text)
 {
     Context context = new Context();
     Parser parser = new Parser(text);
     return (FunExpression)parser.ParseExpression();
 }
Пример #26
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);
        }
Пример #27
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;
        }
Пример #28
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);
            }
        }
Пример #29
0
        public void ParseCompositeExpressionWithTwoConstants()
        {
            Parser parser = new Parser("1,2.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(CompositeExpression));

            var cexpr = (CompositeExpression)expr;

            Assert.IsNotNull(cexpr.Expressions);
            Assert.AreEqual(2, cexpr.Expressions.Count);
            Assert.IsInstanceOfType(cexpr.Expressions[0], typeof(ConstantExpression));
            Assert.IsInstanceOfType(cexpr.Expressions[1], typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)cexpr.Expressions[0]).Value);
            Assert.AreEqual(2, ((ConstantExpression)cexpr.Expressions[1]).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #30
0
        public void ParseSimpleQualifiedCallExpression()
        {
            Parser parser = new Parser("mod:add(1, 2).");

            var result = parser.ParseExpression();

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

            var expr = (QualifiedCallExpression)result;

            Assert.IsInstanceOfType(expr.ModuleExpression, typeof(AtomExpression));
            Assert.AreEqual(((AtomExpression)expr.ModuleExpression).Atom.Name, "mod");

            Assert.IsInstanceOfType(expr.NameExpression, typeof(AtomExpression));
            Assert.AreEqual(((AtomExpression)expr.NameExpression).Atom.Name, "add");

            Assert.IsNotNull(expr.ArgumentExpressions);
            Assert.AreEqual(2, expr.ArgumentExpressions.Count);
            Assert.IsInstanceOfType(expr.ArgumentExpressions[0], typeof(ConstantExpression));
            Assert.IsInstanceOfType(expr.ArgumentExpressions[1], typeof(ConstantExpression));

            var cexpr = (ConstantExpression)expr.ArgumentExpressions[0];
            Assert.AreEqual(1, cexpr.Value);

            cexpr = (ConstantExpression)expr.ArgumentExpressions[1];
            Assert.AreEqual(2, cexpr.Value);

            Assert.IsNull(parser.ParseExpression());
        }