This commit is contained in:
Valk Richard Li 2020-05-18 01:46:25 -07:00 committed by GitHub
parent 4946685da2
commit 54c855e17e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 238 additions and 2 deletions

View File

@ -28,7 +28,7 @@ var math=
# Returns e (Euler's constant) raised to the power specified by the single argument
exp:func(x)
{
return nasal_call_builtin_pow(me.e,x);
return nasal_call_builtin_exp(x);
},
# Returns the natural logarithm of the single argument.
ln:func(x)

View File

@ -120,6 +120,141 @@ int setsize(std::list<std::map<std::string,int> >& local_scope)
return ret_addr;
}
int subvec(std::list<std::map<std::string,int> >& local_scope)
{
int vector_addr=-1,start=-1,length=-1,type=-1;
int begin_iter=0,len_iter=0;
for(std::list<std::map<std::string,int> >::iterator i=local_scope.begin();i!=local_scope.end();++i)
{
if(i->find("vector")!=i->end())
vector_addr=(*i)["vector"];
if(i->find("start")!=i->end())
start=(*i)["start"];
if(i->find("length")!=i->end())
length=(*i)["length"];
}
if(vector_addr<0 || start<0 || length<0)
return -1;
if(nasal_gc.get_scalar(vector_addr).get_type()!=scalar_vector)
{
std::cout<<">> subvec gets a value that is not a vector."<<std::endl;
return -1;
}
type=nasal_gc.get_scalar(start).get_type();
if(type!=scalar_number && type!=scalar_string)
{
std::cout<<">> [Runtime] \'start\' is not a number or numerable string."<<std::endl;
return -1;
}
if(type==scalar_string && !check_numerable_string(nasal_gc.get_scalar(start).get_string().get_string()))
{
std::cout<<">> [Runtime] \'start\' is not a numerable string."<<std::endl;
return -1;
}
if(type==scalar_number)
begin_iter=(int)nasal_gc.get_scalar(start).get_number().get_number();
else if(type==scalar_string)
begin_iter=(int)trans_string_to_number(nasal_gc.get_scalar(start).get_string().get_string());
type=nasal_gc.get_scalar(length).get_type();
if(type!=scalar_nil && type!=scalar_number && type!=scalar_string)
{
std::cout<<">> [Runtime] \'length\' is not a nil type,number or numerable string."<<std::endl;
return -1;
}
if(type==scalar_string && !check_numerable_string(nasal_gc.get_scalar(length).get_string().get_string()))
{
std::cout<<">> [Runtime] \'length\' is not a numerable string."<<std::endl;
return -1;
}
if(type==scalar_nil)
len_iter=nasal_gc.get_scalar(vector_addr).get_vector().get_size()-begin_iter;
else if(type==scalar_number)
len_iter=(int)nasal_gc.get_scalar(length).get_number().get_number();
else if(type==scalar_string)
len_iter=(int)trans_string_to_number(nasal_gc.get_scalar(length).get_string().get_string());
int ret_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(ret_addr).set_type(scalar_vector);
if(type==scalar_nil)
for(int i=begin_iter;i<nasal_gc.get_scalar(vector_addr).get_vector().get_size();++i)
{
int tmp=nasal_gc.get_scalar(vector_addr).get_vector().get_elem(i);
if(tmp<0)
{
std::cout<<">> [Runtime] subvec out of memory boundary."<<std::endl;
return -1;
}
int new_addr=-1;
switch(nasal_gc.get_scalar(tmp).get_type())
{
case scalar_nil:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_nil);
break;
case scalar_number:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_number);
nasal_gc.get_scalar(new_addr).get_number().deep_copy(nasal_gc.get_scalar(tmp).get_number());
break;
case scalar_string:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_string);
nasal_gc.get_scalar(new_addr).get_string().deep_copy(nasal_gc.get_scalar(tmp).get_string());
break;
case scalar_function:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_function);
nasal_gc.get_scalar(new_addr).get_function().deep_copy(nasal_gc.get_scalar(tmp).get_function());
break;
case scalar_vector:
case scalar_hash:
new_addr=tmp;
nasal_gc.reference_add(new_addr);
break;
}
nasal_gc.get_scalar(ret_addr).get_vector().vec_push(new_addr);
}
else
for(int i=0;i<len_iter;++i)
{
int tmp=nasal_gc.get_scalar(vector_addr).get_vector().get_elem(begin_iter+i);
if(tmp<0)
{
std::cout<<">> [Runtime] subvec out of memory boundary."<<std::endl;
return -1;
}
int new_addr=-1;
switch(nasal_gc.get_scalar(tmp).get_type())
{
case scalar_nil:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_nil);
break;
case scalar_number:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_number);
nasal_gc.get_scalar(new_addr).get_number().deep_copy(nasal_gc.get_scalar(tmp).get_number());
break;
case scalar_string:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_string);
nasal_gc.get_scalar(new_addr).get_string().deep_copy(nasal_gc.get_scalar(tmp).get_string());
break;
case scalar_function:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_function);
nasal_gc.get_scalar(new_addr).get_function().deep_copy(nasal_gc.get_scalar(tmp).get_function());
break;
case scalar_vector:
case scalar_hash:
new_addr=tmp;
nasal_gc.reference_add(new_addr);
break;
}
nasal_gc.get_scalar(ret_addr).get_vector().vec_push(new_addr);
}
return ret_addr;
}
int print(std::list<std::map<std::string,int> >& local_scope)
{
int vector_addr=-1;
@ -163,4 +298,70 @@ int system_type(std::list<std::map<std::string,int> >& local_scope)
return ret_addr;
}
int nas_trigfunc(std::list<std::map<std::string,int> >& local_scope,int func_type)
{
int data=-1;
double num=0;
int type=-1;
for(std::list<std::map<std::string,int> >::iterator i=local_scope.begin();i!=local_scope.end();++i)
if(i->find("x")!=i->end())
data=(*i)["x"];
if(data<0)
return -1;
type=nasal_gc.get_scalar(data).get_type();
if(type!=scalar_number && type!=scalar_string)
{
std::cout<<">> [Runtime] x must be a number or numerable string."<<std::endl;
return -1;
}
if(type==scalar_string && !check_numerable_string(nasal_gc.get_scalar(data).get_string().get_string()))
{
std::cout<<">> [Runtime] x is not a numerable string."<<std::endl;
return -1;
}
if(type==scalar_number)
num=nasal_gc.get_scalar(data).get_number().get_number();
else if(type==scalar_string)
num=trans_string_to_number(nasal_gc.get_scalar(data).get_string().get_string());
int ret_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(ret_addr).set_type(scalar_number);
switch(func_type)
{
case 0:nasal_gc.get_scalar(ret_addr).get_number().set_number(std::sin(num));break;
case 1:nasal_gc.get_scalar(ret_addr).get_number().set_number(std::cos(num));break;
case 2:nasal_gc.get_scalar(ret_addr).get_number().set_number(std::tan(num));break;
}
return ret_addr;
}
int nas_exp(std::list<std::map<std::string,int> >& local_scope)
{
int data=-1;
double num=0;
int type=-1;
for(std::list<std::map<std::string,int> >::iterator i=local_scope.begin();i!=local_scope.end();++i)
if(i->find("x")!=i->end())
data=(*i)["x"];
if(data<0)
return -1;
type=nasal_gc.get_scalar(data).get_type();
if(type!=scalar_number && type!=scalar_string)
{
std::cout<<">> [Runtime] x must be a number or numerable string."<<std::endl;
return -1;
}
if(type==scalar_string && !check_numerable_string(nasal_gc.get_scalar(data).get_string().get_string()))
{
std::cout<<">> [Runtime] x is not a numerable string."<<std::endl;
return -1;
}
if(type==scalar_number)
num=nasal_gc.get_scalar(data).get_number().get_number();
else if(type==scalar_string)
num=trans_string_to_number(nasal_gc.get_scalar(data).get_string().get_string());
int ret_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(ret_addr).set_type(scalar_number);
nasal_gc.get_scalar(ret_addr).get_number().set_number(std::exp(num));
return ret_addr;
}
#endif

View File

@ -34,7 +34,7 @@ std::string inline_func_name[nas_lib_func_num]=
"nasal_call_builtin_sin",
"nasal_call_builtin_cos",
"nasal_call_builtin_tan",
"nasal_call_builtin_pow",
"nasal_call_builtin_exp",
"nasal_call_builtin_cpp_math_ln",
"nasal_call_builtin_cpp_math_sqrt",
"nasal_call_builtin_cpp_atan2",
@ -3004,10 +3004,45 @@ int nasal_runtime::builtin_function(std::list<std::map<std::string,int> >& local
ret_addr=append(local_scope);
else if(func_name=="nasal_call_builtin_set_size")
ret_addr=setsize(local_scope);
else if(func_name=="nasal_call_builtin_subvec")
ret_addr=subvec(local_scope);
else if(func_name=="nasal_call_builtin_c_std_puts")
ret_addr=print(local_scope);
else if(func_name=="nasal_call_builtin_scalar_type")
ret_addr=system_type(local_scope);
else if(func_name=="nasal_call_builtin_sin")
ret_addr=nas_trigfunc(local_scope,0);
else if(func_name=="nasal_call_builtin_cos")
ret_addr=nas_trigfunc(local_scope,1);
else if(func_name=="nasal_call_builtin_tan")
ret_addr=nas_trigfunc(local_scope,2);
else if(func_name=="nasal_call_builtin_exp")
ret_addr=nas_exp(local_scope);
// "nasal_call_builtin_contains",
// "nasal_call_builtin_delete",
// "nasal_call_builtin_trans_int",
// "nasal_call_builtin_trans_num",
// "nasal_call_builtin_get_keys",
// "nasal_call_builtin_pop_back",
// "nasal_call_builtin_sizeof",
// "nasal_call_builtin_str_cmp_equal",
// "nasal_call_builtin_cmp",
// "nasal_call_builtin_cpp_sort",
// "nasal_call_builtin_substr",
// "nasal_call_builtin_sprintf",
// "nasal_call_builtin_find_first_occur",
// "nasal_call_builtin_split",
// "nasal_call_builtin_rand",
// "nasal_call_builtin_get_id",
// "nasal_call_builtin_bitcalc",
// "nasal_call_builtin_sbitcalc",
// "nasal_call_builtin_setbit",
// "nasal_call_builtin_null_string_gen",
// "nasal_call_builtin_cpp_math_ln",
// "nasal_call_builtin_cpp_math_sqrt",
// "nasal_call_builtin_cpp_atan2",
return ret_addr;
}