EP Symbolization 完成ScopeWords解析

This commit is contained in:
ninemine
2025-07-05 15:10:24 +08:00
parent acada40141
commit 8123ed4b15
3 changed files with 206 additions and 105 deletions

View File

@@ -6,104 +6,87 @@ namespace Convention.Symbolization.Internal
{ {
public abstract class Keyword : CloneableVariable<Keyword> public abstract class Keyword : CloneableVariable<Keyword>
{ {
protected Keyword(string keyword) : base(keyword) protected Keyword(string keyword) : base(keyword) { }
{
Keywords.TryAdd(keyword, this);
}
public static readonly Dictionary<string, Keyword> Keywords = new(); public static readonly Dictionary<string, Keyword> Keywords = new()
{
{ "import", new Convention.Symbolization.Keyword.Import() },
{ "namespace", new Convention.Symbolization.Keyword.Namespace() },
{ "def", new Convention.Symbolization.Keyword.FunctionDef() },
{ "return", new Convention.Symbolization.Keyword.Return() },
{ "if", new Convention.Symbolization.Keyword.If() },
{ "elif", new Convention.Symbolization.Keyword.Elif() },
{ "else", new Convention.Symbolization.Keyword.Else() },
{ "while", new Convention.Symbolization.Keyword.While() },
{ "break", new Convention.Symbolization.Keyword.Break() },
{ "continue", new Convention.Symbolization.Keyword.Continue() },
{ "struct", new Convention.Symbolization.Keyword.Structure() }
};
public override bool Equals(Keyword other) public override bool Equals(Keyword other)
{ {
return this.GetType() == other.GetType(); return this.GetType() == other.GetType();
} }
public virtual Keyword ControlContext(SymbolizationContext context, int line, int wordIndex, Variable next) public virtual bool ControlScope(SymbolizationContext context, int line, int wordIndex, Variable next)
{ {
return ControlContext(context, next); return ControlScope(context, next);
} }
protected virtual Keyword ControlContext(SymbolizationContext context, Variable next) protected virtual bool ControlScope(SymbolizationContext context, Variable next)
{ {
return null; return true;
} }
} }
public abstract class Keyword<T> : Keyword where T : Keyword<T>, new() public abstract class Keyword<T> : Keyword where T : Keyword<T>, new()
{ {
private static T MyInstance = new(); protected Keyword(string keyword) : base(keyword) { }
protected Keyword(string keyword) : base(keyword)
{
}
public override bool Equals(Variable other) public override bool Equals(Variable other)
{ {
return MyInstance == other; return other is T;
}
public override Keyword CloneVariable(string targetSymbolName)
{
return new T();
} }
} }
} }
namespace Convention.Symbolization.Keyword namespace Convention.Symbolization.Keyword
{ {
/// <summary> public abstract class SingleKeyword<T> : Internal.Keyword<T> where T : SingleKeyword<T>, new()
/// <b><see langword="import"/> file</b>
/// </summary>
public sealed class Import : Internal.Keyword<Import>
{ {
public Import() : base("import") protected SingleKeyword(string keyword) : base(keyword)
{ {
} }
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
public override Internal.Keyword CloneVariable(string targetSymbolName)
{ {
return new Import(); if (next is not Internal.ScriptWordVariable swv ||swv.word!=";")
} throw new InvalidGrammarException($"Expected ';' but got {next}");
return true;
private ToolFile ImportFile = new("./");
private string buffer = "";
protected override Internal.Keyword ControlContext(SymbolizationContext context, Internal.Variable next)
{
if (next is not Internal.ScriptWordVariable swv)
{
throw new InvalidGrammarException($"Not expected a key word: {next}");
}
if (swv.word == ";")
{
var importContext = new SymbolizationContext(context);
importContext.Compile(ImportFile);
return null;
}
else if (swv.word == ".")
{
ImportFile = ImportFile | buffer;
buffer = "";
if (ImportFile.Exists() == false)
throw new FileNotFoundException($"File path not found: {ImportFile}", ImportFile);
}
else
{
buffer += swv.word;
}
return this;
} }
} }
/// <summary> public abstract class SentenceKeyword<T> : Internal.Keyword<T> where T : SentenceKeyword<T>, new()
/// <b><see langword="namespace"/> name { ... }</b>
/// </summary>
public sealed class Namespace : Internal.Keyword<Namespace>
{ {
public Namespace() : base("namespace") protected SentenceKeyword(string keyword) : base(keyword)
{ {
} }
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
public override Internal.Keyword CloneVariable(string targetSymbolName)
{ {
return new Namespace(); if (next is not Internal.ScriptWordVariable swv)
throw new InvalidGrammarException($"Not expected a key word: {next}");
return swv.word != ";";
} }
}
public abstract class NamespaceKeyword<T> : Internal.Keyword<T> where T : NamespaceKeyword<T>, new()
{
protected NamespaceKeyword(string keyword) : base(keyword)
{
}
private enum Pause private enum Pause
{ {
Name, Body Name, Body
@@ -111,50 +94,134 @@ namespace Convention.Symbolization.Keyword
private Pause pause = Pause.Name; private Pause pause = Pause.Name;
private int layer = 0; 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) protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
{ {
if (next is not Internal.ScriptWordVariable swv)
{
throw new InvalidGrammarException($"Not expected a key word: {next}");
}
if (pause == Pause.Name) if (pause == Pause.Name)
{ {
bulidingContext = new(context, context.CurrentNamespace.CreateOrGetSubNamespace(swv.word)); if (next is Internal.ScriptWordVariable swv)
bulidingContext.CurrentNamespace.BeginUpdate();
pause = Pause.Body;
}
else
{
if (swv.word == "{")
layer++;
else if (swv.word == "}")
layer--;
if (layer == 0)
{ {
bulidingContext.Compile(subScriptWords); if (swv.word == "{")
bulidingContext.CurrentNamespace.EndAndTryApplyUpdate(); {
return null; pause = Pause.Body;
layer++;
}
return true;
} }
else else
{ {
if (subScriptWords.TryGetValue(line, out var rline) == false) throw new InvalidGrammarException($"Expected a script word variable for namespace name, but got {next}");
{
subScriptWords.Add(line, rline = new());
}
rline.Add(wordIndex, next);
} }
} }
return this; else
{
if (next is Internal.ScriptWordVariable swv)
{
if (swv.word == "{")
layer++;
else if (swv.word == "}")
layer--;
if (layer == 0)
{
return false;
}
}
return true;
}
}
}
public abstract class VerbObjectScopeKeyword<T> : Internal.Keyword<T> where T : VerbObjectScopeKeyword<T>, new()
{
protected VerbObjectScopeKeyword(string keyword) : base(keyword)
{
}
private enum Pause
{
BeforeExpression, Expression, BeforeBody, Body, SingleSentence
}
private Pause pause = Pause.BeforeExpression;
private int layer = 0;
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
{
if (pause == Pause.BeforeExpression)
{
if (next is not Internal.ScriptWordVariable swv)
throw new InvalidGrammarException($"Not expected a key word: {next}");
if (swv.word == "(")
pause = Pause.Expression;
else
throw new InvalidGrammarException($"Expected '(' symbol for expression start, but got {next}");
}
else if (pause == Pause.Expression)
{
if (next is Internal.ScriptWordVariable swv && swv.word == ")")
{
pause = Pause.BeforeBody;
}
}
else if (pause == Pause.BeforeBody)
{
if (next is not Internal.ScriptWordVariable swv)
throw new InvalidGrammarException($"Not expected keyword for body start, but got {next}");
if (swv.word == "{")
{
pause = Pause.Body;
layer++;
}
else
pause = Pause.SingleSentence;
}
else if (pause == Pause.Body)
{
if (next is Internal.ScriptWordVariable swv)
{
if (swv.word == "{")
layer++;
else if (swv.word == "}")
layer--;
if (layer == 0)
return false;
}
}
else if (pause == Pause.SingleSentence)
{
if (next is Internal.ScriptWordVariable swv && swv.word == ";")
{
return false;
}
}
return true;
}
}
/// <summary>
/// <b><see langword="import"/> file</b>
/// </summary>
public sealed class Import : SentenceKeyword<Import>
{
public Import() : base("import")
{
}
}
/// <summary>
/// <b><see langword="namespace"/> name { ... }</b>
/// </summary>
public sealed class Namespace : NamespaceKeyword<Namespace>
{
public Namespace() : base("namespace")
{
} }
} }
/// <summary> /// <summary>
/// <b><see langword="def"/> FunctionName(parameter-list) return-type { ... return return-type-instance; }</b> /// <b><see langword="def"/> FunctionName(parameter-list) return-type { ... return return-type-instance; }</b>
/// </summary> /// </summary>
public sealed class FunctionDef : Internal.Keyword<FunctionDef> public sealed class FunctionDef : NamespaceKeyword<FunctionDef>
{ {
public FunctionDef() : base("def") public FunctionDef() : base("def")
{ {
@@ -164,7 +231,7 @@ namespace Convention.Symbolization.Keyword
/// <summary> /// <summary>
/// <b><see langword="return"/> symbol;</b> /// <b><see langword="return"/> symbol;</b>
/// </summary> /// </summary>
public sealed class Return : Internal.Keyword<Return> public sealed class Return : SentenceKeyword<Return>
{ {
public Return() : base("return") public Return() : base("return")
{ {
@@ -174,7 +241,7 @@ namespace Convention.Symbolization.Keyword
/// <summary> /// <summary>
/// <b><see langword="if"/>(bool-expression) expression</b> /// <b><see langword="if"/>(bool-expression) expression</b>
/// </summary> /// </summary>
public sealed class If : Internal.Keyword<If> public sealed class If : VerbObjectScopeKeyword<If>
{ {
public If() : base("if") public If() : base("if")
{ {
@@ -182,9 +249,21 @@ namespace Convention.Symbolization.Keyword
} }
/// <summary> /// <summary>
/// <b><see langword="if"/> expression <see langword="else"/> expression</b> /// <b><see langword="if"/> expression <see langword="elif"/> expression</b><para></para>
/// <b><see langword="if"/> expression <see langword="elif"/> expression <see langword="elif"/> expression...</b>
/// </summary> /// </summary>
public sealed class Else : Internal.Keyword<Else> public sealed class Elif : VerbObjectScopeKeyword<Elif>
{
public Elif() : base("elif")
{
}
}
/// <summary>
/// <b><see langword="if"/> expression <see langword="else"/> expression</b><para></para>
/// <b><see langword="if"/> expression <see langword="elif"/> expression... <see langword="else"/> expression</b>
/// </summary>
public sealed class Else : NamespaceKeyword<Else>
{ {
public Else() : base("else") public Else() : base("else")
{ {
@@ -194,7 +273,7 @@ namespace Convention.Symbolization.Keyword
/// <summary> /// <summary>
/// <b><see langword="while"/>(bool-expression) expression</b> /// <b><see langword="while"/>(bool-expression) expression</b>
/// </summary> /// </summary>
public sealed class While : Internal.Keyword<While> public sealed class While : VerbObjectScopeKeyword<While>
{ {
public While() : base("while") public While() : base("while")
{ {
@@ -204,7 +283,7 @@ namespace Convention.Symbolization.Keyword
/// <summary> /// <summary>
/// <b><see langword="break"/>;</b> /// <b><see langword="break"/>;</b>
/// </summary> /// </summary>
public sealed class Break : Internal.Keyword<Break> public sealed class Break : SingleKeyword<Break>
{ {
public Break() : base("break") public Break() : base("break")
{ {
@@ -214,7 +293,7 @@ namespace Convention.Symbolization.Keyword
/// <summary> /// <summary>
/// <b><see langword="continue"/>;</b> /// <b><see langword="continue"/>;</b>
/// </summary> /// </summary>
public sealed class Continue : Internal.Keyword<Continue> public sealed class Continue : SingleKeyword<Continue>
{ {
public Continue() : base("continue") public Continue() : base("continue")
{ {
@@ -224,7 +303,7 @@ namespace Convention.Symbolization.Keyword
/// <summary> /// <summary>
/// <b><see langword="struct"/> structureName { ... }</b> /// <b><see langword="struct"/> structureName { ... }</b>
/// </summary> /// </summary>
public sealed class Structure : Internal.Keyword<Structure> public sealed class Structure : NamespaceKeyword<Structure>
{ {
public Structure() : base("struct") public Structure() : base("struct")
{ {

View File

@@ -20,9 +20,9 @@ namespace Convention.Symbolization
public void Compile(Dictionary<int, Dictionary<int, Internal.Variable>> scriptWords) public void Compile(Dictionary<int, Dictionary<int, Internal.Variable>> scriptWords)
{ {
new Internal.SymbolizationReader() { ScriptWords = scriptWords }.CompleteScopeWord(this);
} }
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
@@ -31,7 +31,7 @@ namespace Convention.Symbolization
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
this.Compile(scriptWords); reader.CompleteScopeWord(this);
} }
public void Compile(ToolFile file) public void Compile(ToolFile file)

View File

@@ -106,28 +106,50 @@ namespace Convention.Symbolization.Internal
#region Read Scope Words #region Read Scope Words
private 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;
}
}
private Dictionary<Keyword, KeywordEntry> ScopeWords = new();
public void CompleteScopeWord(SymbolizationContext rootContext) public void CompleteScopeWord(SymbolizationContext rootContext)
{ {
int wordCounter = 1; Keyword currentKey = null;
Internal.Keyword currentKey = null; bool isNextKeyword = true;
foreach(var line in ScriptWords) foreach(var line in ScriptWords)
{ {
int wordCounter = 1;
foreach (var word in line.Value) foreach (var word in line.Value)
{ {
if (currentKey == null) if (isNextKeyword)
{ {
if(currentKey is Internal.Keyword cky) if (word.Value is Keyword cky)
{ {
currentKey = cky; currentKey = cky;
ScopeWords.Add(currentKey, new(line.Key, wordCounter));
isNextKeyword = false;
} }
else else
{ {
throw new InvalidGrammarException($"Line {line.Key}, word {wordCounter}: Expected a keyword, but got {word}"); throw new InvalidGrammarException($"Line {line.Key}, word {wordCounter}: Expected a keyword, but got {word.Value}");
} }
} }
else else
{ {
currentKey = currentKey.ControlContext(rootContext, line.Key, word.Key, word.Value); 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++; wordCounter++;
} }