-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.cs
802 lines (658 loc) · 31.4 KB
/
Interpreter.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
using System;
using System.Globalization;
using System.Text;
namespace InterpreterLib
{
public class Type
{
public static readonly int BOOLEAN = 0;
public static readonly int INTEGER = 1;
public static readonly int FLOAT = 2;
public static readonly int STRING = 3;
public static readonly int OPERATION_NUMBER = 4;
public static readonly int OPERATION_BOOL = 5;
public static readonly int VARIABLE = 6;
public static readonly int FUNCTION = 7;
}
public class Variable
{
public string Name { get; }
public string Value { get; }
public int Type { get; }
public Variable(string name, string value, int type)
{
Name = name; Value = value; Type = type;
}
public override string ToString()
{
return $"{Value} ({Type})";
}
}
public class Function
{
public string Name { get; }
public string Code { get; }
public string[] Var { get; }
public Function(string name, string code, string[] var)
{
Name = name; Code = code; Var = var;
}
public void Execute(string[] parameters, Interpreter interpreter)
{
StringBuilder functionCode = new StringBuilder(Code); // Use of String Builder because the program need to replace a string by another.
if (parameters.Length < Var.Length) { throw new Exception("No enought parameters was given."); }
if (parameters.Length > Var.Length) { throw new Exception("Too much parameters was given."); }
if (Var.Length == 1 && Var[0] == "") { parameters = []; } // Case of a function without parameters
Interpreter functionInterpreter = new Interpreter(); // Create a new interpreter to execute the function code.
for (int i = 0; i < parameters.Length; i++) // Create local variables with the parameters given in parameters.
{
int parameterType = interpreter.EvaluateType(parameters[i]);
if (parameterType == Type.VARIABLE) { parameterType = interpreter.Variables[parameters[i]].Type; parameters[i] = interpreter.Variables[parameters[i]].Value; } // Replace variable name with value and type with the value type
functionInterpreter.Variables[Var[i]] = new Variable(Var[i], parameters[i], parameterType);
}
foreach (string functionName in interpreter.Functions.Keys) // Add all functions created in the main interpreter to the function interpreter
{
functionInterpreter.Functions[functionName] = interpreter.Functions[functionName];
}
functionInterpreter.EvaluateCode(functionCode.ToString());
}
}
public class Interpreter
{
public Dictionary<string, Variable> Variables;
public Dictionary<string, Function> Functions;
public Interpreter()
{
Variables = new Dictionary<string, Variable>();
Functions = new Dictionary<string, Function>();
}
public void EvaluateCode(string code)
{
string[] lines = code.Split(new[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
int index = 0;
while (index < lines.Length)
{
string line = lines[index];
string trimmedLine = line.Trim();
if (trimmedLine.StartsWith("var ")) // Case of the registration / assignation of a value to a variable
{
EvaluateVariable(trimmedLine[4..]);
}
else if (trimmedLine.StartsWith("print ")) // Case of a print
{
EvaluatePrint(trimmedLine[6..]);
}
else if (trimmedLine.StartsWith("func "))
{ // Case of the registration of a function
StringBuilder functionText = new StringBuilder(trimmedLine[5..]);
index++;
while (index < lines.Length && lines[index].StartsWith("\t"))
{ // Get all lines until their are no tabulation and the line don't finish by the '}' character.
functionText.Append($"\n{lines[index][1..]}");
index++;
}
EvaluateFunctionRegister(functionText.ToString());
index--;
} else if (trimmedLine.StartsWith("for ")) // for var from x to y {}
{ // Case of the registration of a loop
string loopFirstLine = trimmedLine[4..];
StringBuilder loopCode = new StringBuilder();
//if (!(firstLine.EndsWith('{'))) { throw new Exception("Error. Invalid Syntax."); } // Throw new error in case of invalid syntax.
string loopVarName = loopFirstLine.Split(' ')[0]; // Get the title of the loopVar
string loopFromName = loopFirstLine.Split(' ')[2]; // Get the title of the loopFrom
string loopToName = loopFirstLine.Split(' ')[4]; // Get the title of the loopTo
loopToName = loopToName[..(loopToName.Length - 1)];
if (!loopFirstLine.EndsWith(':')) { throw new Exception("Invalid syntax."); }
int loopFromType = EvaluateType(loopFromName);
int loopToType = EvaluateType(loopToName);
if (loopFromType == Type.VARIABLE)
{
loopFromType = Variables[loopFromName].Type;
loopFromName = Variables[loopFromName].Value;
}
if (loopToType == Type.VARIABLE)
{
loopToType = Variables[loopToName].Type;
loopToName = Variables[loopToName].Value;
}
if (loopFromType != Type.INTEGER || loopToType != Type.INTEGER)
{
throw new Exception($"Error. {trimmedLine} is not recognized as a correct expression. ");
}
Variables[loopVarName] = new Variable(loopVarName, loopFromName, Type.INTEGER);
index++;
while (index < lines.Length && lines[index].StartsWith("\t")) //
{ // Get all lines until their are no tabulation and the line don't finish by the '}' character.
loopCode.Append($"\n{lines[index][1..]}");
index++;
}
int from = int.Parse(loopFromName);
int to = int.Parse(loopToName);
// Console.WriteLine(loopText.ToString());
for (int i = from; i < to + 1; i++)
{
Variables[loopVarName] = new Variable(loopVarName, i.ToString(), Type.INTEGER);
this.EvaluateCode(loopCode.ToString());
}
index--;
}
else if (trimmedLine.StartsWith("if "))
{
string loopFirstLine = trimmedLine[3..];
StringBuilder ifCode = new StringBuilder();
//if (!(firstLine.EndsWith('{'))) { throw new Exception("Error. Invalid Syntax."); } // Throw new error in case of invalid syntax.
string condition = loopFirstLine.Split(':')[0];
index++;
while (index < lines.Length && lines[index].StartsWith("\t")) //
{ // Get all lines until their are no tabulation and the line don't finish by the '}' character.
ifCode.Append($"\n{lines[index][1..]}");
index++;
}
if (EvaluateBooleanOperations(condition))
{
EvaluateCode(ifCode.ToString());
}
index--;
}
else if (EvaluateType(trimmedLine) == Type.FUNCTION)
{
EvaluateFunctionCall(trimmedLine);
}
else
{
throw new Exception($"Error. {trimmedLine} is not recognized as a correct expression. ");
}
index++;
}
}
public void EvaluateVariable(string line)
{
if (!line.Contains(" = ")) { throw new Exception("Unable to create a variable without value assignation."); }
string[] var = line.Split(" = ");
int varType = EvaluateType(var[1]);
if (varType == Type.STRING)
{
var[1] = var[1][1..(var[1].Length - 1)];
}
else if (varType == Type.VARIABLE)
{
varType = Variables[var[1]].Type;
var[1] = Variables[var[1]].Value;
} else if (varType == Type.OPERATION_NUMBER)
{
var[1] = EvaluateOperations(var[1]).ToString();
varType = EvaluateType(var[1]);
} else if (varType == Type.OPERATION_BOOL)
{
var[1] = EvaluateBooleanOperations(var[1]).ToString();
varType = Type.BOOLEAN;
}
Variables[var[0]] = new Variable(var[0], var[1], varType);
}
public void EvaluateFunctionRegister(string lines)
{
string firstLine = lines.Split("\n")[0]; // Store the first line to use it to get the function name and args
string functionTitle = firstLine.Split('(')[0]; // Get the title of the function.
int openBracketIndex = firstLine.IndexOf("(");
int closeBracketIndex = firstLine.IndexOf(")");
if (openBracketIndex == -1 || closeBracketIndex == -1 || !(firstLine.EndsWith(':')) || functionTitle.Length == 0) { throw new Exception("Error. Invalid Syntax."); } // Throw new error in case of invalid syntax.
Function function = new Function(functionTitle, lines[(firstLine.Length + 1)..], firstLine[(openBracketIndex + 1)..closeBracketIndex].Split(", ")); // Create a new Function objet that store all the informations about the new function.
Functions[function.Name] = function; // Register the created function into the program function dictionnary.
}
public void EvaluateFunctionCall(string line)
{
string functionName = line.Split('(')[0]; // Get the title of the function
int openBracketIndex = line.IndexOf('(');
int closeBracketIndex = line.IndexOf(')');
Functions[functionName].Execute(line[(openBracketIndex + 1)..closeBracketIndex].Split(", "), this); // Execute the function with the parameters gives in the function call.
}
public void EvaluatePrint(string line)
{
int lineType = EvaluateType(line);
if (lineType == Type.VARIABLE)
{
Console.WriteLine(Variables[line].Value);
}
else if (lineType == Type.OPERATION_NUMBER)
{
Console.WriteLine(EvaluateOperations(line));
} else if (lineType == Type.OPERATION_BOOL)
{
Console.Write(EvaluateBooleanOperations(line));
}
else if (lineType == Type.STRING)
{
Console.WriteLine(line[1..(line.Length - 1)]);
}
else
{
Console.WriteLine(line);
}
}
public double EvaluateOperations(string line)
// This function manages the system of operations and redirects each operation to the corresponding operations function.
// It take a calculation in string type as a parameter and return the result of it as a double.
{
int openBracketIndex = 0;
int closeBracketIndex = 0;
int expressionIndex = 0;
string newExpression;
StringBuilder expressionBuilder = new StringBuilder(line);
if (line.StartsWith('(') && line.EndsWith(')')) { expressionBuilder = new StringBuilder(line[1..(line.Length - 1)]); } // Delete the bracket at the start and the end of the calculus.
while (expressionIndex < expressionBuilder.Length)
{
if (expressionBuilder.ToString()[expressionIndex] == '(') { openBracketIndex = expressionIndex; }
else if (expressionBuilder.ToString()[expressionIndex] == ')')
{
closeBracketIndex = expressionIndex;
expressionBuilder.Replace(expressionBuilder.ToString()[(openBracketIndex)..(closeBracketIndex + 1)], EvaluateOperations(expressionBuilder.ToString()[(openBracketIndex + 1)..(closeBracketIndex)]).ToString());
expressionIndex = 0;
}
expressionIndex++;
}
newExpression = expressionBuilder.ToString();
if (newExpression.Contains('+')) // Case of an addition
{
return EvaluateAddition(newExpression);
}
else if (newExpression.Contains('-')) // Case of a substraction
{
return EvaluateSubstraction(newExpression);
}
else if (newExpression.Contains('*')) // Case of a multiplication
{
return EvaluateMultiplication(newExpression);
}
else if (newExpression.Contains('/')) // Case of a division
{
return EvaluateDivision(newExpression);
}
else if (newExpression.Contains('^')) // Case of a power
{
return EvaluatePower(newExpression);
}
else if (double.TryParse(newExpression, out double _))
{
return double.Parse(newExpression);
}
else
{
throw new Exception($"Unexcepted operation : ${newExpression}");
}
}
public double EvaluateAddition(string line)
{
string[] parts = line.Split(" + ");
double sum = 0;
foreach (string part in parts)
{
int partType = EvaluateType(part);
if (partType == Type.VARIABLE && Variables[part].Type != Type.STRING)
{
sum += double.Parse(Variables[part].Value, CultureInfo.InvariantCulture);
}
else if (partType == Type.INTEGER || partType == Type.FLOAT || partType == Type.OPERATION_NUMBER)
{
sum += double.Parse(part, CultureInfo.InvariantCulture);
}
else
{
throw new Exception($"Unable to add {partType} to double !");
}
}
return sum;
}
public double EvaluateSubstraction(string line)
{
string[] parts = line.Split(" - ")[1..];
double sum;
if (EvaluateType(line.Split(" - ")[0]) == Type.VARIABLE && Variables[line.Split(" - ")[0]].Type != Type.STRING)
{
sum = double.Parse(Variables[line.Split(" - ")[0]].Value);
}
else
{
sum = double.Parse(line.Split(" - ")[0]);
}
foreach (string part in parts)
{
int partType = EvaluateType(part);
if (partType == Type.VARIABLE && Variables[part].Type != Type.STRING)
{
sum -= double.Parse(Variables[part].Value, CultureInfo.InvariantCulture);
}
else if (partType == Type.INTEGER || partType == Type.FLOAT || partType == Type.OPERATION_NUMBER)
{
sum -= double.Parse(part, CultureInfo.InvariantCulture);
}
else
{
throw new Exception($"Unable to substract {partType} to double !");
}
}
return sum;
}
public double EvaluateMultiplication(string line)
{
string[] parts = line.Split(" * ");
double total = 1;
foreach (string part in parts)
{
int partType = EvaluateType(part);
if (partType == Type.VARIABLE && Variables[part].Type != Type.STRING)
{
total *= double.Parse(Variables[part].Value, CultureInfo.InvariantCulture);
}
else if (partType == Type.INTEGER || partType == Type.FLOAT || partType == Type.OPERATION_NUMBER)
{
total *= double.Parse(part, CultureInfo.InvariantCulture);
}
else
{
throw new Exception($"Unable to multiply {partType} to double !");
}
}
return total;
}
public double EvaluateDivision(string line)
{
string[] parts = line.Split(" / ")[1..];
double total;
if (parts.Contains("0")) { throw new Exception("Error. Unable to divide by zero."); }
if (EvaluateType(line.Split(" / ")[0]) == Type.VARIABLE && Variables[line.Split(" / ")[0]].Type != Type.STRING)
{
total = double.Parse(Variables[line.Split(" / ")[0]].Value);
}
else
{
total = double.Parse(line.Split(" / ")[0]);
}
foreach (string part in parts)
{
int partType = EvaluateType(part);
if (partType == Type.VARIABLE && Variables[part].Type != Type.STRING)
{
total /= double.Parse(Variables[part].Value, CultureInfo.InvariantCulture);
}
else if (partType == Type.INTEGER || partType == Type.FLOAT || partType == Type.OPERATION_NUMBER)
{
total /= double.Parse(part, CultureInfo.InvariantCulture);
}
else
{
throw new Exception($"Unable to divide {partType} to double !");
}
}
return total;
}
public double EvaluatePower(string line)
{
string[] parts = line.Split(" ^ ")[1..];
double pow;
if (EvaluateType(line.Split(" ^ ")[0]) == Type.VARIABLE && Variables[line.Split(" ^ ")[0]].Type != Type.STRING)
{
pow = double.Parse(Variables[line.Split(" ^ ")[0]].Value);
}
else if (EvaluateType(line.Split(" ^ ")[0]) != Type.STRING)
{
pow = double.Parse(line.Split(" ^ ")[0]);
} else
{
throw new Exception("Unable to calculate a string power.");
}
foreach (string part in parts)
{
int partType = EvaluateType(part);
if (partType == Type.VARIABLE && Variables[part].Type != Type.STRING)
{
pow = Math.Pow(pow, double.Parse(Variables[part].Value, CultureInfo.InvariantCulture));
}
else if (partType == Type.INTEGER || partType == Type.FLOAT || partType == Type.OPERATION_NUMBER)
{
pow = Math.Pow(pow, double.Parse(part, CultureInfo.InvariantCulture));
}
else
{
throw new Exception($"Unable to power {partType} to double !");
}
}
return pow;
}
public bool EvaluateBooleanOperations(string line)
// This function manages the system of boolean operations and redirects each operation to the corresponding operations function.
// It take a calculation in string type as a parameter and return the result of it as a bool.
{
int openBracketIndex = 0;
int closeBracketIndex = 0;
int expressionIndex = 0;
int bracketBalance = 0;
string newExpression;
StringBuilder expressionBuilder = new StringBuilder(line);
if (line.StartsWith('(') && line.EndsWith(')') && AreBracketsBalanced(line)) { expressionBuilder = new StringBuilder(line[1..(line.Length - 1)]); } // Delete the bracket at the start and the end of the calculus.
while (expressionIndex < expressionBuilder.Length)
{
if (expressionBuilder.ToString()[expressionIndex] == '(') { openBracketIndex = expressionIndex; }
else if (expressionBuilder.ToString()[expressionIndex] == ')')
{
closeBracketIndex = expressionIndex;
string bracketExpression = expressionBuilder.ToString()[(openBracketIndex)..(closeBracketIndex)];
expressionBuilder.Replace(expressionBuilder.ToString()[(openBracketIndex)..(closeBracketIndex + 1)], EvaluateBooleanOperations(expressionBuilder.ToString()[(openBracketIndex + 1)..(closeBracketIndex)]).ToString());
expressionIndex = 0;
}
expressionIndex++;
}
newExpression = expressionBuilder.ToString();
if (newExpression.Contains("==")) // Case of an egual test
{
return EvaluateEgual(newExpression);
}
else if (newExpression.Contains("!=")) // Case of an unegual test
{
return EvaluateUnegual(newExpression);
}
else if (newExpression.Contains(">=")) // Case of a superior or egual test
{
return EvaluateSuperiorEgual(newExpression);
}
else if (newExpression.Contains("<=")) // Case of an inferior or egual test
{
return EvaluateInferiorEgual(newExpression);
}
else if (newExpression.Contains('>')) // Case of a superior test
{
return EvaluateSuperior(newExpression);
}
else if (newExpression.Contains('<')) // Case of an inferior test
{
return EvaluateInferior(newExpression);
} else if (newExpression.Contains('&'))
{
return EvaluateAnd(newExpression);
} else if (newExpression.Contains('|'))
{
return EvaluateOr(newExpression);
}
else if (this.EvaluateType(newExpression) == Type.BOOLEAN)
{
return bool.Parse(newExpression);
}
else
{
throw new Exception($"Unexcepted operation : ${newExpression}");
}
}
public bool AreBracketsBalanced(string expression)
{
int balance = 0;
foreach (char c in expression)
{
if (c == '(') { balance++; }
else if (c == ')') { balance--; }
if (balance == 0) { return false; }
}
return balance == 0;
}
public bool EvaluateAnd(string line)
{
string[] parts = line.Split("&");
if (parts.Length != 2) { throw new Exception("Error. Invalid expression."); }
string firstPart = parts[0][..(parts[0].Length - 1)]; // Remove space at the end of the string
string secondPart = parts[1][1..]; // Remove space at the begginning of the string
int firstPartType = EvaluateType(firstPart);
int secondPartType = EvaluateType(secondPart);
if (firstPartType == Type.VARIABLE)
{
firstPartType = Variables[firstPart].Type;
firstPart = Variables[firstPart].Value;
}
if (secondPartType == Type.VARIABLE)
{
secondPartType = Variables[secondPart].Type;
secondPart = Variables[secondPart].Value;
}
if (firstPartType != Type.BOOLEAN || secondPartType != Type.BOOLEAN) { throw new Exception("Error. Unable to calcul the 'AND' operator between types different that BOOLEAN"); }
return bool.Parse(firstPart) && bool.Parse(secondPart);
}
public bool EvaluateOr(string line)
{
string[] parts = line.Split("|");
if (parts.Length != 2) { throw new Exception("Error. Invalid expression."); }
string firstPart = parts[0][..(parts[0].Length - 1)]; // Remove space at the end of the string
string secondPart = parts[1][1..]; // Remove space at the begginning of the string
int firstPartType = EvaluateType(firstPart);
int secondPartType = EvaluateType(secondPart);
if (firstPartType == Type.VARIABLE)
{
firstPartType = Variables[firstPart].Type;
firstPart = Variables[firstPart].Value;
}
if (secondPartType == Type.VARIABLE)
{
secondPartType = Variables[secondPart].Type;
secondPart = Variables[secondPart].Value;
}
if (firstPartType != Type.BOOLEAN || secondPartType != Type.BOOLEAN) { throw new Exception("Error. Unable to calcul the 'AND' operator between types different that BOOLEAN"); }
return bool.Parse(firstPart) || bool.Parse(secondPart);
}
public bool EvaluateEgual(string line)
{
string[] part = line.Trim().Split(" == "); // The trim need to be remove once we implet this method in the EvaluateOperation function
if (part.Length != 2) { throw new Exception("Invalid expression."); } // Verify that there are exactly 2 values to be compared
// Extract the 2 values
string firstPart = part[0];
string secondPart = part[1];
// Get the types of the 2 parts
int firstElementType = this.EvaluateType(firstPart);
int secondElementType = this.EvaluateType(secondPart);
if (firstElementType == Type.VARIABLE) // Replace the variable type by the type of his value and the content by the value of the variable
{
firstElementType = Variables[firstPart].Type;
firstPart = Variables[firstPart].Value;
}
if (secondElementType == Type.VARIABLE) // Replace the variable type by the type of his value and the content by the value of the variable
{
secondElementType = Variables[firstPart].Type;
secondPart = Variables[firstPart].Value;
}
if (firstElementType == Type.OPERATION_NUMBER)
{
firstPart = EvaluateOperations(firstPart).ToString();
firstElementType = EvaluateType(firstPart);
}
if (secondElementType == Type.OPERATION_NUMBER)
{
secondPart = EvaluateOperations(secondPart).ToString();
secondElementType= EvaluateType(secondPart);
}
return (firstElementType == secondElementType) && (firstPart == secondPart); // Verify that types and value are egual
}
public bool EvaluateUnegual(string line)
{
return !(EvaluateEgual(line.Replace("!=", "=="))); // Return the opposite of the == operator
}
public bool EvaluateSuperior(string line)
{
string[] part = line.Trim().Split(" > "); // The trim need to be remove once we implet this method in the EvaluateOperation function
if (part.Length != 2) { throw new Exception("Invalid expression."); } // Verify that there are exactly 2 values to be compared
// Extract the 2 values
string firstPart = part[0];
string secondPart = part[1];
// Get the types of the 2 parts
int firstElementType = this.EvaluateType(firstPart);
int secondElementType = this.EvaluateType(secondPart);
if (firstElementType == Type.VARIABLE) // Replace the variable type by the type of his value and the content by the value of the variable
{
firstElementType = Variables[firstPart].Type;
firstPart = Variables[firstPart].Value;
}
if (secondElementType == Type.VARIABLE) // Replace the variable type by the type of his value and the content by the value of the variable
{
secondElementType = Variables[firstPart].Type;
secondPart = Variables[firstPart].Value;
}
if (firstElementType == Type.OPERATION_NUMBER)
{
firstPart = EvaluateOperations(firstPart).ToString();
firstElementType = EvaluateType(firstPart);
}
if (secondElementType == Type.OPERATION_NUMBER)
{
secondPart = EvaluateOperations(secondPart).ToString();
secondElementType = EvaluateType(secondPart);
}
if ((firstElementType != Type.INTEGER && firstElementType != Type.FLOAT) && (secondElementType != Type.INTEGER && secondElementType != Type.FLOAT)) { throw new Exception("Comparaison on unsuportable type"); }
return (firstElementType == secondElementType) && (int.Parse(firstPart) > int.Parse(secondPart)); // Verify that types and value are egual
}
public bool EvaluateInferior(string line)
{
if (EvaluateEgual(line.Replace("<", "=="))) { return false; }
return !(EvaluateSuperior(line.Replace("<", ">"))); // Return the opposite of the > operator
}
public bool EvaluateSuperiorEgual(string line)
{
return EvaluateSuperior(line.Replace(">=", ">")) || EvaluateEgual(line.Replace(">=", "=="));
}
public bool EvaluateInferiorEgual(string line)
{
return EvaluateInferior(line.Replace("<=", "<")) || EvaluateEgual(line.Replace("<=", "=="));
}
public int EvaluateType(string value)
{
if (value == "True" || value == "False")
{
return Type.BOOLEAN;
} else if ((value.StartsWith("'") && value.EndsWith("'")) || (value.StartsWith('"') && value.EndsWith('"')))
{
return Type.STRING; // String
}
else if (int.TryParse(value, out _))
{
return Type.INTEGER; // Integer
}
else if (double.TryParse(value, CultureInfo.InvariantCulture, out _))
{
return Type.FLOAT; // Float
}
else if (Variables.ContainsKey(value))
{
return Type.VARIABLE; // Variable
}
else if (value.Contains('&') || value.Contains('|') || value.Contains("==") || value.Contains('>') || value.Contains('<') || value.Contains(">=") || value.Contains("<="))
{
return Type.OPERATION_BOOL;
}
else if (value.Contains('+') || value.Contains('*') || value.Contains('/') || value.Contains('-') || value.Contains('^'))
{
return Type.OPERATION_NUMBER; // Operation
}
else if (Functions.ContainsKey(value.Split('(')[0]))
{
return Type.FUNCTION; // Function
}
else
{
throw new Exception($"Error. Unable to get the type of {value}");
}
}
}
}