Templates are working, but the code is ugly af

This commit is contained in:
nub31
2025-07-09 22:53:45 +02:00
parent 8a5dd88446
commit 9a22198e49
11 changed files with 417 additions and 28 deletions

View File

@@ -1,38 +0,0 @@
namespace NubLang.CLI;
internal static class HexString
{
private static readonly HashSet<string> _used = [];
private static readonly char[] StringChars = "0123456789abcdef".ToArray();
public static string CreateUnique(int length)
{
string hex;
while (true)
{
hex = Create(length);
if (_used.Add(hex))
{
break;
}
}
return hex;
}
public static string Create(int length)
{
var rand = new Random();
var hexString = "";
for (var i = 0; i < length; i++)
{
var randIndex = rand.Next(0, StringChars.Length);
hexString += StringChars[randIndex];
}
return hexString;
}
}

View File

@@ -1,6 +1,7 @@
using System.Reflection;
using NubLang.CLI;
using NubLang;
using NubLang.Common;
using NubLang.Diagnostics;
using NubLang.Generation;
using NubLang.Generation.QBE;
@@ -8,6 +9,7 @@ using NubLang.Syntax.Binding;
using NubLang.Syntax.Binding.Node;
using NubLang.Syntax.Parsing;
using NubLang.Syntax.Parsing.Node;
using NubLang.Syntax.Templating;
using NubLang.Syntax.Tokenization;
using Binder = NubLang.Syntax.Binding.Binder;
@@ -93,6 +95,15 @@ foreach (var file in options.Files)
syntaxTrees[file] = syntaxTree;
}
var templateTable = new TemplateTable(syntaxTrees.Values);
foreach (var (file, syntaxTree) in syntaxTrees)
{
var templateGenerator = new TemplateGenerator(syntaxTree, templateTable);
var definitions = templateGenerator.Generate();
syntaxTrees[file] = syntaxTree with { Definitions = syntaxTree.Definitions.Concat(definitions).ToList() };
}
var definitionTable = new DefinitionTable(syntaxTrees.Values);
var boundSyntaxTrees = new Dictionary<string, BoundSyntaxTree>();