Skip to content

Conversation

@yutaro-sakamoto
Copy link
Contributor

This pull request enhances the code generation process by introducing descriptive suffixes to attribute variable names based on their type and properties. This makes generated code more readable and helps distinguish between different attribute configurations. This PR will resolve #758 completely.

Key changes include:

Attribute Suffix Generation and Usage:

  • Added a suffix field to the attr_list struct and logic to generate a descriptive suffix for each attribute based on its type, digits, scale, and flags. This is handled by the new build_attr_suffix and get_type_short_name functions. [1] [2]
  • Updated all relevant code generation routines to append the generated suffix to attribute variable names when declaring, initializing, and referencing them. This includes changes in member variable declarations, initialization methods, and field factory calls. [1] [2] [3] [4] [5] [6] [7]

Supporting Infrastructure:

  • Added forward declarations for the new suffix-related functions.
  • Modified attribute cache insertion to store the generated suffix.
  • Implemented a lookup function to retrieve the suffix for a given attribute ID, ensuring consistent usage throughout the codebase.

These changes improve code clarity and maintainability by making attribute variables self-descriptive and reducing the risk of naming collisions.

@yutaro-sakamoto yutaro-sakamoto marked this pull request as ready for review December 16, 2025 06:36
Copilot AI review requested due to automatic review settings December 16, 2025 06:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request enhances code generation by adding descriptive suffixes to attribute variable names in generated Java code. The suffixes encode type information, digits, scale, and flags, making the generated code more readable and self-documenting (e.g., a_1_NumericDisplay_Digits9_Scale2_HaveSign).

Key changes:

  • Added suffix generation infrastructure with build_attr_suffix() and lookup_attr_suffix() functions that create descriptive suffixes based on attribute properties
  • Updated attribute struct to store the generated suffix
  • Modified all attribute variable references throughout code generation to append the suffix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@yutaro-sakamoto yutaro-sakamoto changed the title Cobol field attribute var name Improve CobolFieldAttribute variable names Dec 16, 2025
@yutaro-sakamoto
Copy link
Contributor Author

yutaro-sakamoto commented Dec 17, 2025

@h-himo @n-isaka
このPRと#768 により、生成されるJavaコードの可読性を向上させました。
以下に変換例を示しますので、これで問題ないようでしたらこのPRをapproveし、他に良い変換ルールがありましたらコメントを書き込んでください。

COBOLコード

       01 NUM-1 PIC 9(02).
       01 NUM-2 PIC S9(02) SIGN IS TRAILING SEPARATE.
       01 NUM-3 PIC S9(02)V9(3) COMP-3.
       01 NUM-4 PIC S9(02)VP(3) COMP-3.
       01 STR-1 PIC X(5).
       01 STR-2 PIC X(5) JUSTIFIED RIGHT.

従来のコード。

    a_1 = new CobolFieldAttribute (16, 2, 0, 7, null);
    a_2 = new CobolFieldAttribute (18, 5, 3, 1, null);
    a_3 = new CobolFieldAttribute (18, 5, -3, 1, null);
    a_4 = new CobolFieldAttribute (16, 2, 0, 0, null);
    a_5 = new CobolFieldAttribute (33, 0, 0, 0, null);
    a_6 = new CobolFieldAttribute (33, 0, 0, 16, null);

改修後の生成コード。
以下のコードにおける//から始まるコメントは実際に生成されるものではなく、説明のために手動で挿入したもの。

   // 文の順番はコンパイラの都合による順番で、COBOLソース上の宣言順ではない
   // NUM-2
    a_1_NumericDisplay_Digits2_Scale0_HaveSign_SignSeparate_SignLeading = new CobolFieldAttribute(
      CobolFieldAttribute.COB_TYPE_NUMERIC_DISPLAY,
      /* digits= */ 2,
      /* scale= */ 0,
      CobolFieldAttribute.COB_FLAG_HAVE_SIGN
        | CobolFieldAttribute.COB_FLAG_SIGN_SEPARATE
        | CobolFieldAttribute.COB_FLAG_SIGN_LEADING,
      /* pic= */ null);
    // NUM-3
    a_2_NumericPacked_Digits5_Scale3_HaveSign = new CobolFieldAttribute(
      CobolFieldAttribute.COB_TYPE_NUMERIC_PACKED,
      /* digits= */ 5,
      /* scale= */ 3,
      CobolFieldAttribute.COB_FLAG_HAVE_SIGN,
      /* pic= */ null);
    // NUM-4
    a_3_NumericPacked_Digits5_ScaleNeg3_HaveSign = new CobolFieldAttribute(
      CobolFieldAttribute.COB_TYPE_NUMERIC_PACKED,
      /* digits= */ 5,
      /* scale= */ -3,
      CobolFieldAttribute.COB_FLAG_HAVE_SIGN,
      /* pic= */ null);
    // NUM-1
    a_4_NumericDisplay_Digits2_Scale0 = new CobolFieldAttribute(
      CobolFieldAttribute.COB_TYPE_NUMERIC_DISPLAY,
      /* digits= */ 2,
      /* scale= */ 0,
      CobolFieldAttribute.COB_FLAG_NOT_SPECIFIED,
      /* pic= */ null);
    // STR-1
    a_5_Alphanumeric = new CobolFieldAttribute(
      CobolFieldAttribute.COB_TYPE_ALPHANUMERIC,
      /* digits= */ 0,
      /* scale= */ 0,
      CobolFieldAttribute.COB_FLAG_NOT_SPECIFIED,
      /* pic= */ null);
    // STR-2
    a_6_Alphanumeric_Justified = new CobolFieldAttribute(
      CobolFieldAttribute.COB_TYPE_ALPHANUMERIC,
      /* digits= */ 0,
      /* scale= */ 0,
      CobolFieldAttribute.COB_FLAG_JUSTIFIED,
      /* pic= */ null);

変数の命名規則は以下の通りです

PIC 9等の数値系のもの

a_{連番}_{型の名前}_Digits{桁数}_Scale{Scale}_{フラグ}
  • {連番}: コンパイラが付与する連番。これにより変数衝突を防ぐ
  • {型の名前}: PIC 9ならNumericDisplay, COMP-3がついていればNumericPacked等の名前が付く
  • {桁数}: 桁数の数字を入れる
  • {Scale}: 仮想小数点の位置に応じた数値が入る。
    • ただしこの数値が負の場合。Negキーワードを付ける
      • -はJavaの変数名に使えないため
    • 例: Scaleが4の場合は4, Scaleが-3の場合Neg3となる
  • {フラグ}: 符号付き、SEPARATE、LEADING等の組み合わせに応じた名前が付けられる

PIC X等の文字列系のもの

a_{連番}_{型の名前}_{フラグ}
  • {連番}: コンパイラが付与する連番。これにより変数衝突を防ぐ
  • {型の名前}: PIC XならAlphaNumeric, PIC NならNational等の名前が付く
  • {フラグ}: JUSTIFIED等の組み合わせに応じた名前が付けられる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the readability of generation code for CobolFieldAttribute

1 participant