Skip to content

Commit dd0068c

Browse files
add dictionaries
1 parent 51475f3 commit dd0068c

6 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using SER.Code.ArgumentSystem.Arguments;
2+
using SER.Code.ArgumentSystem.BaseArguments;
3+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
4+
using SER.Code.MethodSystem.Methods.DictionaryMethods.Structures;
5+
6+
namespace SER.Code.MethodSystem.Methods.DictionaryMethods;
7+
8+
[UsedImplicitly]
9+
// ReSharper disable once InconsistentNaming
10+
public class Dict_AddMethod : SynchronousMethod
11+
{
12+
public override string Description => "Adds a key-value pair to a dictionary.";
13+
14+
public override Argument[] ExpectedArguments { get; } =
15+
[
16+
new ReferenceArgument<Dict>("dictionary"),
17+
new AnyValueArgument("key"),
18+
new AnyValueArgument("value")
19+
];
20+
21+
public override void Execute()
22+
{
23+
Args.GetReference<Dict>("dictionary").Add(Args.GetAnyValue("key"), Args.GetAnyValue("value"));
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using SER.Code.ArgumentSystem.Arguments;
2+
using SER.Code.ArgumentSystem.BaseArguments;
3+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
4+
using SER.Code.MethodSystem.Methods.DictionaryMethods.Structures;
5+
using SER.Code.ValueSystem;
6+
7+
namespace SER.Code.MethodSystem.Methods.DictionaryMethods;
8+
9+
[UsedImplicitly]
10+
// ReSharper disable once InconsistentNaming
11+
public class Dict_ContainsMethod : ReturningMethod<BoolValue>
12+
{
13+
public override string Description => "Returns true if the dictionary contains the provided key";
14+
15+
public override Argument[] ExpectedArguments { get; } =
16+
[
17+
new ReferenceArgument<Dict>("dictionary"),
18+
new AnyValueArgument("key")
19+
];
20+
21+
public override void Execute()
22+
{
23+
ReturnValue = Args.GetReference<Dict>("dictionary").ContainsKey(Args.GetAnyValue("key"));
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using SER.Code.ArgumentSystem.BaseArguments;
2+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
3+
using SER.Code.MethodSystem.MethodDescriptors;
4+
using SER.Code.MethodSystem.Methods.DictionaryMethods.Structures;
5+
6+
namespace SER.Code.MethodSystem.Methods.DictionaryMethods;
7+
8+
[UsedImplicitly]
9+
// ReSharper disable once InconsistentNaming
10+
public class Dict_EmptyMethod : ReferenceReturningMethod<Dict>, IAdditionalDescription
11+
{
12+
public override string Description => "Creates an empty dictionary.";
13+
14+
public string AdditionalDescription =>
15+
"Dictionary is a collection of unique keys (values) mapped to specific values. " +
16+
"Think of it like a real-world dictionary where the \"word\" is the key and the \"definition\" is the value. " +
17+
"E.g. you can map the value \"hello\" to the \"world\" value. " +
18+
"This data type is used by more experienced users, allowing for dynamic data storage.";
19+
20+
public override Argument[] ExpectedArguments { get; } = [];
21+
22+
public override void Execute()
23+
{
24+
ReturnValue = new Dict();
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using SER.Code.ArgumentSystem.Arguments;
2+
using SER.Code.ArgumentSystem.BaseArguments;
3+
using SER.Code.Exceptions;
4+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
5+
using SER.Code.MethodSystem.MethodDescriptors;
6+
using SER.Code.MethodSystem.Methods.DictionaryMethods.Structures;
7+
using SER.Code.ValueSystem.Other;
8+
9+
namespace SER.Code.MethodSystem.Methods.DictionaryMethods;
10+
11+
[UsedImplicitly]
12+
// ReSharper disable once InconsistentNaming
13+
public class Dict_GetMethod : ReturningMethod, ICanError
14+
{
15+
public override string Description => "Gets a value associated with a key from a dictionary.";
16+
17+
public override TypeOfValue Returns => new UnknownTypeOfValue();
18+
19+
public string[] ErrorReasons =>
20+
[
21+
"There is no key with the given name in the dictionary."
22+
];
23+
24+
public override Argument[] ExpectedArguments { get; } =
25+
[
26+
new ReferenceArgument<Dict>("dictionary"),
27+
new AnyValueArgument("key")
28+
];
29+
30+
public override void Execute()
31+
{
32+
var dictionary = Args.GetReference<Dict>("dictionary");
33+
var key = Args.GetAnyValue("key");
34+
35+
if (dictionary.TryGetValue(key, out var value))
36+
{
37+
ReturnValue = value;
38+
}
39+
else
40+
{
41+
throw new ScriptRuntimeError(this, $"Provided key '{key}' is not in the dictionary.");
42+
}
43+
}
44+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using SER.Code.ArgumentSystem.Arguments;
2+
using SER.Code.ArgumentSystem.BaseArguments;
3+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
4+
using SER.Code.MethodSystem.Methods.DictionaryMethods.Structures;
5+
6+
namespace SER.Code.MethodSystem.Methods.DictionaryMethods;
7+
8+
[UsedImplicitly]
9+
// ReSharper disable once InconsistentNaming
10+
public class Dict_RemoveMethod : SynchronousMethod
11+
{
12+
public override string Description => "Removes a key-value pair from a dictionary.";
13+
14+
public override Argument[] ExpectedArguments { get; } =
15+
[
16+
new ReferenceArgument<Dict>("dictionary"),
17+
new AnyValueArgument("key to remove")
18+
];
19+
20+
public override void Execute()
21+
{
22+
Args.GetReference<Dict>("dictionary").Remove(Args.GetAnyValue("key to remove"));
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using SER.Code.ValueSystem;
2+
3+
namespace SER.Code.MethodSystem.Methods.DictionaryMethods.Structures;
4+
5+
public class Dict : Dictionary<Value, Value>;

0 commit comments

Comments
 (0)