mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-29 02:08:46 +00:00
fixes, part of == for TypeProxy done, cotex enchantments, type chack arguments
This commit is contained in:
parent
6dc9c9b5af
commit
f36ff3638b
8 changed files with 195 additions and 121 deletions
|
|
@ -6,7 +6,9 @@
|
|||
namespace type_check {
|
||||
|
||||
// IN PROGRESS: modifiers ??
|
||||
nodes::TypeCheckResult type_check_literal(const nodes::Literal &literal,
|
||||
SourcesManager &sources_manager);
|
||||
nodes::TypeCheckResult
|
||||
type_check_literal(const nodes::Literal &literal,
|
||||
SourcesManager &sources_manager,
|
||||
nodes::MaybeTypeProxy expected_type);
|
||||
|
||||
} // namespace type_check
|
||||
|
|
|
|||
|
|
@ -11,21 +11,22 @@
|
|||
|
||||
namespace type_check {
|
||||
|
||||
// TODO: make context stack o top of RAII
|
||||
class State {
|
||||
public:
|
||||
bool insert_variable(const std::string &name, nodes::TypeProxy type) {
|
||||
if (contexts_.size() == 0) {
|
||||
if (contexts_.empty()) {
|
||||
error_handling::handle_general_error(
|
||||
"Insert variable into contexts_ with zero elements in State");
|
||||
}
|
||||
|
||||
return contexts_.back().insert({name, type}).second;
|
||||
return contexts_.back().variables.insert({name, type}).second;
|
||||
}
|
||||
|
||||
std::optional<nodes::TypeProxy> find_variable(const std::string &name) {
|
||||
nodes::MaybeTypeProxy find_variable(const std::string &name) {
|
||||
for (ssize_t i = contexts_.size(); i >= 0; --i) {
|
||||
auto iter = contexts_[i].find(name);
|
||||
if (iter != contexts_[i].end()) {
|
||||
auto iter = contexts_[i].variables.find(name);
|
||||
if (iter != contexts_[i].variables.end()) {
|
||||
return iter->second;
|
||||
}
|
||||
}
|
||||
|
|
@ -33,84 +34,136 @@ public:
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
void enter_context() { contexts_.emplace_back(); }
|
||||
void enter_context(const nodes::Node& node) { contexts_.emplace_back(node); }
|
||||
|
||||
void exit_context() {
|
||||
if (contexts_.size() == 0) {
|
||||
// returns brought type, return type is merged with next context or with
|
||||
// brought type in last context
|
||||
nodes::MaybeTypeProxy exit_context() {
|
||||
if (contexts_.empty()) {
|
||||
error_handling::handle_general_error(
|
||||
"Pop from contexts_ with zero elements in State");
|
||||
}
|
||||
|
||||
const auto brought_type = contexts_.back().brought_type;
|
||||
const auto returned_type = contexts_.back().returned_type;
|
||||
contexts_.pop_back();
|
||||
|
||||
if (contexts_.empty()) {
|
||||
// TODO: merge returned and brought types
|
||||
} else {
|
||||
// TODO: merge to previous
|
||||
}
|
||||
|
||||
return brought_type;
|
||||
}
|
||||
|
||||
bool bring_type(nodes::TypeProxy type) {
|
||||
if (contexts_.empty()) {
|
||||
error_handling::handle_general_error(
|
||||
"Set brought type to contexts_ with zero elements in State");
|
||||
}
|
||||
auto &brought_type = contexts_.back().brought_type;
|
||||
if (brought_type.has_value() &&) {
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
brought_type = type;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool return_type(nodes::TypeProxy type) {
|
||||
if (contexts_.empty()) {
|
||||
error_handling::handle_general_error(
|
||||
"Set returned type to contexts_ with zero elements in State");
|
||||
}
|
||||
auto &returned_type = contexts_.back().returned_type;
|
||||
if (returned_type.has_value() &&) {
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
returned_type = type;
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
std::optional<nodes::TypeProxy> brought_type;
|
||||
std::optional<nodes::TypeProxy> returned_type;
|
||||
class Context {
|
||||
public:
|
||||
Context(const nodes::Node& node) : node(node) {}
|
||||
|
||||
public:
|
||||
nodes::MaybeTypeProxy brought_type;
|
||||
nodes::MaybeTypeProxy returned_type;
|
||||
std::unordered_map<std::string, nodes::TypeProxy> variables;
|
||||
const nodes::Node& node;
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<std::unordered_map<std::string, nodes::TypeProxy>> contexts_ = {
|
||||
{}};
|
||||
std::vector<Context> contexts_ = {{}};
|
||||
};
|
||||
|
||||
class Arguments {
|
||||
public:
|
||||
nodes::MaybeTypeProxy expected_type = {};
|
||||
};
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_expression(const nodes::Expression &expression,
|
||||
SourcesManager &sources_manager, State &state);
|
||||
SourcesManager &sources_manager, State &state, const Arguments& arguments = {});
|
||||
|
||||
// --- flow control
|
||||
|
||||
nodes::TypeCheckResult type_check_match(const nodes::Match &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult type_check_condition(const nodes::Condition &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
// --- containers
|
||||
|
||||
nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
// --- modifiers
|
||||
|
||||
nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_name_definition(const nodes::NameDefinition &expression,
|
||||
SourcesManager &sources_manager, State &state);
|
||||
SourcesManager &sources_manager, State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_loop_control(const nodes::LoopControl &expression,
|
||||
SourcesManager &sources_manager, State &state);
|
||||
SourcesManager &sources_manager, State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_modifier_expression(const nodes::ModifierExpression &expression,
|
||||
SourcesManager &sources_manager, State &state);
|
||||
SourcesManager &sources_manager, State &state, const Arguments& arguments = {});
|
||||
|
||||
// --- other
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_name_expression(const nodes::NameExpression &expression,
|
||||
SourcesManager &sources_manager, State &state);
|
||||
SourcesManager &sources_manager, State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_constructor(const nodes::Constructor &expression,
|
||||
SourcesManager &sources_manager, State &state);
|
||||
SourcesManager &sources_manager, State &state, const Arguments& arguments = {});
|
||||
|
||||
nodes::TypeCheckResult type_check_lambda(const nodes::Lambda &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state);
|
||||
State &state, const Arguments& arguments = {});
|
||||
|
||||
} // namespace type_check
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ public:
|
|||
|
||||
const Type *get() const;
|
||||
|
||||
bool operator==(const TypeProxy& other) const;
|
||||
|
||||
private:
|
||||
TypeProxy(TypeStorage &type_storage, size_t id)
|
||||
: type_storage_(&type_storage), id_(id) {}
|
||||
|
|
@ -35,6 +37,8 @@ private:
|
|||
size_t id_;
|
||||
};
|
||||
|
||||
using MaybeTypeProxy = std::optional<TypeProxy>;
|
||||
|
||||
class Type {
|
||||
public:
|
||||
Type(Identifier &&identifier, Modifier modifier = nodes::Modifier::CONST,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue