EP Symboliztion +Namespace

This commit is contained in:
ninemine
2025-07-04 23:00:41 +08:00
parent 6783429ab6
commit acada40141
4 changed files with 80 additions and 12 deletions

View File

@@ -18,7 +18,14 @@ namespace Convention.Symbolization.Internal
return this.GetType() == other.GetType(); return this.GetType() == other.GetType();
} }
public abstract Keyword ControlContext(SymbolizationContext context, ScriptWordVariable next); public virtual Keyword ControlContext(SymbolizationContext context, int line, int wordIndex, Variable next)
{
return ControlContext(context, next);
}
protected virtual Keyword ControlContext(SymbolizationContext context, Variable next)
{
return null;
}
} }
public abstract class Keyword<T> : Keyword where T : Keyword<T>, new() public abstract class Keyword<T> : Keyword where T : Keyword<T>, new()
@@ -56,15 +63,19 @@ namespace Convention.Symbolization.Keyword
private ToolFile ImportFile = new("./"); private ToolFile ImportFile = new("./");
private string buffer = ""; private string buffer = "";
public override Internal.Keyword ControlContext(SymbolizationContext context, Internal.ScriptWordVariable next) protected override Internal.Keyword ControlContext(SymbolizationContext context, Internal.Variable next)
{ {
if (next.word == ";") if (next is not Internal.ScriptWordVariable swv)
{
throw new InvalidGrammarException($"Not expected a key word: {next}");
}
if (swv.word == ";")
{ {
var importContext = new SymbolizationContext(context); var importContext = new SymbolizationContext(context);
importContext.Compile(ImportFile); importContext.Compile(ImportFile);
return null; return null;
} }
else if(next.word==".") else if (swv.word == ".")
{ {
ImportFile = ImportFile | buffer; ImportFile = ImportFile | buffer;
buffer = ""; buffer = "";
@@ -73,7 +84,7 @@ namespace Convention.Symbolization.Keyword
} }
else else
{ {
buffer += next.word; buffer += swv.word;
} }
return this; return this;
} }
@@ -87,6 +98,57 @@ namespace Convention.Symbolization.Keyword
public Namespace() : base("namespace") public Namespace() : base("namespace")
{ {
} }
public override Internal.Keyword CloneVariable(string targetSymbolName)
{
return new Namespace();
}
private enum Pause
{
Name, Body
}
private Pause pause = Pause.Name;
private int layer = 0;
private SymbolizationContext bulidingContext = null;
private Dictionary<int, Dictionary<int, Internal.Variable>> subScriptWords = new();
public override Internal.Keyword ControlContext(SymbolizationContext context,int line,int wordIndex, Internal.Variable next)
{
if (next is not Internal.ScriptWordVariable swv)
{
throw new InvalidGrammarException($"Not expected a key word: {next}");
}
if (pause == Pause.Name)
{
bulidingContext = new(context, context.CurrentNamespace.CreateOrGetSubNamespace(swv.word));
bulidingContext.CurrentNamespace.BeginUpdate();
pause = Pause.Body;
}
else
{
if (swv.word == "{")
layer++;
else if (swv.word == "}")
layer--;
if (layer == 0)
{
bulidingContext.Compile(subScriptWords);
bulidingContext.CurrentNamespace.EndAndTryApplyUpdate();
return null;
}
else
{
if (subScriptWords.TryGetValue(line, out var rline) == false)
{
subScriptWords.Add(line, rline = new());
}
rline.Add(wordIndex, next);
}
}
return this;
}
} }
/// <summary> /// <summary>

View File

@@ -45,7 +45,7 @@ namespace Convention.Symbolization.Internal
Structures.Add(structure.SymbolInfo.SymbolName, structure); Structures.Add(structure.SymbolInfo.SymbolName, structure);
} }
public bool EndAndTApplyUpdate() public bool EndAndTryApplyUpdate()
{ {
Updateable--; Updateable--;
if (Updateable == 0) if (Updateable == 0)

View File

@@ -17,15 +17,21 @@ namespace Convention.Symbolization
private readonly SymbolizationContext ParentContext; private readonly SymbolizationContext ParentContext;
public readonly Internal.Namespace CurrentNamespace; public readonly Internal.Namespace CurrentNamespace;
public void Compile(Dictionary<int, Dictionary<int, Internal.Variable>> scriptWords)
{
}
public void Compile(string allText) public void Compile(string allText)
{ {
// Create a new reader to parse the script words // Create a new reader to parse the script words
Internal.SymbolizationReader reader = new(); Internal.SymbolizationReader reader = new();
foreach (char ch in allText) foreach (char ch in allText)
reader.ReadChar(ch); reader.ReadChar(ch);
var ScriptWords = reader.ScriptWords; var scriptWords = reader.ScriptWords;
// Turn the script words into scope words // Turn the script words into scope words
reader.CompleteScopeWord(this); this.Compile(scriptWords);
} }
public void Compile(ToolFile file) public void Compile(ToolFile file)

View File

@@ -13,7 +13,7 @@ namespace Convention.Symbolization.Internal
private string buffer = ""; private string buffer = "";
private int lineContext = 1; private int lineContext = 1;
private bool isOutOfStringline = true; private bool isOutOfStringline = true;
public Dictionary<int, List<Variable>> ScriptWords = new() { { 1, new() } }; public Dictionary<int, Dictionary<int, Variable>> ScriptWords = new() { { 1, new() } };
private static HashSet<char> ControllerCharSet = new() private static HashSet<char> ControllerCharSet = new()
{ {
@@ -76,11 +76,11 @@ namespace Convention.Symbolization.Internal
} }
if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword)) if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword))
{ {
line.Add(keyword.Clone() as Internal.Variable); line.Add(line.Count + 1, keyword.Clone() as Internal.Variable);
} }
else else
{ {
line.Add(new ScriptWordVariable(buffer)); line.Add(line.Count + 1, new ScriptWordVariable(buffer));
} }
buffer = ""; buffer = "";
} }
@@ -127,7 +127,7 @@ namespace Convention.Symbolization.Internal
} }
else else
{ {
currentKey = currentKey.ControlContext(rootContext, word); currentKey = currentKey.ControlContext(rootContext, line.Key, word.Key, word.Value);
} }
wordCounter++; wordCounter++;
} }