Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ranges module #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/WattleScript.Interpreter/CoreLib/PrototypeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace WattleScript.Interpreter.CoreLib
[WattleScriptModule(Namespace = "prototype")]
public class PrototypeModule
{
private const string NUMBER_PROTOTABLE = "681bf104-NUMBER-PROTO";
private const string BOOLEAN_PROTOTABLE = "77813cb0-BOOLEAN-PROTO";
private const string RANGE_PROTOTABLE = "88c45cc7-RANGE-PROTO";
internal const string NUMBER_PROTOTABLE = "681bf104-NUMBER-PROTO";
internal const string BOOLEAN_PROTOTABLE = "77813cb0-BOOLEAN-PROTO";
internal const string RANGE_PROTOTABLE = "88c45cc7-RANGE-PROTO";

public static void WattleScriptInit(Table globalTable, Table proto)
{
Expand All @@ -30,8 +30,10 @@ public static void EnablePrototypes(Table globalTable)
void Register(string prototableIdent, DataType protoType)
{
//register
var tab = new Table(sc);
var funcs = new Table(sc);
Table tab = new Table(sc);
DynValue mFuncs = sc.Registry.Get(prototableIdent);
Table funcs = mFuncs.IsNil() ? new Table(sc) : mFuncs.Table;

tab.Set("__index", DynValue.NewTable(funcs));
sc.SetTypeMetatable(protoType, tab);
sc.Registry.Set(prototableIdent, DynValue.NewTable(funcs));
Expand Down
106 changes: 106 additions & 0 deletions src/WattleScript.Interpreter/CoreLib/RangesModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;

namespace WattleScript.Interpreter.CoreLib
{
/// <summary>
/// Class implementing string Wattle & Lua functions
/// </summary>
[WattleScriptModule(Namespace = "range")]
public class RangesModule
{
public static void WattleScriptInit(Table globalTable, Table rangesTable)
{
globalTable.OwnerScript.Registry.Set(PrototypeModule.RANGE_PROTOTABLE, DynValue.NewTable(rangesTable));

Table stringMetatable = new Table(globalTable.OwnerScript);
stringMetatable.Set("__index", DynValue.NewTable(rangesTable));
globalTable.OwnerScript.SetTypeMetatable(DataType.Range, stringMetatable);
}

[WattleScriptModuleMethod]
public static DynValue totable(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;
Table table = new Table(executionContext.OwnerScript);

if (range.From < range.To)
{
for (int i = range.From; i <= range.To; i++)
{
table.Append(DynValue.NewNumber(i));
}
}
else if (range.From > range.To)
{
for (int i = range.To; i >= range.From; i--)
{
table.Append(DynValue.NewNumber(i));
}
}

return DynValue.NewTable(table);
}

[WattleScriptModuleMethod]
public static DynValue sort(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;

if (range.From > range.To)
{
(range.To, range.From) = (range.From, range.To);
}

return rangeDv;
}

[WattleScriptModuleMethod]
public static DynValue sum(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;

return DynValue.NewNumber(range.From + range.To);
}

[WattleScriptModuleMethod]
public static DynValue swap(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;

(range.To, range.From) = (range.From, range.To);

return rangeDv;
}

[WattleScriptModuleMethod]
public static DynValue issorted(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;

return DynValue.NewBoolean(range.From <= range.To);
}

[WattleScriptModuleMethod]
public static DynValue max(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;

return DynValue.NewNumber(Math.Max(range.From, range.To));
}

[WattleScriptModuleMethod]
public static DynValue min(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue rangeDv = args.AsType(0, "table", DataType.Range);
Range range = rangeDv.Range;

return DynValue.NewNumber(Math.Min(range.From, range.To));
}
}
}
14 changes: 10 additions & 4 deletions src/WattleScript.Interpreter/DataTypes/DynValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,17 @@ public string ToPrintString()

string typeString = this.Type.ToLuaTypeString();

if (m_Object is UserData ud)
switch (m_Object)
{
string str = ud.Descriptor.AsString(ud.Object);
if (str != null)
return str;
case UserData ud:
{
string str = ud.Descriptor.AsString(ud.Object);
if (str != null)
return str;
break;
}
case Range range:
return $"{range.From}..{range.To}";
}

return refid.FormatTypeString(typeString);
Expand Down
14 changes: 11 additions & 3 deletions src/WattleScript.Interpreter/Modules/CoreModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public enum CoreModules
/// Prototype table for changing types. PrototypeMethods must also be set.
/// </summary>
PrototypeTable = 0x40000,
/// <summary>
/// The "ranges" package (introduced by WattleScript).
/// </summary>
Ranges = 0x80000,

/// <summary>
/// A sort of "hard" sandbox preset, including string, math, table, bit32 packages, constants and table iterators.
Expand All @@ -105,17 +109,21 @@ public enum CoreModules
/// <summary>
/// A softer sandbox preset + prototypes, adding metatables support, error handling, coroutine, time functions, json parsing and dynamic evaluations.
/// </summary>
Preset_SoftSandboxWattle = Preset_SoftSandbox | PrototypeMethods | PrototypeTable,
Preset_SoftSandboxWattle = Preset_SoftSandbox | PrototypeMethods | PrototypeTable | Ranges,
/// <summary>
/// Shared base of <see cref="Preset_DefaultWattle"/> and <see cref="Preset_Default"/>
/// </summary>
Preset_DefaultShared = LoadMethods | OS_System | IO,
/// <summary>
/// The default preset. Includes everything except "debug" as now.
/// Beware that using this preset allows scripts unlimited access to the system.
/// </summary>
Preset_Default = Preset_SoftSandbox | LoadMethods | OS_System | IO,
Preset_Default = Preset_SoftSandbox | Preset_DefaultShared,
/// <summary>
/// The default preset + prototypes. Includes everything except "debug" as now.
/// Beware that using this preset allows scripts unlimited access to the system.
/// </summary>
Preset_DefaultWattle = Preset_Default | PrototypeMethods | PrototypeTable,
Preset_DefaultWattle = Preset_SoftSandboxWattle | Preset_DefaultShared | PrototypeMethods | PrototypeTable,
/// <summary>
/// The complete package.
/// Beware that using this preset allows scripts unlimited access to the system.
Expand Down
2 changes: 2 additions & 0 deletions src/WattleScript.Interpreter/Modules/ModuleRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public static Table RegisterCoreModules(this Table table, CoreModules modules)
if (modules.Has(CoreModules.IO)) RegisterModuleType<IoModule>(table);
if (modules.Has(CoreModules.Debug)) RegisterModuleType<DebugModule>(table);
if (modules.Has(CoreModules.Json)) RegisterModuleType<JsonModule>(table);
if (modules.Has(CoreModules.Ranges)) RegisterModuleType<RangesModule>(table);
if (modules.Has(CoreModules.PrototypeMethods)) {
PrototypeModule.EnablePrototypes(table);
if (modules.Has(CoreModules.PrototypeTable))
{
RegisterModuleType<PrototypeModule>(table);
}
}

return table;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tbl = range.totable(1>..5)
print(tbl[0]);
print(tbl[1]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2
3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 2>..<5
print(x.totable()[1])
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a = 10
b = 2

x = (a..b).sort()
print(x.from)
print(x.to)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2
10
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = (10..2).sort()
print(x.from)
print(x.to)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2
10
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = (4..8).swap()
print(x.from)
print(x.to)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = (10..20).sum()
print(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
30
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x = (10..20).issorted()
y = (2..1).issorted()
z = (1..1).issorted()
print(x)
print(y)
print(z)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
true
false
true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = (4..2).max()
print(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = (4..2).min()
print(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2