diff --git a/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs b/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs index d5f11da..eb6f4f7 100644 --- a/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs +++ b/Nub.Lang/Nub.Lang/Backend/Custom/Generator.cs @@ -10,12 +10,14 @@ public class Generator private readonly List _definitions; private readonly SymbolTable _symbolTable; private readonly StringBuilder _builder; + private readonly LabelFactory _labelFactory; public Generator(IReadOnlyCollection definitions) { _definitions = []; _builder = new StringBuilder(); - _symbolTable = new SymbolTable(); + _labelFactory = new LabelFactory(); + _symbolTable = new SymbolTable(_labelFactory); foreach (var globalVariableDefinition in definitions.OfType()) { @@ -194,14 +196,14 @@ public class Generator private void GenerateIf(IfNode ifStatement, LocalFunc func) { - var endLabel = _symbolTable.LabelFactory.Create(); + var endLabel = _labelFactory.Create(); GenerateIf(ifStatement, endLabel, func); _builder.AppendLine($"{endLabel}:"); } private void GenerateIf(IfNode ifStatement, string endLabel, LocalFunc func) { - var nextLabel = _symbolTable.LabelFactory.Create(); + var nextLabel = _labelFactory.Create(); GenerateExpression(ifStatement.Condition, func); _builder.AppendLine(" cmp rax, 0"); _builder.AppendLine($" je {nextLabel}"); @@ -455,9 +457,8 @@ public class Generator } case StringType: { - var ident = _symbolTable.LabelFactory.Create(); - _symbolTable.DefineString(ident, literal.Literal); - _builder.AppendLine($" mov rax, {ident}"); + var label = _symbolTable.DefineString(literal.Literal); + _builder.AppendLine($" mov rax, {label}"); break; } case PrimitiveType primitive: diff --git a/Nub.Lang/Nub.Lang/Backend/Custom/LabelFactory.cs b/Nub.Lang/Nub.Lang/Backend/Custom/LabelFactory.cs new file mode 100644 index 0000000..6edd4f4 --- /dev/null +++ b/Nub.Lang/Nub.Lang/Backend/Custom/LabelFactory.cs @@ -0,0 +1,7 @@ +namespace Nub.Lang.Backend.Custom; + +public class LabelFactory +{ + private int _index; + public string Create() => $"label{++_index}"; +} \ No newline at end of file diff --git a/Nub.Lang/Nub.Lang/Backend/Custom/SymbolTable.cs b/Nub.Lang/Nub.Lang/Backend/Custom/SymbolTable.cs index b125c70..9a7824a 100644 --- a/Nub.Lang/Nub.Lang/Backend/Custom/SymbolTable.cs +++ b/Nub.Lang/Nub.Lang/Backend/Custom/SymbolTable.cs @@ -7,18 +7,25 @@ public class SymbolTable { private readonly List _funcDefinitions = []; private readonly List _globalVariables = []; - public LabelFactory LabelFactory { get; } = new(); + private LabelFactory _labelFactory; public readonly Dictionary Strings = []; - public void DefineString(string label, string value) + public SymbolTable(LabelFactory labelFactory) { + _labelFactory = labelFactory; + } + + public string DefineString(string value) + { + var label = _labelFactory.Create(); Strings.Add(label, value); + return label; } public void DefineGlobalVariable(GlobalVariableDefinitionNode globalVariableDefinition) { - var identifier = LabelFactory.Create(); + var identifier = _labelFactory.Create(); _globalVariables.Add(new GlobalVariable(globalVariableDefinition.Name, globalVariableDefinition.Value.Type, identifier)); } @@ -55,8 +62,8 @@ public class SymbolTable throw new Exception($"Func {existing} is already defined"); } - var startLabel = LabelFactory.Create(); - var endLabel = LabelFactory.Create(); + var startLabel = _labelFactory.Create(); + var endLabel = _labelFactory.Create(); _funcDefinitions.Add(new LocalFunc(localFuncDefinition.Name, startLabel, endLabel, localFuncDefinition.Parameters, localFuncDefinition.ReturnType, _globalVariables.Concat(ResolveFuncVariables(localFuncDefinition)).ToList())); } @@ -212,11 +219,4 @@ public class LocalFunc : Func } public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}"; -} - -public class LabelFactory -{ - private int _index; - - public string Create() => $"label{++_index}"; } \ No newline at end of file