2 single: Embedding Ring Language in C/C++ Programs; Introduction
4 =============================================
5 Embedding Ring Language in C/C++ Programs
6 =============================================
8 We can use the Ring language from C/C++ programs using the next functions
12 RingState *ring_state_init();
13 ring_state_runcode(RingState *pState,const char *cCode);
14 ring_state_delete(RingState *pState);
17 pair: Embedding Ring Language in C/C++ Programs; Ring State
22 The idea is to use the ring_state_init() to create new state for the Ring Language
23 then call the ring_state_runcode() function to execut Ring code using the same state.
24 When we are done, we call the ring_state_delete() to free the memory.
32 int main(int argc, char *argv[])
34 RingState *pState = ring_state_init();
36 ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
37 ring_state_delete(pState);
45 hello world from the ring programming language
48 pair: Embedding Ring Language in C/C++ Programs; Ring State Functions
53 The Ring API comes with the next functions to create and delete the state.
54 Also we have functions to create new variables and get variables values.
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 ) ;
68 pair: Embedding Ring Language in C/C++ Programs; Ring State Variables
73 We can create more than one ring state in the same program and we can create and modify variable values.
75 To get the variable list we can use the ring_state_findvar() function.
77 To create new variable we can use the ring_state_newvar() function.
86 int main(int argc, char *argv[])
90 RingState *pState = ring_state_init();
91 RingState *pState2 = ring_state_init();
94 ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
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");
99 ring_state_runcode(pState2,"for x = 1 to 5 see x + nl next");
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 ");
105 pList = ring_state_findvar(pState,"x");
107 printf("Printing Ring variable value from C , %.0f\n",
108 ring_list_getdouble(pList,RING_VAR_VALUE));
110 printf("now we will set the ring variable value from C\n");
111 ring_list_setdouble(pList,RING_VAR_VALUE,20);
113 ring_state_runcode(pState,"see 'x value after update : ' + x + nl ");
115 pList = ring_state_newvar(pState,"v1");
116 ring_list_setdouble(pList,RING_VAR_VALUE,10);
118 pList = ring_state_newvar(pState,"v2");
119 ring_list_setdouble(pList,RING_VAR_VALUE,20);
121 ring_state_runcode(pState,"see 'v1 + v2 = ' see v1+v2 see nl");
123 ring_state_runcode(pState,"see 'end of test' + nl");
125 ring_state_delete(pState);
126 ring_state_delete(pState2);
134 hello world from the ring programming language
135 Again from C we will call ring code
151 Now we will display the x variable value from ring code
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