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 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 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); }
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 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); } }
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 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 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); }
private object EvaluateForm(string text) { Parser parser = new Parser(text); IForm form = parser.ParseForm(); return form.Evaluate(this.context); }
private FunctionForm MakeForm(string text) { Parser parser = new Parser(text); return (FunctionForm)parser.ParseForm(); }
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 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 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 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 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 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 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 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 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 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 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 RaiseIfNoPointAtEnd() { Parser parser = new Parser("-export([foo/3, bar/2]);"); try { parser.ParseForm(); Assert.Fail(); } catch (System.Exception ex) { Assert.AreEqual("Unexpected ';'", ex.Message); } }
private object EvaluateExpression(string text) { Parser parser = new Parser(text); IExpression expression = parser.ParseExpression(); return expression.Evaluate(this.context); }
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); } }
private FunExpression MakeExpression(string text) { Context context = new Context(); Parser parser = new Parser(text); return (FunExpression)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 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 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 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 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()); }