Adds comments to basic library functions

This commit is contained in:
Deukhoofd 2018-11-30 12:07:18 +01:00
parent 1f1b8c621e
commit 1243492453
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 34 additions and 16 deletions

View File

@ -11,7 +11,8 @@ namespace Upsilon.StandardLibraries
{ {
internal class BasicFunctions : ScriptLibrary 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) public void Assert(ScriptBoolean boolean, ScriptString message = null)
{ {
if (!boolean) 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) public void Error(ScriptString message)
{ {
throw new Exception(message.Value); 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) public IIterable UpTillNullPairs(IIterable table)
{ {
return new UpTillNullPairsScriptIterator(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) public IIterable Pairs(IIterable table)
{ {
return new PairsScriptIterator(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) public void Require(Script script, string file)
{ {
} }
[StandardLibraryScriptFunction("tonumber")] [StandardLibraryScriptFunction("tonumber", "Parses a string to a number.")]
public ScriptNumber ToNumber(ScriptString obj) public ScriptNumber ToNumber(ScriptString obj)
{ {
var str = obj.Value; 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) public ScriptString ToString(ScriptType obj)
{ {
return new ScriptString(obj.ToString()); return new ScriptString(obj.ToString());
} }
[StandardLibraryScriptFunction("type")] [StandardLibraryScriptFunction("type", "Returns the type of the given object as a string.")]
public ScriptString Type(ScriptType obj) public ScriptString Type(ScriptType obj)
{ {
return new ScriptString(obj.Type.ToString()); return new ScriptString(obj.Type.ToString());

View File

@ -8,17 +8,20 @@ namespace Upsilon.StandardLibraries
{ {
internal abstract class ScriptLibrary internal abstract class ScriptLibrary
{ {
public Dictionary<string, ScriptMethodInfoFunction> LoadMethods() public Dictionary<string, LoadedStandardFunction> LoadMethods()
{ {
var dictionary = new Dictionary<string, ScriptMethodInfoFunction>(); var dictionary = new Dictionary<string, LoadedStandardFunction>();
var methods = GetType().GetMethods(); var methods = GetType().GetMethods();
foreach (var methodInfo in methods) foreach (var methodInfo in methods)
{ {
var attr = methodInfo.GetCustomAttribute<StandardLibraryScriptFunctionAttribute>(); var attr = methodInfo.GetCustomAttribute<StandardLibraryScriptFunctionAttribute>();
if (attr != null) if (attr != null)
{ {
dictionary.Add(attr.Name, dictionary.Add(attr.Name, new LoadedStandardFunction()
new ScriptMethodInfoFunction(new UserDataMethod(methodInfo), this, true)); {
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; }
}
} }

View File

@ -5,13 +5,15 @@ namespace Upsilon.StandardLibraries
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class StandardLibraryScriptFunctionAttribute : Attribute public class StandardLibraryScriptFunctionAttribute : Attribute
{ {
public StandardLibraryScriptFunctionAttribute(string name, bool passScriptReference = false) public StandardLibraryScriptFunctionAttribute(string name, string comment = null, bool passScriptReference = false)
{ {
Name = name; Name = name;
Comment = comment;
PassScriptReference = passScriptReference; PassScriptReference = passScriptReference;
} }
public string Name { get; } public string Name { get; }
public string Comment { get; }
public bool PassScriptReference { get; } public bool PassScriptReference { get; }
} }
} }

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Linq; using System.Linq;
@ -47,9 +48,14 @@ namespace Upsilon.StandardLibraries
var boundFuncs = new Dictionary<string, VariableSymbol>(); var boundFuncs = new Dictionary<string, VariableSymbol>();
foreach (var func in basicFunctions) foreach (var func in basicFunctions)
{ {
funcs.Add(func.Key, func.Value); funcs.Add(func.Key, func.Value.MethodInfoFunction);
boundFuncs.Add(func.Key, new FunctionVariableSymbol(func.Key, true, ImmutableArray<VariableSymbol>.Empty, var functionSymbol = new FunctionVariableSymbol(func.Key, true, ImmutableArray<VariableSymbol>.Empty,
func.Value.ReturnType.GetScriptType(), true){IsBound = true}); 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 scope = new EvaluationScope(funcs);
var boundScope = new BoundScope(boundFuncs, null); var boundScope = new BoundScope(boundFuncs, null);