マクロの便利機能を使ってみよう
前ページで説明したマクロ定義(macro.curl)を、Curlマクロの便利な機能で改善します。具体的には、下記の2点を修正します。
- マクロの引数を1つ以上持つ、可変な引数
- 引数の識別子の文字列をハードコーディングしない
マクロ定義(macro.curl)は下記になります。
{curl 8.0 package}
{curl-file-attributes character-encoding = "shift-jis"}
{package MACRO}
{import * from CURL.IDE.CPA.SOURCE}
{import * from CURL.LANGUAGE.SOURCE}
{define-macro public {logging
?head:token,
?args:{bounded-comma-sequence
1, infinity ,
?:identifier
} ||(1)
}
def common =
{expand-template
"【" & ?tag & "】" & {this-function} & "関数" &
"[" & {this-line} & "行目]"
}
def arg-info = {{Array-of CurlSource}}
{for arg key i in args do ||(2)
{if i > 0 then
{arg-info.append {Operator OperatorKind.Ampersand}}
{arg-info.append {CurlSource.from-string "\",\""}}
{arg-info.append {Operator OperatorKind.Ampersand}}
}
{arg-info.append {CurlSource.from-string "\"" & {arg.get-text} & ":\""}} ||(3)
{arg-info.append {Operator OperatorKind.Ampersand}}
{arg-info.append arg} ||(4)
}
{return
{expand-template
{output ?common & "(" & ?arg-info & ")" }
}
}
}
- (1)可変引数定義
- (2)可変引数を取り出す
- (3)識別子を文字列で取り出す
- (4)識別子
上記のマクロ定義(macro.curl)を前ページで紹介したプログラムと置き換えて実行すると、同じ結果になります。また、展開されるロジックは、下記になります。
{output "【" & "start" & "】" & {this-function} & "関数" & "[" & {this-line} & "行目]"
& "(" & "dividend:"& dividend &","&"divisor:"& divisor & ")" }
{output "【" & "end" & "】" & {this-function} & "関数" & "[" & {this-line} & "行目]"
& "(" & "quotient:"& quotient &","&"remainder:"& remainder & ")" }
上記の改善プログラムは文字列を編集するために雑多なコードになりましたが、(1)可変引数定義、(2)可変引数を取り出す、(3)識別子を文字列で取り出す、(4)識別子という4点が改善ポイントになります。
マニフェストと連携してみよう
マクロ定義時、macro-envが暗黙引数として渡ってきます。そのため、マニフェスト情報を簡単に参照することが可能です。今回は、マニフェスト情報を使用して、マクロを無効にする方法を1つ紹介します。
マニフェストに任意の識別子logging-levelを追加してマクロ定義側で参照し、マクロを無効にします。修正箇所は、下記の2か所です。
{curl 8.0 manifest}
{curl-file-attributes character-encoding = "shift-jis"}
{manifest sample
, logging-level = 0
}
|| 識別子の定義
以下、省略
{curl 8.0 package}
{curl-file-attributes character-encoding = "shift-jis"}
{package MACRO}
{import * from CURL.IDE.CPA.SOURCE}
{import * from CURL.LANGUAGE.SOURCE}
{define-macro public {debuginfo ?head:token,
?arg1:identifier,
?arg2:identifier
}
{if {macro-env.manifest.meta-data.get "logging-level"} == 0 then || 識別子の参照
{return
{expand-template } || 空を返却
}
}
以下、省略
業務APにおいて、業務共通のような情報により動作を変える局面が多々あると思います。レベルにより、出力情報を変更することも可能です。

