[Feat] 增加数学库 std.math
[Fix] 修复resolveModulePath解析二级模块的一些Bug
This commit is contained in:
@@ -1290,6 +1290,20 @@ namespace Fig
|
||||
if (!fs::exists(modPath))
|
||||
throw RuntimeError(FString(
|
||||
std::format("Could not find module `{}`", next.toBasicString())));
|
||||
if (i == pathVec.size() - 1)
|
||||
{
|
||||
// `next` is the last module
|
||||
modPath = modPath / FString(next + u8".fig").toBasicString();
|
||||
if (!fs::exists(modPath))
|
||||
{
|
||||
throw RuntimeError(FString(
|
||||
std::format(
|
||||
"expects {} as parent directory and find next module, but got a file",
|
||||
next.toBasicString())));
|
||||
}
|
||||
found2 = true;
|
||||
path = modPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found2 && !fs::exists(modPath))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Official Module `std.io`
|
||||
Library/std/io/io.fig
|
||||
Official Module `std.io`
|
||||
Library/std/io/io.fig
|
||||
*/
|
||||
|
||||
import _builtins;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Official Module `std.io`
|
||||
Library/std/io/noSpaces.fig
|
||||
Official Module `std.io`
|
||||
Library/std/io/noSpaces.fig
|
||||
*/
|
||||
|
||||
import _builtins;
|
||||
|
||||
155
src/Module/Library/std/math/math.fig
Normal file
155
src/Module/Library/std/math/math.fig
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
Official Module `math`
|
||||
Library/math/math.fig
|
||||
*/
|
||||
|
||||
import _builtins; // load __fmath functions
|
||||
import std.value; // `type` function
|
||||
|
||||
/* call native c mathematical functions */
|
||||
|
||||
public func acos(x)
|
||||
{
|
||||
return __fmath_acos(x);
|
||||
}
|
||||
|
||||
public func acosh(x)
|
||||
{
|
||||
return __fmath_acosh(x);
|
||||
}
|
||||
|
||||
public func asin(x)
|
||||
{
|
||||
return __fmath_asin(x);
|
||||
}
|
||||
|
||||
public func asinh(x)
|
||||
{
|
||||
return __fmath_asinh(x);
|
||||
}
|
||||
|
||||
public func atan(x)
|
||||
{
|
||||
return __fmath_atan(x);
|
||||
}
|
||||
|
||||
public func atan2(x, y)
|
||||
{
|
||||
return __fmath_atan(x, y);
|
||||
}
|
||||
|
||||
public func atanh(x)
|
||||
{
|
||||
return __fmath_atanh(x);
|
||||
}
|
||||
|
||||
public func ceil(x)
|
||||
{
|
||||
return __fmath_ceil(x);
|
||||
}
|
||||
|
||||
public func cos(x)
|
||||
{
|
||||
return __fmath_cos(x);
|
||||
}
|
||||
|
||||
public func cosh(x)
|
||||
{
|
||||
return __fmath_cosh(x);
|
||||
}
|
||||
|
||||
public func exp(x)
|
||||
{
|
||||
return __fmath_exp(x);
|
||||
}
|
||||
|
||||
public func expm1(x)
|
||||
{
|
||||
return __fmath_expm1(x);
|
||||
}
|
||||
|
||||
public func fabs(x)
|
||||
{
|
||||
return __fmath_fabs(x);
|
||||
}
|
||||
|
||||
public func floor(x)
|
||||
{
|
||||
return __fmath_floor(x);
|
||||
}
|
||||
|
||||
public func fmod(x, y)
|
||||
{
|
||||
return __fmath_fmod(x, y);
|
||||
}
|
||||
|
||||
public func frexp(x) -> List
|
||||
{
|
||||
// return List {mantissa, exponent
|
||||
return __fmath_frexp(x);
|
||||
}
|
||||
|
||||
public func gcd(x: Int, y: Int)
|
||||
{
|
||||
return __fmath_gcd(x, y);
|
||||
}
|
||||
|
||||
public func hypot(x, y)
|
||||
{
|
||||
return __fmath_hypot(x, y);
|
||||
}
|
||||
|
||||
public func isequal(x, y)
|
||||
{
|
||||
return __fmath_isequal(x, y);
|
||||
}
|
||||
|
||||
public func log(x)
|
||||
{
|
||||
return __fmath_log(x);
|
||||
}
|
||||
|
||||
public func log10(x)
|
||||
{
|
||||
return __fmath_log10(x);
|
||||
}
|
||||
|
||||
public func log1p(x)
|
||||
{
|
||||
return __fmath_log1p(x);
|
||||
}
|
||||
|
||||
public func log2(x)
|
||||
{
|
||||
return __fmath_log2(x);
|
||||
}
|
||||
|
||||
public func sin(x)
|
||||
{
|
||||
return __fmath_sin(x);
|
||||
}
|
||||
|
||||
public func sinh(x)
|
||||
{
|
||||
return __fmath_sinh(x);
|
||||
}
|
||||
|
||||
public func sqrt(x)
|
||||
{
|
||||
return __fmath_sqrt(x);
|
||||
}
|
||||
|
||||
public func tan(x)
|
||||
{
|
||||
return __fmath_tan(x);
|
||||
}
|
||||
|
||||
public func tanh(x)
|
||||
{
|
||||
return __fmath_tanh(x);
|
||||
}
|
||||
|
||||
public func trunc(x)
|
||||
{
|
||||
return __fmath_trunc(x);
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
Official Module `std`
|
||||
Library/std/std.fig
|
||||
Official Module `std`
|
||||
Library/std/std.fig
|
||||
*/
|
||||
|
||||
import io; // link std.io
|
||||
import value; // link std.type
|
||||
import math; // link std.math
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Official Module `std.value`
|
||||
Library/std/value/value.fig
|
||||
Official Module `std.value`
|
||||
Library/std/value/value.fig
|
||||
*/
|
||||
|
||||
import _builtins;
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
#include <Core/fig_string.hpp>
|
||||
#include <Value/value.hpp>
|
||||
|
||||
#include <numeric>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <print>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
@@ -32,6 +34,36 @@ namespace Fig
|
||||
{u8"__fvalue_double_parse", 1},
|
||||
{u8"__fvalue_double_from", 1},
|
||||
{u8"__fvalue_string_from", 1},
|
||||
/* math start */
|
||||
{u8"__fmath_acos", 1},
|
||||
{u8"__fmath_acosh", 1},
|
||||
{u8"__fmath_asin", 1},
|
||||
{u8"__fmath_asinh", 1},
|
||||
{u8"__fmath_atan", 1},
|
||||
{u8"__fmath_atan2", 2},
|
||||
{u8"__fmath_atanh", 1},
|
||||
{u8"__fmath_ceil", 1},
|
||||
{u8"__fmath_cos", 1},
|
||||
{u8"__fmath_cosh", 1},
|
||||
{u8"__fmath_exp", 1},
|
||||
{u8"__fmath_expm1", 1},
|
||||
{u8"__fmath_fabs", 1},
|
||||
{u8"__fmath_floor", 1},
|
||||
{u8"__fmath_fmod", 2},
|
||||
{u8"__fmath_frexp", 1},
|
||||
{u8"__fmath_gcd", 2},
|
||||
{u8"__fmath_hypot", 2},
|
||||
{u8"__fmath_isequal", 2},
|
||||
{u8"__fmath_log", 1},
|
||||
{u8"__fmath_log10", 1},
|
||||
{u8"__fmath_log1p", 1},
|
||||
{u8"__fmath_log2", 1},
|
||||
{u8"__fmath_sin", 1},
|
||||
{u8"__fmath_sinh", 1},
|
||||
{u8"__fmath_sqrt", 1},
|
||||
{u8"__fmath_tan", 1},
|
||||
{u8"__fmath_tanh", 1},
|
||||
{u8"__fmath_trunc", 1},
|
||||
};
|
||||
|
||||
const std::unordered_map<FString, BuiltinFunction> builtinFunctions{
|
||||
@@ -121,7 +153,158 @@ namespace Fig
|
||||
ObjectPtr val = args[0];
|
||||
return std::make_shared<Object>(val->toString());
|
||||
}},
|
||||
|
||||
/* math start */
|
||||
{u8"__fmath_acos", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(acos(d));
|
||||
}},
|
||||
{u8"__fmath_acosh", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(acosh(d));
|
||||
}},
|
||||
{u8"__fmath_asin", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(asin(d));
|
||||
}},
|
||||
{u8"__fmath_asinh", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(asinh(d));
|
||||
}},
|
||||
{u8"__fmath_atan", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(atan(d));
|
||||
}},
|
||||
{u8"__fmath_atan2", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ValueType::DoubleClass y = args[0]->getNumericValue();
|
||||
ValueType::DoubleClass x = args[1]->getNumericValue();
|
||||
return std::make_shared<Object>(atan2(y, x));
|
||||
}},
|
||||
{u8"__fmath_atanh", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(atanh(d));
|
||||
}},
|
||||
{u8"__fmath_ceil", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(ceil(d));
|
||||
}},
|
||||
{u8"__fmath_cos", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(cos(d));
|
||||
}},
|
||||
{u8"__fmath_cosh", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(cosh(d));
|
||||
}},
|
||||
{u8"__fmath_exp", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(exp(d));
|
||||
}},
|
||||
{u8"__fmath_expm1", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(expm1(d));
|
||||
}},
|
||||
{u8"__fmath_fabs", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(fabs(d));
|
||||
}},
|
||||
{u8"__fmath_floor", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(floor(d));
|
||||
}},
|
||||
{u8"__fmath_fmod", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ValueType::DoubleClass x = args[0]->getNumericValue();
|
||||
ValueType::DoubleClass y = args[1]->getNumericValue();
|
||||
return std::make_shared<Object>(fmod(x, y));
|
||||
}},
|
||||
{u8"__fmath_frexp", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
int e;
|
||||
return std::make_shared<Object>(List(
|
||||
{std::make_shared<Object>(frexp(d, &e)),
|
||||
std::make_shared<Object>(static_cast<ValueType::IntClass>(e))}));
|
||||
}},
|
||||
{u8"__fmath_gcd", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ValueType::IntClass x = args[0]->as<ValueType::IntClass>();
|
||||
ValueType::IntClass y = args[1]->as<ValueType::IntClass>();
|
||||
return std::make_shared<Object>(std::gcd(x, y));
|
||||
}},
|
||||
{u8"__fmath_hypot", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ValueType::DoubleClass x = args[0]->getNumericValue();
|
||||
ValueType::DoubleClass y = args[1]->getNumericValue();
|
||||
return std::make_shared<Object>(hypot(x, y));
|
||||
}},
|
||||
{u8"__fmath_isequal", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ValueType::DoubleClass x = args[0]->getNumericValue();
|
||||
ValueType::DoubleClass y = args[1]->getNumericValue();
|
||||
static const double epsilon = 1e-9;
|
||||
return std::make_shared<Object>(
|
||||
fabs(x - y) < epsilon
|
||||
);
|
||||
}},
|
||||
{u8"__fmath_log", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(log(d));
|
||||
}},
|
||||
{u8"__fmath_log10", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(log10(d));
|
||||
}},
|
||||
{u8"__fmath_log1p", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(log1p(d));
|
||||
}},
|
||||
{u8"__fmath_log2", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(log2(d));
|
||||
}},
|
||||
{u8"__fmath_sin", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(sin(d));
|
||||
}},
|
||||
{u8"__fmath_sinh", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(sinh(d));
|
||||
}},
|
||||
{u8"__fmath_sqrt", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(sqrt(d));
|
||||
}},
|
||||
{u8"__fmath_tan", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(tan(d));
|
||||
}},
|
||||
{u8"__fmath_tanh", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(tanh(d));
|
||||
}},
|
||||
{u8"__fmath_trunc", [](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
ObjectPtr val = args[0];
|
||||
ValueType::DoubleClass d = val->getNumericValue();
|
||||
return std::make_shared<Object>(trunc(d));
|
||||
}},
|
||||
};
|
||||
|
||||
inline bool isBuiltinFunction(const FString &name)
|
||||
@@ -149,4 +332,4 @@ namespace Fig
|
||||
return it->second;
|
||||
}
|
||||
}; // namespace Builtins
|
||||
}; // namespace Fig
|
||||
}; // namespace Fig.
|
||||
Reference in New Issue
Block a user