From 25c971118aa5afe9342478e86f55cb72fc99aa45 Mon Sep 17 00:00:00 2001 From: PuqiAR Date: Fri, 26 Dec 2025 22:26:06 +0800 Subject: [PATCH] =?UTF-8?q?[Feat]=20=E5=8F=AF=E5=8F=98=E5=8F=82=E6=95=B0?= =?UTF-8?q?=20variadic!=20=E7=9B=AE=E5=89=8D=E5=87=BD=E6=95=B0=E8=8B=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8F=AF=E5=8F=98=E5=8F=82=E6=95=B0,?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=8F=AA=E8=83=BD=E4=B8=BA1=E4=B8=AA=20=20?= =?UTF-8?q?=20=20=20=20=20=20=E8=AF=AD=E6=B3=95=20func=20(arg...)=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=88=B0=E7=9A=84=E7=B1=BB=E5=9E=8B=E4=B8=BA?= =?UTF-8?q?=20List=20=20=20=20=20=20=20=20=E8=8B=A5=E8=A6=81=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E7=B1=BB=E5=9E=8B,=E4=BD=BF=E7=94=A8=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=BA=93=20std.value.type=20[Feat]=20=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=BA=93=20std.io=20std.value=20=E5=AE=9E=E7=8E=B0,?= =?UTF-8?q?=E6=8F=90=E4=BE=9Bprint,read=E7=AD=89=E5=87=BD=E6=95=B0=20=20?= =?UTF-8?q?=20=20=20=20=20=20=E8=AF=A6=E8=A7=81=20Library/std=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=20[Impl]=20=E5=88=86=E7=A6=BB=20builtin=20value=20?= =?UTF-8?q?=E5=92=8C=20function=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Evaluator/evaluator.cpp | 22 +++++------ src/Evaluator/evaluator.hpp | 10 +++-- src/Module/Library/std/io/io.fig | 47 +++++++++++++++++++++-- src/Module/Library/std/io/noSpace.fig | 27 +++++++++++++ src/Module/Library/std/std.fig | 3 +- src/Module/Library/std/value/value.fig | 53 ++++++++++++++++++++++++++ src/main.cpp | 2 +- 7 files changed, 143 insertions(+), 21 deletions(-) create mode 100644 src/Module/Library/std/io/noSpace.fig create mode 100644 src/Module/Library/std/value/value.fig diff --git a/src/Evaluator/evaluator.cpp b/src/Evaluator/evaluator.cpp index 78abddd..7cbc659 100644 --- a/src/Evaluator/evaluator.cpp +++ b/src/Evaluator/evaluator.cpp @@ -1338,17 +1338,8 @@ namespace Fig nullptr); evaluator.SetGlobalContext(modctx); - evaluator.RegisterBuiltins(); - - try - { - evaluator.Run(asts); - } - catch (std::exception &e) - { - std::cerr << "load module failed" << '\n'; - throw e; - } + evaluator.RegisterBuiltinsValue(); + evaluator.Run(asts); return evaluator.global; } @@ -1356,11 +1347,16 @@ namespace Fig StatementResult Evaluator::evalImportSt(Ast::Import i, ContextPtr ctx) { const std::vector &pathVec = i->path; + + const FString &modName = pathVec.at(pathVec.size() - 1); // pathVec at least has 1 element + if (modName == u8"_builtins") + { + RegisterBuiltins(); + return StatementResult::normal(); + } auto path = resolveModulePath(pathVec); ContextPtr modCtx = loadModule(path); - const FString &modName = pathVec.at(pathVec.size() - 1); - // std::cerr << modName.toBasicString() << '\n'; DEBUG if (ctx->containsInThisScope(modName)) diff --git a/src/Evaluator/evaluator.hpp b/src/Evaluator/evaluator.hpp index 9806dcd..c8cea2d 100644 --- a/src/Evaluator/evaluator.hpp +++ b/src/Evaluator/evaluator.hpp @@ -72,7 +72,7 @@ namespace Fig FString(u8"")); } - void RegisterBuiltins() + void RegisterBuiltins() // only function { assert(global != nullptr); @@ -87,6 +87,11 @@ namespace Fig std::make_shared(f) ); } + } + + void RegisterBuiltinsValue() + { + assert(global != nullptr); for (auto &[name, val] : Builtins::builtinValues) { @@ -94,8 +99,7 @@ namespace Fig name, val->getTypeInfo(), AccessModifier::Const, - val - ); + val); } } /* Left-value eval*/ diff --git a/src/Module/Library/std/io/io.fig b/src/Module/Library/std/io/io.fig index 648147d..2bfc373 100644 --- a/src/Module/Library/std/io/io.fig +++ b/src/Module/Library/std/io/io.fig @@ -3,7 +3,48 @@ Official Module `std.io` Library/std/io/io.fig */ -public func print(value) -> Null +import _builtins; +import noSpace; // sub module `no space print` + +// outputs + +public func print(objects...) -> Int { - __fstdout_print(value); -} \ No newline at end of file + var length := objects.length(); + for var i:=0; i < length; i += 1 + { + if i > 0 + { + __fstdout_print(" "); + } + __fstdout_print(objects[i]); + } + return length; +} + +public func println(objects...) -> Int +{ + var length := objects.length(); + for var i:=0; i < length; i += 1 + { + if i > 0 + { + __fstdout_print(" "); + } + __fstdout_print(objects[i]); + } + __fstdout_print("\n"); + return length + 1; +} + +// inputs + +public func read() -> String +{ + return __fstdin_read(); +} + +public func readln() -> String +{ + return __fstdin_readln(); +} diff --git a/src/Module/Library/std/io/noSpace.fig b/src/Module/Library/std/io/noSpace.fig new file mode 100644 index 0000000..529ee2f --- /dev/null +++ b/src/Module/Library/std/io/noSpace.fig @@ -0,0 +1,27 @@ +/* +Official Module `std.io` +Library/std/io/noSpaces.fig +*/ + +import _builtins; + +public func print(objects...) -> Int +{ + var length := objects.length(); + for var i:=0; i < length; i += 1 + { + __fstdout_print(objects[i]); + } + return length; +} + +public func println(objects...) -> Int +{ + var length := objects.length(); + for var i:=0; i < length; i += 1 + { + __fstdout_print(objects[i]); + } + __fstdout_print("\n"); + return length + 1; +} \ No newline at end of file diff --git a/src/Module/Library/std/std.fig b/src/Module/Library/std/std.fig index 5c707c1..c07d33c 100644 --- a/src/Module/Library/std/std.fig +++ b/src/Module/Library/std/std.fig @@ -3,4 +3,5 @@ Official Module `std` Library/std/std.fig */ -import io; // link std io \ No newline at end of file +import io; // link std.io +import value; // link std.type \ No newline at end of file diff --git a/src/Module/Library/std/value/value.fig b/src/Module/Library/std/value/value.fig new file mode 100644 index 0000000..0ec2cdc --- /dev/null +++ b/src/Module/Library/std/value/value.fig @@ -0,0 +1,53 @@ +/* +Official Module `std.value` +Library/std/value/value.fig +*/ + +import _builtins; + +public func type(object: Any) -> String +{ + return __fvalue_type(object); +} + +public func int_parse(number: String) -> Int +{ + return __fvalue_int_parse(number); +} + +public func int_from(number: Any) -> Any +{ + var ntype := type(number); + if ntype == "Double" or ntype == "Bool" + { + return __fvalue_int_from(number); + } + if ntype == "Int" + { + return number; + } + return null; +} + +public func double_parse(number: String) -> Double +{ + return __fvalue_double_parse(number); +} + +public func double_from(number: Any) -> Any +{ + if ntype == "Int" or ntype == "Bool" + { + return __fvalue_double_from(number); + } + if ntype == "Double" + { + return number; + } + return null; +} + +public func string_from(obj: Any) -> String +{ + return __fvalue_string_from(obj); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f0613bc..b3edff6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -130,7 +130,7 @@ int main(int argc, char **argv) evaluator.SetSourcePath(sourcePath); evaluator.CreateGlobalContext(); - evaluator.RegisterBuiltins(); + evaluator.RegisterBuiltinsValue(); try {