ADD: string related functions (dup, char-at and cat.
parent
951a538cc2
commit
f9f73dabd3
|
@ -0,0 +1,3 @@
|
||||||
|
(assert= 'hello world' (cat 'hello' ' world'))
|
||||||
|
(assert= 'yoyoyo' (dup 'yo' 3))
|
||||||
|
(assert= 'e' (char-at 'hello' 1))
|
|
@ -18,24 +18,6 @@
|
||||||
return std::make_shared<Constant>(TYPE_BOOL, res, loc)
|
return std::make_shared<Constant>(TYPE_BOOL, res, loc)
|
||||||
|
|
||||||
|
|
||||||
#define FLOAT_OP(NEUTRAL, OP) \
|
|
||||||
if (args.empty()) \
|
|
||||||
{ \
|
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, NEUTRAL, loc); \
|
|
||||||
} \
|
|
||||||
\
|
|
||||||
float result = std::get<float>(args[0]->value()); \
|
|
||||||
\
|
|
||||||
for (size_t i=1; i<args.size(); i++) \
|
|
||||||
{ \
|
|
||||||
result = result OP std::get<float>(args[i]->value()); \
|
|
||||||
} \
|
|
||||||
if (args.size() == 1) \
|
|
||||||
{ \
|
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, OP result, loc); \
|
|
||||||
} \
|
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, result, loc)
|
|
||||||
|
|
||||||
namespace fkstd
|
namespace fkstd
|
||||||
{
|
{
|
||||||
STDRET assert_eq(Loc loc, Module&, STDARGS args)
|
STDRET assert_eq(Loc loc, Module&, STDARGS args)
|
||||||
|
@ -184,7 +166,7 @@ namespace fkstd
|
||||||
return std::make_shared<Constant>(TYPE_INT, result, loc);
|
return std::make_shared<Constant>(TYPE_INT, result, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDRET add_float(Loc loc, Module& mod, STDARGS args)
|
STDRET add_float(Loc loc, Module&, STDARGS args)
|
||||||
{
|
{
|
||||||
if (args.empty())
|
if (args.empty())
|
||||||
{
|
{
|
||||||
|
@ -201,7 +183,7 @@ namespace fkstd
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDRET sub_float(Loc loc, Module& mod, STDARGS args)
|
STDRET sub_float(Loc loc, Module&, STDARGS args)
|
||||||
{
|
{
|
||||||
if (args.empty())
|
if (args.empty())
|
||||||
{
|
{
|
||||||
|
@ -223,7 +205,7 @@ namespace fkstd
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDRET mul_float(Loc loc, Module& mod, STDARGS args)
|
STDRET mul_float(Loc loc, Module&, STDARGS args)
|
||||||
{
|
{
|
||||||
if (args.empty())
|
if (args.empty())
|
||||||
{
|
{
|
||||||
|
@ -240,7 +222,7 @@ namespace fkstd
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDRET div_float(Loc loc, Module& mod, STDARGS args)
|
STDRET div_float(Loc loc, Module&, STDARGS args)
|
||||||
{
|
{
|
||||||
if (args.empty())
|
if (args.empty())
|
||||||
{
|
{
|
||||||
|
@ -262,7 +244,7 @@ namespace fkstd
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDRET mod_float(Loc loc, Module& mod, STDARGS args)
|
STDRET mod_float(Loc loc, Module&, STDARGS args)
|
||||||
{
|
{
|
||||||
if (args.empty())
|
if (args.empty())
|
||||||
{
|
{
|
||||||
|
@ -279,7 +261,7 @@ namespace fkstd
|
||||||
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
return std::make_shared<Constant>(TYPE_FLOAT, result, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDRET pow_float(Loc loc, Module& mod, STDARGS args)
|
STDRET pow_float(Loc loc, Module&, STDARGS args)
|
||||||
{
|
{
|
||||||
if (args.empty())
|
if (args.empty())
|
||||||
{
|
{
|
||||||
|
@ -356,4 +338,45 @@ namespace fkstd
|
||||||
|
|
||||||
return val->at(std::get<int>(args.back()->value()));
|
return val->at(std::get<int>(args.back()->value()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STDRET string_cat(Loc loc, Module&, STDARGS args)
|
||||||
|
{
|
||||||
|
if (args.empty())
|
||||||
|
{
|
||||||
|
return std::make_shared<Constant>(TYPE_STRING, "", loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string value = std::get<std::string>(args[0]->value());
|
||||||
|
|
||||||
|
for (size_t i=1; i<args.size(); i++)
|
||||||
|
{
|
||||||
|
value += std::get<std::string>(args[i]->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_shared<Constant>(TYPE_STRING, value, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
STDRET string_dup(Loc loc, Module&, STDARGS args)
|
||||||
|
{
|
||||||
|
std::string str = std::get<std::string>(args[0]->value());
|
||||||
|
int n = std::get<int>(args[1]->value());
|
||||||
|
std::string res;
|
||||||
|
|
||||||
|
for (int i=0; i<n; i++)
|
||||||
|
{
|
||||||
|
res += str;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_shared<Constant>(TYPE_STRING, res, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
STDRET char_at(Loc loc, Module&, STDARGS args)
|
||||||
|
{
|
||||||
|
std::string str = std::get<std::string>(args[0]->value());
|
||||||
|
int n = std::get<int>(args[1]->value());
|
||||||
|
|
||||||
|
return std::make_shared<Constant>(TYPE_STRING,
|
||||||
|
std::string(1, str[n]),
|
||||||
|
loc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,10 @@ namespace fkstd
|
||||||
|
|
||||||
STDRET bool_not(Loc loc, Module& mod, STDARGS args);
|
STDRET bool_not(Loc loc, Module& mod, STDARGS args);
|
||||||
STDRET array_ref(Loc loc, Module& mod, STDARGS args);
|
STDRET array_ref(Loc loc, Module& mod, STDARGS args);
|
||||||
|
|
||||||
|
STDRET string_cat(Loc loc, Module& mod, STDARGS args);
|
||||||
|
STDRET string_dup(Loc loc, Module& mod, STDARGS args);
|
||||||
|
STDRET char_at(Loc loc, Module& mod, STDARGS args);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -29,6 +29,9 @@ extern "C" void lib(Module& mod)
|
||||||
|
|
||||||
mod.register_function("not", fkstd::bool_not);
|
mod.register_function("not", fkstd::bool_not);
|
||||||
mod.register_function("ref", fkstd::array_ref);
|
mod.register_function("ref", fkstd::array_ref);
|
||||||
|
mod.register_function("cat", fkstd::string_cat);
|
||||||
|
mod.register_function("dup", fkstd::string_dup);
|
||||||
|
mod.register_function("char-at", fkstd::char_at);
|
||||||
|
|
||||||
mod.register_macro("!", fkstd::set_addr);
|
mod.register_macro("!", fkstd::set_addr);
|
||||||
mod.register_macro("assert-static-fail", fkstd::assert_static_fail);
|
mod.register_macro("assert-static-fail", fkstd::assert_static_fail);
|
||||||
|
|
Loading…
Reference in New Issue