diff --git a/include/Ast/FunctionCall.hpp b/include/Ast/FunctionCall.hpp index 24b03d9..15ce8bf 100644 --- a/include/Ast/FunctionCall.hpp +++ b/include/Ast/FunctionCall.hpp @@ -14,7 +14,7 @@ namespace Fig::Ast struct FunctionCallArgs final { - std::vector argv; + std::vector argv; size_t getLength() const { return argv.size(); } }; diff --git a/include/Ast/ValueExpr.hpp b/include/Ast/ValueExpr.hpp index 0866a18..4cb8899 100644 --- a/include/Ast/ValueExpr.hpp +++ b/include/Ast/ValueExpr.hpp @@ -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); diff --git a/include/Value/Type.hpp b/include/Value/Type.hpp index 78e02b2..943782e 100644 --- a/include/Value/Type.hpp +++ b/include/Value/Type.hpp @@ -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 \ No newline at end of file diff --git a/include/Value/function.hpp b/include/Value/function.hpp index 9218edf..7a5f88e 100644 --- a/include/Value/function.hpp +++ b/include/Value/function.hpp @@ -1,32 +1,35 @@ #pragma once -#include #include #include #include +#include +#include +#include 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 &)> builtin; + std::function &)> builtin; int builtinParamCount = -1; std::shared_ptr 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 &)> fn, int argc); + Function(std::function &)> 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 - { - public: - Function(const FunctionStruct &x) : - __ValueWrapper(ValueType::Function) - { - data = std::make_unique(x); - } - Function(Ast::FunctionParameters paras, TypeInfo ret, Ast::BlockStatement body, ContextPtr closureContext) : - __ValueWrapper(ValueType::Function) - { - data = std::make_unique( - std::move(paras), std::move(ret), std::move(body), std::move(closureContext)); - } - Function(std::function &)> 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 diff --git a/include/Value/structInstance.hpp b/include/Value/structInstance.hpp index cdcc432..fab4baa 100644 --- a/include/Value/structInstance.hpp +++ b/include/Value/structInstance.hpp @@ -1,50 +1,32 @@ #pragma once #include - #include 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 - { - public: - StructInstance(const StructInstanceT &x) : - __ValueWrapper(ValueType::StructInstance) - { - data = std::make_unique(x); - } + // ===== Constructors ===== StructInstance(size_t _parentId, ContextPtr _localContext) : - __ValueWrapper(ValueType::StructInstance) - { - data = std::make_unique(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 \ No newline at end of file +} // namespace Fig diff --git a/include/Value/structType.hpp b/include/Value/structType.hpp index 5d961a8..1776857 100644 --- a/include/Value/structType.hpp +++ b/include/Value/structType.hpp @@ -7,56 +7,57 @@ #include #include - #include +#include +#include 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 fields; - StructT(ContextPtr _defContext, std::vector 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 _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 - { - public: - StructType(const StructT &x) : - __ValueWrapper(ValueType::StructType) - { - data = std::make_unique(x); - } - StructType(ContextPtr _defContext, std::vector fieldsMap) : - __ValueWrapper(ValueType::StructType) - { - data = std::make_unique(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 \ No newline at end of file +} // namespace Fig diff --git a/include/builtins.hpp b/include/builtins.hpp index 0226526..e92ac04 100644 --- a/include/builtins.hpp +++ b/include/builtins.hpp @@ -13,13 +13,13 @@ namespace Fig { namespace Builtins { - const std::unordered_map builtinValues = { - {u8"null", Value::getNullInstance()}, - {u8"true", Value(true)}, - {u8"false", Value(false)}, + const std::unordered_map builtinValues = { + {u8"null", Object::getNullInstance()}, + {u8"true", Object(true)}, + {u8"false", Object(false)}, }; - using BuiltinFunction = std::function &)>; + using BuiltinFunction = std::function &)>; const std::unordered_map builtinFunctionArgCounts = { {u8"__fstdout_print", -1}, // variadic @@ -35,91 +35,91 @@ namespace Fig }; const std::unordered_map builtinFunctions{ - {u8"__fstdout_print", [](const std::vector &args) -> Value { + {u8"__fstdout_print", [](const std::vector &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 &args) -> Value { + {u8"__fstdout_println", [](const std::vector &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 &args) -> Value { + {u8"__fstdin_read", [](const std::vector &args) -> Object { std::string input; std::cin >> input; - return Value(FString::fromBasicString(input)); + return Object(FString::fromBasicString(input)); }}, - {u8"__fstdin_readln", [](const std::vector &args) -> Value { + {u8"__fstdin_readln", [](const std::vector &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 &args) -> Value { - return Value(args[0].getTypeInfo().toString()); + {u8"__fvalue_type", [](const std::vector &args) -> Object { + return Object(args[0].getTypeInfo().toString()); }}, - {u8"__fvalue_int_parse", [](const std::vector &args) -> Value { + {u8"__fvalue_int_parse", [](const std::vector &args) -> Object { FString str = args[0].as().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 &args) -> Value { - Value val = args[0]; + {u8"__fvalue_int_from", [](const std::vector &args) -> Object { + Object val = args[0]; if (val.is()) { - return Value(Int(static_cast(val.as().getValue()))); + return Object(Int(static_cast(val.as().getValue()))); } else if (val.is()) { - return Value(Int(val.as().getValue() ? 1 : 0)); + return Object(Int(val.as().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 &args) -> Value { + {u8"__fvalue_double_parse", [](const std::vector &args) -> Object { FString str = args[0].as().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 &args) -> Value { - Value val = args[0]; + {u8"__fvalue_double_from", [](const std::vector &args) -> Object { + Object val = args[0]; if (val.is()) { - return Value(Double(static_cast(val.as().getValue()))); + return Object(Double(static_cast(val.as().getValue()))); } else if (val.is()) { - return Value(Double(val.as().getValue() ? 1.0 : 0.0)); + return Object(Double(val.as().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 &args) -> Value { - Value val = args[0]; - return Value(val.toString()); + {u8"__fvalue_string_from", [](const std::vector &args) -> Object { + Object val = args[0]; + return Object(val.toString()); }}, }; diff --git a/include/context.hpp b/include/context.hpp index c9db521..a8b104c 100644 --- a/include/context.hpp +++ b/include/context.hpp @@ -15,10 +15,10 @@ namespace Fig private: FString scopeName; std::unordered_map varTypes; - std::unordered_map variables; + std::unordered_map variables; std::unordered_map ams; - std::unordered_map functions; + std::unordered_map functions; std::unordered_map functionNames; std::unordered_map 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 types, std::unordered_map vars, std::unordered_map _ams) : + Context(const FString &name, std::unordered_map types, std::unordered_map vars, std::unordered_map _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 get(const FString &name) + std::optional get(const FString &name) { auto it = variables.find(name); if (it != variables.end()) @@ -73,7 +73,7 @@ namespace Fig ContextPtr createCopyWithPublicVariables() { std::unordered_map _varTypes; - std::unordered_map _variables; + std::unordered_map _variables; std::unordered_map _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().getValue(); + auto &fn = value.as(); functions[fn.id] = fn; functionNames[fn.id] = name; } if (ti == ValueType::StructType) { - auto &st = value.as().getValue(); + auto &st = value.as(); structTypeNames[st.id] = name; } } - std::optional getFunction(std::size_t id) + std::optional getFunction(std::size_t id) { auto it = functions.find(id); if (it != functions.end()) diff --git a/include/evaluator.hpp b/include/evaluator.hpp index c07ff5a..6d18857 100644 --- a/include/evaluator.hpp +++ b/include/evaluator.hpp @@ -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 getCurrentContext() { return currentContext; } std::shared_ptr 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""); + Object evalFunctionCall(const Function &, const Ast::FunctionArguments &, FString fnName = u8""); - Value eval(Ast::Expression); + Object eval(Ast::Expression); void run(); void printStackTrace() const; }; diff --git a/include/parser.hpp b/include/parser.hpp index c52f519..a6d5fe0 100644 --- a/include/parser.hpp +++ b/include/parser.hpp @@ -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 diff --git a/include/value.hpp b/include/value.hpp index 52adc12..656d355 100644 --- a/include/value.hpp +++ b/include/value.hpp @@ -18,42 +18,52 @@ namespace Fig { return std::floor(d) == d; } + inline bool isNumberExceededIntLimit(ValueType::DoubleClass d) { static constexpr ValueType::DoubleClass intMaxAsDouble = static_cast(std::numeric_limits::max()); static constexpr ValueType::DoubleClass intMinAsDouble = static_cast(std::numeric_limits::min()); return d > intMaxAsDouble || d < intMinAsDouble; } - class Value + + class Object { public: - using VariantType = std::variant; + 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, n) {} - Value(const Int &i) : + Object(const Int &i) : data(std::in_place_type, i) {} - Value(const Double &d) : - data(std::in_place_type, d) + Object(const Double &d) { ValueType::IntClass casted = static_cast(d.getValue()); if (casted == d.getValue()) - { data.emplace(casted); - } + else + data.emplace(d); } - Value(const String &s) : + Object(const String &s) : data(std::in_place_type, s) {} - Value(const Bool &b) : + Object(const Bool &b) : data(std::in_place_type, b) {} - Value(const Function &f) : + Object(const Function &f) : data(std::in_place_type, f) {} - Value(const StructType &s) : + Object(const StructType &s) : data(std::in_place_type, s) {} - Value(const StructInstance &s) : + Object(const StructInstance &s) : data(std::in_place_type, s) {} template || std::is_same_v || std::is_same_v - || std::is_same_v - || std::is_same_v>> - Value(const T &val) + || std::is_same_v + || std::is_same_v + || std::is_same_v>> + Object(const T &val) { - // 不可以用 data = 的形式 - // __ValueWrapper 构造、拷贝有限制 if constexpr (std::is_same_v) data.emplace(val); else if constexpr (std::is_same_v) { ValueType::IntClass casted = static_cast(val); if (casted == val) - { data.emplace(casted); - } else - { data.emplace(val); - } } else if constexpr (std::is_same_v) data.emplace(val); else if constexpr (std::is_same_v) data.emplace(val); - else if constexpr (std::is_same_v) + else if constexpr (std::is_same_v) data.emplace(val); - else if constexpr (std::is_same_v) + else if constexpr (std::is_same_v) data.emplace(val); - else if constexpr (std::is_same_v) + else if constexpr (std::is_same_v) data.emplace(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(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; + if constexpr (std::is_same_v) + return ValueType::Null; + else if constexpr (std::is_same_v) + return ValueType::Int; + else if constexpr (std::is_same_v) + return ValueType::Double; + else if constexpr (std::is_same_v) + return ValueType::String; + else if constexpr (std::is_same_v) + return ValueType::Bool; + else if constexpr (std::is_same_v) + return ValueType::Function; + else if constexpr (std::is_same_v) + return ValueType::StructType; + else if constexpr (std::is_same_v) + return ValueType::StructInstance; + else + return ValueType::Any; + }, + data); } bool isNull() const { return is(); } @@ -168,263 +189,257 @@ namespace Fig if (is()) return as().getValue(); if (is()) return as().getValue() ? FString(u8"true") : FString(u8"false"); if (is()) - { return FString(std::format("", - as().getValue().id, - static_cast(as().data.get()))); - } + as().id, + static_cast(&as()))); if (is()) - { return FString(std::format("", - as().getValue().id, - static_cast(as().data.get()))); - } + as().id, + static_cast(&as()))); if (is()) - { - return FString(std::format("().getValue().parentId, - static_cast(as().data.get()))); - } + return FString(std::format("", + as().parentId, + static_cast(&as()))); return FString(u8""); } 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(); + bool rhsIsInt = rhs.is(); ValueType::DoubleClass result = lhs.getNumericValue() + rhs.getNumericValue(); - if (isDoubleInteger(result) and !isNumberExceededIntLimit(result)) - { - return Value(static_cast(result)); - } - return Value(ValueType::DoubleClass(result)); + if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result)) + return Object(static_cast(result)); + return Object(ValueType::DoubleClass(result)); } if (lhs.is() && rhs.is()) - return Value(ValueType::StringClass(lhs.as().getValue() + rhs.as().getValue())); - + return Object(ValueType::StringClass(lhs.as().getValue() + rhs.as().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(); + bool rhsIsInt = rhs.is(); ValueType::DoubleClass result = lhs.getNumericValue() - rhs.getNumericValue(); - if (isDoubleInteger(result) and !isNumberExceededIntLimit(result)) - { - return Value(static_cast(result)); - } - return Value(ValueType::DoubleClass(result)); + if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result)) + return Object(static_cast(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(); + bool rhsIsInt = rhs.is(); ValueType::DoubleClass result = lhs.getNumericValue() * rhs.getNumericValue(); - if (isDoubleInteger(result) and !isNumberExceededIntLimit(result)) - { - return Value(static_cast(result)); - } - return Value(ValueType::DoubleClass(result)); + if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result)) + return Object(static_cast(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(result)); - } - return Value(ValueType::DoubleClass(result)); + bool lhsIsInt = lhs.is(); + bool rhsIsInt = rhs.is(); + if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result)) + return Object(static_cast(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(result)); - } - return Value(ValueType::DoubleClass(result)); + bool lhsIsInt = lhs.is(); + bool rhsIsInt = rhs.is(); + if (lhsIsInt && rhsIsInt && !isNumberExceededIntLimit(result)) + return Object(static_cast(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() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Logical AND requires bool", "&&", lhs, rhs))); - return Value(lhs.as().getValue() && rhs.as().getValue()); + return Object(lhs.as().getValue() && rhs.as().getValue()); } - friend Value operator||(const Value &lhs, const Value &rhs) + friend Object operator||(const Object &lhs, const Object &rhs) { if (!lhs.is() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Logical OR requires bool", "||", lhs, rhs))); - return Value(lhs.as().getValue() || rhs.as().getValue()); + return Object(lhs.as().getValue() || rhs.as().getValue()); } - friend Value operator!(const Value &v) + friend Object operator!(const Object &v) { if (!v.is()) - throw ValueError(FStringView(std::format("Logical NOT requires bool: '{}'", - std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data)))); - return Value(!v.as().getValue()); + throw ValueError(FStringView(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString()))); + return Object(!v.as().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()) - return Value(-v.as().getValue()); + return Object(-v.as().getValue()); if (v.is()) - return Value(-v.as().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()) - throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'", - std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data)))); - return Value(~v.as().getValue()); + return Object(-v.as().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()) + throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString()))); + return Object(~v.as().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() && rhs.is()) return lhs.as().getValue() < rhs.as().getValue(); + if (lhs.is() && rhs.is()) + return lhs.as().getValue() < rhs.as().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() && rhs.is()) return lhs.as().getValue() > rhs.as().getValue(); + if (lhs.is() && rhs.is()) + return lhs.as().getValue() > rhs.as().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() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Bitwise AND requires int", "&", lhs, rhs))); - return Value(lhs.as().getValue() & rhs.as().getValue()); + return Object(lhs.as().getValue() & rhs.as().getValue()); } - friend Value bit_or(const Value &lhs, const Value &rhs) + friend Object bit_or(const Object &lhs, const Object &rhs) { if (!lhs.is() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Bitwise OR requires int", "|", lhs, rhs))); - return Value(lhs.as().getValue() | rhs.as().getValue()); + return Object(lhs.as().getValue() | rhs.as().getValue()); } - friend Value bit_xor(const Value &lhs, const Value &rhs) + friend Object bit_xor(const Object &lhs, const Object &rhs) { if (!lhs.is() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Bitwise XOR requires int", "^", lhs, rhs))); - return Value(lhs.as().getValue() ^ rhs.as().getValue()); + return Object(lhs.as().getValue() ^ rhs.as().getValue()); } - friend Value bit_not(const Value &v) + friend Object bit_not(const Object &v) { if (!v.is()) - throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'", - std::visit([](auto &&val) { return val.ti.name.toBasicString(); }, v.data)))); - return Value(~v.as().getValue()); + throw ValueError(FStringView(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString()))); + return Object(~v.as().getValue()); } - friend Value shift_left(const Value &lhs, const Value &rhs) + friend Object shift_left(const Object &lhs, const Object &rhs) { if (!lhs.is() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Shift left requires int", "<<", lhs, rhs))); - return Value(lhs.as().getValue() << rhs.as().getValue()); + return Object(lhs.as().getValue() << rhs.as().getValue()); } - friend Value shift_right(const Value &lhs, const Value &rhs) + friend Object shift_right(const Object &lhs, const Object &rhs) { if (!lhs.is() || !rhs.is()) throw ValueError(FStringView(makeTypeErrorMessage("Shift right requires int", ">>", lhs, rhs))); - return Value(lhs.as().getValue() >> rhs.as().getValue()); + return Object(lhs.as().getValue() >> rhs.as().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(); + bool expIsInt = exp.is(); ValueType::DoubleClass result = std::pow(base.getNumericValue(), exp.getNumericValue()); - if (isDoubleInteger(result) and !isNumberExceededIntLimit(result)) - { - return Value(static_cast(result)); - } - return Value(ValueType::DoubleClass(result)); + if (baseIsInt && expIsInt && isDoubleInteger(result) && !isNumberExceededIntLimit(result)) + return Object(static_cast(result)); + return Object(ValueType::DoubleClass(result)); } throw ValueError(FStringView(makeTypeErrorMessage("Unsupported operation", "**", base, exp))); } }; - using Any = Value; + using Any = Object; } // namespace Fig diff --git a/src/Value/function.cpp b/src/Value/function.cpp deleted file mode 100644 index f01b747..0000000 --- a/src/Value/function.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -namespace Fig -{ - FunctionStruct::FunctionStruct(std::function &)> fn, - int argc) : - id(nextId()), - isBuiltin(true), - builtin(std::move(fn)), - builtinParamCount(argc) {} - - Function::Function(std::function &)> fn, - int argc) : - __ValueWrapper(ValueType::Function) - { - data = std::make_unique(std::move(fn), argc); - } -}; // namespace Fig \ No newline at end of file diff --git a/src/evaluator.cpp b/src/evaluator.cpp index 2b2f818..6f5ea29 100644 --- a/src/evaluator.cpp +++ b/src/evaluator.cpp @@ -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()) { 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().getValue(); + const StructInstance &st = lhs.as(); Ast::VarExpr varExp; if (!(varExp = std::dynamic_pointer_cast(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(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(exp); - Value calleeVal = eval(fnCall->callee); + Object calleeVal = eval(fnCall->callee); if (!calleeVal.is()) { @@ -309,13 +309,13 @@ namespace Fig static constexpr char StructNotFoundErrorName[] = "StructNotFoundError"; throw EvaluatorError(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()) { static constexpr char NotAStructTypeErrorName[] = "NotAStructTypeError"; throw EvaluatorError(FStringView(std::format("'{}' is not a structure type", initExpr->structName.toBasicString())), initExpr->getAAI()); } - const StructT &structT = structTypeVal.as().getValue(); + const StructType &structT = structTypeVal.as(); ContextPtr defContext = structT.defContext; // definition context // check init args @@ -334,7 +334,7 @@ namespace Fig throw EvaluatorError(FStringView(std::format("Structure '{}' expects {} to {} fields, but {} were provided", initExpr->structName.toBasicString(), minArgs, maxArgs, initExpr->args.size())), initExpr->getAAI()); } - std::vector> evaluatedArgs; + std::vector> 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(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(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(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(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(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"; diff --git a/src/parser.cpp b/src/parser.cpp index 742dd1a..2a02120 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -78,7 +78,7 @@ namespace Fig return makeAst(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(FStringView(u8"Illegal number literal")); } - return Value(d); + return Object(d); } else { @@ -109,20 +109,20 @@ namespace Fig { throwAddressableError(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 { diff --git a/xmake.lua b/xmake.lua index 307c88f..9a41ae1 100644 --- a/xmake.lua +++ b/xmake.lua @@ -14,7 +14,6 @@ target("Fig") add_cxxflags("-stdlib=libc++") add_files("src/*.cpp") - add_files("src/Value/function.cpp") add_includedirs("include")