private Function MakeFunction(string text) { Context context = new Context(); Parser parser = new Parser(text); var form = parser.ParseForm(); return (Function)form.Evaluate(context); }
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); } }
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); } }
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); } }
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); } }
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); } }
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); }
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)); }
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")); }
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); }
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); }
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)); }
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)); }
private FunctionForm MakeForm(string text) { Parser parser = new Parser(text); return (FunctionForm)parser.ParseForm(); }
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); }
private object EvaluateForm(string text) { Parser parser = new Parser(text); IForm form = parser.ParseForm(); return form.Evaluate(this.context); }
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); } }
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 ParseEmptyStringAsNullForm() { Parser parser = new Parser(string.Empty); Assert.IsNull(parser.ParseForm()); }