Skip to content

Commit 77e9f05

Browse files
committed
SifoscObject.cs を分割
1 parent 8f9f9d1 commit 77e9f05

File tree

4 files changed

+204
-171
lines changed

4 files changed

+204
-171
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/****
2+
* FACHPI: Fast Chat Protocol/Interface
3+
* Copyright (C) 2023 Takym.
4+
*
5+
* distributed under the MIT License.
6+
****/
7+
8+
using System;
9+
using System.Text;
10+
11+
namespace FastChatProtocolInterface.SimpleFormulaScript
12+
{
13+
public sealed record SifoscArray : SifoscObject
14+
{
15+
public override string Code
16+
{
17+
get
18+
{
19+
var sb = new StringBuilder();
20+
var span = this.Values.Span;
21+
22+
sb.Append('[');
23+
for (int i = 0; i < span.Length; ++i) {
24+
var obj = span[i];
25+
if (obj is null) {
26+
continue;
27+
}
28+
if (i != 0) {
29+
sb.Append(',');
30+
}
31+
sb.Append(obj.Code);
32+
}
33+
sb.Append(']');
34+
35+
return sb.ToString();
36+
}
37+
}
38+
39+
public ReadOnlyMemory<SifoscObject?> Values { get; init; }
40+
41+
protected override bool PrintMembers(StringBuilder builder)
42+
{
43+
// 参考:https://qiita.com/muniel/items/fd843abc55a5626e5c45
44+
45+
bool appendComma = base.PrintMembers(builder);
46+
47+
var s = this.Values.Span;
48+
for (int i = 0; i < s.Length; ++i) {
49+
if (appendComma) {
50+
builder.Append(", ");
51+
}
52+
var obj = s[i];
53+
if (obj is SifoscViewForAllObjects view) {
54+
view.PrintMembersSimply(builder);
55+
} else {
56+
builder.Append(obj);
57+
}
58+
appendComma = true;
59+
}
60+
return appendComma;
61+
}
62+
63+
public override SifoscObject? Add(SifoscObject? other)
64+
{
65+
if (other is SifoscArray otherArray) {
66+
int otherLen = otherArray.Values.Length;
67+
if (otherLen == 0) {
68+
return this;
69+
}
70+
71+
int thisLen = this.Values.Length;
72+
var newArray = new SifoscObject?[thisLen + otherLen];
73+
74+
this.Values.CopyTo(newArray);
75+
otherArray.Values.CopyTo(newArray.AsMemory()[thisLen..]);
76+
77+
return this with { Values = newArray };
78+
}
79+
80+
return null;
81+
}
82+
}
83+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/****
2+
* FACHPI: Fast Chat Protocol/Interface
3+
* Copyright (C) 2023 Takym.
4+
*
5+
* distributed under the MIT License.
6+
****/
7+
8+
namespace FastChatProtocolInterface.SimpleFormulaScript
9+
{
10+
public sealed record SifoscInteger : SifoscObject
11+
{
12+
public long Value { get; init; }
13+
public override string Code => this.Value.ToString();
14+
15+
public override SifoscObject? Plus()
16+
=> this;
17+
18+
public override SifoscObject? Minus()
19+
=> this with { Value = -this.Value };
20+
21+
public override SifoscObject? Add(SifoscObject? other)
22+
{
23+
if (other is SifoscInteger otherInt) {
24+
return this with { Value = this.Value + otherInt.Value };
25+
}
26+
27+
return null;
28+
}
29+
30+
public override SifoscObject? Subtract(SifoscObject? other)
31+
{
32+
if (other is SifoscInteger otherInt) {
33+
return this with { Value = this.Value - otherInt.Value };
34+
}
35+
36+
return null;
37+
}
38+
39+
public override SifoscObject? Multiply(SifoscObject? other)
40+
{
41+
if (other is SifoscInteger otherInt) {
42+
return this with { Value = this.Value * otherInt.Value };
43+
}
44+
45+
return null;
46+
}
47+
48+
public override SifoscObject? Divide(SifoscObject? other)
49+
{
50+
if (other is SifoscInteger otherInt) {
51+
return this with { Value = this.Value / otherInt.Value };
52+
}
53+
54+
return null;
55+
}
56+
57+
public override SifoscObject? Modulo(SifoscObject? other)
58+
{
59+
if (other is SifoscInteger otherInt) {
60+
return this with { Value = this.Value % otherInt.Value };
61+
}
62+
63+
return null;
64+
}
65+
}
66+
}

FastChatProtocolInterface/SimpleFormulaScript/SifoscObject.cs

Lines changed: 0 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System;
99
using System.Collections.Concurrent;
1010
using System.Collections.Generic;
11-
using System.Text;
1211
using System.Threading;
1312

1413
namespace FastChatProtocolInterface.SimpleFormulaScript
@@ -59,77 +58,6 @@ public static IEnumerable<SifoscObject> EnumerateAllObjects()
5958
public virtual SifoscObject? Modulo (SifoscObject? other) => null;
6059
}
6160

62-
public sealed record SifoscArray : SifoscObject
63-
{
64-
public override string Code
65-
{
66-
get
67-
{
68-
var sb = new StringBuilder();
69-
var span = this.Values.Span;
70-
71-
sb.Append('[');
72-
for (int i = 0; i < span.Length; ++i) {
73-
var obj = span[i];
74-
if (obj is null) {
75-
continue;
76-
}
77-
if (i != 0) {
78-
sb.Append(',');
79-
}
80-
sb.Append(obj.Code);
81-
}
82-
sb.Append(']');
83-
84-
return sb.ToString();
85-
}
86-
}
87-
88-
public ReadOnlyMemory<SifoscObject?> Values { get; init; }
89-
90-
protected override bool PrintMembers(StringBuilder builder)
91-
{
92-
// 参考:https://qiita.com/muniel/items/fd843abc55a5626e5c45
93-
94-
bool appendComma = base.PrintMembers(builder);
95-
96-
var s = this.Values.Span;
97-
for (int i = 0; i < s.Length; ++i) {
98-
if (appendComma) {
99-
builder.Append(", ");
100-
}
101-
var obj = s[i];
102-
if (obj is SifoscViewForAllObjects view) {
103-
view.PrintMembersSimply(builder);
104-
} else {
105-
builder.Append(obj);
106-
}
107-
appendComma = true;
108-
}
109-
return appendComma;
110-
}
111-
112-
public override SifoscObject? Add(SifoscObject? other)
113-
{
114-
if (other is SifoscArray otherArray) {
115-
int otherLen = otherArray.Values.Length;
116-
if (otherLen == 0) {
117-
return this;
118-
}
119-
120-
int thisLen = this.Values.Length;
121-
var newArray = new SifoscObject?[thisLen + otherLen];
122-
123-
this .Values.CopyTo(newArray);
124-
otherArray.Values.CopyTo(newArray.AsMemory()[thisLen..]);
125-
126-
return this with { Values = newArray };
127-
}
128-
129-
return null;
130-
}
131-
}
132-
13361
public sealed record SifoscNull : SifoscObject
13462
{
13563
private static readonly SifoscNull _inst = new();
@@ -140,48 +68,6 @@ public sealed record SifoscNull : SifoscObject
14068
private SifoscNull() { }
14169
}
14270

143-
public sealed record SifoscViewForAllObjects : SifoscObject
144-
{
145-
private static readonly SifoscViewForAllObjects _inst = new();
146-
147-
public static SifoscViewForAllObjects Instance => _inst;
148-
public override string Code => "allobj";
149-
150-
private SifoscViewForAllObjects() { }
151-
152-
protected override bool PrintMembers(StringBuilder builder)
153-
{
154-
bool appendComma = base.PrintMembers(builder);
155-
156-
foreach (var item in EnumerateAllObjects()) {
157-
if (appendComma) {
158-
builder.Append(", ");
159-
}
160-
if (item is SifoscViewForAllObjects) {
161-
this.PrintMembersSimply(builder);
162-
} else {
163-
builder.Append(item);
164-
}
165-
appendComma = true;
166-
}
167-
168-
return appendComma;
169-
}
170-
171-
public void PrintMembersSimply(StringBuilder builder)
172-
{
173-
builder
174-
.Append(nameof(SifoscViewForAllObjects))
175-
.Append(" { ");
176-
177-
if (base.PrintMembers(builder)) {
178-
builder.Append(' ');
179-
}
180-
181-
builder.Append('}');
182-
}
183-
}
184-
18571
public sealed record SifoscBoolean : SifoscObject
18672
{
18773
private static readonly SifoscBoolean _true = new();
@@ -194,61 +80,4 @@ public sealed record SifoscBoolean : SifoscObject
19480

19581
private SifoscBoolean() { }
19682
}
197-
198-
public sealed record SifoscInteger : SifoscObject
199-
{
200-
public long Value { get; init; }
201-
public override string Code => this.Value.ToString();
202-
203-
public override SifoscObject? Plus()
204-
=> this;
205-
206-
public override SifoscObject? Minus()
207-
=> this with { Value = -this.Value };
208-
209-
public override SifoscObject? Add(SifoscObject? other)
210-
{
211-
if (other is SifoscInteger otherInt) {
212-
return this with { Value = this.Value + otherInt.Value };
213-
}
214-
215-
return null;
216-
}
217-
218-
public override SifoscObject? Subtract(SifoscObject? other)
219-
{
220-
if (other is SifoscInteger otherInt) {
221-
return this with { Value = this.Value - otherInt.Value };
222-
}
223-
224-
return null;
225-
}
226-
227-
public override SifoscObject? Multiply(SifoscObject? other)
228-
{
229-
if (other is SifoscInteger otherInt) {
230-
return this with { Value = this.Value * otherInt.Value };
231-
}
232-
233-
return null;
234-
}
235-
236-
public override SifoscObject? Divide(SifoscObject? other)
237-
{
238-
if (other is SifoscInteger otherInt) {
239-
return this with { Value = this.Value / otherInt.Value };
240-
}
241-
242-
return null;
243-
}
244-
245-
public override SifoscObject? Modulo(SifoscObject? other)
246-
{
247-
if (other is SifoscInteger otherInt) {
248-
return this with { Value = this.Value % otherInt.Value };
249-
}
250-
251-
return null;
252-
}
253-
}
25483
}

0 commit comments

Comments
 (0)