Skip to content

Commit

Permalink
Math,Time,Str,Array, Convert,File, changes in naming
Browse files Browse the repository at this point in the history
  • Loading branch information
funcieqDEV authored Oct 11, 2024
1 parent 281d710 commit 0d9b526
Showing 1 changed file with 260 additions and 16 deletions.
276 changes: 260 additions & 16 deletions visual_studio/src/std_lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public static IVariables StdLib(Interpreter interpreter)
public static object InstantiateModule(Type moduleType, Interpreter interpreter)
{

// Try to find a constructor that takes an Interpreter as an argument

ConstructorInfo? constructor = moduleType.GetConstructor(new[] { typeof(Interpreter) });


if (constructor != null)
{
// If a matching constructor exists, invoke it with the interpreter instance

return constructor.Invoke(new object[] { interpreter });
}
else
Expand All @@ -70,7 +70,7 @@ public static string ToLowerSnakeCase(string input)
sb.Append(char.ToLower(c));
}

return sb.ToString();
return input;
}
}

Expand All @@ -91,31 +91,64 @@ namespace VSharpLib
using VSharp;

[Module]
class Io
class io
{
public void Println(object? arg)
public void println(object? arg)
{
Console.WriteLine(arg?.ToString() ?? "null");
}

public string? Input(object? message)


public string? input(object? message)
{
Console.WriteLine(message);
Console.Write(message);
return Console.ReadLine();
}


public string? Input()
public string? input()
{
return Console.ReadLine();
}
}

[Module]
class File
{
public string? ReadFile(object name)
{
return System.IO.File.ReadAllText(name.ToString());
}

public void WriteFile(object name, object value)
{
System.IO.File.WriteAllText(name.ToString(), value.ToString());
}
}

[Module]

class Convert
{
public int? ToInt(object? num)
{
return System.Convert.ToInt32(num);
}
public string? ToString(object? s)
{
return System.Convert.ToString(s);
}

public string ReadFile(string name)
public float? ToFloat(object? num)
{
return File.ReadAllText(name);
return System.Convert.ToSingle(num);
}

public bool? ToBool(object? value)
{
return System.Convert.ToBoolean(value);
}
}

[Module]
Expand All @@ -127,6 +160,111 @@ public VSharpObject New()
}
}

[Module]
public class Array
{
public int Length(List<object> list)
{
return list.Count;
}

public bool IsEmpty(List<object> list)
{
return list.Count == 0;
}

public object GetElementAt(List<object> list, int index)
{
if (index < 0 || index >= list.Count)
{
throw new ArgumentOutOfRangeException("Index out of bounds.");
}
return list[index];
}

public void AddElement(List<object> list, object element)
{
list.Add(element);
}

public void RemoveElementAt(List<object> list, int index)
{
if (index < 0 || index >= list.Count)
{
throw new ArgumentOutOfRangeException("Index out of bounds.");
}
list.RemoveAt(index);
}

public void Clear(List<object> list)
{
list.Clear();
}

public bool Contains(List<object> list, object element)
{
return list.Contains(element);
}

public int IndexOf(List<object> list, object element)
{
return list.IndexOf(element);
}


public List<object> Sort(List<object> list)
{
List<object> sortedList = new List<object>(list);
QuickSort(sortedList, 0, sortedList.Count - 1);
return sortedList;
}

private void QuickSort(List<object> list, int low, int high)
{
if (low < high)
{
int pivotIndex = Partition(list, low, high);
QuickSort(list, low, pivotIndex - 1);
QuickSort(list, pivotIndex + 1, high);
}
}

private int Partition(List<object> list, int low, int high)
{
object pivot = list[high];
int i = low - 1;

for (int j = low; j < high; j++)
{

if (list[j] is IComparable comparableElement)
{
if (comparableElement.CompareTo(pivot) <= 0)
{
i++;
Swap(list, i, j);
}
}
else
{
throw new ArgumentException("something went wrong");
}
}
Swap(list, i + 1, high);
return i + 1;
}

private void Swap(List<object> list, int i, int j)
{
object temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}




[Module]
class Error
{
Expand All @@ -139,7 +277,7 @@ public void Throw(object? reason)
[Module]
class Json
{
public object? Parse(string content)
public object? parse(string content)
{
return ParseElement(JsonDocument.Parse(content).RootElement);
}
Expand All @@ -155,7 +293,7 @@ public string ToString(object? json)

public static object? ParseElement(JsonElement element)
{
// Handle JSON object

if (element.ValueKind == JsonValueKind.Object)
{
var dict = new Dictionary<object, object?>();
Expand All @@ -166,7 +304,7 @@ public string ToString(object? json)
return new VSharpObject { Entries = dict };
}

// Handle JSON array

if (element.ValueKind == JsonValueKind.Array)
{
var list = new List<object?>();
Expand All @@ -177,7 +315,7 @@ public string ToString(object? json)
return list;
}

// Handle primitive values

switch (element.ValueKind)
{
case JsonValueKind.String:
Expand All @@ -186,18 +324,124 @@ public string ToString(object? json)
if (element.TryGetInt32(out int intValue))
return intValue;
else
return element.GetDouble(); // For non-integer numbers
return element.GetDouble();
case JsonValueKind.True:
case JsonValueKind.False:
return element.GetBoolean();
case JsonValueKind.Null:
return null;
default:
return element.ToString(); // Fallback for other types
return element.ToString();
}
}
}

[Module]

class Time
{
public DateTime Now()
{
return DateTime.Now;
}

public DateTime Date()
{
return DateTime.Today;
}
}


[Module]

class Str
{
public int Length(string? value)
{
return value.Length;
}
}

[Module]
class Math
{
public int? RandInt(int min, int max)
{
Random rnd = new Random();
return rnd.Next(min, max);
}

public double GetPI()
{
return System.Math.PI;
}

public double Abs(double value)
{
return System.Math.Abs(value);
}

public double Max(double a, double b)
{
return System.Math.Max(a, b);
}

public double Min(double a, double b)
{
return System.Math.Min(a, b);
}

public double Pow(double x, double y)
{
return System.Math.Pow(x, y);
}

public double Sqrt(double value)
{
return System.Math.Sqrt(value);
}

public double Sin(double angle)
{
return System.Math.Sin(angle);
}

public double Cos(double angle)
{
return System.Math.Cos(angle);
}

public double Tan(double angle)
{
return System.Math.Tan(angle);
}

public double Asin(double value)
{
return System.Math.Asin(value);
}

public double Acos(double value)
{
return System.Math.Acos(value);
}

public double Atan(double value)
{
return System.Math.Atan(value);
}

public double Round(double value)
{
return System.Math.Round(value);
}

public double Ceiling(double value)
{
return System.Math.Ceiling(value);
}
}

[Module]
public class Range
{
Expand Down

0 comments on commit 0d9b526

Please sign in to comment.