public static GenericScriptObject BindXNAMath() { var xna = new GenericScriptObject(); xna.SetProperty("v", Function.MakeSystemFunction("Vector3", Arguments.Args("x", "y", "z", Arguments.Optional("w")), "Create a new vector.", (context, arguments) => { if (arguments[3] == null) return new Vector3(AutoBind.NumericArgument(arguments[0]), AutoBind.NumericArgument(arguments[1]), AutoBind.NumericArgument(arguments[2])); else return new Vector4(AutoBind.NumericArgument(arguments[0]), AutoBind.NumericArgument(arguments[1]), AutoBind.NumericArgument(arguments[2]), AutoBind.NumericArgument(arguments[3])); })); xna.SetProperty("helper", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(MathHelper))); xna.SetProperty("matrix", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(Matrix))); xna.SetProperty("quat", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(Quaternion))); xna.SetProperty("v2", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(Vector2), true)); xna.SetProperty("v3", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(Vector3), true)); xna.SetProperty("v4", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(Vector4), true)); xna.SetProperty("rect", AutoBind.GenerateLazyBindingObjectForStaticLibrary(typeof(Rectangle), true)); return xna; }
private void SetupObjectFunctions() { AddFunction("members", "Lists all members of an object", (context, arguments) => { var obj = ArgumentType<ScriptObject>(arguments[0]); return obj.ListProperties(); }, Arguments.Arg("object")); AddFunction("record", "Create a new record.", (context, arguments) => { var obj = new GenericScriptObject(); var vars = AutoBind.ListArgument(arguments[0]); foreach (var item in vars) { var l = ArgumentType<ScriptObject>(item); if (l == null || l._children.Count != 2) throw new ScriptError("Record expects a list of pairs.", null); var arg = l._child(0) as ScriptObject; string mname = ""; if (arg != null && arg.gsp("@type") == "token") mname = arg.gsp("@token"); else mname = Evaluate(context, arg, true).ToString(); obj.SetProperty(mname, Evaluate(context, l._child(1))); } return obj; }, Arguments.Repeat(Arguments.Lazy("pairs"))); AddFunction("clone", "Clone a record.", (context, arguments) => { var r = new GenericScriptObject(arguments[0] as ScriptObject); foreach (var item in arguments[1] as ScriptList) { var list = item as ScriptList; if (list == null || list.Count != 2) throw new ScriptError("Record expects only pairs as arguments.", context.currentNode); r[ScriptObject.AsString(list[0])] = list[1]; } return r; }, Arguments.Arg("object"), Arguments.Mutator(Arguments.Repeat(Arguments.Optional("pairs")), "(@list value)")); AddFunction("set", "Set a member on an object.", (context, arguments) => { if (arguments[0] == null) return arguments[2]; return SetObjectProperty(context, arguments[0], ScriptObject.AsString(arguments[1]), arguments[2]); }, Arguments.Arg("object"), Arguments.Mutator(Arguments.Lazy("name"), "(@identifier-if-token value)"), Arguments.Arg("value")); AddFunction("multi-set", "Set multiple members of an object.", (context, arguments) => { var obj = ArgumentType<ScriptObject>(arguments[0]); var vars = AutoBind.ListArgument(arguments[1]); foreach (var item in vars) { var l = ArgumentType<ScriptObject>(item); if (l == null || l._children.Count != 2) throw new ScriptError("Multi-set expects a list of pairs.", null); var arg = l._child(0) as ScriptObject; string mname = ""; if (arg != null && arg.gsp("@type") == "token") mname = arg.gsp("@token"); else mname = Evaluate(context, arg, true).ToString(); SetObjectProperty(context, obj, mname, Evaluate(context, l._child(1))); } return obj; }, Arguments.Arg("object"), Arguments.Repeat(Arguments.Lazy("pairs"))); AddFunction("delete", "Deletes a property from an object.", (context, arguments) => { var value = (arguments[0] as ScriptObject)[ScriptObject.AsString(arguments[1])]; if (arguments[0] is Scope) (arguments[0] as Scope).PopVariable(ScriptObject.AsString(arguments[1])); else (arguments[0] as ScriptObject).DeleteProperty(ScriptObject.AsString(arguments[1])); return value; }, Arguments.Arg("object"), Arguments.Arg("property-name")); }
private void SetupFileFunctions() { var file_functions = new GenericScriptObject(); file_functions.SetProperty("open", Function.MakeSystemFunction("open", Arguments.Args("file-name", "mode"), "Opens a file", (context, arguments) => { var mode = AutoBind.StringArgument(arguments[1]).ToUpperInvariant(); if (mode == "READ") return System.IO.File.OpenText(AutoBind.StringArgument(arguments[0])); else if (mode == "WRITE") return System.IO.File.CreateText(AutoBind.StringArgument(arguments[0])); else if (mode == "APPEND") return System.IO.File.AppendText(AutoBind.StringArgument(arguments[0])); else context.RaiseNewError("Invalid mode specifier", context.currentNode); return null; })); file_functions.SetProperty("close", Function.MakeSystemFunction("close", Arguments.Args("file"), "Close a file.", (context, arguments) => { if (arguments[0] is System.IO.StreamReader) (arguments[0] as System.IO.StreamReader).Close(); else if (arguments[0] is System.IO.StreamWriter) (arguments[0] as System.IO.StreamWriter).Close(); else context.RaiseNewError("Argument is not a file.", context.currentNode); return null; })); file_functions.SetProperty("read-line", Function.MakeSystemFunction("read-line", Arguments.Args("file"), "Read a line from a file.", (context, arguments) => { var file = arguments[0] as System.IO.StreamReader; if (file == null) { context.RaiseNewError("Argument is not a read file.", context.currentNode); return null; } return file.ReadLine(); })); file_functions.SetProperty("read-all", Function.MakeSystemFunction("read-all", Arguments.Args("file"), "Read all of a file.", (context, arguments) => { var file = arguments[0] as System.IO.StreamReader; if (file == null) { context.RaiseNewError("Argument is not a read file.", context.currentNode); return null; } return file.ReadToEnd(); })); file_functions.SetProperty("write", Function.MakeSystemFunction("write", Arguments.Args("file", "text"), "Write to a file.", (context, arguments) => { var file = arguments[0] as System.IO.StreamWriter; if (file == null) { context.RaiseNewError("Argument is not a write file.", context.currentNode); return null; } file.Write(ScriptObject.AsString(arguments[1])); return null; })); file_functions.SetProperty("more", Function.MakeSystemFunction("more", Arguments.Args("file"), "Is there more to read in this file?", (context, arguments) => { var file = arguments[0] as System.IO.StreamReader; if (file == null) { context.RaiseNewError("Argument is not a read file.", context.currentNode); return null; } if (file.EndOfStream) return null; return true; })); AddGlobalVariable("file", c => file_functions); }