1 Star 3 Fork 0

ValKmjolnir/nas-sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
nas_builtin.h 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
Valk Richard Li 提交于 2021-04-03 23:51 . gc optimized
#ifndef __NAS_BUILTIN_H__
#define __NAS_BUILTIN_H__
nas_val* nas_builtin_print(nas_val*);
nas_val* nas_builtin_type(nas_val*);
nas_val* nas_builtin_time(nas_val*);
nas_val* nas_builtin_rand(nas_val*);
nas_val* nas_builtin_append(nas_val*);
nas_val* nas_builtin_exp(nas_val*);
nas_val* nas_builtin_system(nas_val*);
struct
{
nas_val* (*func_ptr)(nas_val*);
const char* func_name;
}builtin_func[]=
{
{&nas_builtin_print, "print" },
{&nas_builtin_type, "type" },
{&nas_builtin_time, "time" },
{&nas_builtin_rand, "rand" },
{&nas_builtin_append, "append"},
{&nas_builtin_exp, "exp" },
{&nas_builtin_system, "system"},
{NULL, NULL }
};
nas_val* nas_builtin_print(nas_val* elem)
{
std::vector<nas_val*>& vec=elem->ptr.vec->elems;
int size=vec.size();
for(int i=0;i<size;++i)
{
nas_val* tmp=vec[i];
switch(tmp->type)
{
case vm_nil: std::cout<<"nil"; break;
case vm_num: std::cout<<tmp->ptr.num; break;
case vm_str: std::cout<<*tmp->ptr.str; break;
case vm_vec: tmp->ptr.vec->print(); break;
case vm_hash: tmp->ptr.hash->print(); break;
case vm_func: std::cout<<"func(...)"; break;
}
}
return gc_alloc(vm_nil);
}
nas_val* nas_builtin_type(nas_val* elem)
{
if(elem->ptr.vec->elems.size()!=1)
{
std::cout<<">> [vm] builtin_type: must use 1 argument.\n";
return nullptr;
}
nas_val* s=gc_alloc(vm_str);
switch(elem->ptr.vec->get_val(0)->type)
{
case vm_nil: *s->ptr.str="nil"; break;
case vm_num: *s->ptr.str="number"; break;
case vm_str: *s->ptr.str="string"; break;
case vm_vec: *s->ptr.str="vector"; break;
case vm_hash: *s->ptr.str="hash"; break;
case vm_func: *s->ptr.str="function"; break;
}
return s;
}
nas_val* nas_builtin_time(nas_val* elem)
{
if(elem->ptr.vec->elems.size()!=1)
{
std::cout<<">> [vm] builtin_time: must use 1 argument.\n";
return nullptr;
}
time_t begin=(time_t)elem->ptr.vec->elems[0]->ptr.num;
nas_val* ret=gc_alloc(vm_num);
ret->ptr.num=(double)time(&begin);
return ret;
}
nas_val* nas_builtin_rand(nas_val* elem)
{
if(elem->ptr.vec->elems.size()>1)
{
std::cout<<">> [vm] builtin_rand: too much argument(s).\n";
return nullptr;
}
if(elem->ptr.vec->elems.size()==1)
{
srand((unsigned int)elem->ptr.vec->elems[0]->ptr.num);
return gc_alloc(vm_nil);
}
double num=0;
for(int i=0;i<5;++i)
num=(num+rand())*(1.0/(RAND_MAX+1.0));
nas_val* ret_addr=gc_alloc(vm_num);
ret_addr->ptr.num=num;
return ret_addr;
}
nas_val* nas_builtin_append(nas_val* elem)
{
std::vector<nas_val*>& vec=elem->ptr.vec->elems;
if(vec.size()<2)
{
std::cout<<">> [vm] builtin_append: lack argument(s).\n";
return nullptr;
}
if(vec[0]->type!=vm_vec)
{
std::cout<<">> [vm] builtin_append: must use vector.\n";
return nullptr;
}
std::vector<nas_val*>& ref=vec[0]->ptr.vec->elems;
for(int i=1;i<vec.size();++i)
ref.push_back(vec[i]);
return gc_alloc(vm_nil);
}
nas_val* nas_builtin_exp(nas_val* elem)
{
std::vector<nas_val*>& vec=elem->ptr.vec->elems;
if(vec.size()!=1)
{
std::cout<<">> [vm] builtin_exp: must use one argument.\n";
return nullptr;
}
nas_val* res=gc_alloc(vm_num);
res->ptr.num=exp(vec[0]->ptr.num);
return res;
}
nas_val* nas_builtin_system(nas_val* elem)
{
std::vector<nas_val*>& vec=elem->ptr.vec->elems;
if(vec.size()!=1)
{
std::cout<<">> [vm] builtin_system: must use one argument.\n";
return nullptr;
}
if(vec[0]->type!=vm_str)
{
std::cout<<">> [vm] builtin_system: must use string.\n";
return nullptr;
}
system(vec[0]->ptr.str->data());
return gc_alloc(vm_nil);
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/valkmjolnir/nas-sharp.git
[email protected]:valkmjolnir/nas-sharp.git
valkmjolnir
nas-sharp
nas-sharp
master

搜索帮助