This commit is contained in:
Valk Richard Li 2020-09-18 08:46:15 -07:00 committed by GitHub
parent 6c7e7d50ff
commit bd5a0b7d6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 166 additions and 2 deletions

View File

@ -71,4 +71,28 @@ var io=
nasal_call_builtin_foutput(filename,str);
return;
}
};
var bits=
{
bitxor:func(a,b)
{
return nasal_call_builtin_xor(a,b);
},
bitand:func(a,b)
{
return nasal_call_builtin_and(a,b);
},
bitor:func(a,b)
{
return nasal_call_builtin_or(a,b);
},
bitnand:func(a,b)
{
return nasal_call_builtin_nand(a,b);
},
bitnot:func(a)
{
return nasal_call_builtin_not(a);
}
};

View File

@ -479,4 +479,129 @@ int nasal_runtime::builtin_size(int local_scope_addr)
}
return ret_addr;
}
int nasal_runtime::builtin_xor(int local_scope_addr)
{
int a_addr=-1;
if(local_scope_addr>=0)
a_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("a");
if(a_addr<0 || nasal_vm.gc_get(a_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_xor: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int b_addr=-1;
if(local_scope_addr>=0)
b_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("b");
if(b_addr<0 || nasal_vm.gc_get(b_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_xor: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int number_a=(int)nasal_vm.gc_get(a_addr).get_number();
int number_b=(int)nasal_vm.gc_get(b_addr).get_number();
int ret_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(ret_addr).set_type(vm_number);
nasal_vm.gc_get(ret_addr).set_number((double)(number_a^number_b));
return ret_addr;
}
int nasal_runtime::builtin_and(int local_scope_addr)
{
int a_addr=-1;
if(local_scope_addr>=0)
a_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("a");
if(a_addr<0 || nasal_vm.gc_get(a_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_and: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int b_addr=-1;
if(local_scope_addr>=0)
b_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("b");
if(b_addr<0 || nasal_vm.gc_get(b_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_and: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int number_a=(int)nasal_vm.gc_get(a_addr).get_number();
int number_b=(int)nasal_vm.gc_get(b_addr).get_number();
int ret_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(ret_addr).set_type(vm_number);
nasal_vm.gc_get(ret_addr).set_number((double)(number_a&number_b));
return ret_addr;
}
int nasal_runtime::builtin_or(int local_scope_addr)
{
int a_addr=-1;
if(local_scope_addr>=0)
a_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("a");
if(a_addr<0 || nasal_vm.gc_get(a_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_or: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int b_addr=-1;
if(local_scope_addr>=0)
b_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("b");
if(b_addr<0 || nasal_vm.gc_get(b_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_or: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int number_a=(int)nasal_vm.gc_get(a_addr).get_number();
int number_b=(int)nasal_vm.gc_get(b_addr).get_number();
int ret_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(ret_addr).set_type(vm_number);
nasal_vm.gc_get(ret_addr).set_number((double)(number_a|number_b));
return ret_addr;
}
int nasal_runtime::builtin_nand(int local_scope_addr)
{
int a_addr=-1;
if(local_scope_addr>=0)
a_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("a");
if(a_addr<0 || nasal_vm.gc_get(a_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_nand: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int b_addr=-1;
if(local_scope_addr>=0)
b_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("b");
if(b_addr<0 || nasal_vm.gc_get(b_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_nand: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int number_a=(int)nasal_vm.gc_get(a_addr).get_number();
int number_b=(int)nasal_vm.gc_get(b_addr).get_number();
int ret_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(ret_addr).set_type(vm_number);
nasal_vm.gc_get(ret_addr).set_number((double)(~(number_a&number_b)));
return ret_addr;
}
int nasal_runtime::builtin_not(int local_scope_addr)
{
int a_addr=-1;
if(local_scope_addr>=0)
a_addr=nasal_vm.gc_get(local_scope_addr).get_closure().get_value_address("a");
if(a_addr<0 || nasal_vm.gc_get(a_addr).get_type()!=vm_number)
{
std::cout<<">> [runtime] builtin_not: cannot find values or wrong value type(must be number)."<<std::endl;
++error;
return -1;
}
int number=(int)nasal_vm.gc_get(a_addr).get_number();
int ret_addr=nasal_vm.gc_alloc();
nasal_vm.gc_get(ret_addr).set_type(vm_number);
nasal_vm.gc_get(ret_addr).set_number((double)(~number));
return ret_addr;
}
#endif

View File

@ -10,7 +10,7 @@ enum runtime_returned_state
rt_exit_without_error
};
#define BUILTIN_FUNC_NUM 16
#define BUILTIN_FUNC_NUM 21
std::string builtin_func_name[BUILTIN_FUNC_NUM]=
{
"nasal_call_builtin_std_cout",
@ -28,7 +28,12 @@ std::string builtin_func_name[BUILTIN_FUNC_NUM]=
"nasal_call_builtin_trans_num",
"nasal_call_builtin_pop_back",
"nasal_call_builtin_trans_str",
"nasal_call_builtin_size"
"nasal_call_builtin_size",
"nasal_call_builtin_xor",
"nasal_call_builtin_and",
"nasal_call_builtin_or",
"nasal_call_builtin_nand",
"nasal_call_builtin_not"
};
class nasal_runtime
@ -103,6 +108,11 @@ private:
int builtin_pop(int);
int builtin_str(int);
int builtin_size(int);
int builtin_xor(int);
int builtin_and(int);
int builtin_or(int);
int builtin_nand(int);
int builtin_not(int);
public:
nasal_runtime();
~nasal_runtime();
@ -1110,6 +1120,11 @@ int nasal_runtime::call_builtin_function(nasal_ast& node,int local_scope_addr)
case 13:ret_value_addr=builtin_pop(local_scope_addr);break;
case 14:ret_value_addr=builtin_str(local_scope_addr);break;
case 15:ret_value_addr=builtin_size(local_scope_addr);break;
case 16:ret_value_addr=builtin_xor(local_scope_addr);break;
case 17:ret_value_addr=builtin_and(local_scope_addr);break;
case 18:ret_value_addr=builtin_or(local_scope_addr);break;
case 19:ret_value_addr=builtin_nand(local_scope_addr);break;
case 20:ret_value_addr=builtin_not(local_scope_addr);break;
}
return ret_value_addr;
}