public void InvariantCultureTest() { // The StandardEnvironment must always use InvariantCulture // (de-DE uses "," as the decimal separator). Because CONCAT // converts its arguments to string, it's a good test case: var env = new StandardEnvironment(); var fconcat = GetFunction(env, "CONCAT"); var cc = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); try { Assert.AreEqual("1,2", (1.2).ToString(CultureInfo.CurrentCulture)); Assert.AreEqual("1.2", env.Invoke(fconcat, Args(1.2))); Assert.AreEqual("1.23.4", env.Invoke(fconcat, Args(1.2, 3.4))); Assert.AreEqual("1.23.45.67.8", env.Invoke(fconcat, Args(1.2, 3.4, 5.6, 7.8))); } finally { Thread.CurrentThread.CurrentCulture = cc; // restore } }
public void CanRegisterExtraFunctions() { var extra = new ExtraFunctionality(2.0); var env = new StandardEnvironment(); // Register functions (static): env.Register("Pi", ExtraFunctionality.Pi); env.Register <double, double>("Square", ExtraFunctionality.Square); // Register methods (need object state): env.Register("Circumference", extra.Circumference, extra); env.Register <double, double>("Volume", extra.Volume, extra); env.Register <object[], double>("Volume", extra.Volume, extra); // variadic object r1 = env.Invoke(GetFunction(env, "Pi")); Assert.AreEqual(Math.PI, r1); object r2 = env.Invoke(GetFunction(env, "Square"), 2.0); Assert.AreEqual(4.0, r2); object r3 = env.Invoke(GetFunction(env, "Circumference")); Assert.AreEqual(extra.Radius * Math.PI * 2, r3); object r4 = env.Invoke(GetFunction(env, "Volume"), 3.0); Assert.AreEqual(extra.Radius * extra.Radius * Math.PI * 3.0, r4); object r5 = env.Invoke(GetFunction(env, "Volume"), 1.0, 2.0, 3.0); // variadic Assert.AreEqual(extra.Radius * extra.Radius * Math.PI * 6.0, r5); }
public void CanDecodeFunction() { var env = new StandardEnvironment(); var fdecode = GetFunction(env, "DECODE"); Assert.AreEqual(12, env.Invoke(fdecode, Args(12))); Assert.AreEqual(13, env.Invoke(fdecode, Args(12, 13))); Assert.AreEqual(null, env.Invoke(fdecode, Args(12, 8, 16))); Assert.AreEqual(24, env.Invoke(fdecode, Args(12, 12, 24))); Assert.AreEqual(25, env.Invoke(fdecode, Args(12, 8, 16, 25))); // DECODE considers two nulls to be equal (emulate Oracle behaviour): Assert.IsNull(env.Invoke(fdecode, Args(null))); Assert.IsNull(env.Invoke(fdecode, Args(null, null))); Assert.AreEqual("deflt", env.Invoke(fdecode, Args(null, "deflt"))); Assert.AreEqual("deflt", env.Invoke(fdecode, Args(null, "s1", "r1", "s2", "r2", "deflt"))); Assert.AreEqual("r2", env.Invoke(fdecode, Args(null, "s1", "r1", null, "r2", "deflt"))); Assert.IsNull(env.Invoke(fdecode, Args(null, "s1", "r1", "s2", "r2"))); Assert.IsNull(env.Invoke(fdecode, Args(null, "s1", "r1", "s2", "r2", null))); Assert.IsNull(env.Invoke(fdecode, Args("s2", "s1", null, "s2", null, "deflt"))); // DECODE: equate (int) 1 and (double/float) 1.0 etc. Assert.AreEqual("one", env.Invoke(fdecode, Args(1.0, 1, "one", "oops"))); }
public void CanSubstrFunction() { var env = new StandardEnvironment(); var fsubstr = GetFunction(env, "SUBSTR"); Assert.AreEqual("cde", env.Invoke(fsubstr, Args("abcdefg", 2, 3))); Assert.AreEqual("cdefg", env.Invoke(fsubstr, Args("abcdefg", 2))); Assert.AreEqual("cdefg", env.Invoke(fsubstr, Args("abcdefg", 2, 99))); Assert.AreEqual("", env.Invoke(fsubstr, Args("abcdefg", -7, 3))); Assert.AreEqual("a", env.Invoke(fsubstr, Args("abcdefg", -7, 8))); Assert.AreEqual("abcdefgh", env.Invoke(fsubstr, Args("abcdefgh", -7))); Assert.AreEqual("", env.Invoke(fsubstr, Args("abcdefg", 9, 3))); // SUBSTR: the 1st and 2nd arg are null-contagious; the 3rd defaults Assert.IsNull(env.Invoke(fsubstr, Args(null, null, null))); Assert.IsNull(env.Invoke(fsubstr, Args(null, null))); Assert.IsNull(env.Invoke(fsubstr, Args(null, 2, 3))); Assert.IsNull(env.Invoke(fsubstr, Args(null, 2))); Assert.AreEqual("b", env.Invoke(fsubstr, Args("abc", 1, 1))); Assert.AreEqual("bc", env.Invoke(fsubstr, Args("abc", 1, null))); Assert.IsNull(env.Invoke(fsubstr, Args("abc", null, 1))); Assert.IsNull(env.Invoke(fsubstr, Args("abc", null, null))); Assert.IsNull(env.Invoke(fsubstr, Args("abc", null))); }
public void CanEvaluateUnaryOps() { Assert.AreEqual(5, Evaluate("+5")); Assert.AreEqual(-5, Evaluate("+-5")); Assert.AreEqual(25, Evaluate("+ + +25")); Assert.AreEqual(-0.2, Evaluate(" - 0.2 ")); Assert.AreEqual(-0.2, Evaluate("- - -0.2")); Assert.AreEqual(-1.2e-03, Evaluate("-1.2e-03")); Assert.AreEqual(false, Evaluate("not true")); Assert.AreEqual(true, Evaluate("not not true")); Assert.AreEqual(true, Evaluate("not not 5")); var env = new StandardEnvironment(); env.DefineValue("foo", 42.0); Assert.AreEqual(42.0, Evaluate("foo", env)); Assert.AreEqual(false, Evaluate("not foo", env)); Assert.AreEqual(true, Evaluate("not not foo", env)); Assert.AreEqual(false, Evaluate("not not not foo", env)); Assert.AreEqual(5, Evaluate("+ +5")); Catch(() => Evaluate("++5")); Assert.AreEqual(25, Evaluate("- - 25")); Catch(() => Evaluate("-- 25")); // -- is one token }
public static UnumEnvironment FromStandardEnvironment(StandardEnvironment environment) { switch (environment) { case StandardEnvironment.Warlpiri: return(new UnumEnvironment(0, 0)); case StandardEnvironment.HalfPrecisionLike: return(FromConfiguration(UnumConfiguration.FromIeeeConfiguration(IeeeConfiguration.HalfPrecision))); case StandardEnvironment.SinglePrecisionLike: return(FromConfiguration(UnumConfiguration.FromIeeeConfiguration(IeeeConfiguration.SinglePrecision))); case StandardEnvironment.DoublePrecisionLike: return(FromConfiguration(UnumConfiguration.FromIeeeConfiguration(IeeeConfiguration.DoublePrecision))); case StandardEnvironment.ExtendedPrecisionLike: return(FromConfiguration(UnumConfiguration.FromIeeeConfiguration(IeeeConfiguration.ExtendedPrecision))); case StandardEnvironment.QuadPrecisionLike: return(FromConfiguration(UnumConfiguration.FromIeeeConfiguration(IeeeConfiguration.QuadPrecision))); default: return(FromConfiguration(UnumConfiguration.FromIeeeConfiguration(IeeeConfiguration.SinglePrecision))); } }
public static bool SystemListToVector(out object answer, object arg0, object arg1) { TC code = (TC)arg0; switch (code) { case TC.COMBINATION: answer = Combination.FromList((Cons)arg1); break; case TC.ENVIRONMENT: StandardClosure closure = (StandardClosure)((Cons)arg1).Car; object tail = ((Cons)arg1).Cdr; object [] initialValues = tail == null ? new object [0] : ((Cons)tail).ToVector(); answer = new StandardEnvironment <StandardLambda, StandardClosure> (closure, initialValues); break; case TC.PCOMB3: answer = PrimitiveCombination3.Make((Cons)arg1); break; default: throw new NotImplementedException(); } return(false); }
public void CanIsType() { var env = new StandardEnvironment(); Assert.IsTrue(env.IsType(null, "null")); Assert.IsTrue(env.IsType(true, "boolean")); Assert.IsTrue(env.IsType(1.25, "number")); Assert.IsTrue(env.IsType("foo", "string")); Assert.IsFalse(env.IsType(0, "null")); Assert.IsFalse(env.IsType(0, "boolean")); Assert.IsFalse(env.IsType("123", "number")); Assert.IsFalse(env.IsType(123, "string")); Assert.IsFalse(env.IsType(null, "boolean")); Assert.IsFalse(env.IsType(null, "number")); Assert.IsFalse(env.IsType(null, "string")); // Case in type name is ignored: Assert.IsTrue(env.IsType(null, "NULL")); Assert.IsTrue(env.IsType(false, "Boolean")); Assert.IsTrue(env.IsType(-500, "NuMbEr")); Assert.IsTrue(env.IsType("", "STRING")); // Unknown type name must throw an exception: var ex = Assert.Catch <EvaluationException>(() => env.IsType("foo", "NoSuchType")); Console.WriteLine(@"Expected: {0}", ex.Message); }
public void CanEvaluatePrimitives() { var env = new StandardEnvironment(); env.DefineValue("foo", "foo"); env.DefineValue("_foo_bar", "_foo_bar"); // Symbols Assert.AreEqual("foo", Evaluate("foo", env)); Assert.AreEqual("foo", Evaluate(" foo ", env)); Assert.AreEqual("_foo_bar", Evaluate("_foo_bar", env)); // Strings Assert.AreEqual("foo'bar", Evaluate("'foo''bar'", env)); Assert.AreEqual("foo\"bar\"\n\'/\\", Evaluate(@"""foo\""bar\""\n\'\/\\""", env)); // Numbers Assert.AreEqual(123, Evaluate("123")); Assert.AreEqual(1.23, Evaluate("1.23")); Assert.AreEqual(2.7e-5, Evaluate("2.7e-5")); // null and Booleans Assert.AreEqual(null, Evaluate("null")); Assert.AreEqual(false, Evaluate("false")); Assert.AreEqual(true, Evaluate("true")); // Excessive parens... Assert.AreEqual(42, Evaluate("(((23+19)))")); }
private FieldSetter([NotNull] IEnumerable <Assignment> assignments) { _assignments = assignments.ToArray(); _values = new object[_assignments.Length]; _stack = new Stack <object>(); _environment = new StandardEnvironment(); _text = null; }
public void CanLogicalNot() { var env = new StandardEnvironment(); Assert.AreEqual(null, env.Not(null)); Assert.AreEqual(true, env.Not(false)); Assert.AreEqual(false, env.Not(true)); }
// Functions of the same name must differ by their arity! // This is different from C# method overloading! public static StandardEnvironment RegisterGeometryFunctions( [NotNull] this StandardEnvironment env) { env.Register <object, object>("SHAPEAREA", Area); env.Register <object, object>("SHAPELENGTH", Length); // Nice to have: Area/0 and Length/0 that refer to current row's Shape field (whatever it's named) return(env); }
public void CanLengthFunction() { var env = new StandardEnvironment(); var flength = GetFunction(env, "LENGTH"); Assert.AreEqual(3, env.Invoke(flength, Args("abc"))); Assert.AreEqual(0, env.Invoke(flength, Args(""))); Assert.IsNull(env.Invoke(flength, Args(null))); }
public void CanAbsFunction() { var env = new StandardEnvironment(); var fabs = GetFunction(env, "ABS"); Assert.IsNull(env.Invoke(fabs, new object[] { null })); Assert.AreEqual(13.5, env.Invoke(fabs, Args(13.5))); Assert.AreEqual(7.0, env.Invoke(fabs, Args(-7.0))); }
public void CanFloorFunction() { var env = new StandardEnvironment(); var ffloor = GetFunction(env, "FLOOR"); Assert.IsNull(env.Invoke(ffloor, new object[] { null })); Assert.AreEqual(13.0, env.Invoke(ffloor, Args(13.5))); Assert.AreEqual(-8.0, env.Invoke(ffloor, Args(-7.1))); Assert.AreEqual(0, env.Invoke(ffloor, Args(0.99))); }
public void CanCeilFunction() { var env = new StandardEnvironment(); var fceil = GetFunction(env, "CEIL"); Assert.IsNull(env.Invoke(fceil, new object[] { null })); Assert.AreEqual(14.0, env.Invoke(fceil, Args(13.5))); Assert.AreEqual(-7.0, env.Invoke(fceil, Args(-7.1))); Assert.AreEqual(0, env.Invoke(fceil, Args(-0.99))); }
public void CannotDivideByZero() { // The StandardEnvironment throws on divide by zero // (rather than returning a double infinity or NaN or null) var evaluator = ExpressionEvaluator.Create("2.5/0.0"); var env = new StandardEnvironment(); var ex = Assert.Catch <EvaluationException>(() => evaluator.Evaluate(env)); Console.WriteLine(@"Expected: {0}", ex.Message); }
public void CanRandPickFunction() { var env = new StandardEnvironment(); var frandpick = GetFunction(env, "RANDPICK"); env.RandomSeed = 1234; // repeatable randomness Assert.IsNull(env.Invoke(frandpick, Args())); Assert.AreEqual("one", env.Invoke(frandpick, Args("one"))); Assert.AreEqual("foo", env.Invoke(frandpick, Args("foo", "bar"))); Assert.AreEqual(4, env.Invoke(frandpick, Args(1, 2, 3, 3, 3, 4))); }
public void CanRPadFunction() { var env = new StandardEnvironment(); var frpad = GetFunction(env, "RPAD"); Assert.AreEqual("abc ", env.Invoke(frpad, Args("abc", 6))); Assert.AreEqual("123***", env.Invoke(frpad, Args(123, 6, "*"))); // 1st arg is null-contagious, 2nd & 3rd args have defaults Assert.IsNull(env.Invoke(frpad, Args(null, null))); Assert.IsNull(env.Invoke(frpad, Args(null, 5))); Assert.AreEqual("x", env.Invoke(frpad, Args("x", null))); }
public void CanEvaluateVariadicFunction() { var env = new StandardEnvironment(); var r1 = Evaluate("MIN(3,8,1,5)", env); Console.WriteLine(r1); Assert.AreEqual(1, r1); var r2 = Evaluate("MIN(123)", env); Console.WriteLine(r2); Assert.AreEqual(123, r2); }
public void CanEvaluateConditionalOperator() { Assert.AreEqual("T", Evaluate("true ? 'T' : 'F'")); Assert.AreEqual("F", Evaluate("false ? 'T' : 'F'")); Assert.AreEqual(null, Evaluate("null ? 'T' : 'F'")); var env = new StandardEnvironment(); env.DefineValue("xyzzy", "Magic"); env.DefineValue("d", 5); Assert.AreEqual(5, Evaluate("xyzzy ? length(xyzzy) : -1", env)); Assert.AreEqual("Fri", Evaluate("d=0 ? 'Sun' : d=1 ? 'Mon' : d=2 ? 'Tue' : d=3 ? 'Wed' : d=4 ? 'Thu' : d=5 ? 'Fri' : d=6 ? 'Sat' : null", env)); }
public void CanLookupIgnoreCase() { var envIgnoreCase = new StandardEnvironment(); envIgnoreCase.DefineValue("foo", "bar"); Assert.AreEqual("bar", envIgnoreCase.Lookup("foo", null)); Assert.AreEqual("bar", envIgnoreCase.Lookup("FOO", null)); var ex1 = Assert.Catch <EvaluationException>(() => envIgnoreCase.Lookup("foo", "qualifier")); Console.WriteLine(@"Expected exception: {0}", ex1.Message); Assert.IsInstanceOf(typeof(Function), envIgnoreCase.Lookup("TRIM", null)); Assert.IsInstanceOf(typeof(Function), envIgnoreCase.Lookup("Trim", null)); }
public void CanLogicalOr() { var env = new StandardEnvironment(); Assert.AreEqual(null, env.Or(null, null)); Assert.AreEqual(null, env.Or(null, false)); Assert.AreEqual(true, env.Or(null, true)); Assert.AreEqual(null, env.Or(false, null)); Assert.AreEqual(false, env.Or(false, false)); Assert.AreEqual(true, env.Or(false, true)); Assert.AreEqual(true, env.Or(true, null)); Assert.AreEqual(true, env.Or(true, false)); Assert.AreEqual(true, env.Or(true, true)); }
public void setupFixture() { e = new Evaluator(); env = StandardEnvironment.Create(); var setupDatum = getLispFromResource("setup"); if (setupDatum != nil) { foreach (var f in setupDatum.Enumerate()) { e.Evaluate(env, f); } } }
public static StandardEnvironment RegisterConversionFunctions( [NotNull] this StandardEnvironment env /*, * [CanBeNull] IMapContext mapContext*/) { //var instance = new ConversionFunctions {MapContext = mapContext}; env.Register <object, object>("mm2pt", mm2pt); env.Register <object, object>("pt2mm", pt2mm); //env.Register<object, object>("mm2mu", instance.mm2mu, instance); //env.Register<object, object>("mu2mm", instance.mu2mm, instance); //env.Register<object, object>("pt2mu", instance.pt2mu, instance); //env.Register<object, object>("mu2pt", instance.mu2pt, instance); return(env); }
public void CanEvaluateNullCoalescingOperator() { Assert.AreEqual("foo", Evaluate("'foo' ?? 'bar'")); Assert.AreEqual("bar", Evaluate("null ?? 'bar'")); // Here's how ?? is different from || (logical or): Assert.AreEqual(false, Evaluate("false ?? 'bar'")); Assert.AreEqual(true, Evaluate("false or 'bar'")); // "bar" // ?? binds tighter than ?: var env = new StandardEnvironment(); env.DefineValue("nothing", null); Assert.AreEqual("alt", Evaluate("nothing ?? false ? null ?? 'consequent' : 'alt' ?? 'alternative'", env)); }
static void Main(string[] args) { try { var env = StandardEnvironment.Create(); env.Define("args", DatumHelpers.atomList(args)); var statistics = new Statistics(); env = statistics.AddTo(env); ResourceLoader.ExecuteResource(statistics, Assembly.GetExecutingAssembly(), env, "Lisp.REPL.lisp"); } catch (Exception ex) { Console.Error.WriteLine("ERROR:\n{0}\n{1}\n", ex, ex.StackTrace); } }
public void CanLcaseFunction() { var env = new StandardEnvironment(); var flcase = GetFunction(env, "LCASE"); Assert.AreEqual("hello world", env.Invoke(flcase, Args("Hello World"))); Assert.IsNull(env.Invoke(flcase, Args(null))); Assert.Catch <EvaluationException>(() => env.Invoke(flcase, Args())); Assert.Catch <EvaluationException>(() => env.Invoke(flcase, Args("one", "two"))); var ex = Assert.Catch(() => env.Invoke(flcase, Args(123))); Console.WriteLine(@"Expected exception: {0}", ex.Message); }
public void CanIsTrue() { var env = new StandardEnvironment(); // null and false are "falsy", all other values are "truthy" // Generally, IsFalse(x) != IsTrue(x) for all x except null. Assert.IsFalse(env.IsTrue(null)); Assert.IsFalse(env.IsTrue(false)); Assert.IsTrue(env.IsTrue(true)); Assert.IsTrue(env.IsTrue(0.0)); Assert.IsTrue(env.IsTrue(-2.5)); Assert.IsTrue(env.IsTrue(string.Empty)); Assert.IsTrue(env.IsTrue("foo")); }
public void CannotLookupAmbiguous() { var env = new StandardEnvironment(); var one = new ConstantValueTestRow("one", "FOO"); var two = new ConstantValueTestRow("two", "FOO"); env.DefineFields(one, "one"); env.DefineFields(two, "two"); var ex = Assert.Catch <EvaluationException>(() => env.Lookup("FOO", null)); Console.WriteLine(@"Expected exception: {0}", ex.Message); Assert.AreEqual("one", env.Lookup("FOO", "one")); Assert.AreEqual("two", env.Lookup("FOO", "two")); }
internal static LexicalMap Make(StandardEnvironment env) { return new StandardLexicalMap (env); }
StandardLexicalMap(StandardEnvironment env) : base() { this.environment = env; }
public static bool SystemListToVector(out object answer, object arg0, object arg1) { TC code = (TC) arg0; switch (code) { case TC.COMBINATION: answer = Combination.FromList ((Cons) arg1); break; case TC.ENVIRONMENT: StandardClosure closure = (StandardClosure) ((Cons) arg1).Car; object tail = ((Cons) arg1).Cdr; object [] initialValues = tail == null ? new object [0] : ((Cons) tail).ToVector (); answer = new StandardEnvironment<StandardLambda, StandardClosure> (closure, initialValues); break; case TC.PCOMB3: answer = PrimitiveCombination3.Make ((Cons) arg1); break; default: throw new NotImplementedException (); } return false; }
public static bool ObjectSetType(out object answer, object arg0, object arg1) { TC newType = (TC) (int) arg0; // kludge!!!! if ((int) arg0 == 0 && (int) arg1 == 1) answer = new NullEnvironment (); else switch (newType) { case TC.COMBINATION_2: answer = Combination2.Make ((Hunk3) arg1); break; case TC.CONDITIONAL: answer = Conditional.Make ((Hunk3) arg1); break; case TC.CONSTANT: answer = Constant.Decode ((uint) (int) arg1); break; case TC.HUNK3_A: // Probably someone trying to mark a history object. answer = arg1; break; case TC.HUNK3_B: answer = arg1; break; case TC.ENVIRONMENT: object [] args = (object[]) arg1; StandardClosure closure = (StandardClosure) args [0]; object [] actualArgs = new object [args.Length - 1]; for (int i = 0; i < actualArgs.Length; i++) actualArgs [i] = args [i + 1]; answer = new StandardEnvironment<StandardLambda,StandardClosure> (closure, actualArgs); break; // throw new NotImplementedException (); // // answer = (new InterpreterEnvironment ((object []) arg1)); case TC.EXTENDED_LAMBDA: answer = ExtendedLambda.Make ((Hunk3) arg1); break; case TC.PCOMB0: answer = PrimitiveCombination0.Make ((Primitive0) arg1); break; case TC.PCOMB2: answer = PrimitiveCombination2.Make ((Hunk3) arg1); break; case TC.PRIMITIVE: if (!(arg1 is PrimitiveCombination0)) throw new NotImplementedException ("Object-set-type on primitive"); answer = ((PrimitiveCombination0) arg1).Operator; break; case TC.RECORD: answer = new Record ((object []) arg1); return false; case TC.SEQUENCE_3: answer = new Sequence3 ((Hunk3) arg1); break; case TC.THE_ENVIRONMENT: answer = TheEnvironment.Make(); break; case TC.VARIABLE: answer = Variable.Make ((Hunk3) arg1); break; case TC.VECTOR: // Someone wants to see what endian we are! char [] source = (char []) arg1; object [] result = new object [source.Length / 4]; result [1] = ((((((byte) source [3]) * 256) + ((byte) source [2])) * 256) + ((byte) source [1])) * 256 + ((byte) source [0]); result [0] = ((((((byte) source [7]) * 256) + ((byte) source [6])) * 256) + ((byte) source [5])) * 256 + ((byte) source [4]); answer = result; break; case TC.WEAK_CONS: answer = new WeakCons (((Cons) arg1).Car, ((Cons) arg1).Cdr); break; default: throw new NotImplementedException (); } return false; }