Skip to content

Commit 2e904a8

Browse files
Fix Convert function
1 parent 14465dd commit 2e904a8

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

CritLang/Content/test.crit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ola = 34076580437543805743058347540383407658043754380574305834754038340765804375438057430583475403834076580437543805743058347540383407658043754380574305834754038 #BigInteger
88

99
num1 = ola + 69
10-
WriteLine(num1);
10+
WriteLine(Convert(ola, "string");
1111

1212

1313

CritLang/CritVisitor.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,22 @@ public CritVisitor(string version)
9595
public static object? ConvertTo(object?[] args)
9696
{
9797
if (args.Length != 2)
98-
{
9998
throw new Exception("ConvertTo expects 2 arguments, first one the variable to convert to and the second one being the type.");
100-
}
99+
100+
101+
string TypeToConvertTo = args[1]!.ToString()!;
101102

102-
string[] typeOptions = new[] { "int", "float", "string", "bool" };
103-
if (!typeOptions.Contains(args[1]!.ToString()!))
103+
string[] typeOptions = { "int", "float", "string", "bool" };
104+
if (!typeOptions.Contains(TypeToConvertTo))
104105
throw new Exception($"Invalid type...\nMust be one of the following types: {string.Join(", ", typeOptions)}");
105-
//TODO ADD THE REST OF THE TYPES AUTOMATICALLY
106-
return args[1]!.ToString() switch
106+
107+
object? ValueToConvert = args[0]!;
108+
109+
return ValueToConvert switch
107110
{
108-
"int" => Convert.ToInt32(args[0]),
109-
"float" => Convert.ToSingle(args[0]),
110-
"string" => Convert.ToString(args[0]),
111-
"bool" => Convert.ToBoolean(args[0]),
112-
_ => throw new Exception("Invalid type...")
111+
string => Convert.ToString(ValueToConvert),
112+
bool => Convert.ToBoolean(ValueToConvert),
113+
_ => TypeDispatcher(ValueToConvert)
113114
};
114115
}
115116

@@ -118,7 +119,7 @@ public CritVisitor(string version)
118119
{
119120
if (args.Length != 1)
120121
{
121-
throw new Exception("ReadLine expects 1 arguments, being the text to prompt to the use.");
122+
throw new Exception("ReadLine expects 1 arguments, being the text to prompt to the user.");
122123
}
123124

124125
string text = args[0]!.ToString()!;
@@ -623,8 +624,7 @@ private static dynamic TypeDispatcher(dynamic variable)
623624

624625
if (left is null || right is null) return null;
625626

626-
string leftText = left!.ToString()!;
627-
string rightText = right!.ToString()!;
627+
628628

629629
left = TypeDispatcher(left);
630630
right = TypeDispatcher(right);

0 commit comments

Comments
 (0)