forked from PuqiAR/Fig-TreeWalker
阶段性保存,全面修改Value为Object
This commit is contained in:
@@ -14,7 +14,7 @@ namespace Fig::Ast
|
||||
|
||||
struct FunctionCallArgs final
|
||||
{
|
||||
std::vector<Value> argv;
|
||||
std::vector<Object> argv;
|
||||
size_t getLength() const { return argv.size(); }
|
||||
};
|
||||
|
||||
|
||||
@@ -9,13 +9,13 @@ namespace Fig::Ast
|
||||
class ValueExprAst final : public ExpressionAst
|
||||
{
|
||||
public:
|
||||
Value val;
|
||||
Object val;
|
||||
|
||||
ValueExprAst()
|
||||
{
|
||||
type = AstType::ValueExpr;
|
||||
}
|
||||
ValueExprAst(Value _val)
|
||||
ValueExprAst(Object _val)
|
||||
{
|
||||
type = AstType::ValueExpr;
|
||||
val = std::move(_val);
|
||||
|
||||
@@ -63,15 +63,5 @@ namespace Fig
|
||||
using BoolClass = bool;
|
||||
using NullClass = std::monostate;
|
||||
using StringClass = FString;
|
||||
|
||||
/* complex types */
|
||||
struct FunctionStruct;
|
||||
using FunctionClass = FunctionStruct;
|
||||
|
||||
struct StructT;
|
||||
using StructTypeClass = StructT;
|
||||
|
||||
struct StructInstanceT;
|
||||
using StructInstanceClass = StructInstanceT;
|
||||
}; // namespace ValueType
|
||||
}; // namespace Fig
|
||||
@@ -1,32 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <Value/BaseValue.hpp>
|
||||
#include <Ast/functionParameters.hpp>
|
||||
#include <context_forward.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
class Value;
|
||||
|
||||
/* complex objects */
|
||||
struct FunctionStruct
|
||||
class Object;
|
||||
class Function
|
||||
{
|
||||
public:
|
||||
std::size_t id;
|
||||
Ast::FunctionParameters paras;
|
||||
TypeInfo retType;
|
||||
Ast::BlockStatement body;
|
||||
|
||||
bool isBuiltin = false;
|
||||
std::function<Value(const std::vector<Value> &)> builtin;
|
||||
std::function<Object(const std::vector<Object> &)> builtin;
|
||||
int builtinParamCount = -1;
|
||||
|
||||
std::shared_ptr<Context> closureContext;
|
||||
|
||||
FunctionStruct() = default;
|
||||
// ===== Constructors =====
|
||||
Function() :
|
||||
id(nextId()) {}
|
||||
|
||||
FunctionStruct(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body, ContextPtr _closureContext) :
|
||||
Function(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body, ContextPtr _closureContext) :
|
||||
id(nextId()), // 分配唯一 ID
|
||||
paras(std::move(_paras)),
|
||||
retType(std::move(_retType)),
|
||||
@@ -35,27 +38,23 @@ namespace Fig
|
||||
{
|
||||
}
|
||||
|
||||
FunctionStruct(std::function<Value(const std::vector<Value> &)> fn, int argc);
|
||||
Function(std::function<Object(const std::vector<Object> &)> fn, int argc) :
|
||||
id(nextId()), isBuiltin(true), builtin(std::move(fn)), builtinParamCount(argc)
|
||||
{
|
||||
}
|
||||
|
||||
FunctionStruct(const FunctionStruct &other) :
|
||||
id(other.id),
|
||||
paras(other.paras),
|
||||
retType(other.retType),
|
||||
body(other.body),
|
||||
isBuiltin(other.isBuiltin),
|
||||
builtin(other.builtin),
|
||||
builtinParamCount(other.builtinParamCount),
|
||||
closureContext(other.closureContext) {}
|
||||
// ===== Copy / Move =====
|
||||
Function(const Function &other) = default;
|
||||
Function(Function &&) noexcept = default;
|
||||
Function &operator=(const Function &) = default;
|
||||
Function &operator=(Function &&) noexcept = default;
|
||||
|
||||
FunctionStruct &operator=(const FunctionStruct &other) = default;
|
||||
FunctionStruct(FunctionStruct &&) noexcept = default;
|
||||
FunctionStruct &operator=(FunctionStruct &&) noexcept = default;
|
||||
|
||||
bool operator==(const FunctionStruct &other) const noexcept
|
||||
// ===== Comparison =====
|
||||
bool operator==(const Function &other) const noexcept
|
||||
{
|
||||
return id == other.id;
|
||||
}
|
||||
bool operator!=(const FunctionStruct &other) const noexcept
|
||||
bool operator!=(const Function &other) const noexcept
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
@@ -67,31 +66,4 @@ namespace Fig
|
||||
return counter++;
|
||||
}
|
||||
};
|
||||
|
||||
class Function final : public __ValueWrapper<FunctionStruct>
|
||||
{
|
||||
public:
|
||||
Function(const FunctionStruct &x) :
|
||||
__ValueWrapper(ValueType::Function)
|
||||
{
|
||||
data = std::make_unique<FunctionStruct>(x);
|
||||
}
|
||||
Function(Ast::FunctionParameters paras, TypeInfo ret, Ast::BlockStatement body, ContextPtr closureContext) :
|
||||
__ValueWrapper(ValueType::Function)
|
||||
{
|
||||
data = std::make_unique<FunctionStruct>(
|
||||
std::move(paras), std::move(ret), std::move(body), std::move(closureContext));
|
||||
}
|
||||
Function(std::function<Value(const std::vector<Value> &)> fn, int argc);
|
||||
|
||||
bool operator==(const Function &other) const noexcept
|
||||
{
|
||||
if (!data || !other.data) return false;
|
||||
return *data == *other.data; // call -> FunctionStruct::operator== (based on ID comparing)
|
||||
}
|
||||
Function(const Function &) = default;
|
||||
Function(Function &&) noexcept = default;
|
||||
Function &operator=(const Function &) = default;
|
||||
Function &operator=(Function &&) noexcept = default;
|
||||
};
|
||||
} // namespace Fig
|
||||
|
||||
@@ -1,50 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <Value/BaseValue.hpp>
|
||||
|
||||
#include <context_forward.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct StructInstanceT final
|
||||
struct StructInstance
|
||||
{
|
||||
size_t parentId;
|
||||
ContextPtr localContext;
|
||||
|
||||
StructInstanceT(size_t _parentId, ContextPtr _localContext) :
|
||||
parentId(std::move(_parentId)), localContext(std::move(_localContext)) {}
|
||||
|
||||
StructInstanceT(const StructInstanceT &other) :
|
||||
parentId(other.parentId), localContext(other.localContext) {}
|
||||
StructInstanceT &operator=(const StructInstanceT &) = default;
|
||||
StructInstanceT(StructInstanceT &&) = default;
|
||||
StructInstanceT &operator=(StructInstanceT &&) = default;
|
||||
|
||||
bool operator==(const StructInstanceT &) const = default;
|
||||
};
|
||||
|
||||
class StructInstance final : public __ValueWrapper<StructInstanceT>
|
||||
{
|
||||
public:
|
||||
StructInstance(const StructInstanceT &x) :
|
||||
__ValueWrapper(ValueType::StructInstance)
|
||||
{
|
||||
data = std::make_unique<StructInstanceT>(x);
|
||||
}
|
||||
// ===== Constructors =====
|
||||
StructInstance(size_t _parentId, ContextPtr _localContext) :
|
||||
__ValueWrapper(ValueType::StructInstance)
|
||||
{
|
||||
data = std::make_unique<StructInstanceT>(std::move(_parentId), std::move(_localContext));
|
||||
}
|
||||
parentId(_parentId), localContext(std::move(_localContext)) {}
|
||||
|
||||
StructInstance(const StructInstance &other) = default;
|
||||
StructInstance(StructInstance &&) noexcept = default;
|
||||
StructInstance &operator=(const StructInstance &) = default;
|
||||
StructInstance &operator=(StructInstance &&) noexcept = default;
|
||||
|
||||
// ===== Comparison =====
|
||||
bool operator==(const StructInstance &other) const noexcept
|
||||
{
|
||||
return data == other.data;
|
||||
return parentId == other.parentId && localContext == other.localContext;
|
||||
}
|
||||
bool operator!=(const StructInstance &other) const noexcept
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
StructInstance(const StructInstance &) = default;
|
||||
StructInstance(StructInstance &&) = default;
|
||||
StructInstance &operator=(const StructInstance &) = default;
|
||||
StructInstance &operator=(StructInstance &&) = default;
|
||||
};
|
||||
|
||||
}; // namespace Fig
|
||||
} // namespace Fig
|
||||
|
||||
@@ -7,56 +7,57 @@
|
||||
#include <Value/BaseValue.hpp>
|
||||
|
||||
#include <context_forward.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct Field
|
||||
{
|
||||
bool isPublic() const
|
||||
{
|
||||
return am == AccessModifier::Public or am == AccessModifier::PublicConst or am == AccessModifier::PublicFinal;
|
||||
}
|
||||
bool isConst() const
|
||||
{
|
||||
return am == AccessModifier::Const or am == AccessModifier::PublicConst;
|
||||
}
|
||||
bool isFinal() const
|
||||
{
|
||||
return am == AccessModifier::Final or am == AccessModifier::PublicFinal;
|
||||
}
|
||||
|
||||
AccessModifier am;
|
||||
FString name;
|
||||
TypeInfo type;
|
||||
Ast::Expression defaultValue;
|
||||
|
||||
Field(AccessModifier _am, FString _name, TypeInfo _type, Ast::Expression _defaultValue) :
|
||||
am(std::move(_am)), name(std::move(_name)), type(std::move(_type)), defaultValue(std::move(_defaultValue)) {}
|
||||
am(_am), name(std::move(_name)), type(std::move(_type)), defaultValue(std::move(_defaultValue)) {}
|
||||
|
||||
bool isPublic() const
|
||||
{
|
||||
return am == AccessModifier::Public || am == AccessModifier::PublicConst || am == AccessModifier::PublicFinal;
|
||||
}
|
||||
bool isConst() const
|
||||
{
|
||||
return am == AccessModifier::Const || am == AccessModifier::PublicConst;
|
||||
}
|
||||
bool isFinal() const
|
||||
{
|
||||
return am == AccessModifier::Final || am == AccessModifier::PublicFinal;
|
||||
}
|
||||
};
|
||||
struct StructT final// = StructType 结构体定义
|
||||
|
||||
struct StructType
|
||||
{
|
||||
std::size_t id;
|
||||
ContextPtr defContext; // 定义时的上下文
|
||||
std::vector<Field> fields;
|
||||
StructT(ContextPtr _defContext, std::vector<Field> fieldsMap) :
|
||||
defContext(std::move(_defContext)),
|
||||
fields(std::move(fieldsMap))
|
||||
{
|
||||
id = nextId();
|
||||
}
|
||||
StructT(const StructT &other) :
|
||||
id(other.id), fields(other.fields) {}
|
||||
StructT &operator=(const StructT &other) = default;
|
||||
StructT(StructT &&) noexcept = default;
|
||||
StructT &operator=(StructT &&) noexcept = default;
|
||||
|
||||
bool operator==(const StructT &other) const noexcept
|
||||
// ===== Constructors =====
|
||||
StructType(ContextPtr _defContext, std::vector<Field> _fields) :
|
||||
id(nextId()), defContext(std::move(_defContext)), fields(std::move(_fields)) {}
|
||||
|
||||
StructType(const StructType &other) = default;
|
||||
StructType(StructType &&) noexcept = default;
|
||||
StructType &operator=(const StructType &) = default;
|
||||
StructType &operator=(StructType &&) noexcept = default;
|
||||
|
||||
// ===== Comparison =====
|
||||
bool operator==(const StructType &other) const noexcept
|
||||
{
|
||||
return id == other.id;
|
||||
}
|
||||
bool operator!=(const StructT &other) const noexcept
|
||||
bool operator!=(const StructType &other) const noexcept
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
@@ -68,28 +69,4 @@ namespace Fig
|
||||
return counter++;
|
||||
}
|
||||
};
|
||||
|
||||
class StructType final : public __ValueWrapper<StructT>
|
||||
{
|
||||
public:
|
||||
StructType(const StructT &x) :
|
||||
__ValueWrapper(ValueType::StructType)
|
||||
{
|
||||
data = std::make_unique<StructT>(x);
|
||||
}
|
||||
StructType(ContextPtr _defContext, std::vector<Field> fieldsMap) :
|
||||
__ValueWrapper(ValueType::StructType)
|
||||
{
|
||||
data = std::make_unique<StructT>(std::move(_defContext), std::move(fieldsMap));
|
||||
}
|
||||
bool operator==(const StructType &other) const noexcept
|
||||
{
|
||||
return data == other.data;
|
||||
}
|
||||
StructType(const StructType &) = default;
|
||||
StructType(StructType &&) noexcept = default;
|
||||
StructType &operator=(const StructType &) = default;
|
||||
StructType &operator=(StructType &&) noexcept = default;
|
||||
};
|
||||
|
||||
}; // namespace Fig
|
||||
} // namespace Fig
|
||||
|
||||
@@ -13,13 +13,13 @@ namespace Fig
|
||||
{
|
||||
namespace Builtins
|
||||
{
|
||||
const std::unordered_map<FString, Value> builtinValues = {
|
||||
{u8"null", Value::getNullInstance()},
|
||||
{u8"true", Value(true)},
|
||||
{u8"false", Value(false)},
|
||||
const std::unordered_map<FString, Object> builtinValues = {
|
||||
{u8"null", Object::getNullInstance()},
|
||||
{u8"true", Object(true)},
|
||||
{u8"false", Object(false)},
|
||||
};
|
||||
|
||||
using BuiltinFunction = std::function<Value(const std::vector<Value> &)>;
|
||||
using BuiltinFunction = std::function<Object(const std::vector<Object> &)>;
|
||||
|
||||
const std::unordered_map<FString, int> builtinFunctionArgCounts = {
|
||||
{u8"__fstdout_print", -1}, // variadic
|
||||
@@ -35,91 +35,91 @@ namespace Fig
|
||||
};
|
||||
|
||||
const std::unordered_map<FString, BuiltinFunction> builtinFunctions{
|
||||
{u8"__fstdout_print", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdout_print", [](const std::vector<Object> &args) -> Object {
|
||||
for (auto arg : args)
|
||||
{
|
||||
std::print("{}", arg.toString().toBasicString());
|
||||
}
|
||||
return Value(Int(args.size()));
|
||||
return Object(Int(args.size()));
|
||||
}},
|
||||
{u8"__fstdout_println", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdout_println", [](const std::vector<Object> &args) -> Object {
|
||||
for (auto arg : args)
|
||||
{
|
||||
std::print("{}", arg.toString().toBasicString());
|
||||
}
|
||||
std::print("\n");
|
||||
return Value(Int(args.size()));
|
||||
return Object(Int(args.size()));
|
||||
}},
|
||||
{u8"__fstdin_read", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdin_read", [](const std::vector<Object> &args) -> Object {
|
||||
std::string input;
|
||||
std::cin >> input;
|
||||
return Value(FString::fromBasicString(input));
|
||||
return Object(FString::fromBasicString(input));
|
||||
}},
|
||||
{u8"__fstdin_readln", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdin_readln", [](const std::vector<Object> &args) -> Object {
|
||||
std::string line;
|
||||
std::getline(std::cin, line);
|
||||
return Value(FString::fromBasicString(line));
|
||||
return Object(FString::fromBasicString(line));
|
||||
}},
|
||||
{u8"__fvalue_type", [](const std::vector<Value> &args) -> Value {
|
||||
return Value(args[0].getTypeInfo().toString());
|
||||
{u8"__fvalue_type", [](const std::vector<Object> &args) -> Object {
|
||||
return Object(args[0].getTypeInfo().toString());
|
||||
}},
|
||||
{u8"__fvalue_int_parse", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fvalue_int_parse", [](const std::vector<Object> &args) -> Object {
|
||||
FString str = args[0].as<String>().getValue();
|
||||
try
|
||||
{
|
||||
ValueType::IntClass val = std::stoi(str.toBasicString());
|
||||
return Value(Int(val));
|
||||
return Object(Int(val));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Invalid int string for parsing", str.toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_int_from", [](const std::vector<Value> &args) -> Value {
|
||||
Value val = args[0];
|
||||
{u8"__fvalue_int_from", [](const std::vector<Object> &args) -> Object {
|
||||
Object val = args[0];
|
||||
if (val.is<Double>())
|
||||
{
|
||||
return Value(Int(static_cast<ValueType::IntClass>(val.as<Double>().getValue())));
|
||||
return Object(Int(static_cast<ValueType::IntClass>(val.as<Double>().getValue())));
|
||||
}
|
||||
else if (val.is<Bool>())
|
||||
{
|
||||
return Value(Int(val.as<Bool>().getValue() ? 1 : 0));
|
||||
return Object(Int(val.as<Bool>().getValue() ? 1 : 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Type '{}' cannot be converted to int", val.getTypeInfo().toString().toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_double_parse", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fvalue_double_parse", [](const std::vector<Object> &args) -> Object {
|
||||
FString str = args[0].as<String>().getValue();
|
||||
try
|
||||
{
|
||||
ValueType::DoubleClass val = std::stod(str.toBasicString());
|
||||
return Value(Double(val));
|
||||
return Object(Double(val));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Invalid double string for parsing", str.toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_double_from", [](const std::vector<Value> &args) -> Value {
|
||||
Value val = args[0];
|
||||
{u8"__fvalue_double_from", [](const std::vector<Object> &args) -> Object {
|
||||
Object val = args[0];
|
||||
if (val.is<Int>())
|
||||
{
|
||||
return Value(Double(static_cast<ValueType::DoubleClass>(val.as<Int>().getValue())));
|
||||
return Object(Double(static_cast<ValueType::DoubleClass>(val.as<Int>().getValue())));
|
||||
}
|
||||
else if (val.is<Bool>())
|
||||
{
|
||||
return Value(Double(val.as<Bool>().getValue() ? 1.0 : 0.0));
|
||||
return Object(Double(val.as<Bool>().getValue() ? 1.0 : 0.0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Type '{}' cannot be converted to double", val.getTypeInfo().toString().toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_string_from", [](const std::vector<Value> &args) -> Value {
|
||||
Value val = args[0];
|
||||
return Value(val.toString());
|
||||
{u8"__fvalue_string_from", [](const std::vector<Object> &args) -> Object {
|
||||
Object val = args[0];
|
||||
return Object(val.toString());
|
||||
}},
|
||||
|
||||
};
|
||||
|
||||
@@ -15,10 +15,10 @@ namespace Fig
|
||||
private:
|
||||
FString scopeName;
|
||||
std::unordered_map<FString, TypeInfo> varTypes;
|
||||
std::unordered_map<FString, Value> variables;
|
||||
std::unordered_map<FString, Object> variables;
|
||||
std::unordered_map<FString, AccessModifier> ams;
|
||||
|
||||
std::unordered_map<std::size_t, FunctionStruct> functions;
|
||||
std::unordered_map<std::size_t, Function> functions;
|
||||
std::unordered_map<std::size_t, FString> functionNames;
|
||||
|
||||
std::unordered_map<std::size_t, FString> structTypeNames;
|
||||
@@ -28,7 +28,7 @@ namespace Fig
|
||||
Context(const Context &) = default;
|
||||
Context(const FString &name, ContextPtr p = nullptr) :
|
||||
scopeName(name), parent(p) {}
|
||||
Context(const FString &name, std::unordered_map<FString, TypeInfo> types, std::unordered_map<FString, Value> vars, std::unordered_map<FString, AccessModifier> _ams) :
|
||||
Context(const FString &name, std::unordered_map<FString, TypeInfo> types, std::unordered_map<FString, Object> vars, std::unordered_map<FString, AccessModifier> _ams) :
|
||||
scopeName(std::move(name)), varTypes(std::move(types)), variables(std::move(vars)), ams(std::move(_ams)) {}
|
||||
|
||||
void setParent(ContextPtr _parent)
|
||||
@@ -46,7 +46,7 @@ namespace Fig
|
||||
return scopeName;
|
||||
}
|
||||
|
||||
std::optional<Value> get(const FString &name)
|
||||
std::optional<Object> get(const FString &name)
|
||||
{
|
||||
auto it = variables.find(name);
|
||||
if (it != variables.end())
|
||||
@@ -73,7 +73,7 @@ namespace Fig
|
||||
ContextPtr createCopyWithPublicVariables()
|
||||
{
|
||||
std::unordered_map<FString, TypeInfo> _varTypes;
|
||||
std::unordered_map<FString, Value> _variables;
|
||||
std::unordered_map<FString, Object> _variables;
|
||||
std::unordered_map<FString, AccessModifier> _ams;
|
||||
for (const auto &p : this->variables)
|
||||
{
|
||||
@@ -96,7 +96,7 @@ namespace Fig
|
||||
AccessModifier am = getAccessModifier(name); // may throw
|
||||
return am == AccessModifier::Public or am == AccessModifier::PublicConst or am == AccessModifier::PublicFinal;
|
||||
}
|
||||
void set(const FString &name, const Value &value)
|
||||
void set(const FString &name, const Object &value)
|
||||
{
|
||||
if (variables.contains(name))
|
||||
{
|
||||
@@ -115,7 +115,7 @@ namespace Fig
|
||||
throw RuntimeError(FStringView(std::format("Variable '{}' not defined", name.toBasicString())));
|
||||
}
|
||||
}
|
||||
void def(const FString &name, const TypeInfo &ti, AccessModifier am, const Value &value = Any())
|
||||
void def(const FString &name, const TypeInfo &ti, AccessModifier am, const Object &value = Any())
|
||||
{
|
||||
if (containsInThisScope(name))
|
||||
{
|
||||
@@ -127,17 +127,17 @@ namespace Fig
|
||||
|
||||
if (ti == ValueType::Function and value.getTypeInfo() == ValueType::Function)
|
||||
{
|
||||
auto &fn = value.as<Function>().getValue();
|
||||
auto &fn = value.as<Function>();
|
||||
functions[fn.id] = fn;
|
||||
functionNames[fn.id] = name;
|
||||
}
|
||||
if (ti == ValueType::StructType)
|
||||
{
|
||||
auto &st = value.as<StructType>().getValue();
|
||||
auto &st = value.as<StructType>();
|
||||
structTypeNames[st.id] = name;
|
||||
}
|
||||
}
|
||||
std::optional<FunctionStruct> getFunction(std::size_t id)
|
||||
std::optional<Function> getFunction(std::size_t id)
|
||||
{
|
||||
auto it = functions.find(id);
|
||||
if (it != functions.end())
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Fig
|
||||
};
|
||||
struct StatementResult
|
||||
{
|
||||
Value result;
|
||||
Object result;
|
||||
enum class Flow
|
||||
{
|
||||
Normal,
|
||||
@@ -39,26 +39,26 @@ namespace Fig
|
||||
Continue
|
||||
} flow;
|
||||
|
||||
StatementResult(Value val, Flow f = Flow::Normal) :
|
||||
StatementResult(Object val, Flow f = Flow::Normal) :
|
||||
result(val), flow(f)
|
||||
{
|
||||
}
|
||||
|
||||
static StatementResult normal(Value val = Value::getNullInstance())
|
||||
static StatementResult normal(Object val = Object::getNullInstance())
|
||||
{
|
||||
return StatementResult(val, Flow::Normal);
|
||||
}
|
||||
static StatementResult returnFlow(Value val)
|
||||
static StatementResult returnFlow(Object val)
|
||||
{
|
||||
return StatementResult(val, Flow::Return);
|
||||
}
|
||||
static StatementResult breakFlow()
|
||||
{
|
||||
return StatementResult(Value::getNullInstance(), Flow::Break);
|
||||
return StatementResult(Object::getNullInstance(), Flow::Break);
|
||||
}
|
||||
static StatementResult continueFlow()
|
||||
{
|
||||
return StatementResult(Value::getNullInstance(), Flow::Continue);
|
||||
return StatementResult(Object::getNullInstance(), Flow::Continue);
|
||||
}
|
||||
|
||||
bool isNormal() const { return flow == Flow::Normal; }
|
||||
@@ -91,7 +91,7 @@ namespace Fig
|
||||
name,
|
||||
ValueType::Function,
|
||||
AccessModifier::PublicConst,
|
||||
Value(f));
|
||||
Object(f));
|
||||
}
|
||||
|
||||
for (auto &[name, val] : Builtins::builtinValues)
|
||||
@@ -107,16 +107,16 @@ namespace Fig
|
||||
std::shared_ptr<Context> getCurrentContext() { return currentContext; }
|
||||
std::shared_ptr<Context> getGlobalContext() { return globalContext; }
|
||||
|
||||
Value __evalOp(Ast::Operator, const Value &, const Value & = Value::getNullInstance());
|
||||
Value evalBinary(const Ast::BinaryExpr &);
|
||||
Value evalUnary(const Ast::UnaryExpr &);
|
||||
Object __evalOp(Ast::Operator, const Object &, const Object & = Object::getNullInstance());
|
||||
Object evalBinary(const Ast::BinaryExpr &);
|
||||
Object evalUnary(const Ast::UnaryExpr &);
|
||||
|
||||
StatementResult evalBlockStatement(const Ast::BlockStatement &, ContextPtr = nullptr);
|
||||
StatementResult evalStatement(const Ast::Statement &);
|
||||
|
||||
Value evalFunctionCall(const Function &, const Ast::FunctionArguments &, FString fnName = u8"<anonymous>");
|
||||
Object evalFunctionCall(const Function &, const Ast::FunctionArguments &, FString fnName = u8"<anonymous>");
|
||||
|
||||
Value eval(Ast::Expression);
|
||||
Object eval(Ast::Expression);
|
||||
void run();
|
||||
void printStackTrace() const;
|
||||
};
|
||||
|
||||
@@ -298,7 +298,7 @@ namespace Fig
|
||||
static constexpr FString varDefTypeFollowed = u8"(Followed)";
|
||||
|
||||
Ast::VarDef __parseVarDef(bool); // entry: current is keyword `var` or `const` (isConst: Bool)
|
||||
Value __parseValue();
|
||||
Object __parseValue();
|
||||
Ast::ValueExpr __parseValueExpr();
|
||||
Ast::FunctionParameters __parseFunctionParameters(); // entry: current is Token::LeftParen
|
||||
Ast::BlockStatement __parseBlockStatement(); // entry: current is Token::LeftBrace
|
||||
|
||||
@@ -18,42 +18,52 @@ namespace Fig
|
||||
{
|
||||
return std::floor(d) == d;
|
||||
}
|
||||
|
||||
inline bool isNumberExceededIntLimit(ValueType::DoubleClass d)
|
||||
{
|
||||
static constexpr ValueType::DoubleClass intMaxAsDouble = static_cast<ValueType::DoubleClass>(std::numeric_limits<ValueType::IntClass>::max());
|
||||
static constexpr ValueType::DoubleClass intMinAsDouble = static_cast<ValueType::DoubleClass>(std::numeric_limits<ValueType::IntClass>::min());
|
||||
return d > intMaxAsDouble || d < intMinAsDouble;
|
||||
}
|
||||
class Value
|
||||
|
||||
class Object
|
||||
{
|
||||
public:
|
||||
using VariantType = std::variant<Null, Int, Double, String, Bool, Function, StructType, StructInstance>;
|
||||
using VariantType = std::variant<
|
||||
Null,
|
||||
Int,
|
||||
Double,
|
||||
String,
|
||||
Bool,
|
||||
Function,
|
||||
StructType,
|
||||
StructInstance>;
|
||||
|
||||
VariantType data;
|
||||
|
||||
Value() :
|
||||
Object() :
|
||||
data(Null{}) {}
|
||||
Value(const Null &n) :
|
||||
Object(const Null &n) :
|
||||
data(std::in_place_type<Null>, n) {}
|
||||
Value(const Int &i) :
|
||||
Object(const Int &i) :
|
||||
data(std::in_place_type<Int>, i) {}
|
||||
Value(const Double &d) :
|
||||
data(std::in_place_type<Double>, d)
|
||||
Object(const Double &d)
|
||||
{
|
||||
ValueType::IntClass casted = static_cast<ValueType::IntClass>(d.getValue());
|
||||
if (casted == d.getValue())
|
||||
{
|
||||
data.emplace<Int>(casted);
|
||||
}
|
||||
else
|
||||
data.emplace<Double>(d);
|
||||
}
|
||||
Value(const String &s) :
|
||||
Object(const String &s) :
|
||||
data(std::in_place_type<String>, s) {}
|
||||
Value(const Bool &b) :
|
||||
Object(const Bool &b) :
|
||||
data(std::in_place_type<Bool>, b) {}
|
||||
Value(const Function &f) :
|
||||
Object(const Function &f) :
|
||||
data(std::in_place_type<Function>, f) {}
|
||||
Value(const StructType &s) :
|
||||
Object(const StructType &s) :
|
||||
data(std::in_place_type<StructType>, s) {}
|
||||
Value(const StructInstance &s) :
|
||||
Object(const StructInstance &s) :
|
||||
data(std::in_place_type<StructInstance>, s) {}
|
||||
|
||||
template <typename T,
|
||||
@@ -62,58 +72,48 @@ namespace Fig
|
||||
|| std::is_same_v<T, ValueType::DoubleClass>
|
||||
|| std::is_same_v<T, ValueType::StringClass>
|
||||
|| std::is_same_v<T, ValueType::BoolClass>
|
||||
|| std::is_same_v<T, ValueType::FunctionClass>
|
||||
|| std::is_same_v<T, ValueType::StructTypeClass>>>
|
||||
Value(const T &val)
|
||||
|| std::is_same_v<T, Function>
|
||||
|| std::is_same_v<T, StructType>
|
||||
|| std::is_same_v<T, StructInstance>>>
|
||||
Object(const T &val)
|
||||
{
|
||||
// 不可以用 data = 的形式
|
||||
// __ValueWrapper 构造、拷贝有限制
|
||||
if constexpr (std::is_same_v<T, ValueType::IntClass>)
|
||||
data.emplace<Int>(val);
|
||||
else if constexpr (std::is_same_v<T, ValueType::DoubleClass>)
|
||||
{
|
||||
ValueType::IntClass casted = static_cast<ValueType::IntClass>(val);
|
||||
if (casted == val)
|
||||
{
|
||||
data.emplace<Int>(casted);
|
||||
}
|
||||
else
|
||||
{
|
||||
data.emplace<Double>(val);
|
||||
}
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, ValueType::StringClass>)
|
||||
data.emplace<String>(val);
|
||||
else if constexpr (std::is_same_v<T, ValueType::BoolClass>)
|
||||
data.emplace<Bool>(val);
|
||||
else if constexpr (std::is_same_v<T, ValueType::FunctionClass>)
|
||||
else if constexpr (std::is_same_v<T, Function>)
|
||||
data.emplace<Function>(val);
|
||||
else if constexpr (std::is_same_v<T, ValueType::StructTypeClass>)
|
||||
else if constexpr (std::is_same_v<T, StructType>)
|
||||
data.emplace<StructType>(val);
|
||||
else if constexpr (std::is_same_v<T, ValueType::StructInstanceClass>)
|
||||
else if constexpr (std::is_same_v<T, StructInstance>)
|
||||
data.emplace<StructInstance>(val);
|
||||
}
|
||||
Value(const Value &) = default;
|
||||
Value(Value &&) noexcept = default;
|
||||
Value &operator=(const Value &) = default;
|
||||
Value &operator=(Value &&) noexcept = default;
|
||||
|
||||
static Value defaultValue(TypeInfo ti)
|
||||
Object(const Object &) = default;
|
||||
Object(Object &&) noexcept = default;
|
||||
Object &operator=(const Object &) = default;
|
||||
Object &operator=(Object &&) noexcept = default;
|
||||
|
||||
static Object defaultValue(TypeInfo ti)
|
||||
{
|
||||
if (ti == ValueType::Int)
|
||||
return Value(Int(0));
|
||||
return Object(Int(0));
|
||||
else if (ti == ValueType::Double)
|
||||
return Value(Double(0.0));
|
||||
return Object(Double(0.0));
|
||||
else if (ti == ValueType::String)
|
||||
return Value(String(u8""));
|
||||
return Object(String(u8""));
|
||||
else if (ti == ValueType::Bool)
|
||||
return Value(Bool(false));
|
||||
else if (ti == ValueType::Function)
|
||||
return getNullInstance();
|
||||
else if (ti == ValueType::StructType)
|
||||
return getNullInstance();
|
||||
else if (ti == ValueType::StructInstance)
|
||||
return getNullInstance();
|
||||
return Object(Bool(false));
|
||||
else
|
||||
return getNullInstance();
|
||||
}
|
||||
@@ -136,15 +136,36 @@ namespace Fig
|
||||
return std::get<T>(data);
|
||||
}
|
||||
|
||||
static Value getNullInstance()
|
||||
static Object getNullInstance()
|
||||
{
|
||||
static Value v(Null{});
|
||||
static Object v(Null{});
|
||||
return v;
|
||||
}
|
||||
|
||||
TypeInfo getTypeInfo() const
|
||||
{
|
||||
return std::visit([](auto &&val) { return val.ti; }, data);
|
||||
return std::visit([](auto &&val) -> TypeInfo {
|
||||
using T = std::decay_t<decltype(val)>;
|
||||
if constexpr (std::is_same_v<T, Null>)
|
||||
return ValueType::Null;
|
||||
else if constexpr (std::is_same_v<T, Int>)
|
||||
return ValueType::Int;
|
||||
else if constexpr (std::is_same_v<T, Double>)
|
||||
return ValueType::Double;
|
||||
else if constexpr (std::is_same_v<T, String>)
|
||||
return ValueType::String;
|
||||
else if constexpr (std::is_same_v<T, Bool>)
|
||||
return ValueType::Bool;
|
||||
else if constexpr (std::is_same_v<T, Function>)
|
||||
return ValueType::Function;
|
||||
else if constexpr (std::is_same_v<T, StructType>)
|
||||
return ValueType::StructType;
|
||||
else if constexpr (std::is_same_v<T, StructInstance>)
|
||||
return ValueType::StructInstance;
|
||||
else
|
||||
return ValueType::Any;
|
||||
},
|
||||
data);
|
||||
}
|
||||
|
||||
bool isNull() const { return is<Null>(); }
|
||||
@@ -168,263 +189,257 @@ namespace Fig
|
||||
if (is<String>()) return as<String>().getValue();
|
||||
if (is<Bool>()) return as<Bool>().getValue() ? FString(u8"true") : FString(u8"false");
|
||||
if (is<Function>())
|
||||
{
|
||||
return FString(std::format("<Function {} at {:p}>",
|
||||
as<Function>().getValue().id,
|
||||
static_cast<const void *>(as<Function>().data.get())));
|
||||
}
|
||||
as<Function>().id,
|
||||
static_cast<const void *>(&as<Function>())));
|
||||
if (is<StructType>())
|
||||
{
|
||||
return FString(std::format("<StructType {} at {:p}>",
|
||||
as<StructType>().getValue().id,
|
||||
static_cast<const void *>(as<StructType>().data.get())));
|
||||
}
|
||||
as<StructType>().id,
|
||||
static_cast<const void *>(&as<StructType>())));
|
||||
if (is<StructInstance>())
|
||||
{
|
||||
return FString(std::format("<Struct Instance('{}') at {:p}",
|
||||
as<StructInstance>().getValue().parentId,
|
||||
static_cast<const void *>(as<StructInstance>().data.get())));
|
||||
}
|
||||
return FString(std::format("<Struct Instance('{}') at {:p}>",
|
||||
as<StructInstance>().parentId,
|
||||
static_cast<const void *>(&as<StructInstance>())));
|
||||
return FString(u8"<error>");
|
||||
}
|
||||
|
||||
private:
|
||||
static std::string makeTypeErrorMessage(const char *prefix, const char *op,
|
||||
const Value &lhs, const Value &rhs)
|
||||
const Object &lhs, const Object &rhs)
|
||||
{
|
||||
auto lhs_type = std::visit([](auto &&v) { return v.ti.name.toBasicString(); }, lhs.data);
|
||||
auto rhs_type = std::visit([](auto &&v) { return v.ti.name.toBasicString(); }, rhs.data);
|
||||
auto lhs_type = lhs.getTypeInfo().name.toBasicString();
|
||||
auto rhs_type = rhs.getTypeInfo().name.toBasicString();
|
||||
return std::format("{}: {} '{}' {}", prefix, lhs_type, op, rhs_type);
|
||||
}
|
||||
|
||||
public:
|
||||
// math
|
||||
friend Value operator+(const Value &lhs, const Value &rhs)
|
||||
friend Object operator+(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNull() || rhs.isNull())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Cannot add", "+", lhs, rhs)));
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
bool lhsIsInt = lhs.is<Int>();
|
||||
bool rhsIsInt = rhs.is<Int>();
|
||||
ValueType::DoubleClass result = lhs.getNumericValue() + rhs.getNumericValue();
|
||||
if (isDoubleInteger(result) and !isNumberExceededIntLimit(result))
|
||||
{
|
||||
return Value(static_cast<ValueType::IntClass>(result));
|
||||
}
|
||||
return Value(ValueType::DoubleClass(result));
|
||||
if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result))
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(ValueType::DoubleClass(result));
|
||||
}
|
||||
if (lhs.is<String>() && rhs.is<String>())
|
||||
return Value(ValueType::StringClass(lhs.as<String>().getValue() + rhs.as<String>().getValue()));
|
||||
|
||||
return Object(ValueType::StringClass(lhs.as<String>().getValue() + rhs.as<String>().getValue()));
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "+", lhs, rhs)));
|
||||
}
|
||||
|
||||
friend Value operator-(const Value &lhs, const Value &rhs)
|
||||
friend Object operator-(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNull() || rhs.isNull())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Cannot subtract", "-", lhs, rhs)));
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
bool lhsIsInt = lhs.is<Int>();
|
||||
bool rhsIsInt = rhs.is<Int>();
|
||||
ValueType::DoubleClass result = lhs.getNumericValue() - rhs.getNumericValue();
|
||||
if (isDoubleInteger(result) and !isNumberExceededIntLimit(result))
|
||||
{
|
||||
return Value(static_cast<ValueType::IntClass>(result));
|
||||
}
|
||||
return Value(ValueType::DoubleClass(result));
|
||||
if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result))
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(ValueType::DoubleClass(result));
|
||||
}
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "-", lhs, rhs)));
|
||||
}
|
||||
|
||||
friend Value operator*(const Value &lhs, const Value &rhs)
|
||||
friend Object operator*(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNull() || rhs.isNull())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Cannot multiply", "*", lhs, rhs)));
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
bool lhsIsInt = lhs.is<Int>();
|
||||
bool rhsIsInt = rhs.is<Int>();
|
||||
ValueType::DoubleClass result = lhs.getNumericValue() * rhs.getNumericValue();
|
||||
if (isDoubleInteger(result) and !isNumberExceededIntLimit(result))
|
||||
{
|
||||
return Value(static_cast<ValueType::IntClass>(result));
|
||||
}
|
||||
return Value(ValueType::DoubleClass(result));
|
||||
if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result))
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(ValueType::DoubleClass(result));
|
||||
}
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "*", lhs, rhs)));
|
||||
}
|
||||
|
||||
friend Value operator/(const Value &lhs, const Value &rhs)
|
||||
friend Object operator/(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNull() || rhs.isNull())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Cannot divide", "/", lhs, rhs)));
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
ValueType::DoubleClass rnv = rhs.getNumericValue();
|
||||
if (rnv == 0) throw ValueError(FStringView(makeTypeErrorMessage("Division by zero", "/", lhs, rhs)));
|
||||
if (rnv == 0)
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Division by zero", "/", lhs, rhs)));
|
||||
ValueType::DoubleClass result = lhs.getNumericValue() / rnv;
|
||||
if (isDoubleInteger(result) and !isNumberExceededIntLimit(result))
|
||||
{
|
||||
return Value(static_cast<ValueType::IntClass>(result));
|
||||
}
|
||||
return Value(ValueType::DoubleClass(result));
|
||||
bool lhsIsInt = lhs.is<Int>();
|
||||
bool rhsIsInt = rhs.is<Int>();
|
||||
if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result))
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(ValueType::DoubleClass(result));
|
||||
}
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "/", lhs, rhs)));
|
||||
}
|
||||
|
||||
friend Value operator%(const Value &lhs, const Value &rhs)
|
||||
friend Object operator%(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNull() || rhs.isNull())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Cannot modulo", "%", lhs, rhs)));
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
{
|
||||
ValueType::DoubleClass rnv = rhs.getNumericValue();
|
||||
if (rnv == 0) throw ValueError(FStringView(makeTypeErrorMessage("Modulo by zero", "/", lhs, rhs)));
|
||||
if (rnv == 0)
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Modulo by zero", "%", lhs, rhs)));
|
||||
ValueType::DoubleClass result = fmod(lhs.getNumericValue(), rnv);
|
||||
if (isDoubleInteger(result) and !isNumberExceededIntLimit(result))
|
||||
{
|
||||
return Value(static_cast<ValueType::IntClass>(result));
|
||||
}
|
||||
return Value(ValueType::DoubleClass(result));
|
||||
bool lhsIsInt = lhs.is<Int>();
|
||||
bool rhsIsInt = rhs.is<Int>();
|
||||
if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result))
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(ValueType::DoubleClass(result));
|
||||
}
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "%", lhs, rhs)));
|
||||
}
|
||||
|
||||
// logic
|
||||
friend Value operator&&(const Value &lhs, const Value &rhs)
|
||||
friend Object operator&&(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Bool>() || !rhs.is<Bool>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Logical AND requires bool", "&&", lhs, rhs)));
|
||||
return Value(lhs.as<Bool>().getValue() && rhs.as<Bool>().getValue());
|
||||
return Object(lhs.as<Bool>().getValue() && rhs.as<Bool>().getValue());
|
||||
}
|
||||
|
||||
friend Value operator||(const Value &lhs, const Value &rhs)
|
||||
friend Object operator||(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Bool>() || !rhs.is<Bool>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Logical OR requires bool", "||", lhs, rhs)));
|
||||
return Value(lhs.as<Bool>().getValue() || rhs.as<Bool>().getValue());
|
||||
return Object(lhs.as<Bool>().getValue() || rhs.as<Bool>().getValue());
|
||||
}
|
||||
|
||||
friend Value operator!(const Value &v)
|
||||
friend Object operator!(const Object &v)
|
||||
{
|
||||
if (!v.is<Bool>())
|
||||
throw ValueError(FStringView(std::format("Logical NOT requires bool: '{}'",
|
||||
std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data))));
|
||||
return Value(!v.as<Bool>().getValue());
|
||||
throw ValueError(FStringView(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
return Object(!v.as<Bool>().getValue());
|
||||
}
|
||||
|
||||
friend Value operator-(const Value &v)
|
||||
friend Object operator-(const Object &v)
|
||||
{
|
||||
if (v.isNull())
|
||||
throw ValueError(FStringView(std::format("Unary minus cannot be applied to null")));
|
||||
if (v.is<Int>())
|
||||
return Value(-v.as<Int>().getValue());
|
||||
return Object(-v.as<Int>().getValue());
|
||||
if (v.is<Double>())
|
||||
return Value(-v.as<Double>().getValue());
|
||||
throw ValueError(FStringView(std::format("Unary minus requires int or double: '{}'",
|
||||
std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data))));
|
||||
}
|
||||
friend Value operator~(const Value &v)
|
||||
{
|
||||
if (!v.is<Int>())
|
||||
throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'",
|
||||
std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data))));
|
||||
return Value(~v.as<Int>().getValue());
|
||||
return Object(-v.as<Double>().getValue());
|
||||
throw ValueError(FStringView(std::format("Unary minus requires int or double: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
}
|
||||
|
||||
// compare → now returns bool
|
||||
friend bool operator==(const Value &lhs, const Value &rhs)
|
||||
friend Object operator~(const Object &v)
|
||||
{
|
||||
if (!v.is<Int>())
|
||||
throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
return Object(~v.as<Int>().getValue());
|
||||
}
|
||||
|
||||
// compare
|
||||
friend bool operator==(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
return lhs.data == rhs.data;
|
||||
}
|
||||
|
||||
friend bool operator!=(const Value &lhs, const Value &rhs)
|
||||
friend bool operator!=(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
return !(lhs.data == rhs.data);
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
friend bool operator<(const Value &lhs, const Value &rhs)
|
||||
friend bool operator<(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
return lhs.getNumericValue() < rhs.getNumericValue();
|
||||
if (lhs.is<String>() && rhs.is<String>()) return lhs.as<String>().getValue() < rhs.as<String>().getValue();
|
||||
if (lhs.is<String>() && rhs.is<String>())
|
||||
return lhs.as<String>().getValue() < rhs.as<String>().getValue();
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported comparison", "<", lhs, rhs)));
|
||||
}
|
||||
|
||||
friend bool operator<=(const Value &lhs, const Value &rhs)
|
||||
friend bool operator<=(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
return lhs == rhs or lhs < rhs;
|
||||
return lhs == rhs || lhs < rhs;
|
||||
}
|
||||
|
||||
friend bool operator>(const Value &lhs, const Value &rhs)
|
||||
friend bool operator>(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (lhs.isNumeric() and rhs.isNumeric())
|
||||
if (lhs.isNumeric() && rhs.isNumeric())
|
||||
return lhs.getNumericValue() > rhs.getNumericValue();
|
||||
if (lhs.is<String>() && rhs.is<String>()) return lhs.as<String>().getValue() > rhs.as<String>().getValue();
|
||||
if (lhs.is<String>() && rhs.is<String>())
|
||||
return lhs.as<String>().getValue() > rhs.as<String>().getValue();
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported comparison", ">", lhs, rhs)));
|
||||
}
|
||||
|
||||
friend bool operator>=(const Value &lhs, const Value &rhs)
|
||||
friend bool operator>=(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
return lhs == rhs or lhs > rhs;
|
||||
return lhs == rhs || lhs > rhs;
|
||||
}
|
||||
|
||||
// bitwise
|
||||
friend Value bit_and(const Value &lhs, const Value &rhs)
|
||||
friend Object bit_and(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Int>() || !rhs.is<Int>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Bitwise AND requires int", "&", lhs, rhs)));
|
||||
return Value(lhs.as<Int>().getValue() & rhs.as<Int>().getValue());
|
||||
return Object(lhs.as<Int>().getValue() & rhs.as<Int>().getValue());
|
||||
}
|
||||
|
||||
friend Value bit_or(const Value &lhs, const Value &rhs)
|
||||
friend Object bit_or(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Int>() || !rhs.is<Int>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Bitwise OR requires int", "|", lhs, rhs)));
|
||||
return Value(lhs.as<Int>().getValue() | rhs.as<Int>().getValue());
|
||||
return Object(lhs.as<Int>().getValue() | rhs.as<Int>().getValue());
|
||||
}
|
||||
|
||||
friend Value bit_xor(const Value &lhs, const Value &rhs)
|
||||
friend Object bit_xor(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Int>() || !rhs.is<Int>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Bitwise XOR requires int", "^", lhs, rhs)));
|
||||
return Value(lhs.as<Int>().getValue() ^ rhs.as<Int>().getValue());
|
||||
return Object(lhs.as<Int>().getValue() ^ rhs.as<Int>().getValue());
|
||||
}
|
||||
|
||||
friend Value bit_not(const Value &v)
|
||||
friend Object bit_not(const Object &v)
|
||||
{
|
||||
if (!v.is<Int>())
|
||||
throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'",
|
||||
std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data))));
|
||||
return Value(~v.as<Int>().getValue());
|
||||
throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
|
||||
return Object(~v.as<Int>().getValue());
|
||||
}
|
||||
|
||||
friend Value shift_left(const Value &lhs, const Value &rhs)
|
||||
friend Object shift_left(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Int>() || !rhs.is<Int>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Shift left requires int", "<<", lhs, rhs)));
|
||||
return Value(lhs.as<Int>().getValue() << rhs.as<Int>().getValue());
|
||||
return Object(lhs.as<Int>().getValue() << rhs.as<Int>().getValue());
|
||||
}
|
||||
|
||||
friend Value shift_right(const Value &lhs, const Value &rhs)
|
||||
friend Object shift_right(const Object &lhs, const Object &rhs)
|
||||
{
|
||||
if (!lhs.is<Int>() || !rhs.is<Int>())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Shift right requires int", ">>", lhs, rhs)));
|
||||
return Value(lhs.as<Int>().getValue() >> rhs.as<Int>().getValue());
|
||||
return Object(lhs.as<Int>().getValue() >> rhs.as<Int>().getValue());
|
||||
}
|
||||
|
||||
friend Value power(const Value &base, const Value &exp)
|
||||
friend Object power(const Object &base, const Object &exp)
|
||||
{
|
||||
if (base.isNull() || exp.isNull())
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Cannot exponentiate", "**", base, exp)));
|
||||
if (base.isNumeric() and exp.isNumeric())
|
||||
if (base.isNumeric() && exp.isNumeric())
|
||||
{
|
||||
bool baseIsInt = base.is<Int>();
|
||||
bool expIsInt = exp.is<Int>();
|
||||
ValueType::DoubleClass result = std::pow(base.getNumericValue(), exp.getNumericValue());
|
||||
if (isDoubleInteger(result) and !isNumberExceededIntLimit(result))
|
||||
{
|
||||
return Value(static_cast<ValueType::IntClass>(result));
|
||||
}
|
||||
return Value(ValueType::DoubleClass(result));
|
||||
if (baseIsInt && expIsInt && isDoubleInteger(result) && !isNumberExceededIntLimit(result))
|
||||
return Object(static_cast<ValueType::IntClass>(result));
|
||||
return Object(ValueType::DoubleClass(result));
|
||||
}
|
||||
throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "**", base, exp)));
|
||||
}
|
||||
};
|
||||
|
||||
using Any = Value;
|
||||
using Any = Object;
|
||||
} // namespace Fig
|
||||
|
||||
Reference in New Issue
Block a user