api improvements, part of expression type checks

This commit is contained in:
ProgramSnail 2024-01-04 14:30:57 +03:00
parent a2abb598ac
commit 512d011f72
4 changed files with 96 additions and 9 deletions

View file

@ -473,6 +473,8 @@ public:
const Type *get_type() const { return constructor_type_.get(); }
TypeProxy get_type_proxy() const { return constructor_type_; }
size_t arguments_size() const { return arguments_.size(); }
Expression *get_argument_value(size_t id) {

View file

@ -86,6 +86,8 @@ public:
void set_modifier(Modifier modifier) { modifier_ = modifier; }
bool is_modifier(Modifier modifier) const { return modifier_ == modifier; }
//
std::optional<std::string *> get_annotation() {
@ -102,6 +104,12 @@ public:
return std::nullopt;
}
bool is_no_annotation() const { return !annotation_.has_value(); }
bool is_annotation(const std::string &annotation) const {
return annotation_.has_value() && annotation_.value() == annotation;
}
void set_annotation(std::string &&annotation) {
annotation_ = std::move(annotation);
}
@ -176,7 +184,7 @@ public:
bool operator>=(const Type &other) const { return !operator<(other); }
// is parameters count check necessary ??
builtin::types::Type to_builtin() {
builtin::types::Type to_builtin() const {
auto builtin_type = builtin::types::to_type(*name_.get());
auto builtin_type_parameters_count =
@ -191,7 +199,9 @@ public:
return builtin_type;
}
bool is_builtin(builtin::types::Type type) { return to_builtin() == type; }
bool is_builtin(builtin::types::Type type) const {
return to_builtin() == type;
}
private:
Identifier name_;

View file

@ -16,16 +16,18 @@ class State {
friend ContextHolder;
public:
bool insert_variable(const std::string &name, nodes::TypeProxy type) {
bool insert_variable(const std::string &name, nodes::TypeProxy type,
nodes::NameDefinition::Modifier modifier) {
if (contexts_.empty()) {
error_handling::handle_general_error(
"Insert variable into contexts_ with zero elements in State");
}
return contexts_.back().variables.insert({name, type}).second;
return contexts_.back().variables.insert({name, {type, modifier}}).second;
}
nodes::MaybeTypeProxy find_variable(const std::string &name) {
std::optional<std::pair<nodes::TypeProxy, nodes::NameDefinition::Modifier>>
find_variable(const std::string &name) {
for (ssize_t i = contexts_.size(); i >= 0; --i) {
auto iter = contexts_[i].variables.find(name);
if (iter != contexts_[i].variables.end()) {
@ -70,6 +72,7 @@ private:
contexts_.emplace_back(node, error_log);
}
// TODO: argument for property is returned type should be merged
// returns brought type, return type is merged with next context or with
// brought type in last context
nodes::MaybeTypeProxy exit_context() {
@ -125,7 +128,9 @@ public:
public:
nodes::MaybeTypeProxy brought_type;
nodes::MaybeTypeProxy returned_type;
std::unordered_map<std::string, nodes::TypeProxy> variables;
std::unordered_map<std::string, std::pair<nodes::TypeProxy,
nodes::NameDefinition::Modifier>>
variables;
private:
const nodes::Node &node;