es_builtin for type, type utils state fixes

This commit is contained in:
ProgramSnail 2024-01-03 21:55:24 +03:00
parent f03f77191f
commit ffa9c47107
3 changed files with 63 additions and 27 deletions

View file

@ -26,7 +26,7 @@ public:
const Type *get() const;
bool operator==(const TypeProxy& other) const;
bool operator==(const TypeProxy &other) const;
private:
TypeProxy(TypeStorage &type_storage, size_t id)
@ -191,6 +191,8 @@ public:
return builtin_type;
}
bool is_builtin(builtin::types::Type type) { return to_builtin() == type; }
private:
Identifier name_;
std::vector<TypeProxy> parameters_;

View file

@ -42,8 +42,8 @@ public:
"Set brought type to contexts_ with zero elements in State");
}
auto &brought_type = contexts_.back().brought_type;
if (brought_type.has_value() &&) {
// TODO
if (brought_type.has_value() &&
type != brought_type.value()) { // check inheritance, etc ??
return false;
}
brought_type = type;
@ -56,8 +56,8 @@ public:
"Set returned type to contexts_ with zero elements in State");
}
auto &returned_type = contexts_.back().returned_type;
if (returned_type.has_value() &&) {
// TODO
if (returned_type.has_value() &&
type != returned_type.value()) { // check inheritance, etc ??
return false;
}
returned_type = type;
@ -65,7 +65,10 @@ public:
}
private:
void enter_context(const nodes::Node &node) { contexts_.emplace_back(node); }
void enter_context(const nodes::Node &node,
error_handling::ErrorLog &error_log) {
contexts_.emplace_back(node, error_log);
}
// returns brought type, return type is merged with next context or with
// brought type in last context
@ -75,14 +78,33 @@ private:
"Pop from contexts_ with zero elements in State");
}
const auto brought_type = contexts_.back().brought_type;
const auto returned_type = contexts_.back().returned_type;
auto context = std::move(contexts_.back());
contexts_.pop_back();
if (contexts_.empty()) {
// TODO: merge returned and brought types
} else {
// TODO: merge to previous
auto &brought_type = context.brought_type;
const auto &returned_type = context.returned_type;
if (returned_type.has_value()) {
if (contexts_.empty()) {
if (brought_type.has_value()) {
if (returned_type.value() != brought_type.value()) {
context.log_typecheck_error(
"Different returned and brought type in last context");
}
} else {
brought_type = returned_type.value();
}
} else {
auto &previous_returned_type = contexts_.back().returned_type;
if (previous_returned_type.has_value()) {
if (returned_type.value() != previous_returned_type.value()) {
context.log_typecheck_error(
"Different returned type in this context and previous one");
}
} else {
previous_returned_type = returned_type.value();
}
}
}
return brought_type;
@ -91,16 +113,25 @@ private:
public:
class Context {
public:
Context(const nodes::Node &node) : node(node) {}
Context(const nodes::Node &node, error_handling::ErrorLog &error_log)
: node(node), error_log(error_log) {}
void log_typecheck_error(
const std::string &message = "Context typecheck error") {
error_log.add_error(error_handling::ErrorLog::ErrorMessage(
node, message, error_handling::ErrorType::TYPE_CHECK));
}
public:
nodes::MaybeTypeProxy brought_type;
nodes::MaybeTypeProxy returned_type;
std::unordered_map<std::string, nodes::TypeProxy> variables;
private:
const nodes::Node &node;
error_handling::ErrorLog &error_log;
};
private:
std::vector<Context> contexts_ = {{}};
};
@ -111,8 +142,10 @@ public:
class ContextHolder {
public:
ContextHolder(State &state, const nodes::Node &node) : state(state) {
state.enter_context(node);
ContextHolder(State &state, const nodes::Node &node,
error_handling::ErrorLog &error_log)
: state(state) {
state.enter_context(node, error_log);
}
ContextHolder(const ContextHolder &) = delete;