public static void Register(Engine engine) { //@ class List<T> TypeDef_Class ourType = TypeFactory.GetTypeDef_Class("List", new ArgList { IntrinsicTypeDefs.TEMPLATE_0 }, false); ClassDef classDef = engine.defaultContext.CreateClass("List", ourType, null, new List <string> { "T" }); classDef.childAllocator = () => { return(new PebbleList()); }; classDef.Initialize(); //@ List<T> Add(T newValue, ...) or List<T> Push(T newValue, ...) // Adds one or more elements to the end of the list. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { PebbleList scope = thisScope as PebbleList; if (scope.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Add: Attempt to modify a list that is being enumerated by a foreach loop."); return(null); } var list = scope.list; var listType = (TypeDef_Class)scope.classDef.typeDef; var elementType = listType.genericTypes[0]; for (int ii = 0; ii < args.Count; ++ii) { object ret = args[ii]; list.Add(new Variable(null, elementType, ret)); } return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.TEMPLATE_0 }, eval, true, ourType); classDef.AddMemberLiteral("Add", newValue.valType, newValue); classDef.AddMemberLiteral("Push", newValue.valType, newValue); } //@ List<T> Clear() // Removes all elements from the list. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { PebbleList pebList = thisScope as PebbleList; if (pebList.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Clear: Attempt to modify a list that is being enumerated by a foreach loop."); return(null); } var list = pebList.list; list.Clear(); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Clear", newValue.valType, newValue); } //@ num Count() // Returns the number of elements in the list. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var list = (thisScope as PebbleList).list; return(System.Convert.ToDouble(list.Count)); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.NUMBER, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Count", newValue.valType, newValue); } //@ T Get(num index) // Returns the value of the element of the list at the given index. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { double dix = (double)args[0]; int ix = (int)dix; var list = (thisScope as PebbleList).list; // Bounds checking. if (ix < 0 || ix >= list.Count) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Get: Index " + ix + " out of bounds of array of length " + list.Count + "."); return(null); } return(list[ix].value); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.TEMPLATE_0, new ArgList { IntrinsicTypeDefs.NUMBER }, eval, false, ourType); classDef.AddMemberLiteral("Get", newValue.valType, newValue); } //@ List<T> Insert(num index, T item) // Inserts a new element into the list at the given index. Existing elements at and after the given index are pushed further down the list. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { PebbleList scope = thisScope as PebbleList; if (scope.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Insert: Attempt to modify a list that is being enumerated by a foreach loop."); return(null); } var list = scope.list; var listType = (TypeDef_Class)scope.classDef.typeDef; var elementType = listType.genericTypes[0]; var indexDouble = (double)args[0]; var item = args[1]; var index = Convert.ToInt32(indexDouble); if (index < 0 || index > list.Count) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Insert: array index out of bounds."); return(null); } list.Insert(index, new Variable(null, elementType, item)); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.NUMBER, IntrinsicTypeDefs.TEMPLATE_0 }, eval, false, ourType); classDef.AddMemberLiteral("Insert", newValue.valType, newValue); } //@ T Pop() // Returns the value of the last element of the list and removes it from the list. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { PebbleList pebList = thisScope as PebbleList; if (pebList.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Pop: Attempt to remove an element from a list that is being enumerated in a foreach loop."); return(null); } var list = pebList.list; int ix = list.Count - 1; if (ix < 0) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Pop: List is empty."); return(null); } var result = list[ix].value; list.RemoveAt(ix); return(result); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.TEMPLATE_0, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Pop", newValue.valType, newValue); } //@ List<T> RemoveAt(num index) // Removes element at the given index, and returns the list. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { double dix = (double)args[0]; int ix = (int)dix; PebbleList pebList = thisScope as PebbleList; if (pebList.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "RemoveAt: Attempt to modify a list that is being enumerated by a foreach loop."); return(null); } var list = pebList.list; if (ix < 0 || ix >= list.Count) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveAt: Index " + ix + " out of bounds of array of length " + list.Count + "."); return(null); } list.RemoveAt(ix); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.NUMBER }, eval, false, ourType); classDef.AddMemberLiteral("RemoveAt", newValue.valType, newValue); } //@ List<T> RemoveRange(num start, num count) // Removes elements in the given range of indices, and returns the list. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { double dstart = (double)args[0]; int start = (int)dstart; double dcount = (double)args[1]; int count = (int)dcount; PebbleList pebList = thisScope as PebbleList; if (pebList.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "RemoveRange: Attempt to modify a list that is being enumerated by a foreach loop."); return(null); } var list = pebList.list; if (start < 0 || start >= list.Count) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveRange: Start " + start + " out of bounds of array of length " + list.Count + "."); return(null); } if (count < 0) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveRange: Count (" + count + ") cannot be negative."); return(null); } if ((start + count) >= list.Count) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "RemoveRange: Count " + count + " exceeds array length (" + list.Count + ")."); return(null); } list.RemoveRange(start, count); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.NUMBER, IntrinsicTypeDefs.NUMBER }, eval, false, ourType); classDef.AddMemberLiteral("RemoveRange", newValue.valType, newValue); } //@ List<T> Reverse() // Reverses the list and returns it. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var list = (thisScope as PebbleList).list; list.Reverse(); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Reverse", newValue.valType, newValue); } //@ List<T> Set(num index, T newValue) // Changes the value of the element at the given index, and returns the list. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { double dix = (double)args[0]; int ix = (int)dix; object value = args[1]; var list = (thisScope as PebbleList).list; // Bounds checking. if (ix < 0 || ix >= list.Count) { context.SetRuntimeError(RuntimeErrorType.ArrayIndexOutOfBounds, "Set: Index " + ix + " out of bounds of array of length " + list.Count + "."); return(null); } list[ix].value = value; return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.NUMBER, IntrinsicTypeDefs.TEMPLATE_0 }, eval, false, ourType); classDef.AddMemberLiteral("Set", newValue.valType, newValue); } //@ List<T> Shuffle() // Shuffles the list, putting the elements in random order. // Returns the list. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var list = (thisScope as PebbleList).list; Random rng = new Random(); int n = list.Count; while (n > 1) { --n; int k = rng.Next(n + 1); Variable value = list[k]; list[k] = list[n]; list[n] = value; } return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Shuffle", newValue.valType, newValue); } //@ List<T> Sort(functype<num(T, T>)> comparator) // Sorts the list using the given comparator function. // The comparator should behave the same as a C# Comparer. The first argument should be earlier in the // list than the second, return a number < 0. If It should be later, return a number > 0. If their order // is irrelevant, return 0. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var list = (thisScope as PebbleList).list; if (null == args[0]) { context.SetRuntimeError(RuntimeErrorType.ArgumentInvalid, "Sort: comparator may not be null."); return(null); } FunctionValue comparator = (FunctionValue)args[0]; List <object> argvals = new List <object>(); argvals.Add(0); argvals.Add(0); Comparison <Variable> hostComparator = new Comparison <Variable>( (a, b) => { argvals[0] = a.value; argvals[1] = b.value; // Note we use null instead of thisScope here. There is no way the sort function could be a // class member because Sort's signature only takes function's whose type has no class. double result = (double)comparator.Evaluate(context, argvals, null); return(Convert.ToInt32(result)); } ); list.Sort(hostComparator); return(thisScope); }; TypeDef_Function comparatorType = TypeFactory.GetTypeDef_Function(IntrinsicTypeDefs.NUMBER, new ArgList { IntrinsicTypeDefs.TEMPLATE_0, IntrinsicTypeDefs.TEMPLATE_0 }, -1, false, null, false, false); FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { comparatorType }, eval, false, ourType); classDef.AddMemberLiteral("Sort", newValue.valType, newValue); } //@ string ThisToScript(string prefix) // ThisToScript is used by Serialize. A classes' ThisToScript function should return code which can rebuild the class. // Note that it's only the content of the class, not the "new A" part. ie., it's the code that goes in the defstructor. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { string result = ""; string prefix = (string)args[0] + "\t"; var list = (thisScope as PebbleList).list; for (int ii = 0; ii < list.Count; ++ii) { result += prefix + "Add(" + CoreLib.ValueToScript(context, list[ii].value, prefix + "\t", false) + ");\n"; } return(result); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList { IntrinsicTypeDefs.STRING }, eval, false, ourType); classDef.AddMemberLiteral("ThisToScript", newValue.valType, newValue); } //@ string ToString() // Returns a string representation of at least the first few elements of the list. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var list = (thisScope as PebbleList).list; string result = "List(" + list.Count + ")["; for (int ii = 0; ii < Math.Min(4, list.Count); ++ii) { if (ii > 0) { result += ", "; } result += CoreLib.ValueToString(context, list[ii].value, true); } if (list.Count > 4) { result += ", ..."; } return(result + "]"); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("ThisToString", newValue.valType, newValue); } classDef.FinalizeClass(engine.defaultContext); UnitTests.testFuncDelegates.Add("CoreList", RunTests); }
public static void Register(Engine engine) { //@ class Dictionary<K, V> TypeDef_Class ourType = TypeFactory.GetTypeDef_Class("Dictionary", new ArgList { IntrinsicTypeDefs.TEMPLATE_0, IntrinsicTypeDefs.TEMPLATE_1 }, false); ClassDef classDef = engine.defaultContext.CreateClass("Dictionary", ourType, null, new List <string> { "K", "V" }); classDef.childAllocator = () => { return(new PebbleDictionary()); }; classDef.Initialize(); //@ Dictionary<K, V> Add(K key, V value) // Adds a new element to the dictionary. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { object key = args[0]; object value = args[1]; PebbleDictionary scope = thisScope as PebbleDictionary; if (scope.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Add: Attempt to modify a dictionary that is being enumerated by a foreach loop."); return(null); } var dictionary = scope.dictionary; var dictionaryType = (TypeDef_Class)scope.classDef.typeDef; var valueType = dictionaryType.genericTypes[1]; if (dictionary.ContainsKey(key)) { context.SetRuntimeError(RuntimeErrorType.KeyAlreadyExists, "Dictionary already contains key '" + key + "'."); return(null); } dictionary.Add(key, new Variable(null, valueType, value)); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.TEMPLATE_0, IntrinsicTypeDefs.TEMPLATE_1 }, eval, false, ourType); classDef.AddMemberLiteral("Add", newValue.valType, newValue); } //@ Dictionary<K, V> Clear() // Removes all elements from the dictionary. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { PebbleDictionary pebDict = thisScope as PebbleDictionary; var dictionary = pebDict.dictionary; if (pebDict.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Clear: Attempt to modify a dictionary that is being enumerated by a foreach loop."); return(null); } dictionary.Clear(); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Clear", newValue.valType, newValue); } //@ bool ContainsKey(K) // Returns true iff the dictionary contains an element with the given key. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { object key = args[0]; PebbleDictionary pebDict = thisScope as PebbleDictionary; var dictionary = pebDict.dictionary; return(dictionary.ContainsKey(key)); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList { IntrinsicTypeDefs.TEMPLATE_0 }, eval, false, ourType); classDef.AddMemberLiteral("ContainsKey", newValue.valType, newValue); } //@ num Count() // Returns number of elements in the dictionary. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var dictionary = (thisScope as PebbleDictionary).dictionary; return(System.Convert.ToDouble(dictionary.Count)); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.NUMBER, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("Count", newValue.valType, newValue); } //@ V Get(K) // Returns the value of the element with the given key. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { object key = args[0]; PebbleDictionary pebDict = thisScope as PebbleDictionary; // Bounds checking. var dictionary = pebDict.dictionary; if (!dictionary.ContainsKey(key)) { context.SetRuntimeError(RuntimeErrorType.KeyNotFound, "Get: Key '" + key + "' not in dictionary."); return(null); } return(dictionary[key].value); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.TEMPLATE_1, new ArgList { IntrinsicTypeDefs.TEMPLATE_0 }, eval, false, ourType); classDef.AddMemberLiteral("Get", newValue.valType, newValue); } //@ Dictionary<K, V> Remove(K key) // Removes element with given key. // Cannot be used in a foreach loop. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { object key = args[0]; PebbleDictionary pebDict = thisScope as PebbleDictionary; var dictionary = pebDict.dictionary; if (pebDict.enumeratingCount > 0) { context.SetRuntimeError(RuntimeErrorType.ForeachModifyingContainer, "Remove: Attempt to modify a dictionary that is being enumerated by a foreach loop."); return(null); } if (!dictionary.ContainsKey(key)) { context.SetRuntimeError(RuntimeErrorType.KeyNotFound, "Remove: Key '" + key + "' not in dictionary."); return(null); } dictionary.Remove(key); return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.TEMPLATE_0 }, eval, false, ourType); classDef.AddMemberLiteral("Remove", newValue.valType, newValue); } //@ Dictionary<K, V> Set(K key, V newValue) // Replaces value of existing element with the given key. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { object key = args[0]; object value = args[1]; var dictionary = (thisScope as PebbleDictionary).dictionary; // Bounds checking. if (!dictionary.ContainsKey(key)) { context.SetRuntimeError(RuntimeErrorType.KeyNotFound, "Get: Key '" + key + "' not in dictionary."); return(null); } dictionary[key].value = value; return(thisScope); }; FunctionValue_Host newValue = new FunctionValue_Host(ourType, new ArgList { IntrinsicTypeDefs.TEMPLATE_0, IntrinsicTypeDefs.TEMPLATE_1 }, eval, false, ourType); classDef.AddMemberLiteral("Set", newValue.valType, newValue); } //@ string ThisToScript(string prefix) // ThisToScript is used by Serialize. A classes' ThisToScript function should return code which can rebuild the class. // Note that it's only the content of the class, not the "new A" part. ie., it's the code that goes in the defstructor. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { string result = ""; string prefix = (string)args[0] + "\t"; var dictionary = (thisScope as PebbleDictionary).dictionary; //bool first = true; foreach (KeyValuePair <object, Variable> kvp in dictionary) { result += prefix + "Add(" + CoreLib.ValueToScript(context, kvp.Key, prefix + "\t", false) + ", " + CoreLib.ValueToScript(context, kvp.Value.value, prefix + "\t", false) + ");\n"; } return(result); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList { IntrinsicTypeDefs.STRING }, eval, false, ourType); classDef.AddMemberLiteral("ThisToScript", newValue.valType, newValue); } //@ string ToString() // Returns a human readable version of at least the first few elements of the dictionary. { FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => { var dictionary = (thisScope as PebbleDictionary).dictionary; string result = "Dictionary(" + dictionary.Count + ")["; int count = 0; foreach (KeyValuePair <object, Variable> kvp in dictionary) { if (count != 0) { result += ", "; } result += "(" + CoreLib.ValueToString(context, kvp.Key, true) + ", " + CoreLib.ValueToString(context, kvp.Value.value, true) + ")"; if (++count >= 4) { break; } } if (dictionary.Count > 4) { result += ", ..."; } return(result + "]"); }; FunctionValue_Host newValue = new FunctionValue_Host(IntrinsicTypeDefs.STRING, new ArgList { }, eval, false, ourType); classDef.AddMemberLiteral("ThisToString", newValue.valType, newValue); } bool error = false; classDef.FinalizeClass(engine.defaultContext); Pb.Assert(!error); UnitTests.testFuncDelegates.Add("CoreDictionary", RunTests); }