ADD: int arith core functions.

main
bog 2023-09-10 12:46:42 +02:00
parent 03cc9b58e2
commit 664ae63adf
2 changed files with 76 additions and 0 deletions

View File

@ -1,8 +1,80 @@
#include <cmath>
#include "lib/Function.hpp"
#include "lib/Loader.hpp"
#include "lib/Value.hpp"
extern "C" void num_arith(jk::Loader& loader)
{
loader.declare("+", [](auto args){
int res = 0;
for (auto arg: args)
{
res += arg->as_int();
}
return jk::Value::make_int(res);
});
loader.declare("-", [](auto args){
int res = args[0]->as_int();
for (size_t i=1; i<args.size(); i++)
{
res -= args[i]->as_int();
}
return jk::Value::make_int(res);
});
loader.declare("*", [](auto args){
int res = 1;
for (auto arg: args)
{
res *= arg->as_int();
}
return jk::Value::make_int(res);
});
loader.declare("/", [](auto args){
int res = args[0]->as_int();
for (size_t i=1; i<args.size(); i++)
{
res /= args[i]->as_int();
}
return jk::Value::make_int(res);
});
loader.declare("^", [](auto args){
int res = args[0]->as_int();
for (size_t i=1; i<args.size(); i++)
{
res = std::pow(res, args[i]->as_int());
}
return jk::Value::make_int(res);
});
loader.declare("%", [](auto args){
int res = args[0]->as_int();
for (size_t i=1; i<args.size(); i++)
{
res %= args[i]->as_int();
}
return jk::Value::make_int(res);
});
}
extern "C" void lib(jk::Loader& loader)
{
num_arith(loader);
loader.declare("dump", [](auto args){
std::string sep;

View File

@ -175,6 +175,10 @@ namespace jk
return std::isalpha(c)
|| c == '_'
|| c == '-'
|| c == '+'
|| c == '*'
|| c == '^'
|| c == '%'
|| c == '?'
|| c == '!'
|| c == '/';