public void DefAndTestListOpsFromExample() { Parser parser = new Parser(File.OpenText("DefListOpsWithTests.ajshp")); Machine machine = new Machine(); object result = parser.ParseForm(); int ntest = 0; while (result != null) { object value = machine.Evaluate(result); Assert.IsNotNull(value); if (value is bool) { ntest++; Assert.IsTrue((bool)value, string.Format("Test {0} failed", ntest)); } else Assert.IsInstanceOfType(value, typeof(DefinedFunction)); result = parser.ParseForm(); } }
public void ParseACharacter() { Parser parser = new Parser("\\a"); object result = parser.ParseForm(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(char)); Assert.AreEqual('a', result); Assert.IsNull(parser.ParseForm()); }
public void AddNumbers() { Parser parser = new Parser("(+) (+ 1) (+ 1 2) (+ 1 2 3)"); Machine machine = new Machine(); Assert.AreEqual(0, machine.Evaluate(parser.ParseForm())); Assert.AreEqual(1, machine.Evaluate(parser.ParseForm())); Assert.AreEqual(3, machine.Evaluate(parser.ParseForm())); Assert.AreEqual(6, machine.Evaluate(parser.ParseForm())); Assert.IsNull(parser.ParseForm()); }
public static void Main(string[] args) { Machine machine = new Machine(); Parser parser = new Parser(System.Console.In); Console.WriteLine("AjSharpure 0.0.1"); Console.WriteLine("Clojure-like interpreter written in C#"); while (true) { object value = machine.Evaluate(parser.ParseForm()); Console.WriteLine(Utilities.PrintString(value)); } }
public void DefineAMultiFunction() { FnStarPrimitive fnprim = new FnStarPrimitive(); Machine machine = new Machine(); Parser parser = new Parser("([x] (+ x 1)) ([x y] (+ x y 1))"); object[] parameters = new object[2]; parameters[0] = parser.ParseForm(); parameters[1] = parser.ParseForm(); object result = fnprim.Apply(machine, machine.Environment, parameters); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedMultiFunction)); }
public void ShouldExpandSimpleArray() { Parser parser = new Parser("[1 2 3]"); object array = parser.ParseForm(); object result = MacroUtilities.Expand(array, null, null); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IPersistentVector)); IPersistentVector resultVector = (IPersistentVector)result; Assert.AreEqual(3, resultVector.Count); Assert.AreEqual(1, resultVector[0]); Assert.AreEqual(2, resultVector[1]); Assert.AreEqual(3, resultVector[2]); }
public void ShouldExpandSimpleList() { Parser parser = new Parser("(1 2 3)"); object array = parser.ParseForm(); object result = MacroUtilities.Expand(array, null, null); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); IList resultList = (IList)result; Assert.AreEqual(3, resultList.Count); Assert.AreEqual(1, resultList[0]); Assert.AreEqual(2, resultList[1]); Assert.AreEqual(3, resultList[2]); }
public void DefineAndEvaluateInstancePredicate() { Parser parser = new Parser("(def instance? (fn* [type obj] (. type IsInstanceOfType obj))) (instance? System.String \"foo\")"); Machine machine = new Machine(); object value = machine.Evaluate(parser.ParseForm()); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(DefinedFunction)); object result = machine.Evaluate(parser.ParseForm()); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(bool)); Assert.IsTrue((bool)result); Assert.IsNull(parser.ParseForm()); }
public void ParseArray() { Parser parser = new Parser("[1 2 3]"); object obj = parser.ParseForm(); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IPersistentVector)); IPersistentVector vector = (IPersistentVector)obj; Assert.AreEqual(3, vector.Count); Assert.AreEqual(1, vector[0]); Assert.AreEqual(2, vector[1]); Assert.AreEqual(3, vector[2]); Assert.IsNull(parser.ParseForm()); }
public void ParseBooleans() { Parser parser = new Parser("true false"); object value = parser.ParseForm(); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(bool)); Assert.IsTrue((bool)value); value = parser.ParseForm(); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(bool)); Assert.IsFalse((bool)value); Assert.IsNull(parser.ParseForm()); }
public void DefineAFunctionWithVariableArguments() { FnStarPrimitive fnprim = new FnStarPrimitive(); Machine machine = new Machine(); Parser parser = new Parser("[x & xs] (+ x xs)"); object arguments = parser.ParseForm(); object body = parser.ParseForm(); object result = fnprim.Apply(machine, machine.Environment, new object[] { arguments, body }); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedFunction)); DefinedFunction func = (DefinedFunction)result; Assert.AreEqual(1, func.Arity); Assert.IsTrue(func.VariableArity); }
public void ShouldExpandImplicitUnquotedSymbolInList() { Parser parser = new Parser("(1 ~x 3)"); object list = parser.ParseForm(); Machine machine = new Machine(); machine.Environment.SetValue("x", 2); object result = MacroUtilities.Expand(list, machine, machine.Environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); IList resultList = (IList)result; Assert.AreEqual(3, resultList.Count); Assert.AreEqual(1, resultList[0]); Assert.AreEqual(2, resultList[1]); Assert.AreEqual(3, resultList[2]); }
public void ParseBackquotedSymbol() { Parser parser = new Parser("`foo"); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(IList)); IList list = (IList)form; Assert.AreEqual(2, list.Count); Assert.IsInstanceOfType(list[0], typeof(Symbol)); Assert.IsInstanceOfType(list[1], typeof(Symbol)); Assert.AreEqual("backquote", ((Symbol)list[0]).Name); Assert.AreEqual("foo", ((Symbol)list[1]).Name); Assert.IsNull(parser.ParseForm()); }
public void DefineAndEvaluateSimpleList() { Parser parser = new Parser("[x y] (list 'list (unquote x) (unquote y)) 1 2"); object argumentNames = parser.ParseForm(); object body = parser.ParseForm(); DefinedMacro func = new DefinedMacro("simple-list", (ICollection)argumentNames, body); Assert.AreEqual("simple-list", func.Name); object[] arguments = new object[2]; arguments[0] = parser.ParseForm(); arguments[1] = parser.ParseForm(); Machine machine = new Machine(); object result = func.Apply(machine, machine.Environment, arguments); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); Assert.AreEqual(2, ((IList)result).Count); }
public void DefineAndEvaluateAFunctionUsingAmpersand() { Parser parser = new Parser("(def mylist (fn* [& coll] coll)) (mylist 1 2)"); Machine machine = new Machine(); object value = machine.Evaluate(parser.ParseForm()); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(DefinedFunction)); object result = machine.Evaluate(parser.ParseForm()); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); IList list = (IList)result; Assert.AreEqual(2, list.Count); Assert.AreEqual(1, list[0]); Assert.AreEqual(2, list[1]); Assert.IsNull(parser.ParseForm()); }
public void ShouldExpandUnquotedSplicingSymbolInList() { Parser parser = new Parser("(def x (list 2 3)) (1 (unquote-splicing x) 4)"); Machine machine = new Machine(); machine.Evaluate(parser.ParseForm()); object list = parser.ParseForm(); object result = MacroUtilities.Expand(list, machine, machine.Environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IList)); IList resultList = (IList)result; Assert.AreEqual(4, resultList.Count); Assert.AreEqual(1, resultList[0]); Assert.AreEqual(2, resultList[1]); Assert.AreEqual(3, resultList[2]); Assert.AreEqual(4, resultList[3]); }
public void ParseMapAsAssociative() { Parser parser = new Parser("{:one 1 :two 2 :three 3}"); object obj = parser.ParseForm(); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IAssociative)); IAssociative associative = (IAssociative)obj; Assert.AreEqual(3, associative.Count); Assert.AreEqual(1, associative.ValueAt(Keyword.Create("one"))); Assert.AreEqual(2, associative.ValueAt(Keyword.Create("two"))); Assert.AreEqual(3, associative.ValueAt(Keyword.Create("three"))); Assert.IsNull(parser.ParseForm()); }
public void RaiseIfQualifiedArgumentNameInFunction() { FnStarPrimitive fnprim = new FnStarPrimitive(); Machine machine = new Machine(); Parser parser = new Parser("[foo/bar] (+ x xs)"); object arguments = parser.ParseForm(); object body = parser.ParseForm(); fnprim.Apply(machine, machine.Environment, new object[] { arguments, body }); }
public void DefineAndInvokeAMultiFunction() { FnStarPrimitive fnprim = new FnStarPrimitive(); Machine machine = new Machine(); Parser parser = new Parser("([x] (+ x 1)) ([x y] (+ x y 1))"); object[] parameters = new object[2]; parameters[0] = parser.ParseForm(); parameters[1] = parser.ParseForm(); object result = fnprim.Apply(machine, machine.Environment, parameters); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedMultiFunction)); DefinedMultiFunction func = (DefinedMultiFunction)result; object result1 = func.Apply(machine, machine.Environment, new object[] { 1 }); Assert.IsNotNull(result1); Assert.IsInstanceOfType(result1, typeof(int)); Assert.AreEqual(2, result1); object result2 = func.Apply(machine, machine.Environment, new object[] { 1, 2 }); Assert.IsNotNull(result2); Assert.IsInstanceOfType(result2, typeof(int)); Assert.AreEqual(4, result2); }
public void ShouldExpandUnquotedSymbol() { Parser parser = new Parser("(unquote x)"); object list = parser.ParseForm(); Machine machine = new Machine(); machine.Environment.SetValue("x", "y"); object result = MacroUtilities.Expand(list, machine, machine.Environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual("y", result); }
public void RaiseIfTooManyVarsMarkers() { FnStarPrimitive fnprim = new FnStarPrimitive(); Machine machine = new Machine(); Parser parser = new Parser("[& x & y] (+ x xs)"); object arguments = parser.ParseForm(); object body = parser.ParseForm(); fnprim.Apply(machine, machine.Environment, new object[] { arguments, body }); }
public void ParseInteger() { Parser parser = new Parser("123"); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(int)); Assert.AreEqual(123, (int) form); }
public void ParseEmptyString() { Parser parser = new Parser(string.Empty); Assert.IsNull(parser.ParseForm()); }
public void ParseList() { Parser parser = new Parser("(1 2 3)"); object obj = parser.ParseForm(); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IList)); IList list = (IList)obj; Assert.AreEqual(3, list.Count); Assert.AreEqual(1, list[0]); Assert.AreEqual(2, list[1]); Assert.AreEqual(3, list[2]); Assert.IsNull(parser.ParseForm()); }
public void ParseSymbolWithMetadata() { Parser parser = new Parser("#^{:one 1 :two 2} foo"); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(Symbol)); Symbol symbol = (Symbol)form; Assert.AreEqual("foo", symbol.Name); Assert.AreEqual("foo", symbol.FullName); Assert.IsNotNull(symbol.Metadata); Assert.IsInstanceOfType(symbol.Metadata, typeof(IDictionary)); IDictionary dict = (IDictionary)symbol.Metadata; Assert.IsTrue(dict.Contains(Keyword.Create("one"))); Assert.IsTrue(dict.Contains(Keyword.Create("two"))); Assert.AreEqual(1, dict[Keyword.Create("one")]); Assert.AreEqual(2, dict[Keyword.Create("two")]); Assert.IsNull(parser.ParseForm()); }
public void ParseSymbolIgnoringPrecedingComment() { Parser parser = new Parser("; this is a symbol\r\nfoo"); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(Symbol)); Symbol symbol = (Symbol)form; Assert.AreEqual("foo", symbol.Name); Assert.IsNull(symbol.Namespace); Assert.IsNull(parser.ParseForm()); }
public void ParseSymbol() { Parser parser = new Parser("foo"); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(Symbol)); Symbol symbol = (Symbol)form; Assert.AreEqual("foo", symbol.Name); Assert.IsNull(symbol.Namespace); Assert.IsNull(parser.ParseForm()); }
public void ParseString() { Parser parser = new Parser("\"foo\""); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(string)); Assert.AreEqual("foo", form); }
public void ParseListWithMetadata() { Parser parser = new Parser("#^{:one 1 :two 2} (1 2)"); object form = parser.ParseForm(); Assert.IsNotNull(form); Assert.IsInstanceOfType(form, typeof(IList)); Assert.IsInstanceOfType(form, typeof(IObject)); IList list = (IList)form; Assert.AreEqual(2, list.Count); Assert.AreEqual(1, list[0]); Assert.AreEqual(2, list[1]); IObject iobj = (IObject)form; Assert.IsNotNull(iobj.Metadata); IDictionary dict = (IDictionary)iobj.Metadata; Assert.IsTrue(dict.Contains(Keyword.Create("one"))); Assert.IsTrue(dict.Contains(Keyword.Create("two"))); Assert.AreEqual(1, dict[Keyword.Create("one")]); Assert.AreEqual(2, dict[Keyword.Create("two")]); Assert.IsNull(parser.ParseForm()); }
public void ParseMap() { Parser parser = new Parser("{:one 1 :two 2 :three 3}"); object obj = parser.ParseForm(); Assert.IsNotNull(obj); Assert.IsInstanceOfType(obj, typeof(IDictionary)); IDictionary dictionary = (IDictionary) obj; Assert.AreEqual(3, dictionary.Count); Assert.AreEqual(1, dictionary[Keyword.Create("one")]); Assert.AreEqual(2, dictionary[Keyword.Create("two")]); Assert.AreEqual(3, dictionary[Keyword.Create("three")]); Assert.IsNull(parser.ParseForm()); }