跳到內容

環/教程/在 C/C++ 程式中嵌入環直譯器

來自華夏公益教科書,開放的書籍,為開放的世界
<

在 C/C++ 程式中嵌入環直譯器

[編輯 | 編輯原始碼]

我們可以使用以下函式從 C/C++ 程式中呼叫環直譯器

	RingState *ring_state_init();
	ring_state_runcode(RingState *pState,const char *cCode);
	ring_state_delete(RingState *pState);



環狀態

[編輯 | 編輯原始碼]

想法是使用 ring_state_init() 為環直譯器建立新的狀態,然後呼叫 ring_state_runcode() 函式使用相同的狀態執行環程式碼。完成後,我們呼叫 ring_state_delete() 釋放記憶體。

示例

	#include "ring.h"
	#include "stdlib.h"
	int main(int argc, char *argv[])
	{	
	  RingState *pState = ring_state_init();
	  printf("welcome\n");
	  ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
	  ring_state_delete(pState);
	}

輸出

	welcome
	hello world from the ring programming language



環狀態函式

[編輯 | 編輯原始碼]

環 API 帶有以下函式來建立和刪除狀態。我們還有函式來建立新變數和獲取變數值。

	RingState * ring_state_init ( void ) ;
	RingState * ring_state_delete ( RingState *pRingState ) ;
	void ring_state_runcode ( RingState *pRingState,const char *cStr ) ;
	List * ring_state_findvar ( RingState *pRingState,const char *cStr ) ;
	List * ring_state_newvar ( RingState *pRingState,const char *cStr ) ;
	void ring_state_main ( int argc, char *argv[] ) ;
	void ring_state_runfile ( RingState *pRingState,const char *cFileName ) ;



環狀態變數

[編輯 | 編輯原始碼]

我們可以在同一個程式中建立多個環狀態,並且可以建立和修改變數值。

要獲取變數列表,我們可以使用 ring_state_findvar() 函式。

要建立新變數,我們可以使用 ring_state_newvar() 函式。

示例

	#include "ring.h"
	#include "stdlib.h"
	
	int main(int argc, char *argv[])
	{
	  List *pList;

  	  RingState *pState = ring_state_init();
	  RingState *pState2 = ring_state_init();

	  printf("welcome\n");
	  ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");

	  printf("Again from C we will call ring code\n");
	  ring_state_runcode(pState,"for x = 1 to 10 see x + nl next");

	  ring_state_runcode(pState2,"for x = 1 to 5 see x + nl next");

	  printf("Now we will display the x variable value from ring code\n");
	  ring_state_runcode(pState,"see 'x value : ' + x + nl ");
	  ring_state_runcode(pState2,"see 'x value : ' + x + nl ");

	  pList = ring_state_findvar(pState,"x");

	  printf("Printing Ring variable value from C, %.0f\n",
		  	ring_list_getdouble(pList,RING_VAR_VALUE));

	  printf("now we will set the ring variable value from C\n");
	  ring_list_setdouble(pList,RING_VAR_VALUE,20);

	  ring_state_runcode(pState,"see 'x value after update : ' + x + nl ");

	  pList = ring_state_newvar(pState,"v1");
	  ring_list_setdouble(pList,RING_VAR_VALUE,10);	

	  pList = ring_state_newvar(pState,"v2");
	  ring_list_setdouble(pList,RING_VAR_VALUE,20);

	  ring_state_runcode(pState,"see 'v1 + v2 = ' see v1+v2 see nl");

	  ring_state_runcode(pState,"see 'end of test' + nl");

	  ring_state_delete(pState);
	  ring_state_delete(pState2);
	}

輸出

	welcome
	hello world from the ring programming language
	Again from C we will call ring code
	1
	2
	3
	4
	5
	6
	7
	8
	9
	10
	1
	2
	3
	4
	5
	Now we will display the x variable value from ring code
	x value : 11
	x value : 6
	Printing Ring variable value from C, 11
	now we will set the ring variable value from C
	x value after update : 20
	v1 + v2 = 30
	end of test


華夏公益教科書