using Canon.Core.CodeGenerators; using Canon.Core.Enums; using Canon.Core.LexicalParser; namespace Canon.Core.SyntaxNodes; public class ConstDeclarations : NonTerminatedSyntaxNode { public override NonTerminatorType Type => NonTerminatorType.ConstDeclarations; /// /// 声明的常量列表 /// public IEnumerable<(IdentifierSemanticToken, ConstValue)> ConstValues => GetConstValues(); public static ConstDeclarations Create(List children) { return new ConstDeclarations { Children = children }; } private IEnumerable<(IdentifierSemanticToken, ConstValue)> GetConstValues() { if (Children.Count == 0) { yield break; } ConstDeclaration declaration = Children[1].Convert(); while (true) { yield return declaration.ConstValue; if (declaration.IsRecursive) { declaration = declaration.Children[0].Convert(); } else { break; } } } public override void GenerateCCode(CCodeBuilder builder) { foreach (var pair in ConstValues.Reverse()) { builder.AddString(" const"); //获取常量类型 var token = pair.Item2.Children[0].Convert().Token; var tokenType = token.TokenType; if (tokenType == SemanticTokenType.Number) { if (token.Convert().NumberType == NumberType.Integer) { builder.AddString(" int "); } else { builder.AddString(" double "); } } else if (tokenType == SemanticTokenType.Character) { builder.AddString(" char "); } else { builder.AddString(" bool "); } builder.AddString(pair.Item1.IdentifierName + " ="); pair.Item2.GenerateCCode(builder); builder.AddString(";"); } } }