part of execute_visitor, minor fixes, function & operator fixes

This commit is contained in:
ProgramSnail 2023-05-09 14:55:04 +03:00
parent d31979166e
commit 78de51f6f2
6 changed files with 345 additions and 247 deletions

View file

@ -42,6 +42,14 @@ inline void HandleTypecheckError(const std::string& message,
exit(1);
}
inline void HandleRuntimeError(const std::string& message,
const interpreter::tokens::BaseNode& node) { // TODO: place in code
std::cout << "Runtime Error: " << message << " at ";
PrintPosition(std::cout, node.start_position, node.end_position);
std::cout << ".\n";
exit(1);
}
// ...
} // namespace error_handling

View file

@ -7,6 +7,8 @@
#include "global_info.hpp"
#include "interpreter_tree.hpp"
#include "type_info_contexts.hpp"
#include "utils.hpp"
#include "values.hpp"
#include "visitor.hpp"
#include "error_handling.hpp"
@ -54,9 +56,9 @@ private:
// Definition parts
void Visit(FunctionDefinition* node) override;
void Visit(TypeDefinition* node) override;
void Visit(AnyAnnotatedType* node) override;
// // void Visit(FunctionDefinition* node) override;
// // void Visit(TypeDefinition* node) override;
// // void Visit(AnyAnnotatedType* node) override;
// Flow control -----------------
@ -99,7 +101,7 @@ private:
// Name
void Visit(PartitionName* node) override;
// // void Visit(PartitionName* node) override;
void Visit(NameExpression* node) override;
void Visit(TupleName* node) override;
void Visit(VariantName* node) override;
@ -126,9 +128,9 @@ private:
// Identifiers, constants, etc. -----------------
void Visit(ExtendedName* node) override;
// // void Visit(ExtendedName* node) override;
void Visit(std::string* node) override; // std::string
// // void Visit(std::string* node) override; // std::string
void Visit(FloatNumberLiteral* node) override;
void Visit(NumberLiteral* node) override;
@ -137,10 +139,34 @@ private:
void Visit(UnitLiteral* node) override;
void Visit(BoolLiteral* node) override;
bool HandleCondition(Expression& condition, const BaseNode& base_node);
template<typename T>
T* ExtractValue(utils::IdType value, const BaseNode& base_node) {
std::optional<T*> maybe_value_info = context_manager_.GetValue<T>(value);
if (!maybe_value_info.has_value()) {
error_handling::HandleRuntimeError("Value has value class that is different from exprected one", base_node);
}
return maybe_value_info.value();
}
template<typename T>
T* ExtractInternalValue(utils::IdType value, const BaseNode& base_node) {
info::value::InternalValue* value_info = ExtractValue<info::value::InternalValue>(value, base_node);
std::optional<T*> maybe_internal_value_info = value_info->GetValue<T>();
if (!maybe_internal_value_info.has_value()) {
error_handling::HandleRuntimeError("Value has internal value class that is different from exprected one", base_node);
}
return maybe_internal_value_info.value();
}
private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& type_info_context_manager_;
info::ContextManager& context_manager_;
utils::IdType current_value_;
std::optional<LoopControlExpression> active_loop_control_expression_;
std::optional<utils::IdType> return_value_; // TODO: work outside block ??
};
} // namespace interpreter

View file

@ -499,6 +499,8 @@ struct BinaryOperatorExpression {
OperatorIdentifier operator_name;
SubExpression left_expression;
SubExpression right_expression;
utils::IdType function_id_;
};
struct UnaryOperatorExpression {
@ -506,6 +508,8 @@ struct UnaryOperatorExpression {
OperatorIdentifier operator_name;
Expression expression;
utils::IdType function_id_;
};
struct ReferenceExpression {
@ -532,6 +536,8 @@ struct FunctionCallExpression {
ExtendedName name;
std::vector<std::unique_ptr<TypeExpression>> parameters;
std::vector<SubExpressionToken> arguments;
utils::IdType function_id_;
};
struct TupleExpression {

View file

@ -18,13 +18,20 @@ struct Unit {};
struct InternalValue {
public:
InternalValue() = default;
InternalValue(std::variant<double,
long long,
std::string,
char,
bool,
Unit>&& value) : value(std::move(value)) {}
explicit InternalValue(std::variant<double,
long long,
std::string,
char,
bool,
Unit>&& value) : value(std::move(value)) {}
template<typename T>
std::optional<T*> GetValue() {
if (!std::holds_alternative<T>(value)) {
return std::nullopt;
}
return std::get<T>(value);
}
public:
std::variant<double,
long long,
@ -37,20 +44,19 @@ public:
struct TupleValue {
public:
TupleValue() = default;
TupleValue(std::unordered_map<std::string, utils::IdType>&& fields) : fields(fields) {}
explicit TupleValue(std::vector<std::pair<std::optional<std::string>, utils::IdType>>&& fields) : fields(std::move(fields)) {}
public:
std::unordered_map<std::string, utils::IdType> fields;
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
};
struct VariantValue {
public:
VariantValue() = default;
VariantValue(size_t constructor, TupleValue value)
: constructor(constructor), value(value) {}
VariantValue(TupleValue value) // TODO: add type & constructor??
: value(value) {}
public:
size_t constructor;
TupleValue value;
};
@ -58,7 +64,7 @@ struct ReferenceToValue {
public:
ReferenceToValue() = default;
ReferenceToValue(const std::vector<utils::ReferenceModifier>& references,
utils::IdType value)
utils::IdType value)
: references(references), value(value) {}
public:
@ -70,7 +76,7 @@ struct FunctionValue {
public:
FunctionValue() = default;
FunctionValue(std::variant<interpreter::tokens::FunctionDeclaration*,
interpreter::tokens::LambdaFunction*> function)
interpreter::tokens::LambdaFunction*> function)
: function(function) {}
public:
@ -82,17 +88,19 @@ public:
struct ArrayValue {
public:
ArrayValue() = default;
ArrayValue(const std::vector<utils::IdType>& elements)
: elements(elements) {}
explicit ArrayValue(const std::vector<utils::IdType>& elements,
bool is_constant_size)
: elements(elements), is_constant_size(is_constant_size) {}
public:
std::vector<utils::IdType> elements;
bool is_constant_size = false;
};
struct OptionalValue {
public:
OptionalValue() = default;
OptionalValue(utils::IdType value) : value(value) {}
explicit OptionalValue(utils::IdType value) : value(value) {}
public:
std::optional<utils::IdType> value;