Skip to content

Commit

Permalink
add some more string docs, document table module
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 13, 2023
1 parent 789304e commit f7e0396
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 36 deletions.
58 changes: 42 additions & 16 deletions VirtualMachine/Ceres.Runtime/Modules/StringModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ private static DynamicValue Repeat(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("index_of")]
[EvilDocFunction(
"Finds the zero-based starting index of the first occurrence of `needle` in `haystack`.",
Returns = "The starting index of the first occurrence of `needle` in `haystack`, or `-1` if not found.",
ReturnType = DynamicValueType.Number
)]
[EvilDocArgument("haystack", "A String to be searched through.", DynamicValueType.String)]
[EvilDocArgument("needle", "A String to seek for.", DynamicValueType.String)]
private static DynamicValue IndexOf(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(2)
Expand All @@ -171,6 +178,13 @@ private static DynamicValue IndexOf(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("last_index_of")]
[EvilDocFunction(
"Finds the zero-based starting index of the last occurrence of `needle` in `haystack`.",
Returns = "The starting index of the last occurrence of `needle` in `haystack`, or `-1` if not found.",
ReturnType = DynamicValueType.Number
)]
[EvilDocArgument("haystack", "A String to be searched through.", DynamicValueType.String)]
[EvilDocArgument("needle", "A String to seek for.", DynamicValueType.String)]
private static DynamicValue LastIndexOf(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(2)
Expand All @@ -180,44 +194,56 @@ private static DynamicValue LastIndexOf(Fiber _, params DynamicValue[] args)
return haystack.LastIndexOf(needle, StringComparison.InvariantCulture);
}

[RuntimeModuleFunction("is_empty")]
private static DynamicValue IsEmpty(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
.ExpectStringAt(0, out var value);

return string.IsNullOrEmpty(value);
}

[RuntimeModuleFunction("is_whitespace")]
[EvilDocFunction(
"Checks if the provided String consists only of whitespace charracters.",
Returns = "`true` if the provided String consists only of whitespace characterrs, `false` otherwise.",
ReturnType = DynamicValueType.Boolean)]
[EvilDocArgument("str", "A String to be checked.", DynamicValueType.String)]
private static DynamicValue IsWhiteSpace(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
.ExpectStringAt(0, out var value);
.ExpectStringAt(0, out var str);

return string.IsNullOrWhiteSpace(value);
return string.IsNullOrWhiteSpace(str);
}

[RuntimeModuleFunction("lpad")]
[EvilDocFunction(
"Pads a shorter `str` so that its length matches `total_width` by appeding `padding_char` to its left side.",
Returns = "A String padded in the way described above.",
ReturnType = DynamicValueType.String
)]
[EvilDocArgument("str", "A String to be padded.", DynamicValueType.String)]
[EvilDocArgument("padding_char", "A character to be used for padding.", DynamicValueType.String)]
[EvilDocArgument("total_width", "Total length of the string to be matched.", DynamicValueType.Number)]
private static DynamicValue LeftPad(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(3)
.ExpectStringAt(0, out var source)
.ExpectCharAt(1, out var pad)
.ExpectStringAt(0, out var str)
.ExpectCharAt(1, out var paddingChar)
.ExpectIntegerAt(2, out var totalWidth);

return source.PadLeft((int)totalWidth, pad);
return str.PadLeft((int)totalWidth, paddingChar);
}

[RuntimeModuleFunction("rpad")]
[EvilDocFunction(
"Pads a shorter `str` so that its length matches `total_width` by appeding `padding_char` to its right side.",
Returns = "A String padded in the way described above.",
ReturnType = DynamicValueType.String
)]
[EvilDocArgument("str", "A String to be padded.", DynamicValueType.String)]
[EvilDocArgument("padding_char", "A character to be used for padding.", DynamicValueType.String)]
[EvilDocArgument("total_width", "Total length of the string to be matched.", DynamicValueType.Number)]
private static DynamicValue RightPad(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(3)
.ExpectStringAt(0, out var source)
.ExpectStringAt(0, out var str)
.ExpectCharAt(1, out var pad)
.ExpectIntegerAt(2, out var totalWidth);

return source.PadRight((int)totalWidth, pad);
return str.PadRight((int)totalWidth, pad);
}

[RuntimeModuleFunction("trim")]
Expand Down
62 changes: 62 additions & 0 deletions VirtualMachine/Ceres.Runtime/Modules/TableModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Ceres.ExecutionEngine.Concurrency;
using Ceres.ExecutionEngine.TypeSystem;
using Ceres.Runtime.Extensions;
using EVIL.CommonTypes.TypeSystem;

namespace Ceres.Runtime.Modules
{
Expand All @@ -9,6 +10,9 @@ public class TableModule : RuntimeModule
public override string FullyQualifiedName => "tbl";

[RuntimeModuleFunction("clear")]
[EvilDocFunction(
"Removes all values from the provided table."
)]
private static DynamicValue Clear(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
Expand All @@ -20,6 +24,14 @@ private static DynamicValue Clear(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("rawset")]
[EvilDocFunction(
"Sets the given Table's key to the provided value, ignoring any script-defined `__set` override.",
Returns = "The value that has been set.",
IsAnyReturn = true
)]
[EvilDocArgument("table", "A Table whose key is to be set.", DynamicValueType.Table)]
[EvilDocArgument("key", "A key to use when setting the value.", CanBeNil = true)]
[EvilDocArgument("value", "A value to set the Table's key to.", CanBeNil = true)]
private static DynamicValue RawSet(Fiber _, params DynamicValue[] args)
{
args.ExpectTableAt(0, out var table)
Expand All @@ -31,6 +43,13 @@ private static DynamicValue RawSet(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("rawget")]
[EvilDocFunction(
"Gets the given Table's key to the provided value, ignoring any script-defined `__get` override.",
Returns = "The value that resides in the provided Table under the given key.",
IsAnyReturn = true
)]
[EvilDocArgument("table", "A Table whose value to retrieve.", DynamicValueType.Table)]
[EvilDocArgument("key", "A key to use when retrieving the value.", CanBeNil = true)]
private static DynamicValue RawGet(Fiber _, params DynamicValue[] args)
{
args.ExpectTableAt(0, out var table)
Expand All @@ -40,6 +59,12 @@ private static DynamicValue RawGet(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("freeze")]
[EvilDocFunction(
"Freezes the provided Table so that no keys can be set or removed.",
Returns = "The frozen Table. This value is returned by reference.",
ReturnType = DynamicValueType.Table
)]
[EvilDocArgument("table", "A Table to be frozen.", DynamicValueType.Table)]
private static DynamicValue Freeze(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
Expand All @@ -51,6 +76,12 @@ private static DynamicValue Freeze(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("unfreeze")]
[EvilDocFunction(
"Unfreezes the provided Table so that keys can be set or removed.",
Returns = "The unfrozen Table. This value is returned by reference.",
ReturnType = DynamicValueType.Table
)]
[EvilDocArgument("table", "A Table to be unfrozen.", DynamicValueType.Table)]
private static DynamicValue Unfreeze(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
Expand All @@ -62,6 +93,12 @@ private static DynamicValue Unfreeze(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("is_frozen")]
[EvilDocFunction(
"Checks if the given Table is frozen.",
Returns = "`true` if the provided Table is frozen, `false` otherwise.",
ReturnType = DynamicValueType.Boolean
)]
[EvilDocArgument("table", "A Table whose freeze status to check.", DynamicValueType.Table)]
private static DynamicValue /*IsFrozen is already a symbol...*/ _IsFrozen(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
Expand All @@ -71,6 +108,12 @@ private static DynamicValue Unfreeze(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("keys")]
[EvilDocFunction(
"Retrieves all keys present in the given Table.",
Returns = "An Array containing all keys in the provided Table, in no particular order.",
ReturnType = DynamicValueType.Array
)]
[EvilDocArgument("table", "A Table whose keys to retrieve.", DynamicValueType.Table)]
private static DynamicValue Keys(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
Expand All @@ -80,6 +123,12 @@ private static DynamicValue Keys(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("values")]
[EvilDocFunction(
"Retrieves all values present in the given Table.",
Returns = "An Array containing all values in the provided Table, in no particular order.",
ReturnType = DynamicValueType.Array
)]
[EvilDocArgument("table", "A Table whose keys to retrieve.", DynamicValueType.Table)]
private static DynamicValue Values(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
Expand All @@ -89,6 +138,19 @@ private static DynamicValue Values(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("cpy")]
[EvilDocFunction(
"Copies the given Table, returning a new instance.",
Returns = "A new Table containing the values from the original one.",
ReturnType = DynamicValueType.Table
)]
[EvilDocArgument("table", "A Table to be copied.", DynamicValueType.Table)]
[EvilDocArgument(
"deep",
"Whether the copy should be deep (nested Tables are also completely copied) " +
"or shallow (nested Table references are copied).",
DynamicValueType.Boolean,
DefaultValue = "false"
)]
private static DynamicValue Copy(Fiber _, params DynamicValue[] args)
{
args.ExpectTableAt(0, out var table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static class RuntimeModuleDocExtensions
sb.AppendLine("**Description** ");
sb.AppendLine(evilDocFunctionAttribute.Description);

if (string.IsNullOrEmpty(evilDocFunctionAttribute.Returns))
if (!string.IsNullOrEmpty(evilDocFunctionAttribute.Returns))
{
sb.AppendLine();
sb.AppendLine("**Returns** ");
Expand Down
36 changes: 17 additions & 19 deletions VirtualMachine/Ceres/ExecutionEngine/Collections/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DynamicValue GetOverride(TableOverride op)
{
if (_overrides.TryGetValue(op, out var chunk))
return chunk;

return Nil;
}

Expand Down Expand Up @@ -95,7 +95,7 @@ protected virtual (DynamicValue Key, DynamicValue Value) OnSet(DynamicValue key,
return (key, value);
}

public DynamicValue Index(DynamicValue key)
public DynamicValue Index(DynamicValue key)
=> OnIndex(key);

protected virtual DynamicValue OnIndex(DynamicValue key)
Expand Down Expand Up @@ -168,38 +168,36 @@ public Table Unfreeze(bool deep = false)
return this;
}

public Table GetKeys()
public Array GetKeys()
{
var keys = new Table();

lock (_values)
{
var keys = new Array(_values.Count);
var i = 0;

foreach (var kvp in _values)
{
keys.Set(i++, kvp.Key);
keys[i++] = kvp.Key;
}
}

return keys;
return keys;
}
}

public Table GetValues()
public Array GetValues()
{
var values = new Table();

lock (_values)
{
var values = new Array(_values.Count);
var i = 0;

foreach (var kvp in _values)
{
values.Set(i++, kvp.Value);
values[i++] = kvp.Value;
}

return values;
}

return values;
}

public Table ShallowCopy()
Expand All @@ -208,8 +206,8 @@ public Table ShallowCopy()

foreach (var ovr in _overrides)
copy._overrides.TryAdd(ovr.Key, ovr.Value);
foreach (var kvp in this)

foreach (var kvp in this)
copy[kvp.Key] = kvp.Value;

return copy;
Expand All @@ -218,7 +216,7 @@ public Table ShallowCopy()
public Table DeepCopy()
{
var copy = new Table();

foreach (var ovr in _overrides)
copy.SetOverride(ovr.Key, ovr.Value);

Expand All @@ -233,15 +231,15 @@ public Table DeepCopy()
copy[kvp.Key] = kvp.Value;
}
}

return copy;
}

public bool IsDeeplyEqualTo(Table other)
{
if (Length != other.Length)
return false;

lock (_values)
{
for (var i = 0; i < _values.Keys.Count; i++)
Expand Down

0 comments on commit f7e0396

Please sign in to comment.