1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

N-Prolog TCLTKライブラリ 最初の一歩

Last updated at Posted at 2025-03-30

とっかかり

N-PrologのC埋め込み機能を利用して多くのC言語用ライブラリをN-Prologで使えるようにしよう、というプロジェクトを進めています。手始めに以前、手掛けたことがあるTCLTKのライブラリを開発しています。基本部分ができました。

埋め込みコード

下記のコードをごらんください。N-Prologの埋め込み機能を使っています。cdeclare/1 clibrary/1 cinline/1が埋め込みのための拡張述語です。

% tcl/tk library
clibrary($-ltcl -ltk$).
cdeclare($#ifdef __linux__
          #include <tcl/tcl.h>
          #else
          #include <tcl.h>
          #endif$).
cdeclare($#include <stdio.h>$).

cdeclare($#define BUFFSIZE 1024
         Tcl_Interp *interp;
         char buff[BUFFSIZE];$).

tk_interp(Str) :-
    cinline($strcpy(buff,Jgetname(Jderef(varStr,th)));
             Tcl_Eval(interp,buff);
             return(Jexec_all(rest,Jget_sp(th),th));$).


tk_init :-
    cinline($interp = Tcl_CreateInterp();
             Tcl_Init(interp);               
             Tk_Init(interp);
             return(Jexec_all(rest,Jget_sp(th),th));$).

tk_exit :-
    cinline($Tcl_DeleteInterp(interp);
             return(Jexec_all(rest,Jget_sp(th),th));$).
            

これらは基本部分です。tk_init/0でインタプリタを初期化します。tk_interp/1は文字列を受け取りこれをTCLTKのインタープリタに送ります。tk_exitはインタプリタの終了させます。

テスト

適当な文字列をインタープリタに送って動作テストをしてみましょう。通例にしたがって「ハローワールド」をやってみます。

bandicam 2025-03-30 11-55-37-813.jpg

上手くいきました。これが動作すればあとは簡単です。様々なTCLTKの機能を述語で表現するとともに、それらを文字列に変換してTCLTKのインタプリタに渡せばいいのです。残りの作業はひたすらにこの変換述語を準備するだけです。完成が見えてきました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?