Skip to content

Commit

Permalink
add +, - and # before function name (and after OPERATION keyword) to …
Browse files Browse the repository at this point in the history
…allow making functions public, private and protected
  • Loading branch information
PocketMiner82 committed Mar 25, 2024
1 parent 0b9c92d commit 24c7b6b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
30 changes: 23 additions & 7 deletions pseudocodeIde/interpreter/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ namespace pseudocodeIde.interpreter
{
public class Parser
{
private const string FUNCTION_HEADER_TEMPLATE = "private %type% %identifier%(%insideParens%) {";
private const string FUNCTION_HEADER_TEMPLATE = "%visibility% %type% %identifier%(%insideParens%) {";

private static readonly Dictionary<TokenType, string> TOKEN_TO_CSHARP = new Dictionary<TokenType, string>();
private static readonly Dictionary<TokenType, string> TOKEN_TO_CSHARP = new Dictionary<TokenType, string>();

private static readonly Dictionary<TokenType, string> VISIBILITY = new Dictionary<TokenType, string>();

private static readonly List<char> NO_SEMICOLON_AFTER = new List<char>();

Expand Down Expand Up @@ -64,6 +66,11 @@ static Parser()
TOKEN_TO_CSHARP.Add(NULL, "null");


VISIBILITY.Add(PLUS, "public");
VISIBILITY.Add(MINUS, "private");
VISIBILITY.Add(HASH, "protected");


NO_SEMICOLON_AFTER.Add('}');
NO_SEMICOLON_AFTER.Add('{');
NO_SEMICOLON_AFTER.Add('\n');
Expand Down Expand Up @@ -559,11 +566,19 @@ private string handleFunction()
{
string output = "";
string insideParens = "";
string visibility = "";
TokenType type = TYPE_VOID;

Token operationKeyword = this.currentToken.Value;
Token functionKeyword = this.currentToken.Value;

Token possibleVisibility = this.advance();
Token possibleIdentifier = possibleVisibility;
if (VISIBILITY.ContainsKey(possibleVisibility.type))
{
visibility = VISIBILITY[possibleIdentifier.type];
possibleIdentifier = this.advance();
}

Token possibleIdentifier = this.advance();
Token possibleLeftParen = this.advance();

Token possibleRightParen = this.advance();
Expand All @@ -572,7 +587,7 @@ private string handleFunction()

if (possibleIdentifier.type != IDENTIFIER || possibleLeftParen.type != LEFT_PAREN)
{
Logger.error(operationKeyword.line, "Unerwartete Zeichen nach dem OPERATION-Keyword.");
Logger.error(functionKeyword.line, "Unerwartete Zeichen nach dem OPERATION-Keyword.");
return "";
}

Expand All @@ -581,7 +596,7 @@ private string handleFunction()
{
if (possibleRightParen.type == NEW_LINE)
{
Logger.error(operationKeyword.line, "Neue Zeile vor dem Ende der OPERATION-Definition.");
Logger.error(functionKeyword.line, "Neue Zeile vor dem Ende der OPERATION-Definition.");
return "\n";
}
else if (possibleRightParen.type == RIGHT_PAREN
Expand All @@ -605,7 +620,7 @@ private string handleFunction()
}
else
{
Logger.error(operationKeyword.line, "Unerwartete Zeichen.");
Logger.error(functionKeyword.line, "Unerwartete Zeichen.");
return "";
}
}
Expand All @@ -618,6 +633,7 @@ private string handleFunction()
this.isInConstructor = false;

return output + FUNCTION_HEADER_TEMPLATE
.Replace("%visibility%", visibility)
.Replace("%type%", TOKEN_TO_CSHARP[type])
.Replace("%identifier%", "_" + possibleIdentifier.lexeme)
.Replace("%insideParens%", insideParens);
Expand Down
1 change: 1 addition & 0 deletions pseudocodeIde/interpreter/scanner/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private void scanToken()
case '←': this.addToken(VAR_ASSIGN); break;
case '&': this.addToken(SINGLE_AND); break;
case '|': this.addToken(SINGLE_OR); break;
case '#': this.addToken(HASH); break;

// single or two char lexems
case '!':
Expand Down
2 changes: 1 addition & 1 deletion pseudocodeIde/interpreter/scanner/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public enum TokenType
{
// Single-character tokens.
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACKET, RIGHT_BRACKET,
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACKET, RIGHT_BRACKET, HASH,
SINGLE_AND, SINGLE_OR, COLON, COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR, NEW_LINE,

// One or two character tokens.
Expand Down

0 comments on commit 24c7b6b

Please sign in to comment.