refact: 重构代码生成 (#72)

Reviewed-on: PostGuard/Canon#72
This commit is contained in:
2024-05-04 11:56:06 +08:00
parent 6130adfa7c
commit 8da24523c9
25 changed files with 1121 additions and 538 deletions

View File

@@ -10,7 +10,7 @@ public class CompoundStatement : NonTerminatedSyntaxNode
/// <summary>
/// 是否为主函数部分
/// </summary>
public bool IsMain;
public bool IsMain { get; set; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{

View File

@@ -72,6 +72,8 @@ public class ConstValue : NonTerminatedSyntaxNode
}
}
public string ValueString { get; set; } = string.Empty;
/// <summary>
/// 使用数值产生式的事件
/// </summary>

View File

@@ -50,10 +50,23 @@ public class Expression : NonTerminatedSyntaxNode
/// 当前表达式对应的数组下标维度的左边界
/// </summary>
public int LeftBound;
/// <summary>
/// 是否为FOR语句中的起始语句
/// </summary>
public bool IsForConditionBegin { get; set; }
/// <summary>
/// 是否为FOR语句中的结束语句
/// </summary>
public bool IsForConditionEnd { get; set; }
public bool IsAssign { get; set; }
/// <summary>
/// 是否为IF语句中的条件语句
/// </summary>
public bool IsIfCondition { get; set; }
private IdentifierSemanticToken? _iterator;
public IdentifierSemanticToken Iterator

View File

@@ -20,16 +20,11 @@ public class ParethnesisGeneratorEventArgs : EventArgs
public required Expression Expression { get; init; }
}
public class NoParameterProcedureCallGeneratorEventArgs : EventArgs
{
public required IdentifierSemanticToken ProcedureName { get; init; }
}
public class ProcedureCallGeneratorEventArgs : EventArgs
{
public required IdentifierSemanticToken ProcedureName { get; init; }
public required ExpressionList Parameters { get; init; }
public List<Expression> Parameters { get; } = [];
}
public class NotGeneratorEventArgs : EventArgs
@@ -103,11 +98,6 @@ public class Factor : NonTerminatedSyntaxNode
/// </summary>
public event EventHandler<BooleanGeneratorEventArgs>? OnBooleanGenerator;
/// <summary>
/// 没有参数的过程调用产生式事件
/// </summary>
public event EventHandler<NoParameterProcedureCallGeneratorEventArgs>? OnNoParameterProcedureCallGenerator;
/// <summary>
/// 过程调用产生式的事件
/// </summary>
@@ -185,7 +175,7 @@ public class Factor : NonTerminatedSyntaxNode
else
{
// factor -> id ( )
OnNoParameterProcedureCallGenerator?.Invoke(this, new NoParameterProcedureCallGeneratorEventArgs
OnProcedureCallGenerator?.Invoke(this, new ProcedureCallGeneratorEventArgs
{
ProcedureName = terminatedSyntaxNode.Token.Convert<IdentifierSemanticToken>()
});
@@ -194,11 +184,13 @@ public class Factor : NonTerminatedSyntaxNode
else if (Children.Count == 4)
{
// factor -> id ( expressionList)
OnProcedureCallGenerator?.Invoke(this, new ProcedureCallGeneratorEventArgs
ProcedureCallGeneratorEventArgs eventArgs = new()
{
ProcedureName = Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
Parameters = Children[2].Convert<ExpressionList>()
});
ProcedureName =
Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
};
eventArgs.Parameters.AddRange(Children[2].Convert<ExpressionList>().Expressions);
OnProcedureCallGenerator?.Invoke(this, eventArgs);
}
else
{

View File

@@ -66,6 +66,11 @@ public class IdentifierList : NonTerminatedSyntaxNode
/// </summary>
public bool IsProcedure { get; set; }
/// <summary>
/// 是否为变量定义中使用
/// </summary>
public bool IsVariableDefinition { get; set; }
public event EventHandler<IdentifierGeneratorEventArgs>? OnIdentifierGenerator;
public event EventHandler<TypeGeneratorEventArgs>? OnTypeGenerator;

View File

@@ -23,6 +23,11 @@ public class IdentifierVarPart : NonTerminatedSyntaxNode
/// </summary>
public List<int> LeftBounds = new();
/// <summary>
/// 索引中的表达式
/// </summary>
public List<Expression> Expressions { get; } = [];
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
@@ -42,7 +47,16 @@ public class IdentifierVarPart : NonTerminatedSyntaxNode
public static IdentifierVarPart Create(List<SyntaxNodeBase> children)
{
return new IdentifierVarPart { Children = children };
IdentifierVarPart result = new() { Children = children };
if (children.Count == 3)
{
ExpressionList expressionList = children[1].Convert<ExpressionList>();
result.Expressions.AddRange(expressionList.Expressions);
}
return result;
}
private void RaiseEvent()

View File

@@ -1,6 +1,6 @@
using System.Collections;
using Canon.Core.Abstractions;
using Canon.Core.Enums;
using Canon.Core.SemanticParser;
namespace Canon.Core.SyntaxNodes;
@@ -12,6 +12,44 @@ public abstract class NonTerminatedSyntaxNode : SyntaxNodeBase, IEnumerable<Synt
public required List<SyntaxNodeBase> Children { get; init; }
private PascalType? _variableType;
private string? _variableName;
public PascalType VariableType
{
get
{
if (_variableType is null)
{
throw new InvalidOperationException("Did not set the type of the node");
}
return _variableType;
}
set
{
_variableType = value;
}
}
public string VariableName
{
get
{
if (_variableName is null)
{
throw new InvalidOperationException("Did not set the name of the node");
}
return _variableName;
}
set
{
_variableName = value;
}
}
public IEnumerator<SyntaxNodeBase> GetEnumerator()
{
yield return this;

View File

@@ -16,9 +16,17 @@ public class ProcedureCall : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ProcedureCall;
/// <summary>
/// 调用函数的名称
/// </summary>
public IdentifierSemanticToken ProcedureId
=> Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>();
/// <summary>
/// 调用函数的参数
/// </summary>
public List<Expression> Parameters { get; } = [];
/// <summary>
/// 调用函数时含有参数的事件
/// </summary>
@@ -62,7 +70,14 @@ public class ProcedureCall : NonTerminatedSyntaxNode
public static ProcedureCall Create(List<SyntaxNodeBase> children)
{
return new ProcedureCall { Children = children };
ProcedureCall result = new() { Children = children };
if (children.Count == 4)
{
result.Parameters.AddRange(children[2].Convert<ExpressionList>().Expressions);
}
return result;
}
private void RaiseEvent()

View File

@@ -9,28 +9,6 @@ public class Variable : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.Variable;
private PascalType? _variableType;
/// <summary>
/// Variable实际的类型,用于数组赋值
/// </summary>
public PascalType VariableType
{
get
{
if (_variableType is null)
{
throw new InvalidOperationException();
}
return _variableType;
}
set
{
_variableType = value;
}
}
/// <summary>
/// 变量的名称
/// </summary>