
Local e method
In this case, you define an e method within a struct and indicate that the method's body is implemented as a C routine. When you call the e method, the name of the enclosing struct instance is passed to the C routine and the C routine is executed.
This way of calling C routines is useful when the C routine manipulates the fields of a particular struct instance. The syntax for declaring an e method whose body is implemented as a C routine is:
e-method-name(param,...) [:result-type] is C routine c-routine-name
Example-Local routine |
. |
1 #include
2 #include "c_interface2.h"
3
4 void c_interface2 (int i, char* s) {
5 int n;
6 if ( i < 5) {
7 for (n = 0; n < i; n ++) {
8 printf("Hello World\n");
9 }
10 } else {
11 printf("%s\n",s);
12 }
13 }
1 <'
2 routine c_hello(i : int, s : string) is C routine c_interface2;
3
4 extend sys {
5
6 run() is also {
7 c_hello(1,"Test");
8 c_hello(5,"Hello Deepak");
9 };
10 };
11 '>
Compiling the example | |
. | |
Operation | Commands |
Create h file | sn_compile.sh -h_only c_interface2.e -o c_interface2.h |
Create c object file | gcc -c -o c_interface2.o c_interface2.c |
Link c to e | sn_compile.sh -l c_interface2.o c_interface2.e -o c_interface2.run |
Simulate | c_interface2.run -c "test" |
local e method as dynamic Object
1 #include
2
3 void c_interface (int i, char* s) {
4 int n;
5 if ( i < 5) {
6 for (n = 0; n < i; n ++) {
7 printf("Hello World\n");
8 }
9 } else {
10 printf("%s\n",s);
11 }
12 }
1 <'
2 extend sys {
3 c_interface(i:int, s:string) is foreign dynamic C routine c_interface3:;
4
5 run() is also {
6 var i : int = 1;
7 c_interface(1,"");
8 c_interface(5,"Hello Deepak");
9 };
10 };
11 '>
Compiling the example | . | ||||||||
|
Bạn Có Đam Mê Với Vi Mạch hay Nhúng - Bạn Muốn Trau Dồi Thêm Kĩ Năng
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn