SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

CurlデベロッパーセンターPick Up(AD)

自分でつくれる!Curl言語
~マクロを用いた言語カスタマイズ~(基礎編)

  • このエントリーをはてなブックマークに追加

構成

 マクロの制約として、同じパッケージで定義したマクロはそのパッケージでは使えないという制約があります。実際の業務で使用する構成に、プログラム例1の構成を変更します。構成は(1)マニフェスト(2)マクロ定義(3)xfloorプロシージャ(4)スタートプログラムにします。

(1)マニフェスト(manifest.mcurl)
{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"
}
(2)マクロ定義(macro.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}
                }
            }                                           || 出口の情報(ここまで)
        }
    }
}
(3)xfloorプロシージャ(math.curl)
{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)}
}
(4)スタートプログラム(start.curl)
{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クラスのオブジェクトに展開してネイティブコードを生成します。そのため、実行時に無駄な判定が無くなり、高速に処理できます。

次のページ
マクロの便利機能を使ってみよう

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
CurlデベロッパーセンターPick Up連載記事一覧

もっと読む

この記事の著者

五百藏 隆文(イオロイ タカフミ)

1988年よりCOBOLコンパイラの開発に10年間従事した後、公共系の設計・開発を担当。 2010年よりCurlコンパイラの開発に参画する。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/6130 2011/09/14 14:00

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング