Skip to content

Commit de4e287

Browse files
committed
added better errors to system functions
1 parent 0448b64 commit de4e287

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

7Sharp/Intrerpreter/Sysfunctions.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,22 @@ public static int Int(object value)
7272

7373
public static double Atan(double v) => Math.Atan(v);
7474

75-
public static double Atan2(double x, double y) => Math.Atan2(x, y);
75+
public static double Atan2(double x, double y) => x == 0 && y == 0 ? 0 : Math.Atan2(x, y);
7676

7777
public static double Deg2Rad(double v) => v * Math.PI / 180D;
7878

7979
public static double Rad2Deg(double v) => v * 180D / Math.PI;
8080
#endregion
8181

8282
[ManualDocs("sleep", "{\"title\":\"sleep(ms)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"sleep(<number of miliseconds to wait>);\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Waits for \"},{\"text\":\"ms\",\"color\":\"Green\"},{\"text\":\" miliseconds.\"}]}]}")]
83-
public static void Sleep(int ms) => Thread.Sleep(ms);
83+
public static void Sleep(int ms)
84+
{
85+
if (ms < 0)
86+
{
87+
throw new InterpreterException("sleep: Cannot sleep for a negative number of miliseconds!");
88+
}
89+
Thread.Sleep(ms);
90+
}
8491

8592
[ManualDocs("len", "{\"title\":\"len(obj)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"len(<array or string>)\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Returns the length of \"},{\"text\":\"obj\",\"color\":\"Green\"},{\"text\":\", if it is a string or an array. If not it will throw an error.\"}]}]}")]
8693
public static int Len(object obj)
@@ -105,7 +112,7 @@ public static object[] Chars(string s)
105112
{
106113
if (s == null)
107114
{
108-
throw new InterpreterException("chars: string passed was null!");
115+
throw new InterpreterException("chars: string passed in was null!");
109116
}
110117
return Utils.ToArray(s.ToArray());
111118
}
@@ -123,14 +130,18 @@ public static string _7sToString(object obj)
123130
}
124131
if (obj is System.Collections.IEnumerable ie)
125132
{
126-
return $"Array [{string.Join(", ", Utils.ToArray(ie))}]";
133+
return $"[{string.Join(", ", Utils.ToArray(ie))}]";
127134
}
128135
return obj.ToString();
129136
}
130137

131138
[ManualDocs("arrayAdd", "{\"title\":\"arrayAdd(arr, value)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"arrayAdd(<array>, <value>)\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Returns \"},{\"text\":\"array\",\"color\":\"Green\"},{\"text\":\" with \"},{\"text\":\"value\",\"color\":\"Green\"},{\"text\":\" added to the end.\"}]}]}")]
132139
public static object[] ArrayAdd(object[] arr, object value)
133140
{
141+
if (arr == null)
142+
{
143+
throw new InterpreterException("arrayAdd: Cannot add to an array that is null!");
144+
}
134145
Array.Resize(ref arr, arr.Length + 1);
135146
arr[arr.Length - 1] = value;
136147
return arr;
@@ -139,6 +150,10 @@ public static object[] ArrayAdd(object[] arr, object value)
139150
[ManualDocs("arrayRemove", "{\"title\":\"arrayRemove(arr, index)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"arrayAdd(<array>, <index>)\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Returns \"},{\"text\":\"array\",\"color\":\"Green\"},{\"text\":\", but the element at index \"},{\"text\":\"index\",\"color\":\"Green\"},{\"text\":\" is removed.\"}]}]}")]
140151
public static object[] ArrayRemove(object[] arr, int index)
141152
{
153+
if (arr == null)
154+
{
155+
throw new InterpreterException("arrayRemove: Cannot remove from an array that is null!");
156+
}
142157
if (index < 0 || index >= arr.Length)
143158
{
144159
throw new InterpreterException("Attempted to remove an element that is not in bounds of the array");
@@ -147,10 +162,24 @@ public static object[] ArrayRemove(object[] arr, int index)
147162
}
148163

149164
[ManualDocs("sqrt", "{\"title\":\"sqrt(x)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"sqrt(<number>);\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Takes the square root of\"},{\"text\":\"x\",\"color\":\"Green\"},{\"text\":\".\"}]}]}")]
150-
public static double Sqrt(double x) => Math.Sqrt(x);
165+
public static double Sqrt(double x)
166+
{
167+
if (x < 0)
168+
{
169+
throw new InterpreterException("sqrt: Complex numbers are not supported! (Attempted to take the square root of a negative number)");
170+
}
171+
return Math.Sqrt(x);
172+
}
151173

152174
[ManualDocs("pow", "{\"title\":\"pow(x, n)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"pow(<number>, <number>);\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Raise \"},{\"text\":\"x\",\"color\":\"Green\"},{\"text\":\" to the power of \"},{\"text\":\"n\",\"color\":\"Green\"}]}]}")]
153-
public static double Pow(double x, double y) => Math.Pow(x, y);
175+
public static double Pow(double x, double y)
176+
{
177+
if (x == 0 && y == 0)
178+
{
179+
throw new InterpreterException("pow: Cannot raise 0 to the 0th power!");
180+
}
181+
return Math.Pow(x, y);
182+
}
154183

155184
[ManualDocs("fgColor", "{\"title\":\"fgColor(color)\",\"sections\":[{\"header\":\"Syntax\",\"text\":[{\"text\":\"fgColor(<color number or name>);\"}]},{\"header\":\"Behavior\",\"text\":[{\"text\":\"Set the text color to \"},{\"text\":\"color\",\"color\":\"Green\"}]},{\"header\":\"Valid colors\",\"text\":[{\"text\":\"BLACK BLUE CYAN DARK_BLUE DARK_CYAN DARK_GRAY DARK_GREEN DARK_MAGENTA DARK_RED DARK_YELLOW GRAY GREEN MAGENTA RED WHITE YELLOW\",\"color\":\"Cyan\"}]}]}")]
156185
public static void FgColor(int color)

0 commit comments

Comments
 (0)