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()); }
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 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()); }
public void ParseSimpleDivide() { Parser parser = new Parser("10/20."); IExpression expression = parser.ParseExpression(); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(DivideExpression)); DivideExpression divideexpression = (DivideExpression)expression; Assert.IsInstanceOfType(divideexpression.LeftExpression, typeof(ConstantExpression)); Assert.IsInstanceOfType(divideexpression.RightExpression, typeof(ConstantExpression)); Assert.AreEqual(10, ((ConstantExpression)divideexpression.LeftExpression).Value); Assert.AreEqual(20, ((ConstantExpression)divideexpression.RightExpression).Value); }
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()); }
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 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); } }
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 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 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); }
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 void ParseSimpleMatch() { Parser parser = new Parser("X=ok."); IExpression expression = parser.ParseExpression(); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(MatchExpression)); MatchExpression matchexpression = (MatchExpression)expression; Assert.IsNotNull(matchexpression.LeftExpression); Assert.IsNotNull(matchexpression.RightExpression); Assert.IsNull(parser.ParseExpression()); }
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); }
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 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 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 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()); }
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()); }
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()); }
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); }
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 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); }
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()); }
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()); }
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 ParseSendExpression() { Parser parser = new Parser("X ! 1."); var expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.IsInstanceOfType(expr, typeof(SendExpression)); var sexpr = (SendExpression)expr; Assert.IsInstanceOfType(sexpr.ProcessExpression, typeof(VariableExpression)); Assert.AreEqual("X", ((VariableExpression)sexpr.ProcessExpression).Variable.Name); Assert.IsInstanceOfType(sexpr.MessageExpression, typeof(ConstantExpression)); Assert.AreEqual(1, ((ConstantExpression)sexpr.MessageExpression).Value); Assert.IsNull(parser.ParseExpression()); }
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); } }
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()); }
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); } }
public void ParseSimpleAdd() { Parser parser = new Parser("10+20."); IExpression expression = parser.ParseExpression(); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(AddExpression)); AddExpression addexpression = (AddExpression)expression; Assert.IsInstanceOfType(addexpression.LeftExpression, typeof(ConstantExpression)); Assert.IsInstanceOfType(addexpression.RightExpression, typeof(ConstantExpression)); Assert.AreEqual(10, ((ConstantExpression)addexpression.LeftExpression).Value); Assert.AreEqual(20, ((ConstantExpression)addexpression.RightExpression).Value); }