EP Symbolization 构建作用域

This commit is contained in:
2025-07-07 00:28:37 +08:00
parent 522010e7cd
commit 71fc384095
4 changed files with 215 additions and 103 deletions

View File

@@ -17,27 +17,49 @@ namespace Convention.Symbolization
private readonly SymbolizationContext ParentContext;
public readonly Internal.Namespace CurrentNamespace;
private void Compile(Internal.SymbolizationReader reader)
private void CompileToParent()
{
reader.CompleteScopeWord(this);
if (ParentContext == null)
return;
}
public void Compile(Dictionary<int, Dictionary<int, Internal.Variable>> scriptWords)
// Compile begin at scope words to sub context
public void Compile(Dictionary<Internal.Keyword, List<Internal.Variable>> scopeWords)
{
// Turn the script words into scope words
Compile(new Internal.SymbolizationReader() { ScriptWords = scriptWords });
foreach (var (keyword, words) in scopeWords)
{
}
}
// Compile begin at script words to scope words
private void CompileBeginAtScript2Scope(Internal.SymbolizationReader reader)
{
foreach (var word in reader.ScriptWords)
{
reader.ReadWord(word);
}
this.Compile(reader.ScopeWords);
}
// Compile begin at script words to scope words
public void Compile(List<Internal.Variable> scriptWords)
{
this.CompileBeginAtScript2Scope(new() { ScriptWords = scriptWords });
}
// Compile begin at text to script words
public void Compile(string allText)
{
// Create a new reader to parse the script words
Internal.SymbolizationReader reader = new();
reader ??= new();
foreach (char ch in allText)
reader.ReadChar(ch);
// Turn the script words into scope words
Compile(reader);
this.CompileBeginAtScript2Scope(reader);
}
// Compile begin at text to script words
public void Compile(ToolFile file)
{
if (file.Exists() == false)

View File

@@ -12,10 +12,11 @@ namespace Convention.Symbolization.Internal
private string buffer = "";
private int lineContext = 1;
private int wordContext = 0;
private bool isOutOfStringline = true;
public Dictionary<int, Dictionary<int, Variable>> ScriptWords = new() { { 1, new() } };
public List<Variable> ScriptWords = new();
private static HashSet<char> ControllerCharSet = new()
private readonly static HashSet<char> ControllerCharSet = new()
{
'{','}','(',')','[',']',
'+','-','*','/','%',
@@ -61,7 +62,6 @@ namespace Convention.Symbolization.Internal
buffer = ch.ToString();
CompleteWord();
}
void PushChar(char ch)
{
buffer += ch;
@@ -70,25 +70,23 @@ namespace Convention.Symbolization.Internal
{
if (buffer.Length > 0)
{
if (ScriptWords.TryGetValue(lineContext, out var line) == false)
{
ScriptWords.Add(lineContext, line = new());
}
if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword))
{
line.Add(line.Count + 1, keyword.Clone() as Internal.Variable);
ScriptWords.Add(keyword.CloneVariable(buffer, lineContext, wordContext));
}
else
{
line.Add(line.Count + 1, new ScriptWordVariable(buffer));
ScriptWords.Add(new ScriptWordVariable(buffer, lineContext, wordContext + 1));
}
buffer = "";
wordContext++;
}
}
void CompleteLine()
{
CompleteWord();
lineContext++; ;
lineContext++;
wordContext = 0;
}
void BeginString()
{
@@ -106,52 +104,31 @@ namespace Convention.Symbolization.Internal
#region Read Scope Words
public class KeywordEntry
{
public int line = 1;
public int wordIndex = 1;
public Dictionary<int, Dictionary<int, Variable>> Container = new();
public KeywordEntry(int line, int wordIndex)
{
this.line = line;
this.wordIndex = wordIndex;
}
}
public Dictionary<Keyword, List<Variable>> ScopeWords = new();
public Keyword currentKey = null;
public bool isNextKeyword = true;
public void CompleteScopeWord(SymbolizationContext rootContext)
public void ReadWord(Variable word)
{
Dictionary<Keyword, KeywordEntry> ScopeWords = new();
Keyword currentKey = null;
bool isNextKeyword = true;
foreach(var line in ScriptWords)
if (isNextKeyword)
{
int wordCounter = 1;
foreach (var word in line.Value)
if (word is Keyword cky)
{
if (isNextKeyword)
{
if (word.Value is Keyword cky)
{
currentKey = cky;
ScopeWords.Add(currentKey, new(line.Key, wordCounter));
isNextKeyword = false;
}
else
{
throw new InvalidGrammarException($"Line {line.Key}, Word {wordCounter}: Expected a keyword, but got {word.Value}");
}
}
else
{
var container = ScopeWords[currentKey].Container;
if (container.ContainsKey(line.Key) == false)
container.Add(line.Key, new() { { wordCounter, word.Value } });
else
container[line.Key][wordCounter] = word.Value;
isNextKeyword = currentKey.ControlScope(rootContext, line.Key, word.Key, word.Value) == false;
}
wordCounter++;
currentKey = cky;
ScopeWords.Add(currentKey, new());
isNextKeyword = false;
}
else
{
throw new InvalidGrammarException(
$"Line {word.SymbolInfo.LineIndex}, Word {word.SymbolInfo.WordIndex}: Expected a keyword, but got {word}"
);
}
}
else
{
ScopeWords[currentKey].Add(word);
isNextKeyword = currentKey.ControlScope(word) == false;
}
}