- Added a method which will replace the `.`s in a symbol with underscores

Instruction

- The symbol names need to be valid in C so let's just make it a rule they will only have _'s (underscores) to replace any characters like `.`s
This commit is contained in:
Tristan B. Velloza Kildaire 2022-12-11 21:14:31 +02:00
parent 585405d9e9
commit be95288855
2 changed files with 25 additions and 1 deletions

View File

@ -3,6 +3,7 @@ module compiler.codegen.instruction;
import std.conv : to;
import compiler.typecheck.dependency.core : Context;
import std.string : cmp;
import misc.utils : symbolRename;
public class Instruction
{
@ -99,7 +100,13 @@ public final class VariableDeclaration : StorageDeclaration
auto typedEntityVariable = context.tc.getResolver().resolveBest(context.getContainer(), varName); //TODO: Remove `auto`
string typedEntityVariableName = context.tc.getResolver().generateName(context.getContainer(), typedEntityVariable);
return varType~" "~typedEntityVariableName~";";
//NOTE: We should remove all dots from generated symbol names as it won't be valid C (I don't want to say C because
// a custom CodeEmitter should be allowed, so let's call it a general rule)
//
//simple_variables.x -> simple_variables_x
string renamedSymbol = symbolRename(typedEntityVariableName);
return varType~" "~renamedSymbol~";";
}
}

View File

@ -1,6 +1,7 @@
module misc.utils;
import std.string : cmp;
import std.array : replace;
public bool isPresent(string[] arr, string t)
{
@ -29,4 +30,20 @@ public bool isCharacterAlpha(char character)
public bool isCharacterNumber(char character)
{
return (character >= 48 && character <= 57);
}
/**
* Takes in a symbol name (string) and replaces
* all the "."s with an underscore as to make
* the names ready for ceoe emitting
*
* Params:
* symbolIn = The symbol name to transform
* Returns: The transformed symbol name
*/
public string symbolRename(string symbolIn)
{
string symbolOut = replace(symbolIn, ".", "_");
return symbolOut;
}