Skip to content

Commit

Permalink
feat(vm): null keyword and Null type.
Browse files Browse the repository at this point in the history
refactor(vm): change `typeOf` to `dumpTypeOf`
  • Loading branch information
PoetaKodu committed Oct 16, 2022
1 parent 80bab64 commit a51ba6a
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 184 deletions.
9 changes: 3 additions & 6 deletions VM/include/RigCVM/Builtin/Functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ auto printCharacters(Instance &vm_, Function::ArgSpan args_) -> OptValue;
/// printCharacters("Hello, {}!\n", text);
auto print(Instance &vm_, Function::ArgSpan args_) -> OptValue;

/// @brief Returns the type of the given value, as a Array<Char, ?>.
///
/// @note This function will be completely reworked in the future because of
/// varying return type (could not be properly compiled).
/// @brief Prints the type of the given value.
///
/// @example
///
/// var text = "Hello, world!";
/// print("{}\n", typeOf(text)); // prints "Array<Char, 13>"
auto typeOf(Instance &vm_, Function::ArgSpan args_) -> OptValue;
/// dumpTypeOf(text); // prints "Array<Char, 13>"
auto dumpTypeOf(Instance &vm_, Function::ArgSpan args_) -> OptValue;

/// @brief Reads an int from standard input.
/// @returns Int32 - the read value.
Expand Down
1 change: 1 addition & 0 deletions VM/include/RigCVM/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct BuiltinTypes
};

Accessor Void;
Accessor Null;
Accessor Int16;
Accessor Int32;
Accessor Int64;
Expand Down
12 changes: 9 additions & 3 deletions VM/include/RigCVM/TypeSystem/CoreType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

namespace rigc::vm
{

struct NullType {
void* value = nullptr;
};

class CoreType : public IType
{
public:
enum Kind : uint8_t
{
Void,
Void, Null,
Int16, Int32, Int64,
Uint16, Uint32, Uint64,
Float32, Float64,
Expand All @@ -24,7 +29,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),
size_t(0), sizeof(NullType),
sizeof(int16_t), sizeof(int32_t), sizeof(int64_t),
sizeof(uint16_t), sizeof(uint32_t), sizeof(uint64_t),
sizeof(float), sizeof(double),
Expand All @@ -33,7 +38,7 @@ class CoreType : public IType
});

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

HANDLE_TYPE (void, Void)
ELSE_HANDLE_TYPE(NullType, Null)
ELSE_HANDLE_TYPE(int16_t, Int16)
ELSE_HANDLE_TYPE(int32_t, Int32)
ELSE_HANDLE_TYPE(int64_t, Int64)
Expand Down
8 changes: 3 additions & 5 deletions VM/include/RigCVM/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ struct FrameBasedValue : ValueBase

using OptValue = std::optional<Value>;

using ConversionFunc = OptValue(Instance &, Value const&);
using ConversionFunc = Func<OptValue(Instance &, Value const&)>;

template <typename T>
auto addTypeConversion(Instance &vm_, Scope& universeScope_, DeclType const& from_, DeclType const& to_, ConversionFunc& func_) -> void;
template <typename T>
auto addTypeConversion(Instance &vm_, Scope& universeScope_, StringView from_, StringView to_, ConversionFunc& func_) -> void;
auto addTypeConversion(Instance &vm_, Scope& universeScope_, DeclType const& from_, DeclType const& to_, ConversionFunc func_) -> void;
auto addTypeConversion(Instance &vm_, Scope& universeScope_, StringView from_, StringView to_, ConversionFunc func_) -> void;
}
6 changes: 3 additions & 3 deletions VM/src/Builtin/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ auto print(Instance &vm_, Function::ArgSpan args_) -> OptValue
}

////////////////////////////////////////
auto typeOf(Instance &vm_, Function::ArgSpan args_) -> OptValue
auto dumpTypeOf(Instance &vm_, Function::ArgSpan args_) -> OptValue
{
auto name = args_[0].safeRemoveRef().type->name();
auto t = vm_.arrayOf(*vm_.builtinTypes.Char.raw, name.size());
fmt::print("{}", name);

return vm_.allocateOnStack( t, name.data(), name.size() );
return std::nullopt;
}

////////////////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions VM/src/Executors/All.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ auto evaluateSymbol(Instance &vm_, rigc::ParserNode const& expr_) -> OptValue
{
// Either "PossiblyTemplatedSymbol" or "PossiblyTemplatedSymbolNoDisamb"
auto& name = *findElem<rigc::Name>(expr_, false);

if (name.string_view() == "null")
{
// TODO: protect against `null<TemplateArgs...>`
return vm_.allocateOnStack<void const*>(vm_.builtinTypes.Null.shared(), nullptr);
}

auto opt = vm_.findVariableByName(name.string_view());

// if (!opt) {
Expand Down
4 changes: 2 additions & 2 deletions VM/src/Executors/ExpressionExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ auto ExpressionExecutor::evalInfixOperator(StringView op_, Action& lhs_, Action&
.withHelp("Check the spelling of the rhs.")
.withLine(vm.lastEvaluatedLine);

auto const rhsType = vm.findType(rhs->string_view());
auto const rhsType = vm.evaluateType(*rhs);
if(!rhsType)
throw RigCError("Rhs of the conversion operator should be a type.")
.withHelp("Check the spelling of the rhs and if the type is in scope.")
.withLine(vm.lastEvaluatedLine);

return vm.tryConvert(lhs, rhsType->shared_from_this());
return vm.tryConvert(lhs, rhsType);
}
else
{
Expand Down
18 changes: 9 additions & 9 deletions VM/src/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ auto setupUniverseScope(Instance &vm_, Scope& scope_) -> void
SetupCoreType<CppName>(vm_, scope_, *vm_.builtinTypes.RigCName.raw);

MAKE_BUILTIN_TYPE(void, Void);
MAKE_BUILTIN_TYPE(NullType, Null);
MAKE_BUILTIN_TYPE(bool, Bool);
MAKE_BUILTIN_TYPE(char, Char);
MAKE_BUILTIN_TYPE(char16_t, Char16);
Expand All @@ -39,18 +40,17 @@ auto setupUniverseScope(Instance &vm_, Scope& scope_) -> void
scope_.addType(std::make_unique<MethodType>());

SETUP_BUILTIN_TYPE(bool, Bool);
SETUP_BUILTIN_TYPE(void, Void);
SETUP_BUILTIN_TYPE(char, Char);
SETUP_BUILTIN_TYPE(char16_t, Char16);
SETUP_BUILTIN_TYPE(char32_t, Char32);
SETUP_BUILTIN_TYPE(int16_t, Int16);
SETUP_BUILTIN_TYPE(int32_t, Int32);
SETUP_BUILTIN_TYPE(int64_t, Int64);
SETUP_BUILTIN_TYPE(int16_t, Int16);
SETUP_BUILTIN_TYPE(int32_t, Int32);
SETUP_BUILTIN_TYPE(int64_t, Int64);
SETUP_BUILTIN_TYPE(uint16_t, Uint16);
SETUP_BUILTIN_TYPE(uint32_t, Uint32);
SETUP_BUILTIN_TYPE(uint64_t, Uint64);
SETUP_BUILTIN_TYPE(float, Float32);
SETUP_BUILTIN_TYPE(double, Float64);
SETUP_BUILTIN_TYPE(float, Float32);
SETUP_BUILTIN_TYPE(double, Float64);


auto addrOfChar = constructTemplateType<AddrType>(scope_, vm_.builtinTypes.Char.shared());
Expand Down Expand Up @@ -96,11 +96,11 @@ auto setupUniverseScope(Instance &vm_, Scope& scope_) -> void
}
// "typeof" builtin function
{
auto func = Function{ &builtin::typeOf, {}, 0 };
auto func = Function{ &builtin::dumpTypeOf, {}, 0 };
func.variadic = true;
func.raw().name = "builtin::typeOf";
func.raw().name = "builtin::dumpTypeOf";

scope_.registerFunction(vm_, "typeOf", std::move(func));
scope_.registerFunction(vm_, "dumpTypeOf", std::move(func));
}
// "readInt" builtin function
{
Expand Down
Loading

0 comments on commit a51ba6a

Please sign in to comment.