Skip to content

Commit d9f5714

Browse files
committed
SIFOSC、配列から値を取得する。
1 parent 8f5e231 commit d9f5714

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

FastChatProtocolInterface/SimpleFormulaScript/SifoscArray.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,16 @@ protected override bool PrintMembers(StringBuilder builder)
7979

8080
return null;
8181
}
82+
83+
public override SifoscObject? Get(SifoscObject? other)
84+
{
85+
if (other is SifoscInteger index &&
86+
0 <= index.Value &&
87+
index.Value < this.Values.Length) {
88+
return this.Values.Span[unchecked((int)(index.Value))];
89+
}
90+
91+
return null;
92+
}
8293
}
8394
}

FastChatProtocolInterface/SimpleFormulaScript/SifoscObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Concurrent;
1010
using System.Collections.Generic;
11+
using System.Net.Http.Headers;
1112
using System.Threading;
1213

1314
namespace FastChatProtocolInterface.SimpleFormulaScript
@@ -49,6 +50,9 @@ private ulong CreateIdentifier()
4950
public static IEnumerable<SifoscObject> EnumerateAllObjects()
5051
=> _objects.Values;
5152

53+
public static SifoscObject? GetObject(ulong id)
54+
=> _objects.TryGetValue(id, out var result) ? result : null;
55+
5256
public virtual SifoscObject? Plus () => null;
5357
public virtual SifoscObject? Minus () => null;
5458
public virtual SifoscObject? Negate () => null;
@@ -60,6 +64,7 @@ public static IEnumerable<SifoscObject> EnumerateAllObjects()
6064
public virtual SifoscObject? And (SifoscObject? other) => null;
6165
public virtual SifoscObject? Or (SifoscObject? other) => null;
6266
public virtual SifoscObject? Xor (SifoscObject? other) => null;
67+
public virtual SifoscObject? Get (SifoscObject? other) => null;
6368
}
6469

6570
public sealed record SifoscNull : SifoscObject

FastChatProtocolInterface/SimpleFormulaScript/SifoscParser.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public static bool TryParseValueExpression(this SourceCode sc, [NotNullWhen(true
178178
sc.SkipSpaces();
179179

180180
return sc.TryParseParenthesisExpression(out result)
181+
|| sc.TryParseGetExpression (out result)
181182
|| sc.TryParseSignExpression (out result)
182183
|| sc.TryParseNotExpression (out result)
183184
|| sc.TryParseValueLiteral (out result);
@@ -200,6 +201,34 @@ public static bool TryParseParenthesisExpression(this SourceCode sc, [NotNullWhe
200201
return false;
201202
}
202203

204+
public static bool TryParseGetExpression(this SourceCode sc, [NotNullWhen(true)][MaybeNullWhen(false)] out SifoscObject? result)
205+
{
206+
sc.BeginScope();
207+
208+
if (sc.TryParseValueExpression(out var left)) {
209+
sc.SkipSpaces();
210+
if (sc.AdvanceIf('.') && sc.TryParseValueExpression(out var right)) {
211+
result = left.Get(right);
212+
213+
if (result is null) {
214+
goto fail;
215+
} else {
216+
goto succeed;
217+
}
218+
}
219+
}
220+
221+
result = null;
222+
223+
fail:
224+
sc.EndScope(true);
225+
return false;
226+
227+
succeed:
228+
sc.EndScope(false);
229+
return true;
230+
}
231+
203232
public static bool TryParseSignExpression(this SourceCode sc, [NotNullWhen(true)][MaybeNullWhen(false)] out SifoscObject? result)
204233
{
205234
sc.BeginScope();

FastChatProtocolInterface/SimpleFormulaScript/SifoscViewForAllObjects.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,14 @@ public void PrintMembersSimply(StringBuilder builder)
5151

5252
builder.Append('}');
5353
}
54+
55+
public override SifoscObject? Get(SifoscObject? other)
56+
{
57+
if (other is SifoscInteger id) {
58+
return GetObject(unchecked((ulong)(id.Value)));
59+
}
60+
61+
return null;
62+
}
5463
}
5564
}

0 commit comments

Comments
 (0)