From be952888550626d433a531cf04b195ca891a9daa Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 11 Dec 2022 21:14:31 +0200 Subject: [PATCH] Utils - 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 --- source/tlang/compiler/codegen/instruction.d | 9 ++++++++- source/tlang/misc/utils.d | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/source/tlang/compiler/codegen/instruction.d b/source/tlang/compiler/codegen/instruction.d index 8b1823c6..bfa88b1d 100644 --- a/source/tlang/compiler/codegen/instruction.d +++ b/source/tlang/compiler/codegen/instruction.d @@ -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~";"; } } diff --git a/source/tlang/misc/utils.d b/source/tlang/misc/utils.d index 1b19bb00..e1e5c4e6 100644 --- a/source/tlang/misc/utils.d +++ b/source/tlang/misc/utils.d @@ -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; } \ No newline at end of file