[Feat] 可变参数 variadic! 目前函数若使用可变参数,参数只能为1个
语法 func (arg...) 获取到的类型为 List
若要判断类型,使用标准库 std.value.type
[Feat] 标准库 std.io std.value 实现,提供print,read等函数
详见 Library/std 实现
[Impl] 分离 builtin value 和 function注册
This commit is contained in:
@@ -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<FString> &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))
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace Fig
|
||||
FString(u8"<Global>"));
|
||||
}
|
||||
|
||||
void RegisterBuiltins()
|
||||
void RegisterBuiltins() // only function
|
||||
{
|
||||
assert(global != nullptr);
|
||||
|
||||
@@ -87,6 +87,11 @@ namespace Fig
|
||||
std::make_shared<Object>(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*/
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
27
src/Module/Library/std/io/noSpace.fig
Normal file
27
src/Module/Library/std/io/noSpace.fig
Normal file
@@ -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;
|
||||
}
|
||||
@@ -3,4 +3,5 @@ Official Module `std`
|
||||
Library/std/std.fig
|
||||
*/
|
||||
|
||||
import io; // link std io
|
||||
import io; // link std.io
|
||||
import value; // link std.type
|
||||
53
src/Module/Library/std/value/value.fig
Normal file
53
src/Module/Library/std/value/value.fig
Normal file
@@ -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);
|
||||
}
|
||||
@@ -130,7 +130,7 @@ int main(int argc, char **argv)
|
||||
|
||||
evaluator.SetSourcePath(sourcePath);
|
||||
evaluator.CreateGlobalContext();
|
||||
evaluator.RegisterBuiltins();
|
||||
evaluator.RegisterBuiltinsValue();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user