From 124349245321de28fc07593240f978775f560690 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 30 Nov 2018 12:07:18 +0100 Subject: [PATCH] Adds comments to basic library functions --- Upsilon/StandardLibraries/BasicFunctions.cs | 17 +++++++++-------- Upsilon/StandardLibraries/ScriptLibrary.cs | 17 +++++++++++++---- .../StandardLibraryScriptFunctionAttribute.cs | 4 +++- Upsilon/StandardLibraries/StaticScope.cs | 12 +++++++++--- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/Upsilon/StandardLibraries/BasicFunctions.cs b/Upsilon/StandardLibraries/BasicFunctions.cs index 6652b4d..e1a54b1 100644 --- a/Upsilon/StandardLibraries/BasicFunctions.cs +++ b/Upsilon/StandardLibraries/BasicFunctions.cs @@ -11,7 +11,8 @@ namespace Upsilon.StandardLibraries { internal class BasicFunctions : ScriptLibrary { - [StandardLibraryScriptFunction("assert")] + [StandardLibraryScriptFunction("assert", "Asserts that the parameter passed is true. Throws an exception if it is not true. " + + "Can take a message to show in the exception, otherwise throws with message \"assertion failed!\"")] public void Assert(ScriptBoolean boolean, ScriptString message = null) { if (!boolean) @@ -20,32 +21,32 @@ namespace Upsilon.StandardLibraries } } - [StandardLibraryScriptFunction("error")] + [StandardLibraryScriptFunction("error", "Throw an exception with given error message")] public void Error(ScriptString message) { throw new Exception(message.Value); } - [StandardLibraryScriptFunction("ipairs")] + [StandardLibraryScriptFunction("ipairs", "Iterates over an iterable variable, like a table, until it encounters a nil value.")] public IIterable UpTillNullPairs(IIterable table) { return new UpTillNullPairsScriptIterator(table); } - [StandardLibraryScriptFunction("pairs")] + [StandardLibraryScriptFunction("pairs", "Iterates over an iterable variable, like a table, skipping all nil values.")] public IIterable Pairs(IIterable table) { return new PairsScriptIterator(table); } - [StandardLibraryScriptFunction("require", true)] + [StandardLibraryScriptFunction("require", "Loads a module from the module path, using the given script loader.", true)] public void Require(Script script, string file) { } - [StandardLibraryScriptFunction("tonumber")] + [StandardLibraryScriptFunction("tonumber", "Parses a string to a number.")] public ScriptNumber ToNumber(ScriptString obj) { var str = obj.Value; @@ -59,13 +60,13 @@ namespace Upsilon.StandardLibraries } } - [StandardLibraryScriptFunction("tostring")] + [StandardLibraryScriptFunction("tostring", "Returns the string value of the given object.")] public ScriptString ToString(ScriptType obj) { return new ScriptString(obj.ToString()); } - [StandardLibraryScriptFunction("type")] + [StandardLibraryScriptFunction("type", "Returns the type of the given object as a string.")] public ScriptString Type(ScriptType obj) { return new ScriptString(obj.Type.ToString()); diff --git a/Upsilon/StandardLibraries/ScriptLibrary.cs b/Upsilon/StandardLibraries/ScriptLibrary.cs index 5a9e88f..ec20497 100644 --- a/Upsilon/StandardLibraries/ScriptLibrary.cs +++ b/Upsilon/StandardLibraries/ScriptLibrary.cs @@ -8,17 +8,20 @@ namespace Upsilon.StandardLibraries { internal abstract class ScriptLibrary { - public Dictionary LoadMethods() + public Dictionary LoadMethods() { - var dictionary = new Dictionary(); + var dictionary = new Dictionary(); var methods = GetType().GetMethods(); foreach (var methodInfo in methods) { var attr = methodInfo.GetCustomAttribute(); if (attr != null) { - dictionary.Add(attr.Name, - new ScriptMethodInfoFunction(new UserDataMethod(methodInfo), this, true)); + dictionary.Add(attr.Name, new LoadedStandardFunction() + { + MethodInfoFunction = new ScriptMethodInfoFunction(new UserDataMethod(methodInfo), this, true), + CommentValue = attr.Comment + }); } } @@ -26,4 +29,10 @@ namespace Upsilon.StandardLibraries } } + + internal class LoadedStandardFunction + { + public ScriptMethodInfoFunction MethodInfoFunction { get; set; } + public string CommentValue { get; set; } + } } \ No newline at end of file diff --git a/Upsilon/StandardLibraries/StandardLibraryScriptFunctionAttribute.cs b/Upsilon/StandardLibraries/StandardLibraryScriptFunctionAttribute.cs index c368c84..0426507 100644 --- a/Upsilon/StandardLibraries/StandardLibraryScriptFunctionAttribute.cs +++ b/Upsilon/StandardLibraries/StandardLibraryScriptFunctionAttribute.cs @@ -5,13 +5,15 @@ namespace Upsilon.StandardLibraries [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public class StandardLibraryScriptFunctionAttribute : Attribute { - public StandardLibraryScriptFunctionAttribute(string name, bool passScriptReference = false) + public StandardLibraryScriptFunctionAttribute(string name, string comment = null, bool passScriptReference = false) { Name = name; + Comment = comment; PassScriptReference = passScriptReference; } public string Name { get; } + public string Comment { get; } public bool PassScriptReference { get; } } } \ No newline at end of file diff --git a/Upsilon/StandardLibraries/StaticScope.cs b/Upsilon/StandardLibraries/StaticScope.cs index b7c51a2..9101eb9 100644 --- a/Upsilon/StandardLibraries/StaticScope.cs +++ b/Upsilon/StandardLibraries/StaticScope.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -47,9 +48,14 @@ namespace Upsilon.StandardLibraries var boundFuncs = new Dictionary(); foreach (var func in basicFunctions) { - funcs.Add(func.Key, func.Value); - boundFuncs.Add(func.Key, new FunctionVariableSymbol(func.Key, true, ImmutableArray.Empty, - func.Value.ReturnType.GetScriptType(), true){IsBound = true}); + funcs.Add(func.Key, func.Value.MethodInfoFunction); + var functionSymbol = new FunctionVariableSymbol(func.Key, true, ImmutableArray.Empty, + func.Value.MethodInfoFunction.ReturnType.GetScriptType(), true) + { + IsBound = true, + CommentValue = func.Value.CommentValue?.Split('\n') + }; + boundFuncs.Add(func.Key, functionSymbol); } var scope = new EvaluationScope(funcs); var boundScope = new BoundScope(boundFuncs, null);