OSDN Git Service

...。
[ring-lang-081/ring.git] / docs / ja-jp / build / html / _sources / embedding.txt
1 .. index:: 
2         single: C/C++ プログラムへ Ring を組み込むには; はじめに
3
4 ======================================
5 C/C++ プログラムへ Ring を組み込むには
6 ======================================
7
8 この関数により C/C++ プログラムから Ring を使えます。
9
10 .. code-block:: C
11
12         RingState *ring_state_init();
13         ring_state_runcode(RingState *pState,const char *cCode);
14         ring_state_delete(RingState *pState);
15
16 .. index:: 
17         pair: C/C++ プログラムへ Ring を組み込むには; Ring のステート
18
19 Ring のステート
20 ================
21
22 考えかたとしては ring_state_init() で Ring ステートの新規作成後に、
23 ring_state_runcode() 関数を呼び出すことで、同一ステートの Ring コードの実行します。
24 処理完了後に ring_state_delete() を呼び出して、メモリを解放します。
25
26 用例:
27
28 .. code-block:: C
29
30         #include "ring.h"
31         #include "stdlib.h"
32         int main(int argc, char *argv[])
33         {       
34           RingState *pState = ring_state_init();
35           printf("welcome\n");
36           ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
37           ring_state_delete(pState);
38         }
39
40 実行結果:
41
42 .. code-block:: C
43
44         welcome
45         hello world from the ring programming language
46
47 .. index:: 
48         pair: C/C++ プログラムへ Ring を組み込むには; Ring ステート関数
49
50 Ring ステート関数
51 =================
52
53 Ring の API にはステートの作成と削除を行う関数です。
54 また、新しい変数の作成と変数の値を取得するための関数もあります。
55
56 .. code-block:: C
57
58         RingState * ring_state_init ( void ) ;
59         RingState * ring_state_delete ( RingState *pRingState ) ;
60         void ring_state_runcode ( RingState *pRingState,const char *cStr ) ;
61         List * ring_state_findvar ( RingState *pRingState,const char *cStr ) ;
62         List * ring_state_newvar ( RingState *pRingState,const char *cStr ) ;
63         void ring_state_main ( int argc, char *argv[] ) ;
64         void ring_state_runfile ( RingState *pRingState,const char *cFileName ) ;
65         void ring_state_runobjectfile ( RingState *pRingState,const char *cFileName ) ;
66
67 .. index:: 
68         pair: C/C++ プログラムへ Ring を組み込むには; Ring ステート変数
69
70 Ring ステート変数
71 =================
72
73 同一プログラム内で複数の Ring のステートを作成できます。また、変数の作成と値の変更もできます。
74
75 ring_state_findvar() 関数は変数のリストを取得します。
76
77 ring_state_newvar() 関数は新しい変数を作成します。
78
79 用例:
80
81 .. code-block:: C
82
83         #include "ring.h"
84         #include "stdlib.h"
85         
86         int main(int argc, char *argv[])
87         {
88           List *pList;
89
90           RingState *pState = ring_state_init();
91           RingState *pState2 = ring_state_init();
92
93           printf("welcome\n");
94           ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
95
96           printf("Again from C we will call ring code\n");
97           ring_state_runcode(pState,"for x = 1 to 10 see x + nl next");
98
99           ring_state_runcode(pState2,"for x = 1 to 5 see x + nl next");
100
101           printf("Now we will display the x variable value from ring code\n");
102           ring_state_runcode(pState,"see 'x value : ' + x + nl ");
103           ring_state_runcode(pState2,"see 'x value : ' + x + nl ");
104
105           pList = ring_state_findvar(pState,"x");
106
107           printf("Printing Ring variable value from C , %.0f\n",
108                         ring_list_getdouble(pList,RING_VAR_VALUE));
109
110           printf("now we will set the ring variable value from C\n");
111           ring_list_setdouble(pList,RING_VAR_VALUE,20);
112
113           ring_state_runcode(pState,"see 'x value after update : ' + x + nl ");
114
115           pList = ring_state_newvar(pState,"v1");
116           ring_list_setdouble(pList,RING_VAR_VALUE,10); 
117
118           pList = ring_state_newvar(pState,"v2");
119           ring_list_setdouble(pList,RING_VAR_VALUE,20);
120
121           ring_state_runcode(pState,"see 'v1 + v2 = ' see v1+v2 see nl");
122
123           ring_state_runcode(pState,"see 'end of test' + nl");
124
125           ring_state_delete(pState);
126           ring_state_delete(pState2);
127         }
128
129 実行結果:
130
131 .. code-block:: ring
132
133         welcome
134         hello world from the ring programming language
135         Again from C we will call ring code
136         1
137         2
138         3
139         4
140         5
141         6
142         7
143         8
144         9
145         10
146         1
147         2
148         3
149         4
150         5
151         Now we will display the x variable value from ring code
152         x value : 11
153         x value : 6
154         Printing Ring variable value from C , 11
155         now we will set the ring variable value from C
156         x value after update : 20
157         v1 + v2 = 30
158         end of test