Example #1
0
        public void CreateModuleWithoutParentContext()
        {
            Module module = new Module(null);

            Assert.IsNotNull(module.Context);
            Assert.IsNull(module.Context.Parent);
            Assert.AreSame(module, module.Context.Module);
        }
Example #2
0
        public void CreateModuleWithParentContext()
        {
            Context parent = new Context();
            Module module = new Module(parent);

            Assert.IsNotNull(module.Context);
            Assert.IsNotNull(module.Context.Parent);
            Assert.AreSame(parent, module.Context.Parent);
        }
Example #3
0
        public void EvaluateModuleForm()
        {
            ModuleForm form = new ModuleForm("mymodule");

            Assert.AreEqual("mymodule", form.Name);

            Module module = new Module(null);

            form.Evaluate(module.Context);

            Assert.AreEqual("mymodule", form.Name);
        }
Example #4
0
        public void EvaluateExportForm()
        {
            ExportForm form = new ExportForm(new string[] { "foo/1", "bar/2" });

            Assert.AreEqual(2, form.Names.Count);
            Assert.IsTrue(form.Names.Contains("foo/1"));
            Assert.IsTrue(form.Names.Contains("bar/2"));

            Module module = new Module(null);

            form.Evaluate(module.Context);

            Assert.AreEqual(2, module.ExportNames.Count);
            Assert.IsTrue(module.ExportNames.Contains("foo/1"));
            Assert.IsTrue(module.ExportNames.Contains("bar/2"));
        }
Example #5
0
        public void AddExportNames()
        {
            Module module = new Module(null);

            Assert.IsNotNull(module.ExportNames);
            Assert.AreEqual(0, module.ExportNames.Count);

            module.AddExportNames(new string[] { "foo/1", "foo/2" });

            Assert.AreEqual(2, module.ExportNames.Count);
            Assert.IsTrue(module.ExportNames.Contains("foo/1"));
            Assert.IsTrue(module.ExportNames.Contains("foo/2"));

            module.AddExportNames(new string[] { "foo/2", "bar/1" });

            Assert.AreEqual(3, module.ExportNames.Count);
            Assert.IsTrue(module.ExportNames.Contains("foo/1"));
            Assert.IsTrue(module.ExportNames.Contains("foo/2"));
            Assert.IsTrue(module.ExportNames.Contains("bar/1"));
        }
        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));
        }
Example #7
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;
        }
        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);
            }
        }
Example #9
0
 public Context(Context parent = null, Module module = null)
 {
     this.parent = parent;
     this.module = module;
 }