Add translator to poyo
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace PoyoLang.Translator.SourceGenerator;
|
||||
|
||||
@@ -15,12 +15,12 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
|
||||
// There will be only one of those but incremental generators work as pipelines
|
||||
var dictionaries = texts
|
||||
.Where(static text => text.Path.EndsWith("dictionary.json"))
|
||||
.Where(static text => text.Path.EndsWith("dictionary.txt"))
|
||||
.Select(static (text, _) => text.GetText());
|
||||
|
||||
var parsedDictionaries = dictionaries
|
||||
.Select(static (dictionary, _) =>
|
||||
JsonSerializer.Deserialize<Dictionary<string, string>>(dictionary!.ToString())
|
||||
ReadCustomDictionary(dictionary!)
|
||||
);
|
||||
|
||||
var formattedDictionaries = parsedDictionaries
|
||||
@@ -39,6 +39,28 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
});
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> ReadCustomDictionary(SourceText text)
|
||||
{
|
||||
var dictionary = new Dictionary<string, string>();
|
||||
|
||||
foreach (var line in text.ToString().Split('\n'))
|
||||
{
|
||||
var span = line.TrimEnd('\r').AsSpan();
|
||||
|
||||
// Reached end of file
|
||||
if (span.Length < 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var splitIndex = span.IndexOf('=');
|
||||
|
||||
dictionary[span[..splitIndex].ToString()] = span[(splitIndex + 1)..].ToString();
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
private static List<Node> BuildPrefixTree(Dictionary<string, string> dictionary)
|
||||
{
|
||||
var rootNodes = new List<Node>();
|
||||
@@ -93,6 +115,7 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
|
||||
namespace PoyoLang.Translator;
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -108,8 +131,9 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
// Next letter method definition
|
||||
source.Append(
|
||||
"""
|
||||
public void NextLetter(ref ReadOnlySpan<char> text, StringBuilder output)
|
||||
private void NextLetter(ref ReadOnlySpan<char> text, StringBuilder output)
|
||||
{
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -120,9 +144,10 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var isCaps = char.IsUpper(text[0]);
|
||||
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -131,11 +156,13 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
// Next letter method end
|
||||
source.Append(
|
||||
"""
|
||||
|
||||
// Punctuation/Unknown characters case
|
||||
text = text[1..];
|
||||
|
||||
output.Append(text[0]);
|
||||
|
||||
text = text[1..];
|
||||
}
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -158,6 +185,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
$$"""
|
||||
{{indent}}switch (text[{{depth}}])
|
||||
{{indent}}{
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -170,7 +199,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
source.Append(
|
||||
$$"""
|
||||
{{indent}} case '{{node.Letter}}' or '{{char.ToUpper(node.Letter)}}':
|
||||
{{indent}}
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -181,6 +211,7 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
$$"""
|
||||
{{indent}} if (text.Length > {{depth + 1}})
|
||||
{{indent}} {
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -190,6 +221,7 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
source.Append(
|
||||
$$"""
|
||||
{{indent}} }
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
@@ -203,6 +235,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
{{indent}} output.Append(isCaps ? "{{targetUpper}}" : "{{targetLower}}");
|
||||
{{indent}}
|
||||
{{indent}} return;
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
@@ -210,8 +244,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
// Switch-case end
|
||||
source.Append(
|
||||
$$"""
|
||||
|
||||
{{indent}}}
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user