EP 0.1.1 转换

This commit is contained in:
ninemine
2025-07-04 21:39:02 +08:00
parent 6806d90829
commit 6783429ab6
6 changed files with 134 additions and 67 deletions

View File

@@ -15,8 +15,6 @@ namespace Convention.Symbolization
}
private readonly SymbolizationContext ParentContext;
private Dictionary<int, List<Internal.Variable>> ScriptWords;
public readonly Dictionary<int, List<Internal.Variable>> ScriptCommands = new();
public readonly Internal.Namespace CurrentNamespace;
public void Compile(string allText)
@@ -25,9 +23,9 @@ namespace Convention.Symbolization
Internal.SymbolizationReader reader = new();
foreach (char ch in allText)
reader.ReadChar(ch);
ScriptWords = reader.ScriptWords;
// Turn the script words into commands
// TODO
var ScriptWords = reader.ScriptWords;
// Turn the script words into scope words
reader.CompleteScopeWord(this);
}
public void Compile(ToolFile file)

View File

@@ -8,10 +8,12 @@ namespace Convention.Symbolization.Internal
{
public class SymbolizationReader
{
#region Read Script Words
private string buffer = "";
private int lineContext = 0;
private int lineContext = 1;
private bool isOutOfStringline = true;
public Dictionary<int, List<Variable>> ScriptWords = new() { { 0, new() } };
public Dictionary<int, List<Variable>> ScriptWords = new() { { 1, new() } };
private static HashSet<char> ControllerCharSet = new()
{
@@ -22,6 +24,7 @@ namespace Convention.Symbolization.Internal
'^',
':',',','.','?',/*'\'','"',*/
'@','#','$',
';'
};
public void ReadChar(char ch)
@@ -30,16 +33,16 @@ namespace Convention.Symbolization.Internal
{
if (ControllerCharSet.Contains(ch))
CompleteSingleSymbol(ch);
else if (char.IsWhiteSpace(ch) || ch == '\n' || ch == '\r' || ch == '\t')
else if (ch == '\n')
CompleteLine();
else if (char.IsWhiteSpace(ch) || ch == '\r' || ch == '\t')
CompleteWord();
else if (buffer.Length == 0 && ch == '"')
BeginString();
else if (ch == ';')
CompleteLine();
else if (char.IsLetter(ch) || char.IsDigit(ch))
PushChar(ch);
else
throw new NotImplementedException($"{lineContext+1} line, {buffer} + <{ch}> not implemented");
throw new NotImplementedException($"{lineContext + 1} line, {buffer} + <{ch}> not implemented");
}
else
{
@@ -48,7 +51,7 @@ namespace Convention.Symbolization.Internal
else if (ch == '"')
EndString();
else
throw new NotImplementedException($"{lineContext+1} line, \"{buffer}\" + \'{ch}\' not implemented");
throw new NotImplementedException($"{lineContext + 1} line, \"{buffer}\" + \'{ch}\' not implemented");
}
}
@@ -67,16 +70,25 @@ namespace Convention.Symbolization.Internal
{
if (buffer.Length > 0)
{
ScriptWords[lineContext].Add(new ScriptWordVariable(buffer));
if (ScriptWords.TryGetValue(lineContext, out var line) == false)
{
ScriptWords.Add(lineContext, line = new());
}
if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword))
{
line.Add(keyword.Clone() as Internal.Variable);
}
else
{
line.Add(new ScriptWordVariable(buffer));
}
buffer = "";
}
}
void CompleteLine()
{
CompleteWord();
ScriptWords[lineContext].Add(new ScriptWordVariable(";"));
lineContext++;
ScriptWords.Add(lineContext, new());
lineContext++; ;
}
void BeginString()
{
@@ -89,5 +101,39 @@ namespace Convention.Symbolization.Internal
isOutOfStringline = true;
CompleteWord();
}
#endregion
#region Read Scope Words
public void CompleteScopeWord(SymbolizationContext rootContext)
{
int wordCounter = 1;
Internal.Keyword currentKey = null;
foreach(var line in ScriptWords)
{
foreach (var word in line.Value)
{
if (currentKey == null)
{
if(currentKey is Internal.Keyword cky)
{
currentKey = cky;
}
else
{
throw new InvalidGrammarException($"Line {line.Key}, word {wordCounter}: Expected a keyword, but got {word}");
}
}
else
{
currentKey = currentKey.ControlContext(rootContext, word);
}
wordCounter++;
}
}
}
#endregion
}
}