From cd106fc5133f2fce73308d86ecc44b752e2081a2 Mon Sep 17 00:00:00 2001 From: PuqiAR Date: Mon, 29 Dec 2025 17:25:06 +0800 Subject: [PATCH] =?UTF-8?q?[Feat]=20=E5=A2=9E=E5=8A=A0=E6=95=B0=E5=AD=A6?= =?UTF-8?q?=E5=BA=93=20std.math=20[Fix]=20=E4=BF=AE=E5=A4=8DresolveModuleP?= =?UTF-8?q?ath=E8=A7=A3=E6=9E=90=E4=BA=8C=E7=BA=A7=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=BA=9BBug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Evaluator/evaluator.cpp | 14 ++ src/Module/Library/std/io/io.fig | 4 +- src/Module/Library/std/io/noSpace.fig | 4 +- src/Module/Library/std/math/math.fig | 155 ++++++++++++++++++++ src/Module/Library/std/std.fig | 9 +- src/Module/Library/std/value/value.fig | 4 +- src/Module/builtins.hpp | 187 ++++++++++++++++++++++++- 7 files changed, 365 insertions(+), 12 deletions(-) create mode 100644 src/Module/Library/std/math/math.fig diff --git a/src/Evaluator/evaluator.cpp b/src/Evaluator/evaluator.cpp index 7cbc659..26366e7 100644 --- a/src/Evaluator/evaluator.cpp +++ b/src/Evaluator/evaluator.cpp @@ -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)) diff --git a/src/Module/Library/std/io/io.fig b/src/Module/Library/std/io/io.fig index 2bfc373..c579e96 100644 --- a/src/Module/Library/std/io/io.fig +++ b/src/Module/Library/std/io/io.fig @@ -1,6 +1,6 @@ /* -Official Module `std.io` -Library/std/io/io.fig + Official Module `std.io` + Library/std/io/io.fig */ import _builtins; diff --git a/src/Module/Library/std/io/noSpace.fig b/src/Module/Library/std/io/noSpace.fig index 529ee2f..09e33a9 100644 --- a/src/Module/Library/std/io/noSpace.fig +++ b/src/Module/Library/std/io/noSpace.fig @@ -1,6 +1,6 @@ /* -Official Module `std.io` -Library/std/io/noSpaces.fig + Official Module `std.io` + Library/std/io/noSpaces.fig */ import _builtins; diff --git a/src/Module/Library/std/math/math.fig b/src/Module/Library/std/math/math.fig new file mode 100644 index 0000000..758ecb8 --- /dev/null +++ b/src/Module/Library/std/math/math.fig @@ -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); +} \ No newline at end of file diff --git a/src/Module/Library/std/std.fig b/src/Module/Library/std/std.fig index c07d33c..a1b3667 100644 --- a/src/Module/Library/std/std.fig +++ b/src/Module/Library/std/std.fig @@ -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 \ No newline at end of file +import io; // link std.io +import value; // link std.type +import math; // link std.math \ No newline at end of file diff --git a/src/Module/Library/std/value/value.fig b/src/Module/Library/std/value/value.fig index 0ec2cdc..70d9c9d 100644 --- a/src/Module/Library/std/value/value.fig +++ b/src/Module/Library/std/value/value.fig @@ -1,6 +1,6 @@ /* -Official Module `std.value` -Library/std/value/value.fig + Official Module `std.value` + Library/std/value/value.fig */ import _builtins; diff --git a/src/Module/builtins.hpp b/src/Module/builtins.hpp index 208f677..42af424 100644 --- a/src/Module/builtins.hpp +++ b/src/Module/builtins.hpp @@ -3,11 +3,13 @@ #include #include +#include #include #include #include #include #include +#include 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 builtinFunctions{ @@ -121,7 +153,158 @@ namespace Fig ObjectPtr val = args[0]; return std::make_shared(val->toString()); }}, - + /* math start */ + {u8"__fmath_acos", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(acos(d)); + }}, + {u8"__fmath_acosh", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(acosh(d)); + }}, + {u8"__fmath_asin", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(asin(d)); + }}, + {u8"__fmath_asinh", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(asinh(d)); + }}, + {u8"__fmath_atan", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(atan(d)); + }}, + {u8"__fmath_atan2", [](const std::vector &args) -> ObjectPtr { + ValueType::DoubleClass y = args[0]->getNumericValue(); + ValueType::DoubleClass x = args[1]->getNumericValue(); + return std::make_shared(atan2(y, x)); + }}, + {u8"__fmath_atanh", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(atanh(d)); + }}, + {u8"__fmath_ceil", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(ceil(d)); + }}, + {u8"__fmath_cos", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(cos(d)); + }}, + {u8"__fmath_cosh", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(cosh(d)); + }}, + {u8"__fmath_exp", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(exp(d)); + }}, + {u8"__fmath_expm1", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(expm1(d)); + }}, + {u8"__fmath_fabs", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(fabs(d)); + }}, + {u8"__fmath_floor", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(floor(d)); + }}, + {u8"__fmath_fmod", [](const std::vector &args) -> ObjectPtr { + ValueType::DoubleClass x = args[0]->getNumericValue(); + ValueType::DoubleClass y = args[1]->getNumericValue(); + return std::make_shared(fmod(x, y)); + }}, + {u8"__fmath_frexp", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + int e; + return std::make_shared(List( + {std::make_shared(frexp(d, &e)), + std::make_shared(static_cast(e))})); + }}, + {u8"__fmath_gcd", [](const std::vector &args) -> ObjectPtr { + ValueType::IntClass x = args[0]->as(); + ValueType::IntClass y = args[1]->as(); + return std::make_shared(std::gcd(x, y)); + }}, + {u8"__fmath_hypot", [](const std::vector &args) -> ObjectPtr { + ValueType::DoubleClass x = args[0]->getNumericValue(); + ValueType::DoubleClass y = args[1]->getNumericValue(); + return std::make_shared(hypot(x, y)); + }}, + {u8"__fmath_isequal", [](const std::vector &args) -> ObjectPtr { + ValueType::DoubleClass x = args[0]->getNumericValue(); + ValueType::DoubleClass y = args[1]->getNumericValue(); + static const double epsilon = 1e-9; + return std::make_shared( + fabs(x - y) < epsilon + ); + }}, + {u8"__fmath_log", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(log(d)); + }}, + {u8"__fmath_log10", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(log10(d)); + }}, + {u8"__fmath_log1p", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(log1p(d)); + }}, + {u8"__fmath_log2", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(log2(d)); + }}, + {u8"__fmath_sin", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(sin(d)); + }}, + {u8"__fmath_sinh", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(sinh(d)); + }}, + {u8"__fmath_sqrt", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(sqrt(d)); + }}, + {u8"__fmath_tan", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(tan(d)); + }}, + {u8"__fmath_tanh", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(tanh(d)); + }}, + {u8"__fmath_trunc", [](const std::vector &args) -> ObjectPtr { + ObjectPtr val = args[0]; + ValueType::DoubleClass d = val->getNumericValue(); + return std::make_shared(trunc(d)); + }}, }; inline bool isBuiltinFunction(const FString &name) @@ -149,4 +332,4 @@ namespace Fig return it->second; } }; // namespace Builtins -}; // namespace Fig \ No newline at end of file +}; // namespace Fig. \ No newline at end of file