Skip to content

Commit

Permalink
feat(vm): added Void type
Browse files Browse the repository at this point in the history
  • Loading branch information
PoetaKodu committed Jul 17, 2022
1 parent fb8a54e commit e4d6b7c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 87 deletions.
6 changes: 5 additions & 1 deletion VM/include/RigCVM/TypeSystem/CoreType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CoreType : public IType
public:
enum Kind : uint8_t
{
Void,
Int16, Int32, Int64,
Uint16, Uint32, Uint64,
Float32, Float64,
Expand All @@ -23,6 +24,7 @@ class CoreType : public IType
static constexpr auto Num = static_cast<size_t>(Kind::MAX);

static constexpr auto Sizes = std::to_array({
size_t(0),
sizeof(int16_t), sizeof(int32_t), sizeof(int64_t),
sizeof(uint16_t), sizeof(uint32_t), sizeof(uint64_t),
sizeof(float), sizeof(double),
Expand All @@ -31,6 +33,7 @@ class CoreType : public IType
});

static constexpr auto Names = std::to_array<std::string_view>({
"Void",
"Int16", "Int32", "Int64",
"Uint16", "Uint32", "Uint64",
"Float32", "Float64",
Expand All @@ -44,7 +47,8 @@ class CoreType : public IType
#define HANDLE_TYPE(CppName, EnumValue) if constexpr (std::is_same_v<T, CppName>) return EnumValue;
#define ELSE_HANDLE_TYPE(CppName, EnumValue) else if constexpr (std::is_same_v<T, CppName>) return EnumValue;

HANDLE_TYPE (int16_t, Int16)
HANDLE_TYPE (void, Void)
ELSE_HANDLE_TYPE(int16_t, Int16)
ELSE_HANDLE_TYPE(int32_t, Int32)
ELSE_HANDLE_TYPE(int64_t, Int64)
ELSE_HANDLE_TYPE(uint16_t, Uint16)
Expand Down
13 changes: 12 additions & 1 deletion VM/include/RigCVM/TypeSystem/IType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,16 @@ struct IType : public std::enable_shared_from_this<IType>
};

template <typename T>
auto CreateCoreType(Instance &vm_, Scope& universeScope_, std::string_view name_, size_t size_ = sizeof(T)) -> IType*;
struct SafeCoreTypeSize {
static constexpr auto value = sizeof(T);
};

template <>
struct SafeCoreTypeSize<void> {
static constexpr auto value = 0;
};

template <typename T>
auto CreateCoreType(Instance &vm_, Scope& universeScope_, std::string_view name_, size_t size_ = SafeCoreTypeSize<T>::value) -> IType*;

}
177 changes: 92 additions & 85 deletions VM/src/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,106 +104,112 @@ auto CreateCoreType(Instance &vm_, Scope& universeScope_, std::string_view name_
{
auto t = std::make_shared<CoreType>(CoreType::fromCppType<T>());
universeScope_.addType(t);
Function::Params infixParams;
infixParams[0] = { "lhs", t };
infixParams[1] = { "rhs", t };

Function::Params infixAssignParams;
infixAssignParams[0] = { "lhs", constructTemplateType<RefType>(universeScope_, t) };
infixAssignParams[1] = { "rhs", t };

Function::Params prePostfixParams;
prePostfixParams[0] = { "lhs", constructTemplateType<RefType>(universeScope_, t) };

#define MAKE_INFIX_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
{ \
return builtin##Name##Operator<T>(vm_, args_[0], args_[1]); \
}; \
universeScope_.registerOperator(vm_, Incantation, Operator::Infix, Function(OPERATOR_##Name, infixParams, 2));

#define MAKE_INFIX_ASSIGN_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
if constexpr (!std::is_same_v<T, void>)
{
Function::Params infixParams;
infixParams[0] = { "lhs", t };
infixParams[1] = { "rhs", t };


Function::Params infixAssignParams;
infixAssignParams[0] = { "lhs", constructTemplateType<RefType>(universeScope_, t) };
infixAssignParams[1] = { "rhs", t };

Function::Params prePostfixParams;
prePostfixParams[0] = { "lhs", constructTemplateType<RefType>(universeScope_, t) };

#define MAKE_INFIX_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
{ \
return builtin##Name##Operator<T>(vm_, args_[0], args_[1]); \
}; \
universeScope_.registerOperator(vm_, Incantation, Operator::Infix, Function(OPERATOR_##Name, infixParams, 2));

#define MAKE_INFIX_ASSIGN_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
{ \
return builtin##Name##Operator<T>(vm_, args_[0], args_[1]); \
}; \
{ \
return builtin##Name##Operator<T>(vm_, args_[0], args_[1]); \
}; \
{ \
auto& op = universeScope_.registerOperator(vm_, Incantation, Operator::Infix, Function(OPERATOR_##Name, infixAssignParams, 2)); \
op.returnsRef = true; \
}

#define MAKE_POSTFIX_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
auto& op = universeScope_.registerOperator(vm_, Incantation, Operator::Infix, Function(OPERATOR_##Name, infixAssignParams, 2)); \
op.returnsRef = true; \
}

#define MAKE_POSTFIX_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
{ \
return builtin##Name##Operator<T>(vm_, args_[0]); \
}; \
universeScope_.registerOperator(vm_, Incantation, Operator::Postfix, Function(OPERATOR_##Name, prePostfixParams, 1));

#define MAKE_PREFIX_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
{ \
return builtin##Name##Operator<T>(vm_, args_[0]); \
}; \
{ \
return builtin##Name##Operator<T>(vm_, args_[0]); \
}; \
universeScope_.registerOperator(vm_, Incantation, Operator::Postfix, Function(OPERATOR_##Name, prePostfixParams, 1));
auto& op = universeScope_.registerOperator(vm_, Incantation, Operator::Prefix, Function(OPERATOR_##Name, prePostfixParams, 1)); \
op.returnsRef = true; \
}

#define MAKE_PREFIX_OP(Name, Incantation) \
static auto const& OPERATOR_##Name = [](Instance &vm_, Function::ArgSpan args_) \
{ \
return builtin##Name##Operator<T>(vm_, args_[0]); \
}; \
{ \
auto& op = universeScope_.registerOperator(vm_, Incantation, Operator::Prefix, Function(OPERATOR_##Name, prePostfixParams, 1)); \
op.returnsRef = true; \
if constexpr (!std::is_same_v<T, bool>)
{
// Math
MAKE_INFIX_OP(Add, "+");
MAKE_INFIX_OP(Sub, "-");
MAKE_INFIX_OP(Mult, "*");
MAKE_INFIX_OP(Div, "/");

if constexpr (!std::is_floating_point_v<T>)
{
MAKE_INFIX_OP(Mod, "%");
MAKE_INFIX_ASSIGN_OP(ModAssign, "%=");
}

// Math (assignment)
MAKE_INFIX_ASSIGN_OP(AddAssign, "+=");
MAKE_INFIX_ASSIGN_OP(SubAssign, "-=");
MAKE_INFIX_ASSIGN_OP(MultAssign, "*=");
MAKE_INFIX_ASSIGN_OP(DivAssign, "/=");

// Postfix
MAKE_POSTFIX_OP(PostIncrement, "++");
MAKE_POSTFIX_OP(PostDecrement, "--");

// Prefix
MAKE_PREFIX_OP(PreIncrement, "++");
MAKE_PREFIX_OP(PreDecrement, "--");

// Relational
MAKE_INFIX_OP(LowerThan, "<");
MAKE_INFIX_OP(GreaterThan, ">");
MAKE_INFIX_OP(LowerEqThan, "<=");
MAKE_INFIX_OP(GreaterEqThan, ">=");
}

if constexpr (!std::is_same_v<T, bool>)
{
// Math
MAKE_INFIX_OP(Add, "+");
MAKE_INFIX_OP(Sub, "-");
MAKE_INFIX_OP(Mult, "*");
MAKE_INFIX_OP(Div, "/");

if constexpr (!std::is_floating_point_v<T>)
if constexpr (std::is_same_v<T, bool>)
{
MAKE_INFIX_OP(Mod, "%");
MAKE_INFIX_ASSIGN_OP(ModAssign, "%=");
// Logical
MAKE_INFIX_OP(LogicalAnd, "and");
MAKE_INFIX_OP(LogicalOr, "or");
}

// Math (assignment)
MAKE_INFIX_ASSIGN_OP(AddAssign, "+=");
MAKE_INFIX_ASSIGN_OP(SubAssign, "-=");
MAKE_INFIX_ASSIGN_OP(MultAssign, "*=");
MAKE_INFIX_ASSIGN_OP(DivAssign, "/=");

// Postfix
MAKE_POSTFIX_OP(PostIncrement, "++");
MAKE_POSTFIX_OP(PostDecrement, "--");

// Prefix
MAKE_PREFIX_OP(PreIncrement, "++");
MAKE_PREFIX_OP(PreDecrement, "--");

// Relational
MAKE_INFIX_OP(LowerThan, "<");
MAKE_INFIX_OP(GreaterThan, ">");
MAKE_INFIX_OP(LowerEqThan, "<=");
MAKE_INFIX_OP(GreaterEqThan, ">=");
}
MAKE_INFIX_OP(Equal, "==");
MAKE_INFIX_OP(NotEqual, "!=");

if constexpr (std::is_same_v<T, bool>)
{
// Logical
MAKE_INFIX_OP(LogicalAnd, "and");
MAKE_INFIX_OP(LogicalOr, "or");
}
// Assignment
MAKE_INFIX_ASSIGN_OP(Assign, "=");

// Relational
MAKE_INFIX_OP(Equal, "==");
MAKE_INFIX_OP(NotEqual, "!=");

// Assignment
MAKE_INFIX_ASSIGN_OP(Assign, "=");
#undef MAKE_INFIX_OP
#undef MAKE_INFIX_ASSIGN_OP
#undef MAKE_POSTFIX_OP
#undef MAKE_PREFIX_OP
}

return t.get();

#undef MAKE_INFIX_OP
#undef MAKE_INFIX_ASSIGN_OP
#undef MAKE_POSTFIX_OP
#undef MAKE_PREFIX_OP
}

template <typename T>
Expand Down Expand Up @@ -238,6 +244,7 @@ auto addTypeConversion(Instance &vm_, Scope& universeScope_, std::string_view fr
template auto addTypeConversion<TypeName>(Instance&, Scope&, std::string_view, std::string_view, ConversionFunc&) -> void


LINK_BUILTIN_TYPE(void);
LINK_BUILTIN_TYPE(bool);
LINK_BUILTIN_TYPE(char);
LINK_BUILTIN_TYPE(char16_t);
Expand Down

0 comments on commit e4d6b7c

Please sign in to comment.