阶段性保存,全面修改Value为Object

This commit is contained in:
2025-12-22 19:18:43 +08:00
parent 34a56beb90
commit 271846143a
15 changed files with 334 additions and 418 deletions

View File

@@ -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(); }
};

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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());
}},
};

View File

@@ -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())

View File

@@ -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;
};

View File

@@ -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

View File

@@ -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

View File

@@ -1,19 +0,0 @@
#include <value.hpp>
#include <Value/function.hpp>
namespace Fig
{
FunctionStruct::FunctionStruct(std::function<Value(const std::vector<Value> &)> fn,
int argc) :
id(nextId()),
isBuiltin(true),
builtin(std::move(fn)),
builtinParamCount(argc) {}
Function::Function(std::function<Value(const std::vector<Value> &)> fn,
int argc) :
__ValueWrapper(ValueType::Function)
{
data = std::make_unique<FunctionStruct>(std::move(fn), argc);
}
}; // namespace Fig

View File

@@ -4,7 +4,7 @@
namespace Fig
{
Value Evaluator::__evalOp(Ast::Operator op, const Value &lhs, const Value &rhs)
Object Evaluator::__evalOp(Ast::Operator op, const Object &lhs, const Object &rhs)
{
using Fig::Ast::Operator;
switch (op)
@@ -20,8 +20,8 @@ namespace Fig
case Operator::Or: return lhs || rhs;
case Operator::Not: return !lhs;
case Operator::Equal: return Value(lhs == rhs);
case Operator::NotEqual: return Value(lhs != rhs);
case Operator::Equal: return Object(lhs == rhs);
case Operator::NotEqual: return Object(lhs != rhs);
case Operator::Less: return lhs < rhs;
case Operator::LessEqual: return lhs <= rhs;
case Operator::Greater: return lhs > rhs;
@@ -43,11 +43,11 @@ namespace Fig
}
}
Value Evaluator::evalBinary(const Ast::BinaryExpr &binExp)
Object Evaluator::evalBinary(const Ast::BinaryExpr &binExp)
{
if (binExp->op == Ast::Operator::Dot)
{
const Value &lhs = eval(binExp->lexp);
const Object &lhs = eval(binExp->lexp);
if (!lhs.is<StructInstance>())
{
static constexpr char AccessOpObjectNotStructError[] = "AccessOpObjectNotStructError";
@@ -55,7 +55,7 @@ namespace Fig
std::format("Object not a struct")),
binExp->lexp->getAAI());
}
const StructInstanceT &st = lhs.as<StructInstance>().getValue();
const StructInstance &st = lhs.as<StructInstance>();
Ast::VarExpr varExp;
if (!(varExp = std::dynamic_pointer_cast<Ast::VarExprAst>(binExp->rexp)))
{
@@ -96,7 +96,7 @@ namespace Fig
}
return __evalOp(binExp->op, eval(binExp->lexp), eval(binExp->rexp));
}
Value Evaluator::evalUnary(const Ast::UnaryExpr &unExp)
Object Evaluator::evalUnary(const Ast::UnaryExpr &unExp)
{
using Fig::Ast::Operator;
switch (unExp->op)
@@ -112,9 +112,9 @@ namespace Fig
}
}
Value Evaluator::evalFunctionCall(const Function &fn, const Ast::FunctionArguments &fnArgs, FString fnName)
Object Evaluator::evalFunctionCall(const Function &fn, const Ast::FunctionArguments &fnArgs, FString fnName)
{
FunctionStruct fnStruct = fn.getValue();
const Function& fnStruct = fn;
Ast::FunctionCallArgs evaluatedArgs;
if (fnStruct.isBuiltin)
{
@@ -143,7 +143,7 @@ namespace Fig
for (i = 0; i < fnParas.posParas.size(); i++)
{
TypeInfo expectedType(fnParas.posParas[i].second); // look up type info, if exists a type with the name, use it, else throw
Value argVal = eval(fnArgs.argv[i]);
Object argVal = eval(fnArgs.argv[i]);
TypeInfo actualType = argVal.getTypeInfo();
if (expectedType != actualType and expectedType != ValueType::Any)
{
@@ -158,14 +158,14 @@ namespace Fig
size_t defParamIndex = i - fnParas.posParas.size();
TypeInfo expectedType = fnParas.defParas[defParamIndex].second.first;
Value defaultVal = eval(fnParas.defParas[defParamIndex].second.second);
Object defaultVal = eval(fnParas.defParas[defParamIndex].second.second);
if (expectedType != defaultVal.getTypeInfo() and expectedType != ValueType::Any)
{
static constexpr char DefaultParameterTypeErrorName[] = "DefaultParameterTypeError";
throw EvaluatorError<DefaultParameterTypeErrorName>(FStringView(std::format("In function '{}', default parameter '{}' has type '{}', which does not match the expected type '{}'", fnName.toBasicString(), fnParas.defParas[defParamIndex].first.toBasicString(), defaultVal.getTypeInfo().toString().toBasicString(), expectedType.toString().toBasicString())), currentAddressInfo);
}
Value argVal = eval(fnArgs.argv[i]);
Object argVal = eval(fnArgs.argv[i]);
TypeInfo actualType = argVal.getTypeInfo();
if (expectedType != actualType and expectedType != ValueType::Any)
{
@@ -178,7 +178,7 @@ namespace Fig
for (; i < fnParas.size(); i++)
{
size_t defParamIndex = i - fnParas.posParas.size();
Value defaultVal = eval(fnParas.defParas[defParamIndex].second.second);
Object defaultVal = eval(fnParas.defParas[defParamIndex].second.second);
evaluatedArgs.argv.push_back(defaultVal);
}
// create new context for function call
@@ -206,7 +206,7 @@ namespace Fig
currentContext->def(paramName, paramType, argAm, evaluatedArgs.argv[j]);
}
// execute function body
Value retVal = Value::getNullInstance();
Object retVal = Object::getNullInstance();
for (const auto &stmt : fnStruct.body->stmts)
{
StatementResult sr = evalStatement(stmt);
@@ -225,7 +225,7 @@ namespace Fig
return retVal;
}
Value Evaluator::eval(Ast::Expression exp)
Object Evaluator::eval(Ast::Expression exp)
{
using Fig::Ast::AstType;
switch (exp->getType())
@@ -255,7 +255,7 @@ namespace Fig
case AstType::FunctionCall: {
auto fnCall = std::dynamic_pointer_cast<Ast::FunctionCallExpr>(exp);
Value calleeVal = eval(fnCall->callee);
Object calleeVal = eval(fnCall->callee);
if (!calleeVal.is<Function>())
{
@@ -309,13 +309,13 @@ namespace Fig
static constexpr char StructNotFoundErrorName[] = "StructNotFoundError";
throw EvaluatorError<StructNotFoundErrorName>(FStringView(std::format("Structure type '{}' not found", initExpr->structName.toBasicString())), initExpr->getAAI());
}
Value structTypeVal = currentContext->get(initExpr->structName).value();
Object structTypeVal = currentContext->get(initExpr->structName).value();
if (!structTypeVal.is<StructType>())
{
static constexpr char NotAStructTypeErrorName[] = "NotAStructTypeError";
throw EvaluatorError<NotAStructTypeErrorName>(FStringView(std::format("'{}' is not a structure type", initExpr->structName.toBasicString())), initExpr->getAAI());
}
const StructT &structT = structTypeVal.as<StructType>().getValue();
const StructType &structT = structTypeVal.as<StructType>();
ContextPtr defContext = structT.defContext; // definition context
// check init args
@@ -334,7 +334,7 @@ namespace Fig
throw EvaluatorError<StructInitArgumentMismatchErrorName>(FStringView(std::format("Structure '{}' expects {} to {} fields, but {} were provided", initExpr->structName.toBasicString(), minArgs, maxArgs, initExpr->args.size())), initExpr->getAAI());
}
std::vector<std::pair<FString, Value>> evaluatedArgs;
std::vector<std::pair<FString, Object>> evaluatedArgs;
for (const auto &[argName, argExpr] : initExpr->args)
{
evaluatedArgs.push_back({argName, eval(argExpr)});
@@ -363,7 +363,7 @@ namespace Fig
ContextPtr previousContext = currentContext;
currentContext = defContext; // evaluate default value in definition context
Value defaultVal = eval(field.defaultValue); // it can't be null here
Object defaultVal = eval(field.defaultValue); // it can't be null here
currentContext = previousContext;
@@ -378,7 +378,7 @@ namespace Fig
continue;
}
const Value &argVal = evaluatedArgs[i].second;
const Object &argVal = evaluatedArgs[i].second;
if (expectedType != argVal.getTypeInfo() && expectedType != ValueType::Any)
{
static constexpr char StructFieldTypeMismatchErrorName[] = "StructFieldTypeMismatchError";
@@ -404,7 +404,7 @@ namespace Fig
// use default value
ContextPtr previousContext = currentContext;
currentContext = defContext; // evaluate default value in definition context
Value defaultVal = eval(field.defaultValue); // it can't be null here
Object defaultVal = eval(field.defaultValue); // it can't be null here
currentContext = previousContext;
// type check
@@ -418,7 +418,7 @@ namespace Fig
instanceCtx->def(fieldName, field.type, field.am, defaultVal);
continue;
}
const Value &argVal = evaluatedArgs[i].second;
const Object &argVal = evaluatedArgs[i].second;
if (field.type != argVal.getTypeInfo() && field.type != ValueType::Any)
{
static constexpr char StructFieldTypeMismatchErrorName[] = "StructFieldTypeMismatchError";
@@ -432,7 +432,7 @@ namespace Fig
}
default:
throw RuntimeError(FStringView("Unknown expression type:" + std::to_string(static_cast<int>(exp->getType()))));
return Value::getNullInstance();
return Object::getNullInstance();
}
}
StatementResult Evaluator::evalBlockStatement(const Ast::BlockStatement &blockSt, ContextPtr context)
@@ -472,7 +472,7 @@ namespace Fig
static constexpr char RedeclarationErrorName[] = "RedeclarationError";
throw EvaluatorError<RedeclarationErrorName>(FStringView(std::format("Variable '{}' already defined in this scope", varDef->name.toBasicString())), currentAddressInfo);
}
Value val;
Object val;
TypeInfo varTypeInfo;
if (varDef->typeName == Parser::varDefTypeFollowed)
{
@@ -497,7 +497,7 @@ namespace Fig
else if (!varDef->typeName.empty())
{
varTypeInfo = TypeInfo(varDef->typeName); // may throw
val = Value::defaultValue(varTypeInfo);
val = Object::defaultValue(varTypeInfo);
}
AccessModifier am = (varDef->isPublic ? (varDef->isConst ? AccessModifier::PublicConst : AccessModifier::Public) : (varDef->isConst ? AccessModifier::Const : AccessModifier::Normal));
currentContext->def(varDef->name, varTypeInfo, am, val);
@@ -524,7 +524,7 @@ namespace Fig
fnDef->name,
ValueType::Function,
am,
Value(Function(
Object(Function(
fnDef->paras,
TypeInfo(fnDef->retType),
fnDef->body,
@@ -556,7 +556,7 @@ namespace Fig
stDef->name,
ValueType::StructType,
am,
Value(StructType(
Object(StructType(
defContext,
fields)));
return StatementResult::normal();
@@ -573,7 +573,7 @@ namespace Fig
// static constexpr char ConstAssignmentErrorName[] = "ConstAssignmentError";
// throw EvaluatorError<ConstAssignmentErrorName>(FStringView(std::format("Cannot assign to constant variable '{}'", varAssign->varName.toBasicString())), currentAddressInfo);
// }
// Value val = eval(varAssign->valueExpr);
// Object val = eval(varAssign->valueExpr);
// if (currentContext->getTypeInfo(varAssign->varName) != ValueType::Any)
// {
// TypeInfo expectedType = currentContext->getTypeInfo(varAssign->varName);
@@ -589,7 +589,7 @@ namespace Fig
// };
case AstType::IfSt: {
auto ifSt = std::dynamic_pointer_cast<Ast::IfSt>(stmt);
Value condVal = eval(ifSt->condition);
Object condVal = eval(ifSt->condition);
if (condVal.getTypeInfo() != ValueType::Bool)
{
static constexpr char ConditionTypeErrorName[] = "ConditionTypeError";
@@ -602,7 +602,7 @@ namespace Fig
// else
for (const auto &elif : ifSt->elifs)
{
Value elifCondVal = eval(elif->condition);
Object elifCondVal = eval(elif->condition);
if (elifCondVal.getTypeInfo() != ValueType::Bool)
{
static constexpr char ConditionTypeErrorName[] = "ConditionTypeError";
@@ -623,7 +623,7 @@ namespace Fig
auto whileSt = std::dynamic_pointer_cast<Ast::WhileSt>(stmt);
while (true)
{
Value condVal = eval(whileSt->condition);
Object condVal = eval(whileSt->condition);
if (condVal.getTypeInfo() != ValueType::Bool)
{
static constexpr char ConditionTypeErrorName[] = "ConditionTypeError";
@@ -667,7 +667,7 @@ namespace Fig
while (true) // use while loop to simulate for loop, cause we need to check condition type every iteration
{
Value condVal = eval(forSt->condition);
Object condVal = eval(forSt->condition);
if (condVal.getTypeInfo() != ValueType::Bool)
{
static constexpr char ConditionTypeErrorName[] = "ConditionTypeError";

View File

@@ -78,7 +78,7 @@ namespace Fig
return makeAst<Ast::VarDefAst>(isPublic, isConst, name, tiName, exp);
}
Value Parser::__parseValue()
Object Parser::__parseValue()
{
FString _val = currentToken().getValue();
if (currentToken().getType() == TokenType::LiteralNumber)
@@ -95,7 +95,7 @@ namespace Fig
{
throwAddressableError<SyntaxError>(FStringView(u8"Illegal number literal"));
}
return Value(d);
return Object(d);
}
else
{
@@ -109,20 +109,20 @@ namespace Fig
{
throwAddressableError<SyntaxError>(FStringView(u8"Illegal number literal"));
}
return Value(i);
return Object(i);
}
}
else if (currentToken().getType() == TokenType::LiteralString)
{
return Value(_val);
return Object(_val);
}
else if (currentToken().getType() == TokenType::LiteralBool)
{
return Value((_val == u8"true" ? true : false));
return Object((_val == u8"true" ? true : false));
}
else if (currentToken().getType() == TokenType::LiteralNull)
{
return Value::getNullInstance();
return Object::getNullInstance();
}
else
{

View File

@@ -14,7 +14,6 @@ target("Fig")
add_cxxflags("-stdlib=libc++")
add_files("src/*.cpp")
add_files("src/Value/function.cpp")
add_includedirs("include")