mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
fixes
This commit is contained in:
parent
890bd90eba
commit
3fca384446
7 changed files with 510 additions and 23 deletions
|
|
@ -1,40 +1,139 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "symbols_info.hpp"
|
||||
#include "values.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
class ContextManager {
|
||||
public:
|
||||
void CallFunction(const std::vector<VariableInfo>&);
|
||||
void EnterContext();
|
||||
void ExitContext();
|
||||
ContextManager() {
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
utils::IdType AddValue(const T& value, utils::ValueType value_type) {
|
||||
return value_manager_.AddValue(value, value_type);
|
||||
}
|
||||
|
||||
utils::IdType AddAnyValue(value::Value&& value, utils::ValueType value_type) {
|
||||
return value_manager_.AddAnyValue(std::move(value), value_type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GetValue(utils::IdType value_id) {
|
||||
return value_manager_.GetValue<T>(value_id);
|
||||
}
|
||||
|
||||
value::Value* GetAnyValue(utils::IdType value_id) {
|
||||
return value_manager_.GetAnyValue(value_id);
|
||||
}
|
||||
|
||||
utils::ValueType GetValueType(utils::IdType value_id) {
|
||||
return value_manager_.GetValueType(value_id);
|
||||
}
|
||||
|
||||
utils::IdType ToModifiedValue(utils::IdType value_id, utils::ValueType new_value_type) {
|
||||
value::Value value = *GetAnyValue(value_id);
|
||||
return AddAnyValue(std::move(value), new_value_type);
|
||||
}
|
||||
|
||||
value::ValueManager* GetValueManager() {
|
||||
return &value_manager_;
|
||||
}
|
||||
|
||||
void EnterContext() {
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
void ExitContext() {
|
||||
if (contexts_.empty()) {
|
||||
// error
|
||||
}
|
||||
|
||||
contexts_.pop_back();
|
||||
}
|
||||
|
||||
void ExitFromAllContexts() {
|
||||
contexts_.clear();
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
bool DefineVariable(const std::string& name, utils::IdType value_id) {
|
||||
// check in previous contexts ??
|
||||
return contexts_.back().DefineVariable(name, value_id);
|
||||
}
|
||||
|
||||
|
||||
bool RemoveVariable(const std::string& name) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
if (contexts_[i].RemoveVariable(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void EnterVariableContext(const std::string& name, utils::IdType value_id) {
|
||||
// for variable namespaces, for loops
|
||||
contexts_.emplace_back();
|
||||
|
||||
DefineVariable(name, value_id);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
auto maybe_type = contexts_[i].GetVariableInfo(name);
|
||||
if (maybe_type.has_value()) {
|
||||
return maybe_type.value();
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void DefineVariable(const VariableInfo& variable);
|
||||
void ChangeVariableValue(const std::string& name, const Value& new_value);
|
||||
const Value& GetVariableValue(const std::string& name);
|
||||
private:
|
||||
class Context {
|
||||
public:
|
||||
Context(bool hide_previous = false) : hide_previous_(hide_previous) {}
|
||||
Context() = default;
|
||||
|
||||
void DefineVariable(const VariableInfo& variable);
|
||||
void ChangeVaraibleValue(const std::string& name, const Value& neew_value);
|
||||
const Value& GetVariableValue(const std::string& name);
|
||||
bool DefineVariable(const std::string& name, utils::IdType value_id) {
|
||||
if (name == "_") { // placeholder // TODO: ??
|
||||
return true;
|
||||
}
|
||||
|
||||
if (variables_.count(name) > 0) {
|
||||
return false;
|
||||
}
|
||||
variables_[name] = value_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RemoveVariable(const std::string& name) {
|
||||
return variables_.erase(name);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
|
||||
auto variable_iter = variables_.find(name);
|
||||
|
||||
if (variable_iter == variables_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return variable_iter->second;
|
||||
}
|
||||
|
||||
bool IsFirst() { return hide_previous_; }
|
||||
private:
|
||||
bool hide_previous_;
|
||||
std::unordered_map<std::string, VariableInfo> variables_;
|
||||
std::unordered_map<std::string, utils::IdType> variables_;
|
||||
};
|
||||
|
||||
// Context global_context_; // ??
|
||||
std::vector<Context> contexts_;
|
||||
value::ValueManager value_manager_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include "visitor.hpp"
|
||||
#include "global_info.hpp"
|
||||
|
||||
// TODO: class fields and constructors can't be accessed not from associated namespace
|
||||
|
||||
namespace interpreter {
|
||||
|
||||
class TypeCheckVisitor : public Visitor {
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@ public:
|
|||
|
||||
void ExitFromAllContexts() {
|
||||
contexts_.clear();
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||
|
|
|
|||
|
|
@ -111,25 +111,25 @@ struct Value { // DefinedValue ??
|
|||
class ValueManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddType(const T& value, utils::ValueType value_type) {
|
||||
utils::IdType AddValue(const T& value, utils::ValueType value_type) {
|
||||
values_.push_back(std::pair<Value, utils::ValueType> {value, value_type});
|
||||
return values_.size() - 1;
|
||||
}
|
||||
|
||||
utils::IdType AddAnyType(Value&& value, utils::ValueType value_type) {
|
||||
utils::IdType AddAnyValue(Value&& value, utils::ValueType value_type) {
|
||||
values_.push_back(std::pair<Value, utils::ValueType> {std::move(value), value_type});
|
||||
return values_.size() - 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GetType(utils::IdType value_id) {
|
||||
std::optional<T*> GetValue(utils::IdType value_id) {
|
||||
if (!std::holds_alternative<T>(values_.at(value_id).first.value)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return &std::get<T>(values_.at(value_id).first.value);
|
||||
}
|
||||
|
||||
Value* GetAnyType(utils::IdType value_id) {
|
||||
Value* GetAnyValue(utils::IdType value_id) {
|
||||
return &values_.at(value_id).first;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue