Allow identifiers to contain numbers and underscores, however numbers may not be the first character of the identifier

Updated test cases to test this
This commit is contained in:
Tristan B. Velloza Kildaire 2021-03-05 11:27:58 +02:00
parent b0f0aa9361
commit 60516134ff
3 changed files with 49 additions and 11 deletions

View File

@ -414,6 +414,12 @@ public final class Parser
}
/* Almost like parseBody but has more */
/**
* TODO: For certain things like `parseClass` we should
* keep track of what level we are at as we shouldn't allow
* one to define classes within functions
*/
/* TODO: Variables should be allowed to have letters in them and underscores */
public void parse()
{
/* TODO: Do parsing here */

View File

@ -66,17 +66,53 @@ public static bool isType(string tokenStr)
"long") == 0 || cmp(tokenStr, "ulong") == 0 || cmp(tokenStr, "void") == 0;
}
private static bool isAlpha(string token)
/**
* Checks if the given character is a letter
*/
private static bool isCharacterAlpha(char character)
{
return (character >= 65 && character <= 90) || (character >= 97 && character <= 122);
}
/**
* Checks if the given character is a number
*/
private static bool isCharacterNumber(char character)
{
return (character >= 48 && character <= 57);
}
private static bool isIdentifier(string token)
{
/* This is used to prevent the first character from not being number */
bool isFirstRun = true;
foreach (char character; token)
{
if ((character >= 65 && character <= 90) || (character >= 97 && character <= 122))
if(isFirstRun)
{
/* Only allow underscore of letter */
if(isCharacterAlpha(character) || character == '_')
{
}
else
{
return false;
}
isFirstRun = false;
}
else
{
return false;
if(isCharacterAlpha(character) || character == '_' || isCharacterNumber(character))
{
}
else
{
return false;
}
}
}
@ -193,12 +229,8 @@ public static SymbolType getSymbolType(Token tokenIn)
{
return SymbolType.DELETE;
}
/* Identifier check (TODO: Track vars) */
else if (isAlpha(token))
else if (isIdentifier(token))
{
return SymbolType.IDENTIFIER;
}

View File

@ -3,14 +3,14 @@ ubyte y;
ubyte k = 1;
class clazzOne
class clazz1
{
print("Hello world");
}
class clazzTwo
class clazz_2_1
{
}