Skip to content

Fix boucles imbriquées #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions Interpreter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System.Globalization;
using System.Text;

namespace InterpreterLib
Expand Down Expand Up @@ -106,17 +105,17 @@ public void EvaluateCode(string code)

index++;

while (index < lines.Length && lines[index].StartsWith("\t") && !lines[index].EndsWith('}'))
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(lines[index].Replace("\t", "\n"));
functionText.Append($"\n{lines[index][1..]}");
index++;
}

EvaluateFunctionRegister(functionText.ToString());
} else if (trimmedLine.StartsWith("for ")) // for var from x to y {}
{ // Case of the registration of a loop
string loopFirstLine = trimmedLine[4..];
StringBuilder loopText = new StringBuilder();
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
Expand Down Expand Up @@ -145,22 +144,21 @@ public void EvaluateCode(string code)

index++;

while (index < lines.Length && lines[index].StartsWith("\t") && !lines[index].EndsWith('}'))
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.
loopText.Append(lines[index].Replace("\t", "\n"));
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(loopText.ToString());

this.EvaluateCode(loopCode.ToString());
}

}
Expand Down