-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuiltinFunctions.cs
298 lines (258 loc) · 11.8 KB
/
BuiltinFunctions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System.Diagnostics.CodeAnalysis;
using PseudoCode.Core.Runtime.Errors;
using PseudoCode.Core.Runtime.Instances;
using PseudoCode.Core.Runtime.Operations;
using PseudoCode.Core.Runtime.Types;
using Type = PseudoCode.Core.Runtime.Types.Type;
namespace PseudoCode.Core.Runtime.Reflection;
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class BuiltinFunctions
{
public static readonly Random Random = new();
[Documentation("Checks if a file is read to the end")]
[BuiltinFunction("EOF")]
[ParamType("path", "STRING")]
[ReturnType("BOOLEAN")]
public static Instance Eof(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var path = arguments[0].Get<string>();
return parentScope.FindDefinition(Type.BooleanId).Type.Instance(program.OpenFiles[path].Eof());
}
[Documentation("Returns a string containing `x` characters from the right of the source string")]
[BuiltinNativeFunction("RIGHT")]
public static string Right(string thisString, int x)
{
if (x < 1 || x > thisString.Length)
throw new OutOfBoundsError(
$"Cannot take substring [^{x}..] of \"{thisString}\": Length of string is {thisString.Length}", null);
return thisString[^x..];
}
[Documentation("Returns a string containing `x` characters from the start of the source string")]
[BuiltinNativeFunction("LEFT")]
public static string Left(string thisString, int x)
{
if (x < 1 || x > thisString.Length)
throw new OutOfBoundsError(
$"Cannot take substring [..{x}] of \"{thisString}\": Length of string is {thisString.Length}",
null);
return thisString[..x];
}
[Documentation("Returns the length of the string")]
[BuiltinNativeFunction("LENGTH")]
public static int Length(string thisString)
{
return thisString.Length;
}
[Documentation("Returns if `target` falls between the bounds inclusively.\n**You shouldn't use this**")]
[BuiltinNativeFunction("__in_range")]
public static bool InRange(RealNumberType target, RealNumberType from, RealNumberType to)
{
return from <= target && target <= to;
}
[Documentation("Returns the substring of `length` of the source string starting from `index`")]
[BuiltinNativeFunction("MID")]
public static string Mid(string thisString, int index, int length)
{
if (index < 1 || index > thisString.Length || length < 1 || index + length - 1 > thisString.Length)
throw new OutOfBoundsError(
$"Cannot take substring [{index}..{index + length}] of \"{thisString}\": Length of string is {thisString.Length}",
null);
return thisString.Substring(index - 1, length);
}
[Documentation("Turns the character into lower case.\n`'C' -> 'c'`\n`'c' -> 'c'`")]
[BuiltinNativeFunction("LCASE")]
public static char LowerCase(char thisChar)
{
return char.ToLower(thisChar);
}
[Documentation("Turns the character into upper case.\n`'C' -> 'C'`\n`'c' -> 'C'`")]
[BuiltinNativeFunction("UCASE")]
public static char UpperCase(char thisChar)
{
return char.ToUpper(thisChar);
}
[Documentation("Turns all the characters in the string into upper case.\n`\"aBcD\" -> \"ABCD\"`")]
[BuiltinNativeFunction("TO_UPPER")]
public static string ToUpper(string str) => str.ToUpper();
[Documentation("Turns all the characters in the string into lower case.\n`\"aBcD\" -> \"abcd\"`")]
[BuiltinNativeFunction("TO_LOWER")]
public static string ToLower(string str) => str.ToLower();
[Documentation("Converts a number into a string")]
[BuiltinNativeFunction("NUM_TO_STR")]
public static string NumToStr(RealNumberType num) => num.ToString();
[Documentation("Converts a string into a number")]
[BuiltinNativeFunction("STR_TO_NUM")]
public static RealNumberType StrToNum(string str) => RealNumberType.Parse(str);
[Documentation("Returns if the given string can be parsed as a number")]
[BuiltinNativeFunction("IS_NUM")]
public static bool IsNum(string str) => RealNumberType.TryParse(str, out _);
[Documentation("Returns the ascii index of a character")]
[BuiltinNativeFunction("ASC")]
public static int Ascii(char chr) => chr;
[Documentation("Returns the character from the given ascii index")]
[BuiltinNativeFunction("CHR")]
public static char Char(int ascii) => (char)ascii;
[Documentation("Returns the day component of a date")]
[BuiltinNativeFunction("DAY")]
public static int Day(DateOnly date) => date.Day;
[Documentation("Returns the month component of a date")]
[BuiltinNativeFunction("MONTH")]
public static int Month(DateOnly date) => date.Month;
[Documentation("Returns the year component of a date")]
[BuiltinNativeFunction("YEAR")]
public static int Year(DateOnly date) => date.Year;
[Documentation("Returns the day of a date (`Sunday` = 1, `Saturday` = 7)")]
[BuiltinNativeFunction("DAYINDEX")]
public static int DayIndex(DateOnly date) => (int)date.DayOfWeek + 1;
[Documentation("Constructs a date using given `day`, `month`, and `year`")]
[BuiltinNativeFunction("SETDATE")]
public static DateOnly SetDate(int day, int month, int year)
{
return new DateOnly(year, month, day);
}
[Documentation("Returns the date of today")]
[BuiltinNativeFunction("TODAY")]
public static DateOnly Today() => DateOnly.FromDateTime(DateTime.Today);
[Documentation("Returns the integer component of a `REAL` number")]
[BuiltinNativeFunction("INT")]
public static int Int(RealNumberType x)
{
return (int)x;
}
[Documentation("Returns the index of the first occasion of the character in the string")]
[BuiltinNativeFunction("FIND")]
public static int Find(string str, char chr)
{
return str.IndexOf(chr) + 1;
}
[Documentation("Returns the index of the first occasion of the character in the string starting from `index`")]
[BuiltinNativeFunction("INSTR")]
public static int InString(int index, string str, char chr)
{
return Find(str[(index - 1)..], chr) + index - 1;
}
[Documentation("Returns a random number [0..x)")]
[BuiltinNativeFunction("RAND")]
public static RealNumberType RandomReal(int x)
{
// ReSharper disable once RedundantCast
return (RealNumberType)(Random.NextDouble() * x);
}
[Documentation("**NONSTANDARD** Inserts an item to the set")]
[BuiltinFunction("SET_INSERT")]
[ParamType("set", "ANY", isSet: true)]
[ParamType("item", "ANY")]
public static Instance SetInsert(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var item = SetCheckAndCast(setInstance, arguments[1]);
set.Add(item);
return Instance.Null;
}
[Documentation("**NONSTANDARD** Removes an item from the set")]
[BuiltinFunction("SET_REMOVE")]
[ParamType("set", "ANY", isSet: true)]
[ParamType("item", "ANY")]
public static Instance SetRemove(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var item = SetCheckAndCast(setInstance, arguments[1]);
set.Remove(item);
return Instance.Null;
}
[Documentation("**NONSTANDARD** Checks if the set contains the item")]
[BuiltinFunction("SET_CONTAINS")]
[ParamType("set", "ANY", isSet: true)]
[ParamType("item", "ANY")]
[ReturnType("BOOLEAN")]
public static Instance SetContains(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var item = SetCheckAndCast(setInstance, arguments[1]);
var res = set.Contains(item);
return parentScope.FindDefinition(Type.BooleanId).Type.Instance(res);
}
[Documentation(
"**NONSTANDARD** Checks if the set contains the item. If the item is found, assign the item to `out`")]
[BuiltinFunction("SET_TRY_GET")]
[ParamType("set", "ANY", isSet: true)]
[ParamType("item", "ANY")]
[ParamType("out", "ANY", isReference: true)]
[ReturnType("BOOLEAN")]
public static Instance SetTryGet(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var item = SetCheckAndCast(setInstance, arguments[1]);
var res = set.TryGetValue(item, out var outItem);
if (res) arguments[2].Type.Assign(arguments[2], outItem);
return parentScope.FindDefinition(Type.BooleanId).Type.Instance(res);
}
[Documentation("**NONSTANDARD** Only include items that are present in only one of the sets but not both")]
[BuiltinFunction("SET_SYMMETRIC_DIFFERENCE")]
[ParamType("targetSet", "ANY", isSet: true)]
[ParamType("anotherSet", "ANY", isSet: true)]
public static Instance SetSymmetricDifference(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var setType = (SetType)setInstance.Type;
var set2 = arguments[1];
set.SymmetricExceptWith(setType.HandledCastFrom(set2).Get<HashSet<Instance>>());
return Instance.Null;
}
[Documentation("**NONSTANDARD** Only include items that are present in both sets in target set")]
[BuiltinFunction("SET_INTERSECT")]
[ParamType("targetSet", "ANY", isSet: true)]
[ParamType("anotherSet", "ANY", isSet: true)]
public static Instance SetIntersect(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var setType = (SetType)setInstance.Type;
var set2 = arguments[1];
set.IntersectWith(setType.HandledCastFrom(set2).Get<HashSet<Instance>>());
return Instance.Null;
}
[Documentation("**NONSTANDARD** Adds items that are not in the target set but in the other set")]
[BuiltinFunction("SET_UNION")]
[ParamType("targetSet", "ANY", isSet: true)]
[ParamType("anotherSet", "ANY", isSet: true)]
public static Instance SetUnion(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var setType = (SetType)setInstance.Type;
var set2 = arguments[1];
set.UnionWith(setType.HandledCastFrom(set2).Get<HashSet<Instance>>());
return Instance.Null;
}
[Documentation("**NONSTANDARD** Excludes items that are present in both sets")]
[BuiltinFunction("SET_DIFFERENCE")]
[ParamType("targetSet", "ANY", isSet: true)]
[ParamType("anotherSet", "ANY", isSet: true)]
public static Instance SetDifference(Scope parentScope, PseudoProgram program, Instance[] arguments)
{
var setInstance = arguments[0];
var set = setInstance.Get<HashSet<Instance>>();
var setType = (SetType)setInstance.Type;
var set2 = arguments[1];
set.ExceptWith(setType.HandledCastFrom(set2).Get<HashSet<Instance>>());
return Instance.Null;
}
private static Instance SetCheckAndCast(Instance setInstance, Instance item)
{
var setType = (SetType)setInstance.Type;
if (setType.ElementType != item.Type)
{
if (setType.ElementType.IsConvertableFrom(item.Type))
item = setType.ElementType.HandledCastFrom(item);
else
throw new UnsupportedCastError($"Cannot insert {item.Type} into {setType}", null);
}
return item;
}
}