Skip to content

Commit

Permalink
implement nested lists
Browse files Browse the repository at this point in the history
  • Loading branch information
PocketMiner82 committed Mar 18, 2024
1 parent 0c72e49 commit fd796f6
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions pseudocodeIde/interpreter/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,14 @@ private string parseToken(char prevChar, bool ignoreSpecialCases=false, bool isI
Token possibleLeftBracket = this.peek();
if (possibleLeftBracket.type == LEFT_BRACKET)
{
this.advance();
output += "new() {";
string arrayInitOutput = handleArrayInit(possibleLeftBracket);

while(!this.isAtEnd() && this.peek().type != RIGHT_BRACKET && this.peek().type != NEW_LINE)
if (arrayInitOutput == null)
{
output += this.parseToken(this.advance().lexeme.Last(), true);
}

if (!this.isAtEnd() && this.peek().type == RIGHT_BRACKET)
{
this.advance();
output += "}";

if (this.peek().type == NEW_LINE)
{
output += ";";
}
}
else
{
Logger.error(possibleLeftBracket.line, $"']' erwartet, nicht '{this.currentToken.Value.lexeme}'.\n\n{output}");
return "";
}

output += arrayInitOutput;
}
return output;

Expand All @@ -183,6 +168,47 @@ private string parseToken(char prevChar, bool ignoreSpecialCases=false, bool isI
}
}

private string handleArrayInit(Token possibleLeftBracket, bool noSemicolon = false)
{
this.advance();
string output = "new() {";

while (!this.isAtEnd() && this.peek().type != RIGHT_BRACKET)
{
// ignore new lines
if (this.peek().type == NEW_LINE)
{
this.advance();
continue;
}

if (this.peek().type == LEFT_BRACKET)
{
output += this.handleArrayInit(possibleLeftBracket, true);
}

output += this.parseToken(this.advance().lexeme.Last(), true);
}

if (!this.isAtEnd() && this.peek().type == RIGHT_BRACKET)
{
this.advance();
output += "}";

if ((this.peek().type == NEW_LINE || this.currentToken.Next.Value.type == EOF) && !noSemicolon)
{
output += ";";
}
}
else
{
Logger.error(possibleLeftBracket.line, $"']' erwartet, nicht '{this.currentToken.Value.lexeme}'.\n\n{output}");
return null;
}

return output;
}

private string handleFor()
{
string output = "";
Expand Down

0 comments on commit fd796f6

Please sign in to comment.