Library: Added core library function streq and cmp according to FlightGear Nasal
This commit is contained in:
parent
953ad80482
commit
441c02d0fb
8
lib.nas
8
lib.nas
|
@ -100,6 +100,10 @@ var substr=func(str,begin,length)
|
|||
{
|
||||
return nasal_call_builtin_substr(str,begin,length);
|
||||
}
|
||||
var streq=func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_streq(a,b);
|
||||
}
|
||||
var left=func(string,length)
|
||||
{
|
||||
return nasal_call_builtin_left(string,length);
|
||||
|
@ -108,6 +112,10 @@ var right=func(string,length)
|
|||
{
|
||||
return nasal_call_builtin_right(string,length);
|
||||
}
|
||||
var cmp=func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_cmp(a,b);
|
||||
}
|
||||
|
||||
var io=
|
||||
{
|
||||
|
|
|
@ -59,8 +59,10 @@ bool builtin_die_state;// used in builtin_die
|
|||
nasal_val* builtin_die(nasal_val*,nasal_gc&);
|
||||
nasal_val* builtin_type(nasal_val*,nasal_gc&);
|
||||
nasal_val* builtin_substr(nasal_val*,nasal_gc&);
|
||||
nasal_val* builtin_streq(nasal_val*,nasal_gc&);
|
||||
nasal_val* builtin_left(nasal_val*,nasal_gc&);
|
||||
nasal_val* builtin_right(nasal_val*,nasal_gc&);
|
||||
nasal_val* builtin_cmp(nasal_val*,nasal_gc&);
|
||||
|
||||
void builtin_error_occurred(std::string func_name,std::string info)
|
||||
{
|
||||
|
@ -113,8 +115,10 @@ struct FUNC_TABLE
|
|||
{"nasal_call_builtin_die", builtin_die},
|
||||
{"nasal_call_builtin_type", builtin_type},
|
||||
{"nasal_call_builtin_substr", builtin_substr},
|
||||
{"nasal_call_builtin_streq", builtin_streq},
|
||||
{"nasal_call_builtin_left", builtin_left},
|
||||
{"nasal_call_builtin_right", builtin_right},
|
||||
{"nasal_call_builtin_cmp", builtin_cmp},
|
||||
{"", NULL}
|
||||
};
|
||||
|
||||
|
@ -779,6 +783,21 @@ nasal_val* builtin_substr(nasal_val* local_scope_addr,nasal_gc& gc)
|
|||
ret_addr->set_string(tmp);
|
||||
return ret_addr;
|
||||
}
|
||||
nasal_val* builtin_streq(nasal_val* local_scope_addr,nasal_gc& gc)
|
||||
{
|
||||
nasal_val* a_addr=in_builtin_find("a");
|
||||
nasal_val* b_addr=in_builtin_find("b");
|
||||
nasal_val* ret_addr=gc.gc_alloc(vm_num);
|
||||
if(a_addr->get_type()!=vm_str || b_addr->get_type()!=vm_str)
|
||||
{
|
||||
ret_addr->set_number((double)0);
|
||||
return ret_addr;
|
||||
}
|
||||
int ret=(a_addr->get_string()==b_addr->get_string());
|
||||
|
||||
ret_addr->set_number((double)ret);
|
||||
return ret_addr;
|
||||
}
|
||||
nasal_val* builtin_left(nasal_val* local_scope_addr,nasal_gc& gc)
|
||||
{
|
||||
nasal_val* string_addr=in_builtin_find("string");
|
||||
|
@ -827,4 +846,35 @@ nasal_val* builtin_right(nasal_val* local_scope_addr,nasal_gc& gc)
|
|||
ret_addr->set_string(tmp);
|
||||
return ret_addr;
|
||||
}
|
||||
nasal_val* builtin_cmp(nasal_val* local_scope_addr,nasal_gc& gc)
|
||||
{
|
||||
nasal_val* a_addr=in_builtin_find("a");
|
||||
nasal_val* b_addr=in_builtin_find("b");
|
||||
if(a_addr->get_type()!=vm_str)
|
||||
{
|
||||
builtin_error_occurred("cmp","\"a\" must be string");
|
||||
return NULL;
|
||||
}
|
||||
if(b_addr->get_type()!=vm_str)
|
||||
{
|
||||
builtin_error_occurred("cmp","\"b\" must be string");
|
||||
return NULL;
|
||||
}
|
||||
std::string a=a_addr->get_string();
|
||||
std::string b=b_addr->get_string();
|
||||
int alen=a.length();
|
||||
int blen=b.length();
|
||||
nasal_val* ret_addr=gc.gc_alloc(vm_num);
|
||||
for(int i=0; i<alen && i<blen; i++)
|
||||
{
|
||||
int diff = a[i] - b[i];
|
||||
if(diff)
|
||||
{
|
||||
ret_addr->set_number((double)diff < 0 ? -1 : 1);
|
||||
return ret_addr;
|
||||
}
|
||||
}
|
||||
ret_addr->set_number(alen == blen ? 0 : (alen < blen ? -1 : 1));
|
||||
return ret_addr;
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue