Cross-calling Basic and C Procedures

Top  Previous  Next

It is possible to call BASIC subs and functions from C and C functions from BASIC. I will refer to this as "cross-calling".

Since Tibbo BASIC arrays and structures are not compatible with arrays and structures of Tibbo C, it is not possible to cross-call procedures that have array or structure arguments or return the same.

 

note_warning-wtThere is no cross-checking of argument and return value types. You could define f as a word argument in the BASIC declaration, while having it as a string type in the C implementation, or vice versa. TIDE will not be able to catch such a discrepancy. Be very careful with this!

 

 

The following table will help you with cross-calling: it puts together corresponding language elements of Tibbo BASIC and C:

 

Tibbo BASIC

Tibbo C

Subs and functions

sub name()

end sub

void name(){

}

function name() as simple_type

end function

simple_type name(){

}

Argument passing:

sub name(x as simple_type)...

void name(simple_type x)...

sub name(ByVal x as simple_type)...

void name(simple_type x)...

sub name(ByRef x as simple_type)

void name(simple_type *x)...

 

Here is an example of calling two C functions from BASIC:

 

Tibbo BASIC:

Tibbo C:

declare function c_increment(x as byte) as byte
declare sub c_str_double(byref s as string) 'notice how byref becomes * on C side
 
sub on_sys_init
  dim x as byte=5
  x=c_increment(x) 'x will be 6
 
  dim s as string="TIBBO"
  c_str_double(s) 's will be "TIBBOTIBBO"
end sub

unsigned char c_increment(unsigned char x){
  return ++x;
}
 
void c_str_double(string *s){
  *s=*s+*s;
}

 

The same, but the other way around — C calls a function and a sub in BASIC:

 

Tibbo BASIC:

Tibbo C:

function basic_increment(x as byte) as byte
  basic_increment=x+1
end function
 
sub basic_str_double(byref s as string)
  s=s+s
end sub

unsigned char basic_increment(unsigned char x);
void basic_str_double(string *s);
 
void on_sys_init(){
  unsigned char x=5;
  x=basic_increment(x);
 
  string s="TIBBO";
  basic_str_double(&s);
}