構成
マクロの制約として、同じパッケージで定義したマクロはそのパッケージでは使えないという制約があります。実際の業務で使用する構成に、プログラム例1の構成を変更します。構成は(1)マニフェスト(2)マクロ定義(3)xfloorプロシージャ(4)スタートプログラムにします。
{curl 8.0 manifest}
{curl-file-attributes character-encoding = "shift-jis"}
{manifest sample}
{component package MACRO,
location = "macro.curl"
}
{component package MATH,
location = "math.curl"
}
{component file start.curl,
location = "start.curl"
}
{curl 8.0 package}
{curl-file-attributes character-encoding = "shift-jis"}
{package MACRO}
{define-macro public {logging
?tag:token,
?arg1:identifier,
?arg2:identifier
}
{return
{if {{tag.get-text}.equal? "\"start\""} then || 入口の情報(ここから)
{expand-template
{output "【" & ?tag & "】" &
{format "%s関数 [%s行目](dividend:%d, divisor:%d)",
{this-function}, {this-line}, ?arg1, ?arg2}
} || 入口の情報(ここまで)
}
else
{expand-template || 出口の情報(ここから)
{output "【" & ?tag & "】" &
{format "%s関数 [%s行目](quotient:%d, remainder:%d)",
{this-function}, {this-line}, ?arg1, ?arg2}
}
} || 出口の情報(ここまで)
}
}
}
{curl 8.0 package}
{curl-file-attributes character-encoding = "shift-jis"}
{package MATH}
{import * from MACRO}
{define-proc public {xfloor dividend:int, divisor:int}:(int, int)
{logging "start", dividend, divisor} || マクロ呼出
def (quotient, remainder ) = {floor dividend, divisor}
{logging "end", quotient, remainder} || マクロ呼出
{return (quotient, remainder)}
}
{curl 8.0 applet}
{curl-file-attributes character-encoding = "shift-jis"}
{applet manifest = "manifest.mcurl"}
{import * from MATH}
{value
{try
{xfloor 5, 1}
catch th:Exception do
th.message
}
}
実行
start.curlを実行すると「5」が表示されます。マクロの実行結果は、Curl RTEコンソールに下記が表示されます。
【start】xfloor関数[9行目](dividend:5,divisor:1) 【end】xfloor関数[11行目](quotient:5,remainder:0)
解説
-
ポイント1
マクロの実行結果では、展開した箇所の関数名や行番号が取得できます。
-
ポイント2
マクロ定義は、macro.curlのロジックを記述します。今回の例ではxfloorプロシージャでマクロを入口と出口に2か所記述しましたが、実際に展開されるロジックは、下記になります。
{output "【" & "start" & "】" &
{format "%s関数 [%s行目](dividend:%d, divisor:%d)",
{this-function}, {this-line}, dividend, divisor}
}
{output "【" & "end" & "】" &
{format "%s関数 [%s行目](quotient:%d, remainder:%d)",
{this-function}, {this-line}, quotient, remainder}
}
マクロ定義は、展開出力パターンを示します。コンパイラはそれを解析し、最適なCurlSourceクラスのオブジェクトに展開してネイティブコードを生成します。そのため、実行時に無駄な判定が無くなり、高速に処理できます。

