mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-14 10:58:44 +00:00
print & read builtin functions, fixes. execution of programs partially works
This commit is contained in:
parent
27f643dfbc
commit
2556efcaba
16 changed files with 537 additions and 215 deletions
|
|
@ -7,6 +7,7 @@
|
|||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "error_handling.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
|
@ -15,7 +16,7 @@ template<typename Value, typename ValueManager>
|
|||
class ContextManager {
|
||||
public:
|
||||
ContextManager() {
|
||||
contexts_.emplace_back();
|
||||
contexts_.emplace_back(true); // no difference ??
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -57,67 +58,103 @@ public:
|
|||
return &value_manager_;
|
||||
}
|
||||
|
||||
void EnterContext() {
|
||||
contexts_.emplace_back();
|
||||
void EnterContext(bool is_hiding_previous = false) {
|
||||
contexts_.emplace_back(is_hiding_previous);
|
||||
}
|
||||
|
||||
void ExitContext() {
|
||||
if (contexts_.empty()) {
|
||||
// error
|
||||
error_handling::HandleInternalError("contexts_ is empty",
|
||||
"ContextManager.ExitContext",
|
||||
std::nullopt);
|
||||
}
|
||||
|
||||
contexts_.pop_back();
|
||||
}
|
||||
|
||||
void ExitFromAllContexts() {
|
||||
contexts_.clear();
|
||||
contexts_.emplace_back();
|
||||
void ChangeHidingOfCurrentContextTo(bool is_hiding_previous) {
|
||||
contexts_.back().ChangeHidingTo(is_hiding_previous);
|
||||
}
|
||||
|
||||
bool DefineVariable(const std::string& name, utils::IdType value_id) {
|
||||
// check in previous contexts ??
|
||||
return contexts_.back().DefineVariable(name, value_id);
|
||||
}
|
||||
|
||||
bool DefineLocalType(const std::string& name, utils::IdType type_id) {
|
||||
if (GetLocalType(name).has_value()) {
|
||||
return false;
|
||||
}
|
||||
return contexts_.back().DefineLocalType(name, type_id);
|
||||
}
|
||||
|
||||
bool AssignVariable(const std::string& name, utils::IdType value_id) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
if (contexts_[i].AssignVariable(name, value_id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (contexts_[i].IsHidingPrevious()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AssignLocalType(const std::string& name, utils::IdType type_id) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
if (contexts_[i].AssignLocalType(name, type_id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (contexts_[i].IsHidingPrevious()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (contexts_[i].IsHidingPrevious()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void EnterVariableContext(const std::string& name, utils::IdType value_id) {
|
||||
// for variable namespaces, for loops
|
||||
contexts_.emplace_back();
|
||||
void EnterVariableContext(const std::string& name,
|
||||
utils::IdType value_id,
|
||||
bool is_hiding_previous = false) {
|
||||
EnterContext(is_hiding_previous);
|
||||
|
||||
DefineVariable(name, value_id);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
|
||||
std::optional<utils::IdType> FindVariable(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();
|
||||
auto maybe_variable = contexts_[i].FindVariable(name);
|
||||
if (maybe_variable.has_value()) {
|
||||
return maybe_variable.value();
|
||||
}
|
||||
|
||||
if (contexts_[i].IsHidingPrevious()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetLocalType(const std::string& name) {
|
||||
std::optional<utils::IdType> FindLocalType(const std::string& name) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
auto maybe_type = contexts_[i].GetLocalType(name);
|
||||
auto maybe_type = contexts_[i].FindLocalType(name);
|
||||
if (maybe_type.has_value()) {
|
||||
return maybe_type.value();
|
||||
}
|
||||
|
||||
if (contexts_[i].IsHidingPrevious()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
|
@ -125,10 +162,10 @@ public:
|
|||
private:
|
||||
class Context {
|
||||
public:
|
||||
Context() = default;
|
||||
explicit Context(bool is_hiding_previous) : is_hiding_previous_(is_hiding_previous) {}
|
||||
|
||||
bool DefineVariable(const std::string& name, utils::IdType value_id) {
|
||||
if (name == "_") { // placeholder // TODO: ??
|
||||
if (name == "_") { // placeholder
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -147,11 +184,33 @@ private:
|
|||
return true;
|
||||
}
|
||||
|
||||
bool AssignVariable(const std::string& name, utils::IdType value_id) {
|
||||
if (name == "_") { // placeholder
|
||||
return true;
|
||||
}
|
||||
|
||||
auto variable_iter = variables_.find(name);
|
||||
if (variable_iter == variables_.end()) {
|
||||
return false;
|
||||
}
|
||||
variable_iter->second = value_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssignLocalType(const std::string& name, utils::IdType type_id) {
|
||||
auto local_type_iter = local_types_.find(name);
|
||||
if (local_type_iter == local_types_.end()) {
|
||||
return false;
|
||||
}
|
||||
local_type_iter->second = type_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RemoveVariable(const std::string& name) {
|
||||
return variables_.erase(name);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
|
||||
std::optional<utils::IdType> FindVariable(const std::string& name) {
|
||||
auto variable_iter = variables_.find(name);
|
||||
|
||||
if (variable_iter == variables_.end()) {
|
||||
|
|
@ -161,7 +220,7 @@ private:
|
|||
return variable_iter->second;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetLocalType(const std::string& name) {
|
||||
std::optional<utils::IdType> FindLocalType(const std::string& name) {
|
||||
auto local_abstract_type_iter = local_types_.find(name);
|
||||
|
||||
if (local_abstract_type_iter == local_types_.end()) {
|
||||
|
|
@ -171,9 +230,19 @@ private:
|
|||
return local_abstract_type_iter->second;
|
||||
}
|
||||
|
||||
bool IsHidingPrevious() {
|
||||
return is_hiding_previous_;
|
||||
}
|
||||
|
||||
void ChangeHidingTo(bool is_hiding_previous) {
|
||||
is_hiding_previous_ = is_hiding_previous;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, utils::IdType> variables_;
|
||||
std::unordered_map<std::string, utils::IdType> local_types_;
|
||||
|
||||
bool is_hiding_previous_ = false;
|
||||
};
|
||||
|
||||
std::vector<Context> contexts_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue