EP Symbolization 构建作用域
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using static System.Net.Mime.MediaTypeNames;
|
||||||
|
|
||||||
namespace Convention.Symbolization.Internal
|
namespace Convention.Symbolization.Internal
|
||||||
{
|
{
|
||||||
@@ -10,17 +11,17 @@ namespace Convention.Symbolization.Internal
|
|||||||
|
|
||||||
public static readonly Dictionary<string, Keyword> Keywords = new()
|
public static readonly Dictionary<string, Keyword> Keywords = new()
|
||||||
{
|
{
|
||||||
{ "import", new Convention.Symbolization.Keyword.Import() },
|
{ "import", new Convention.Symbolization.Keyword.Import(0,0) },
|
||||||
{ "namespace", new Convention.Symbolization.Keyword.Namespace() },
|
{ "namespace", new Convention.Symbolization.Keyword.Namespace(0,0) },
|
||||||
{ "def", new Convention.Symbolization.Keyword.FunctionDef() },
|
{ "def", new Convention.Symbolization.Keyword.FunctionDef(0,0) },
|
||||||
{ "return", new Convention.Symbolization.Keyword.Return() },
|
{ "return", new Convention.Symbolization.Keyword.Return(0,0) },
|
||||||
{ "if", new Convention.Symbolization.Keyword.If() },
|
{ "if", new Convention.Symbolization.Keyword.If(0,0) },
|
||||||
{ "elif", new Convention.Symbolization.Keyword.Elif() },
|
{ "elif", new Convention.Symbolization.Keyword.Elif(0,0) },
|
||||||
{ "else", new Convention.Symbolization.Keyword.Else() },
|
{ "else", new Convention.Symbolization.Keyword.Else(0,0) },
|
||||||
{ "while", new Convention.Symbolization.Keyword.While() },
|
{ "while", new Convention.Symbolization.Keyword.While(0,0) },
|
||||||
{ "break", new Convention.Symbolization.Keyword.Break() },
|
{ "break", new Convention.Symbolization.Keyword.Break(0,0) },
|
||||||
{ "continue", new Convention.Symbolization.Keyword.Continue() },
|
{ "continue", new Convention.Symbolization.Keyword.Continue(0,0) },
|
||||||
{ "struct", new Convention.Symbolization.Keyword.Structure() }
|
{ "struct", new Convention.Symbolization.Keyword.Structure(0,0) }
|
||||||
};
|
};
|
||||||
|
|
||||||
public override bool Equals(Keyword other)
|
public override bool Equals(Keyword other)
|
||||||
@@ -28,14 +29,9 @@ namespace Convention.Symbolization.Internal
|
|||||||
return this.GetType() == other.GetType();
|
return this.GetType() == other.GetType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool ControlScope(SymbolizationContext context, int line, int wordIndex, Variable next)
|
public abstract bool ControlScope(Variable next);
|
||||||
{
|
|
||||||
return ControlScope(context, next);
|
public abstract void BuildScope(SymbolizationContext context, List<Variable> scopeWords);
|
||||||
}
|
|
||||||
protected virtual bool ControlScope(SymbolizationContext context, Variable next)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class Keyword<T> : Keyword where T : Keyword<T>
|
public abstract class Keyword<T> : Keyword where T : Keyword<T>
|
||||||
@@ -56,11 +52,18 @@ namespace Convention.Symbolization.Keyword
|
|||||||
protected SingleKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
|
protected SingleKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
|
public override bool ControlScope(Internal.Variable next)
|
||||||
{
|
{
|
||||||
if (next is not Internal.ScriptWordVariable swv ||swv.Word!=";")
|
if (next is Internal.ScriptWordVariable swv && swv.Word == ";")
|
||||||
throw new InvalidGrammarException($"Expected ';' but got {next}");
|
|
||||||
return true;
|
return true;
|
||||||
|
throw new InvalidGrammarException($"Expected ';' but got {next}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void BuildScope();
|
||||||
|
|
||||||
|
public sealed override void BuildScope(SymbolizationContext context, List<Internal.Variable> scopeWords)
|
||||||
|
{
|
||||||
|
BuildScope();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,12 +72,19 @@ namespace Convention.Symbolization.Keyword
|
|||||||
protected SentenceKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
|
protected SentenceKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
|
public override bool ControlScope(Internal.Variable next)
|
||||||
{
|
{
|
||||||
if (next is not Internal.ScriptWordVariable swv)
|
if (next is not Internal.ScriptWordVariable swv)
|
||||||
throw new InvalidGrammarException($"Not expected a key Word: {next}");
|
throw new InvalidGrammarException($"Not expected a key Word: {next}");
|
||||||
return swv.Word != ";";
|
return swv.Word != ";";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract void BuildSentence(SymbolizationContext context, List<Internal.Variable> sentence);
|
||||||
|
|
||||||
|
public sealed override void BuildScope(SymbolizationContext context, List<Internal.Variable> scopeWords)
|
||||||
|
{
|
||||||
|
BuildSentence(context, scopeWords.GetRange(0, scopeWords.Count - 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class NamespaceKeyword<T> : Internal.Keyword<T> where T : NamespaceKeyword<T>
|
public abstract class NamespaceKeyword<T> : Internal.Keyword<T> where T : NamespaceKeyword<T>
|
||||||
@@ -90,7 +100,7 @@ namespace Convention.Symbolization.Keyword
|
|||||||
private Pause pause = Pause.Name;
|
private Pause pause = Pause.Name;
|
||||||
private int layer = 0;
|
private int layer = 0;
|
||||||
|
|
||||||
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
|
public override bool ControlScope(Internal.Variable next)
|
||||||
{
|
{
|
||||||
if (pause == Pause.Name)
|
if (pause == Pause.Name)
|
||||||
{
|
{
|
||||||
@@ -124,6 +134,53 @@ namespace Convention.Symbolization.Keyword
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract void BuildSpace(
|
||||||
|
SymbolizationContext context,
|
||||||
|
List<Internal.ScriptWordVariable> spaceExpression,
|
||||||
|
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd,
|
||||||
|
List<Internal.Variable> insideScopeWords
|
||||||
|
);
|
||||||
|
|
||||||
|
public override void BuildScope(SymbolizationContext context, List<Internal.Variable> scopeWords)
|
||||||
|
{
|
||||||
|
List<Internal.ScriptWordVariable> spaceExpression = new();
|
||||||
|
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd = new();
|
||||||
|
Stack<Internal.Variable> stack = new();
|
||||||
|
bool IsSpaceExpression = true;
|
||||||
|
foreach (var word in scopeWords)
|
||||||
|
{
|
||||||
|
var swv = word as Internal.ScriptWordVariable;
|
||||||
|
if (IsSpaceExpression)
|
||||||
|
{
|
||||||
|
if (swv.Word == "{")
|
||||||
|
{
|
||||||
|
IsSpaceExpression = false;
|
||||||
|
stack.Push(swv);
|
||||||
|
layer++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
spaceExpression.Add(swv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (swv.Word == "{")
|
||||||
|
{
|
||||||
|
stack.Push(swv);
|
||||||
|
layer++;
|
||||||
|
}
|
||||||
|
else if (swv.Word == "}")
|
||||||
|
{
|
||||||
|
scopeStartAndEnd.Add(stack.Pop(), swv);
|
||||||
|
layer--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BuildSpace(context, spaceExpression, scopeStartAndEnd,
|
||||||
|
scopeWords.GetRange(spaceExpression.Count, scopeWords.Count - spaceExpression.Count));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class VerbObjectScopeKeyword<T> : Internal.Keyword<T> where T : VerbObjectScopeKeyword<T>
|
public abstract class VerbObjectScopeKeyword<T> : Internal.Keyword<T> where T : VerbObjectScopeKeyword<T>
|
||||||
@@ -140,7 +197,7 @@ namespace Convention.Symbolization.Keyword
|
|||||||
private Pause pause = Pause.BeforeExpression;
|
private Pause pause = Pause.BeforeExpression;
|
||||||
private int layer = 0;
|
private int layer = 0;
|
||||||
|
|
||||||
protected override bool ControlScope(SymbolizationContext context, Internal.Variable next)
|
public override bool ControlScope(Internal.Variable next)
|
||||||
{
|
{
|
||||||
if (pause == Pause.BeforeExpression)
|
if (pause == Pause.BeforeExpression)
|
||||||
{
|
{
|
||||||
@@ -191,6 +248,62 @@ namespace Convention.Symbolization.Keyword
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void BuildScope(SymbolizationContext context, List<Internal.Variable> scopeWords)
|
||||||
|
{
|
||||||
|
pause = Pause.BeforeExpression;
|
||||||
|
layer = 0;
|
||||||
|
foreach (var word in scopeWords)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@@ -17,27 +17,49 @@ namespace Convention.Symbolization
|
|||||||
private readonly SymbolizationContext ParentContext;
|
private readonly SymbolizationContext ParentContext;
|
||||||
public readonly Internal.Namespace CurrentNamespace;
|
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
|
foreach (var (keyword, words) in scopeWords)
|
||||||
Compile(new Internal.SymbolizationReader() { ScriptWords = scriptWords });
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
public void Compile(string allText)
|
||||||
{
|
{
|
||||||
// Create a new reader to parse the script words
|
|
||||||
Internal.SymbolizationReader reader = new();
|
Internal.SymbolizationReader reader = new();
|
||||||
|
reader ??= new();
|
||||||
foreach (char ch in allText)
|
foreach (char ch in allText)
|
||||||
reader.ReadChar(ch);
|
reader.ReadChar(ch);
|
||||||
// Turn the script words into scope words
|
// Turn the script words into scope words
|
||||||
Compile(reader);
|
this.CompileBeginAtScript2Scope(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compile begin at text to script words
|
||||||
public void Compile(ToolFile file)
|
public void Compile(ToolFile file)
|
||||||
{
|
{
|
||||||
if (file.Exists() == false)
|
if (file.Exists() == false)
|
||||||
|
@@ -12,10 +12,11 @@ namespace Convention.Symbolization.Internal
|
|||||||
|
|
||||||
private string buffer = "";
|
private string buffer = "";
|
||||||
private int lineContext = 1;
|
private int lineContext = 1;
|
||||||
|
private int wordContext = 0;
|
||||||
private bool isOutOfStringline = true;
|
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();
|
buffer = ch.ToString();
|
||||||
CompleteWord();
|
CompleteWord();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PushChar(char ch)
|
void PushChar(char ch)
|
||||||
{
|
{
|
||||||
buffer += ch;
|
buffer += ch;
|
||||||
@@ -70,25 +70,23 @@ namespace Convention.Symbolization.Internal
|
|||||||
{
|
{
|
||||||
if (buffer.Length > 0)
|
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))
|
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
|
else
|
||||||
{
|
{
|
||||||
line.Add(line.Count + 1, new ScriptWordVariable(buffer));
|
ScriptWords.Add(new ScriptWordVariable(buffer, lineContext, wordContext + 1));
|
||||||
}
|
}
|
||||||
buffer = "";
|
buffer = "";
|
||||||
|
wordContext++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void CompleteLine()
|
void CompleteLine()
|
||||||
{
|
{
|
||||||
CompleteWord();
|
CompleteWord();
|
||||||
lineContext++; ;
|
lineContext++;
|
||||||
|
wordContext = 0;
|
||||||
}
|
}
|
||||||
void BeginString()
|
void BeginString()
|
||||||
{
|
{
|
||||||
@@ -106,52 +104,31 @@ namespace Convention.Symbolization.Internal
|
|||||||
|
|
||||||
#region Read Scope Words
|
#region Read Scope Words
|
||||||
|
|
||||||
public class KeywordEntry
|
public Dictionary<Keyword, List<Variable>> ScopeWords = new();
|
||||||
{
|
public Keyword currentKey = null;
|
||||||
public int line = 1;
|
public bool isNextKeyword = true;
|
||||||
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 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)
|
|
||||||
{
|
|
||||||
int wordCounter = 1;
|
|
||||||
foreach (var word in line.Value)
|
|
||||||
{
|
{
|
||||||
if (isNextKeyword)
|
if (isNextKeyword)
|
||||||
{
|
{
|
||||||
if (word.Value is Keyword cky)
|
if (word is Keyword cky)
|
||||||
{
|
{
|
||||||
currentKey = cky;
|
currentKey = cky;
|
||||||
ScopeWords.Add(currentKey, new(line.Key, wordCounter));
|
ScopeWords.Add(currentKey, new());
|
||||||
isNextKeyword = false;
|
isNextKeyword = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidGrammarException($"Line {line.Key}, Word {wordCounter}: Expected a keyword, but got {word.Value}");
|
throw new InvalidGrammarException(
|
||||||
|
$"Line {word.SymbolInfo.LineIndex}, Word {word.SymbolInfo.WordIndex}: Expected a keyword, but got {word}"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var container = ScopeWords[currentKey].Container;
|
ScopeWords[currentKey].Add(word);
|
||||||
if (container.ContainsKey(line.Key) == false)
|
isNextKeyword = currentKey.ControlScope(word) == 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++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -10,10 +10,10 @@ public class Program
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var context = new SymbolizationRunner();
|
var runner = new SymbolizationRunner();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
context.Compile("example_script.txt");
|
runner.Compile("example_script.txt");
|
||||||
Console.WriteLine("Script compiled successfully.");
|
Console.WriteLine("Script compiled successfully.");
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException ex)
|
catch (FileNotFoundException ex)
|
||||||
|
Reference in New Issue
Block a user