[VER] 0.3.5-alpha
[FEAT] 现在,我们的内置类型 Int, String等也有构造函数了!像是StructType一样调用,Int{10}
[FEAT] List,Map赋值默认为Alias, 现在,通过 List{other}/Map{other} 来创建新的container! 复制出新的容器
[IMPL] Object toString对于 String type 输出进行修改!
[IMPL] ... 还有一些忘记了!
[ALL] Happy new year! And hope Fig-lang become better! Let's build a new world!
This commit is contained in:
@@ -108,7 +108,7 @@ BreakConstructorInitializers: AfterColon
|
||||
BreakStringLiterals: false
|
||||
|
||||
# 每行字符的限制,0表示没有限制
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 120
|
||||
|
||||
CompactNamespaces: true
|
||||
|
||||
|
||||
BIN
docs/FigRuntimeValueSystem.png
Normal file
BIN
docs/FigRuntimeValueSystem.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 633 KiB |
@@ -4,7 +4,7 @@
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
|
||||
#define __FCORE_VERSION "0.3.4-alpha"
|
||||
#define __FCORE_VERSION "0.3.5-alpha"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define __FCORE_PLATFORM "Windows"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
94
src/Module/Library/_builtins/_builtins.fig
Normal file
94
src/Module/Library/_builtins/_builtins.fig
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Module `_builtins`
|
||||
|
||||
This module represents Fig builtin runtime bindings.
|
||||
|
||||
Visible to IDEs and documentation tools,
|
||||
NOT importable, NOT executable as a real module.
|
||||
|
||||
Library/_builtins/_builtins.fig
|
||||
|
||||
Importing `_builtins` in real programs loads native implementations,
|
||||
not this file. This file only serves as documentation / IDE reference.
|
||||
|
||||
Copyright © 2025 PuqiAR. All rights reserved.
|
||||
*/
|
||||
|
||||
// =======================
|
||||
// stdout
|
||||
// =======================
|
||||
|
||||
func __fstdout_print(values ...) -> Int;
|
||||
|
||||
func __fstdout_println(values ...) -> Int;
|
||||
|
||||
|
||||
// =======================
|
||||
// stdin
|
||||
// =======================
|
||||
|
||||
func __fstdin_read() -> String;
|
||||
|
||||
func __fstdin_readln() -> String;
|
||||
|
||||
|
||||
// =======================
|
||||
// value utilities
|
||||
// =======================
|
||||
|
||||
func __fvalue_type(value: Any) -> String;
|
||||
|
||||
func __fvalue_int_parse(str: String) -> Int;
|
||||
func __fvalue_int_from(value: Any) -> Int;
|
||||
|
||||
func __fvalue_double_parse(str: String) -> Double;
|
||||
func __fvalue_double_from(value: Any) -> Double;
|
||||
|
||||
func __fvalue_string_from(value: Any) -> String;
|
||||
|
||||
|
||||
// =======================
|
||||
// math
|
||||
// =======================
|
||||
|
||||
func __fmath_acos(x: Any) -> Double;
|
||||
func __fmath_acosh(x: Any) -> Double;
|
||||
func __fmath_asin(x: Any) -> Double;
|
||||
func __fmath_asinh(x: Any) -> Double;
|
||||
func __fmath_atan(x: Any) -> Double;
|
||||
func __fmath_atan2(y: Any, x: Any) -> Double;
|
||||
func __fmath_atanh(x: Any) -> Double;
|
||||
|
||||
func __fmath_ceil(x: Any) -> Double;
|
||||
func __fmath_cos(x: Any) -> Double;
|
||||
func __fmath_cosh(x: Any) -> Double;
|
||||
|
||||
func __fmath_exp(x: Any) -> Double;
|
||||
func __fmath_expm1(x: Any) -> Double;
|
||||
|
||||
func __fmath_fabs(x: Any) -> Double;
|
||||
func __fmath_floor(x: Any) -> Double;
|
||||
func __fmath_fmod(x: Any, y: Any) -> Double;
|
||||
|
||||
// returns List[Double, Int]
|
||||
func __fmath_frexp(x: Any) -> List;
|
||||
|
||||
func __fmath_gcd(a: Int, b: Int) -> Int;
|
||||
func __fmath_hypot(a: Any, b: Any) -> Double;
|
||||
|
||||
func __fmath_isequal(a: Any, b: Any) -> Bool;
|
||||
|
||||
func __fmath_log(x: Any) -> Double;
|
||||
func __fmath_log10(x: Any) -> Double;
|
||||
func __fmath_log1p(x: Any) -> Double;
|
||||
func __fmath_log2(x: Any) -> Double;
|
||||
|
||||
func __fmath_sin(x: Any) -> Double;
|
||||
func __fmath_sinh(x: Any) -> Double;
|
||||
|
||||
func __fmath_sqrt(x: Any) -> Double;
|
||||
|
||||
func __fmath_tan(x: Any) -> Double;
|
||||
func __fmath_tanh(x: Any) -> Double;
|
||||
|
||||
func __fmath_trunc(x: Any) -> Double;
|
||||
42
src/Module/Library/lang/lang.fig
Normal file
42
src/Module/Library/lang/lang.fig
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
DO NOT IMPORT THIS FILE
|
||||
DO NOT IMPORT THIS FILE
|
||||
DO NOT IMPORT THIS FILE
|
||||
|
||||
Module `lang`
|
||||
|
||||
Visible to IDEs and documentation tools,
|
||||
NOT importable, NOT executable as a real module.
|
||||
|
||||
Library/lang/lang.fig
|
||||
|
||||
Copyright © 2025 PuqiAR. All rights reserved.
|
||||
*/
|
||||
|
||||
|
||||
// values
|
||||
|
||||
var null: Null;
|
||||
|
||||
var true: Bool;
|
||||
var false: Bool;
|
||||
|
||||
|
||||
// Error interface
|
||||
|
||||
public interface Error
|
||||
{
|
||||
toString() -> String;
|
||||
getErrorClass() -> String;
|
||||
getErrorMessage() -> String;
|
||||
}
|
||||
|
||||
public struct Any {}
|
||||
public struct Int {}
|
||||
public struct Null {}
|
||||
public struct String {}
|
||||
public struct Bool {}
|
||||
public struct Double {}
|
||||
public struct Function {}
|
||||
public struct List {}
|
||||
public struct Map {}
|
||||
@@ -87,7 +87,7 @@ public func fmod(x, y)
|
||||
|
||||
public func frexp(x) -> List
|
||||
{
|
||||
// return List {mantissa, exponent
|
||||
// return List {mantissa, exponent}
|
||||
return __fmath_frexp(x);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,22 +35,24 @@ namespace Fig
|
||||
{u8"Error",
|
||||
std::make_shared<Object>(InterfaceType(
|
||||
ErrorInterfaceTypeInfo,
|
||||
{Ast::InterfaceMethod(u8"toString",
|
||||
Ast::FunctionParameters({}, {}),
|
||||
u8"String",
|
||||
nullptr),
|
||||
Ast::InterfaceMethod(u8"getErrorClass",
|
||||
Ast::FunctionParameters({}, {}),
|
||||
u8"String",
|
||||
nullptr),
|
||||
Ast::InterfaceMethod(u8"getErrorMessage",
|
||||
Ast::FunctionParameters({}, {}),
|
||||
u8"String",
|
||||
nullptr)}))},
|
||||
{Ast::InterfaceMethod(u8"toString", Ast::FunctionParameters({}, {}), u8"String", nullptr),
|
||||
Ast::InterfaceMethod(u8"getErrorClass", Ast::FunctionParameters({}, {}), u8"String", nullptr),
|
||||
Ast::InterfaceMethod(u8"getErrorMessage", Ast::FunctionParameters({}, {}), u8"String", nullptr)}))},
|
||||
|
||||
{u8"Any", std::make_shared<Object>(StructType(ValueType::Any, nullptr, {}, true))},
|
||||
{u8"Int", std::make_shared<Object>(StructType(ValueType::Int, nullptr, {}, true))},
|
||||
{u8"Null", std::make_shared<Object>(StructType(ValueType::Null, nullptr, {}, true))},
|
||||
{u8"String", std::make_shared<Object>(StructType(ValueType::String, nullptr, {}, true))},
|
||||
{u8"Bool", std::make_shared<Object>(StructType(ValueType::Bool, nullptr, {}, true))},
|
||||
{u8"Double", std::make_shared<Object>(StructType(ValueType::Double, nullptr, {}, true))},
|
||||
{u8"Function", std::make_shared<Object>(StructType(ValueType::Function, nullptr, {}, true))},
|
||||
{u8"List", std::make_shared<Object>(StructType(ValueType::List, nullptr, {}, true))},
|
||||
{u8"Map", std::make_shared<Object>(StructType(ValueType::Map, nullptr, {}, true))},
|
||||
// Type `StructType` `StructInstance` `Module` `InterfaceType`
|
||||
// Not allowed to call constructor!
|
||||
};
|
||||
|
||||
using BuiltinFunction =
|
||||
std::function<ObjectPtr(const std::vector<ObjectPtr> &)>;
|
||||
using BuiltinFunction = std::function<ObjectPtr(const std::vector<ObjectPtr> &)>;
|
||||
|
||||
const std::unordered_map<FString, int> builtinFunctionArgCounts = {
|
||||
{u8"__fstdout_print", -1}, // variadic
|
||||
@@ -98,41 +100,30 @@ namespace Fig
|
||||
const std::unordered_map<FString, BuiltinFunction> builtinFunctions{
|
||||
{u8"__fstdout_print",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
for (auto arg : args)
|
||||
{
|
||||
std::print("{}", arg->toStringIO().toBasicString());
|
||||
}
|
||||
return std::make_shared<Object>(
|
||||
ValueType::IntClass(args.size()));
|
||||
for (auto arg : args) { std::print("{}", arg->toStringIO().toBasicString()); }
|
||||
return std::make_shared<Object>(ValueType::IntClass(args.size()));
|
||||
}},
|
||||
{u8"__fstdout_println",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
for (auto arg : args)
|
||||
{
|
||||
std::print("{}", arg->toStringIO().toBasicString());
|
||||
}
|
||||
for (auto arg : args) { std::print("{}", arg->toStringIO().toBasicString()); }
|
||||
std::print("\n");
|
||||
return std::make_shared<Object>(
|
||||
ValueType::IntClass(args.size()));
|
||||
return std::make_shared<Object>(ValueType::IntClass(args.size()));
|
||||
}},
|
||||
{u8"__fstdin_read",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
std::string input;
|
||||
std::cin >> input;
|
||||
return std::make_shared<Object>(
|
||||
FString::fromBasicString(input));
|
||||
return std::make_shared<Object>(FString::fromBasicString(input));
|
||||
}},
|
||||
{u8"__fstdin_readln",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
std::string line;
|
||||
std::getline(std::cin, line);
|
||||
return std::make_shared<Object>(
|
||||
FString::fromBasicString(line));
|
||||
return std::make_shared<Object>(FString::fromBasicString(line));
|
||||
}},
|
||||
{u8"__fvalue_type",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
return std::make_shared<Object>(
|
||||
args[0]->getTypeInfo().toString());
|
||||
return std::make_shared<Object>(args[0]->getTypeInfo().toString());
|
||||
}},
|
||||
{u8"__fvalue_int_parse",
|
||||
[](const std::vector<ObjectPtr> &args) -> ObjectPtr {
|
||||
@@ -144,9 +135,7 @@ namespace Fig
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw RuntimeError(
|
||||
FString(std::format("Invalid int string for parsing",
|
||||
str.toBasicString())));
|
||||
throw RuntimeError(FString(std::format("Invalid int string for parsing", str.toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_int_from",
|
||||
@@ -155,19 +144,16 @@ namespace Fig
|
||||
if (val->is<ValueType::DoubleClass>())
|
||||
{
|
||||
return std::make_shared<Object>(
|
||||
static_cast<ValueType::IntClass>(
|
||||
val->as<ValueType::DoubleClass>()));
|
||||
static_cast<ValueType::IntClass>(val->as<ValueType::DoubleClass>()));
|
||||
}
|
||||
else if (val->is<ValueType::BoolClass>())
|
||||
{
|
||||
return std::make_shared<Object>(
|
||||
static_cast<ValueType::IntClass>(
|
||||
val->as<ValueType::BoolClass>() ? 1 : 0));
|
||||
static_cast<ValueType::IntClass>(val->as<ValueType::BoolClass>() ? 1 : 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw RuntimeError(FString(std::format(
|
||||
"Type '{}' cannot be converted to int",
|
||||
throw RuntimeError(FString(std::format("Type '{}' cannot be converted to int",
|
||||
val->getTypeInfo().toString().toBasicString())));
|
||||
}
|
||||
}},
|
||||
@@ -176,16 +162,12 @@ namespace Fig
|
||||
FString str = args[0]->as<ValueType::StringClass>();
|
||||
try
|
||||
{
|
||||
ValueType::DoubleClass val =
|
||||
std::stod(str.toBasicString());
|
||||
return std::make_shared<Object>(
|
||||
ValueType::DoubleClass(val));
|
||||
ValueType::DoubleClass val = std::stod(str.toBasicString());
|
||||
return std::make_shared<Object>(ValueType::DoubleClass(val));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw RuntimeError(FString(
|
||||
std::format("Invalid double string for parsing",
|
||||
str.toBasicString())));
|
||||
throw RuntimeError(FString(std::format("Invalid double string for parsing", str.toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_double_from",
|
||||
@@ -194,18 +176,16 @@ namespace Fig
|
||||
if (val->is<ValueType::IntClass>())
|
||||
{
|
||||
return std::make_shared<Object>(
|
||||
static_cast<ValueType::DoubleClass>(
|
||||
val->as<ValueType::IntClass>()));
|
||||
static_cast<ValueType::DoubleClass>(val->as<ValueType::IntClass>()));
|
||||
}
|
||||
else if (val->is<ValueType::BoolClass>())
|
||||
{
|
||||
return std::make_shared<Object>(ValueType::DoubleClass(
|
||||
val->as<ValueType::BoolClass>() ? 1.0 : 0.0));
|
||||
return std::make_shared<Object>(
|
||||
ValueType::DoubleClass(val->as<ValueType::BoolClass>() ? 1.0 : 0.0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw RuntimeError(FString(std::format(
|
||||
"Type '{}' cannot be converted to double",
|
||||
throw RuntimeError(FString(std::format("Type '{}' cannot be converted to double",
|
||||
val->getTypeInfo().toString().toBasicString())));
|
||||
}
|
||||
}},
|
||||
@@ -310,10 +290,8 @@ namespace Fig
|
||||
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))}));
|
||||
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 {
|
||||
@@ -406,8 +384,7 @@ namespace Fig
|
||||
auto it = builtinFunctions.find(name);
|
||||
if (it == builtinFunctions.end())
|
||||
{
|
||||
throw RuntimeError(FString(std::format(
|
||||
"Builtin function '{}' not found", name.toBasicString())));
|
||||
throw RuntimeError(FString(std::format("Builtin function '{}' not found", name.toBasicString())));
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
@@ -417,8 +394,7 @@ namespace Fig
|
||||
auto it = builtinFunctionArgCounts.find(name);
|
||||
if (it == builtinFunctionArgCounts.end())
|
||||
{
|
||||
throw RuntimeError(FString(std::format(
|
||||
"Builtin function '{}' not found", name.toBasicString())));
|
||||
throw RuntimeError(FString(std::format("Builtin function '{}' not found", name.toBasicString())));
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace Fig
|
||||
if (numIndex >= list.size())
|
||||
throw RuntimeError(FString(
|
||||
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
|
||||
return list.at(numIndex);
|
||||
return list.at(numIndex).value;
|
||||
}
|
||||
else if (kind == Kind::MapElement) // map
|
||||
{
|
||||
|
||||
@@ -37,9 +37,11 @@ namespace Fig
|
||||
ContextPtr defContext; // 定义时的上下文
|
||||
std::vector<Field> fields;
|
||||
|
||||
bool builtin = false;
|
||||
|
||||
// ===== Constructors =====
|
||||
StructType(TypeInfo _type, ContextPtr _defContext, std::vector<Field> _fields) :
|
||||
type(std::move(_type)), defContext(std::move(_defContext)), fields(std::move(_fields)) {}
|
||||
StructType(TypeInfo _type, ContextPtr _defContext, std::vector<Field> _fields, bool _builtin = false) :
|
||||
type(std::move(_type)), defContext(std::move(_defContext)), fields(std::move(_fields)), builtin(_builtin) {}
|
||||
|
||||
StructType(const StructType &other) = default;
|
||||
StructType(StructType &&) noexcept = default;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Value/Type.hpp"
|
||||
#include <Value/Type.hpp>
|
||||
#include <Value/value.hpp>
|
||||
#include <Context/context.hpp>
|
||||
|
||||
|
||||
@@ -33,15 +33,37 @@ namespace Fig
|
||||
}
|
||||
class Object;
|
||||
using ObjectPtr = std::shared_ptr<Object>;
|
||||
using List = std::vector<ObjectPtr>;
|
||||
|
||||
|
||||
FString prettyType(std::shared_ptr<const Object> obj);
|
||||
|
||||
bool operator==(const Object &, const Object &);
|
||||
|
||||
struct Element
|
||||
{
|
||||
ObjectPtr value;
|
||||
Element(ObjectPtr _value) :
|
||||
value(_value) {}
|
||||
|
||||
bool operator==(const Element &other) const
|
||||
{
|
||||
return *value == *other.value;
|
||||
}
|
||||
|
||||
void deepCopy(const Element &e)
|
||||
{
|
||||
value = std::make_shared<Object>(*e.value);
|
||||
}
|
||||
};
|
||||
using List = std::vector<Element>;
|
||||
|
||||
struct ValueKey
|
||||
{
|
||||
ObjectPtr value;
|
||||
ValueKey(ObjectPtr _value) :
|
||||
value(_value) {}
|
||||
|
||||
void deepCopy(const ValueKey &vk) { value = std::make_shared<Object>(*vk.value); }
|
||||
};
|
||||
|
||||
struct ValueKeyHash
|
||||
@@ -180,7 +202,7 @@ namespace Fig
|
||||
const List &list = as<List>();
|
||||
if (i >= list.size())
|
||||
return Object::getNullInstance();
|
||||
return list[i];
|
||||
return list[i].value;
|
||||
}},
|
||||
{u8"push", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||
if (args.size() != 1)
|
||||
@@ -407,7 +429,7 @@ namespace Fig
|
||||
if (is<ValueType::NullClass>()) return FString(u8"null");
|
||||
if (is<ValueType::IntClass>()) return FString(std::to_string(as<ValueType::IntClass>()));
|
||||
if (is<ValueType::DoubleClass>()) return FString(std::format("{}", as<ValueType::DoubleClass>()));
|
||||
if (is<ValueType::StringClass>()) return FString(u8"<String \"") + as<ValueType::StringClass>() + FString(u8"\" >");
|
||||
if (is<ValueType::StringClass>()) return FString(u8"\"" + as<ValueType::StringClass>() + u8"\"");
|
||||
if (is<ValueType::BoolClass>()) return as<ValueType::BoolClass>() ? FString(u8"true") : FString(u8"false");
|
||||
if (is<Function>())
|
||||
return FString(std::format("<Function '{}' at {:p}>",
|
||||
@@ -430,7 +452,7 @@ namespace Fig
|
||||
{
|
||||
if (!first_flag)
|
||||
output += u8", ";
|
||||
output += ele->toString();
|
||||
output += ele.value->toString();
|
||||
first_flag = false;
|
||||
}
|
||||
output += u8"]";
|
||||
|
||||
Reference in New Issue
Block a user