Add new functions

This commit is contained in:
Valk Richard Li 2019-11-27 16:01:11 +08:00 committed by GitHub
parent 3c97db02bc
commit 3aae6494ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 0 deletions

View File

@ -694,8 +694,11 @@ void abstract_syntax_tree::run_root()
{
recursion_depth=0;
++recursion_depth;
// before running initializing
while(!ret_stack.empty())ret_stack.pop();
scope.set_clear();
srand(unsigned(time(NULL)));
int beg_time,end_time;
exit_type=__process_exited_successfully;
@ -1057,6 +1060,43 @@ var abstract_syntax_tree::run_func(std::list<var> parameter,var self_func)
ret.set_string(str_for_input);
ret_stack.push(ret);
}
else if(self_func.get_name()=="size")
{
if(!parameter.empty())
{
std::list<var>::iterator i=parameter.begin();
if(i->get_type()==__var_array)
{
int cnt=0;
cnt=i->get_array_size();
ret.set_type(__var_number);
ret.set_number((double)cnt);
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] line "<<this->line<<": incorrect value type.must use an array."<<std::endl;
}
}
else
{
exit_type=__lack_parameter;
std::cout<<">>[Runtime-error] line "<<this->line<<": lack parameter(s)."<<std::endl;
}
// this must be added when running a function
if(exit_type!=__process_exited_successfully)
ret.set_type(__null_type);
ret_stack.push(ret);
}
else if(self_func.get_name()=="rand")
{
ret.set_type(__var_number);
double content=rand();
while(content>=1)content/=10;
ret.set_number(content);
// this must be added when running a function
ret_stack.push(ret);
}
else
{
for(;para_name!=para.children.end();++para_name,++para_value)

View File

@ -71,6 +71,7 @@ class var
var get_hash_member(std::string);
var* get_hash_member_addr(std::string);
abstract_syntax_tree get_function();
int get_array_size();
};
var error_var;
@ -197,4 +198,11 @@ abstract_syntax_tree var::get_function()
{
return function;
}
int var::get_array_size()
{
int cnt=0;
for(std::list<var>::iterator i=balloon_array.begin();i!=balloon_array.end();++i)++cnt;
return cnt;
}
#endif

View File

@ -33,4 +33,17 @@ var int=func(value)
var num=func(value)
{
return __call_Cpp_type_trans_num(value);
};
# size function is used to count the number of members in an array
var size=func(value)
{
return __call_Cpp_value_count(value);
};
# rand function is used to make random float numbers between 0 and 1 ([0,1))
# written by C: srand(unsigned(time(NULL))) rand()
var rand=func()
{
return __call_Cpp_rand();
};

View File

@ -67,6 +67,7 @@ var tanh=func(__x)
return 1-2/(__x*__x+1);
};
# relu function is a normal function used in deep neural networks
var relu=func(__x)
{
if(x>=0){return __x;}