Files
Convention-CSharp/Convention/[Symbolization]/Detail/Keyword.cs

525 lines
18 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2025-07-07 00:28:37 +08:00
using System.IO;
2025-07-02 15:21:26 +08:00
namespace Convention.Symbolization.Internal
{
2025-07-04 21:39:02 +08:00
public abstract class Keyword : CloneableVariable<Keyword>
2025-07-02 15:21:26 +08:00
{
2025-07-05 16:57:59 +08:00
protected Keyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex) { }
2025-07-02 15:21:26 +08:00
public static readonly Dictionary<string, Keyword> Keywords = new()
{
2025-07-07 00:28:37 +08:00
{ "import", new Convention.Symbolization.Keyword.Import(0,0) },
{ "namespace", new Convention.Symbolization.Keyword.Namespace(0,0) },
{ "def", new Convention.Symbolization.Keyword.FunctionDef(0,0) },
{ "return", new Convention.Symbolization.Keyword.Return(0,0) },
{ "if", new Convention.Symbolization.Keyword.If(0,0) },
{ "elif", new Convention.Symbolization.Keyword.Elif(0,0) },
{ "else", new Convention.Symbolization.Keyword.Else(0,0) },
{ "while", new Convention.Symbolization.Keyword.While(0,0) },
{ "break", new Convention.Symbolization.Keyword.Break(0,0) },
{ "continue", new Convention.Symbolization.Keyword.Continue(0,0) },
{ "struct", new Convention.Symbolization.Keyword.Structure(0,0) }
};
2025-07-04 21:39:02 +08:00
public override bool Equals(Keyword other)
{
return this.GetType() == other.GetType();
}
2025-07-07 00:28:37 +08:00
public abstract bool ControlScope(Variable next);
public abstract void BuildScope(SymbolizationContext context, List<Variable> scopeWords);
2025-07-02 15:21:26 +08:00
}
2025-07-05 16:57:59 +08:00
public abstract class Keyword<T> : Keyword where T : Keyword<T>
2025-07-02 15:21:26 +08:00
{
2025-07-07 00:28:37 +08:00
protected Keyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex) { }
2025-07-02 15:21:26 +08:00
public override bool Equals(Variable other)
2025-07-02 15:21:26 +08:00
{
return other is T;
2025-07-02 15:21:26 +08:00
}
}
}
namespace Convention.Symbolization.Keyword
{
2025-07-05 16:57:59 +08:00
public abstract class SingleKeyword<T> : Internal.Keyword<T> where T : SingleKeyword<T>
2025-07-02 21:20:29 +08:00
{
2025-07-07 00:28:37 +08:00
protected SingleKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
2025-07-02 21:20:29 +08:00
{
}
2025-07-07 00:28:37 +08:00
public override bool ControlScope(Internal.Variable next)
2025-07-04 21:39:02 +08:00
{
2025-07-07 00:28:37 +08:00
if (next is Internal.ScriptWordVariable swv && swv.Word == ";")
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();
2025-07-04 21:39:02 +08:00
}
}
2025-07-04 21:39:02 +08:00
2025-07-05 16:57:59 +08:00
public abstract class SentenceKeyword<T> : Internal.Keyword<T> where T : SentenceKeyword<T>
{
2025-07-07 00:28:37 +08:00
protected SentenceKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
{
}
2025-07-07 00:28:37 +08:00
public override bool ControlScope(Internal.Variable next)
2025-07-04 21:39:02 +08:00
{
2025-07-04 23:00:41 +08:00
if (next is not Internal.ScriptWordVariable swv)
2025-07-05 16:57:59 +08:00
throw new InvalidGrammarException($"Not expected a key Word: {next}");
return swv.Word != ";";
}
2025-07-07 00:28:37 +08:00
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));
}
}
2025-07-05 16:57:59 +08:00
public abstract class NamespaceKeyword<T> : Internal.Keyword<T> where T : NamespaceKeyword<T>
{
2025-07-07 00:28:37 +08:00
protected NamespaceKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
{
}
private enum Pause
{
Name, Body
}
private Pause pause = Pause.Name;
private int layer = 0;
2025-07-07 00:28:37 +08:00
public override bool ControlScope(Internal.Variable next)
{
if (pause == Pause.Name)
2025-07-04 21:39:02 +08:00
{
if (next is Internal.ScriptWordVariable swv)
{
2025-07-05 16:57:59 +08:00
if (swv.Word == "{")
{
pause = Pause.Body;
layer++;
}
return true;
}
else
{
2025-07-05 16:57:59 +08:00
throw new InvalidGrammarException($"Expected a script Word variable for namespace name, but got {next}");
}
2025-07-04 21:39:02 +08:00
}
else
{
if (next is Internal.ScriptWordVariable swv)
{
2025-07-05 16:57:59 +08:00
if (swv.Word == "{")
layer++;
2025-07-05 16:57:59 +08:00
else if (swv.Word == "}")
layer--;
if (layer == 0)
{
return false;
}
}
return true;
2025-07-04 21:39:02 +08:00
}
}
2025-07-07 00:28:37 +08:00
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));
}
2025-07-02 21:20:29 +08:00
}
2025-07-05 16:57:59 +08:00
public abstract class VerbObjectScopeKeyword<T> : Internal.Keyword<T> where T : VerbObjectScopeKeyword<T>
2025-07-02 15:21:26 +08:00
{
2025-07-07 00:28:37 +08:00
protected VerbObjectScopeKeyword(string keyword, int lineIndex, int wordIndex) : base(keyword, lineIndex, wordIndex)
2025-07-02 15:21:26 +08:00
{
}
2025-07-04 23:00:41 +08:00
private enum Pause
{
BeforeExpression, Expression, BeforeBody, Body, SingleSentence
2025-07-04 23:00:41 +08:00
}
private Pause pause = Pause.BeforeExpression;
2025-07-04 23:00:41 +08:00
private int layer = 0;
2025-07-07 00:28:37 +08:00
public override bool ControlScope(Internal.Variable next)
2025-07-04 23:00:41 +08:00
{
if (pause == Pause.BeforeExpression)
2025-07-04 23:00:41 +08:00
{
if (next is not Internal.ScriptWordVariable swv)
2025-07-05 16:57:59 +08:00
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}");
2025-07-04 23:00:41 +08:00
}
else if (pause == Pause.Expression)
2025-07-04 23:00:41 +08:00
{
2025-07-05 16:57:59 +08:00
if (next is Internal.ScriptWordVariable swv && swv.Word == ")")
{
pause = Pause.BeforeBody;
}
2025-07-04 23:00:41 +08:00
}
else if (pause == Pause.BeforeBody)
2025-07-04 23:00:41 +08:00
{
2025-07-08 00:17:44 +08:00
if (next is Internal.ScriptWordVariable swv && swv.Word == "{")
2025-07-04 23:00:41 +08:00
{
pause = Pause.Body;
layer++;
2025-07-04 23:00:41 +08:00
}
else
pause = Pause.SingleSentence;
}
else if (pause == Pause.Body)
{
if (next is Internal.ScriptWordVariable swv)
2025-07-04 23:00:41 +08:00
{
2025-07-05 16:57:59 +08:00
if (swv.Word == "{")
layer++;
2025-07-05 16:57:59 +08:00
else if (swv.Word == "}")
layer--;
if (layer == 0)
return false;
}
}
else if (pause == Pause.SingleSentence)
{
2025-07-05 16:57:59 +08:00
if (next is Internal.ScriptWordVariable swv && swv.Word == ";")
{
return false;
2025-07-04 23:00:41 +08:00
}
}
return true;
}
2025-07-07 00:28:37 +08:00
2025-07-08 00:17:44 +08:00
public abstract void BuildSpace(
SymbolizationContext context,
List<Internal.Variable> spaceExpression,
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd,
List<Internal.Variable> insideScopeWords
);
2025-07-07 00:28:37 +08:00
public override void BuildScope(SymbolizationContext context, List<Internal.Variable> scopeWords)
{
2025-07-08 00:17:44 +08:00
var pause = Pause.BeforeExpression;
List<Internal.Variable> expression = new();
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd = new();
List<Internal.Variable> insideScopeWords = new();
Stack<Internal.Variable> stack = new();
2025-07-07 00:28:37 +08:00
foreach (var word in scopeWords)
{
if (pause == Pause.BeforeExpression)
{
2025-07-08 00:17:44 +08:00
pause = Pause.Expression;
2025-07-07 00:28:37 +08:00
}
else if (pause == Pause.Expression)
{
2025-07-08 00:17:44 +08:00
if (word is Internal.ScriptWordVariable swv && swv.Word == ")")
2025-07-07 00:28:37 +08:00
{
pause = Pause.BeforeBody;
}
2025-07-08 00:17:44 +08:00
else
{
expression.Add(word);
}
2025-07-07 00:28:37 +08:00
}
else if (pause == Pause.BeforeBody)
{
2025-07-08 00:17:44 +08:00
if (word is Internal.ScriptWordVariable swv && swv.Word == "{")
2025-07-07 00:28:37 +08:00
{
pause = Pause.Body;
2025-07-08 00:17:44 +08:00
stack.Push(word);
2025-07-07 00:28:37 +08:00
}
else
2025-07-08 00:17:44 +08:00
{
2025-07-07 00:28:37 +08:00
pause = Pause.SingleSentence;
2025-07-08 00:17:44 +08:00
insideScopeWords.Add(word);
}
2025-07-07 00:28:37 +08:00
}
else if (pause == Pause.Body)
{
2025-07-08 00:17:44 +08:00
if (word is Internal.ScriptWordVariable swv)
2025-07-07 00:28:37 +08:00
{
if (swv.Word == "{")
2025-07-08 00:17:44 +08:00
stack.Push(word);
2025-07-07 00:28:37 +08:00
else if (swv.Word == "}")
2025-07-08 00:17:44 +08:00
scopeStartAndEnd.Add(stack.Pop(), word);
2025-07-07 00:28:37 +08:00
}
2025-07-08 00:17:44 +08:00
else
insideScopeWords.Add(word);
2025-07-07 00:28:37 +08:00
}
else if (pause == Pause.SingleSentence)
{
2025-07-08 00:17:44 +08:00
insideScopeWords.Add(word);
2025-07-07 00:28:37 +08:00
}
}
2025-07-08 00:17:44 +08:00
BuildSpace(context, expression, scopeStartAndEnd, insideScopeWords);
2025-07-07 00:28:37 +08:00
}
}
/// <summary>
/// <b><see langword="import"/> file</b>
/// </summary>
public sealed class Import : SentenceKeyword<Import>
{
2025-07-07 00:28:37 +08:00
public Import(int lineIndex, int wordIndex) : base("import", lineIndex, wordIndex)
{
}
2025-07-05 16:57:59 +08:00
2025-07-08 00:17:44 +08:00
public override void BuildSentence(SymbolizationContext context, List<Internal.Variable> sentence)
{
ToolFile importPath = new(string.Join('/', sentence.ConvertAll(x => (x as Internal.ScriptWordVariable).Word)));
if(importPath.Exists()==false)
throw new FileNotFoundException(importPath.ToString());
new SymbolizationContext(context).Compile(importPath);
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
{
return new Import(lineIndex, wordIndex);
}
}
/// <summary>
/// <b><see langword="namespace"/> name { ... }</b>
/// </summary>
public sealed class Namespace : NamespaceKeyword<Namespace>
{
2025-07-07 00:28:37 +08:00
public Namespace(int lineIndex, int wordIndex) : base("namespace", lineIndex, wordIndex)
{
2025-07-04 23:00:41 +08:00
}
2025-07-08 00:17:44 +08:00
public override void BuildSpace(
SymbolizationContext context,
List<Internal.ScriptWordVariable> spaceExpression,
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd,
List<Internal.Variable> insideScopeWords
)
{
//TODO
//将命名空间放在新的子上下文中编译
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
{
return new Namespace(lineIndex, wordIndex);
}
2025-07-02 21:20:29 +08:00
}
/// <summary>
2025-07-04 21:39:02 +08:00
/// <b><see langword="def"/> FunctionName(parameter-list) return-type { ... return return-type-instance; }</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class FunctionDef : NamespaceKeyword<FunctionDef>
2025-07-02 21:20:29 +08:00
{
2025-07-07 00:28:37 +08:00
public FunctionDef(int lineIndex, int wordIndex) : base("def", lineIndex, wordIndex)
2025-07-05 16:57:59 +08:00
{
}
2025-07-08 00:17:44 +08:00
public override void BuildSpace(
SymbolizationContext context,
List<Internal.ScriptWordVariable> spaceExpression,
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd,
List<Internal.Variable> insideScopeWords
)
{
//TODO
//套用语法规则实现表达式向可执行语句的转换
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
2025-07-02 15:21:26 +08:00
{
2025-07-05 16:57:59 +08:00
return new FunctionDef(lineIndex, wordIndex);
2025-07-02 15:21:26 +08:00
}
}
2025-07-02 21:20:29 +08:00
/// <summary>
2025-07-03 18:46:52 +08:00
/// <b><see langword="return"/> symbol;</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class Return : SentenceKeyword<Return>
2025-07-02 15:21:26 +08:00
{
2025-07-07 00:28:37 +08:00
public Return(int lineIndex, int wordIndex) : base("return", lineIndex, wordIndex)
2025-07-02 15:21:26 +08:00
{
}
2025-07-08 00:17:44 +08:00
public override void BuildSentence(SymbolizationContext context, List<Internal.Variable> sentence)
{
var symbol = sentence[0];
//TODO
//直接转换为可执行语句
//在上下文中搜索被返回的symbol, 存在则返回对应对象, 不存在则返回symbol对象
//运行时检查返回值是否满足函数签名的返回值, 否则抛出异常
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
{
return new Return(lineIndex, wordIndex);
}
2025-07-02 21:20:29 +08:00
}
2025-07-02 18:46:09 +08:00
2025-07-02 21:20:29 +08:00
/// <summary>
2025-07-03 18:46:52 +08:00
/// <b><see langword="if"/>(bool-expression) expression</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class If : VerbObjectScopeKeyword<If>
2025-07-02 21:20:29 +08:00
{
2025-07-07 00:28:37 +08:00
public If(int lineIndex, int wordIndex) : base("if", lineIndex, wordIndex)
2025-07-05 16:57:59 +08:00
{
}
2025-07-08 00:17:44 +08:00
public override void BuildSpace(
SymbolizationContext context,
List<Internal.Variable> spaceExpression,
Dictionary<Internal.Variable, Internal.Variable> scopeStartAndEnd,
List<Internal.Variable> insideScopeWords
)
{
//TODO
//检查表达式是否成功, 成功则套用函数翻译规则将作用域翻译为可执行语句
//否则跳转到末尾
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
2025-07-02 15:21:26 +08:00
{
2025-07-05 16:57:59 +08:00
return new If(lineIndex, wordIndex);
2025-07-02 18:46:09 +08:00
}
}
2025-07-02 21:20:29 +08:00
/// <summary>
/// <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>
public sealed class Elif : VerbObjectScopeKeyword<Elif>
{
2025-07-07 00:28:37 +08:00
public Elif(int lineIndex, int wordIndex) : base("elif", lineIndex, wordIndex)
2025-07-05 16:57:59 +08:00
{
}
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
{
2025-07-05 16:57:59 +08:00
return new Elif(lineIndex, wordIndex);
}
}
/// <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>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class Else : NamespaceKeyword<Else>
2025-07-02 18:46:09 +08:00
{
2025-07-07 00:28:37 +08:00
public Else(int lineIndex, int wordIndex) : base("else", lineIndex, wordIndex)
2025-07-02 18:46:09 +08:00
{
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
{
return new Else(lineIndex, wordIndex);
}
2025-07-02 21:20:29 +08:00
}
/// <summary>
2025-07-03 18:46:52 +08:00
/// <b><see langword="while"/>(bool-expression) expression</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class While : VerbObjectScopeKeyword<While>
2025-07-02 21:20:29 +08:00
{
2025-07-07 00:28:37 +08:00
public While(int lineIndex, int wordIndex) : base("while", lineIndex, wordIndex)
2025-07-05 16:57:59 +08:00
{
}
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
2025-07-02 18:46:09 +08:00
{
2025-07-05 16:57:59 +08:00
return new While(lineIndex, wordIndex);
2025-07-02 18:46:09 +08:00
}
}
2025-07-02 21:20:29 +08:00
/// <summary>
2025-07-03 18:46:52 +08:00
/// <b><see langword="break"/>;</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class Break : SingleKeyword<Break>
2025-07-02 18:46:09 +08:00
{
2025-07-07 00:28:37 +08:00
public Break(int lineIndex, int wordIndex) : base("break", lineIndex, wordIndex)
2025-07-02 18:46:09 +08:00
{
}
2025-07-05 16:57:59 +08:00
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
{
return new Break(lineIndex, wordIndex);
}
2025-07-02 18:46:09 +08:00
}
2025-07-02 21:20:29 +08:00
/// <summary>
2025-07-03 18:46:52 +08:00
/// <b><see langword="continue"/>;</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class Continue : SingleKeyword<Continue>
2025-07-02 18:46:09 +08:00
{
2025-07-07 00:28:37 +08:00
public Continue(int lineIndex, int wordIndex) : base("continue", lineIndex, wordIndex)
2025-07-05 16:57:59 +08:00
{
}
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
2025-07-02 18:46:09 +08:00
{
2025-07-05 16:57:59 +08:00
return new Continue(lineIndex, wordIndex);
2025-07-02 18:46:09 +08:00
}
2025-07-02 15:21:26 +08:00
}
2025-07-02 21:20:29 +08:00
/// <summary>
2025-07-03 18:46:52 +08:00
/// <b><see langword="struct"/> structureName { ... }</b>
2025-07-02 21:20:29 +08:00
/// </summary>
public sealed class Structure : NamespaceKeyword<Structure>
2025-07-02 15:21:26 +08:00
{
2025-07-07 00:28:37 +08:00
public Structure(int lineIndex, int wordIndex) : base("struct", lineIndex, wordIndex)
2025-07-05 16:57:59 +08:00
{
}
public override Internal.Keyword CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
2025-07-02 15:21:26 +08:00
{
2025-07-05 16:57:59 +08:00
return new Structure(lineIndex, wordIndex);
2025-07-02 15:21:26 +08:00
}
}
}