mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-25 13:07:13 +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
23
include/builtin_functions.hpp
Normal file
23
include/builtin_functions.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
// for clangd
|
||||
|
||||
namespace info::builtin {
|
||||
|
||||
template<typename T>
|
||||
inline void Print(const T& value) { // only for strings ??
|
||||
std::cout << "\x1b[1;32mOutput:\x1b[0m " << value << '\n';
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T Read() {
|
||||
T value;
|
||||
std::cout << "\x1b[1;32mInput:\x1b[0m ";
|
||||
std::cin >> value;
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
// for clangd
|
||||
#include "types.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
// TODO
|
||||
|
||||
namespace info {
|
||||
|
||||
struct BuiltinFunction {
|
||||
public:
|
||||
BuiltinFunction(const std::vector<utils::IdType>& argument_types, utils::IdType return_type)
|
||||
: argument_types(argument_types), return_type(return_type) {}
|
||||
|
||||
static std::optional<BuiltinFunction> FindMethod(const std::string& name, info::type::TypeManager& type_manager) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
static std::optional<BuiltinFunction> FindStaticMethod(const std::string& name, info::type::TypeManager& type_manager) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<utils::IdType> argument_types;
|
||||
utils::IdType return_type;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -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_;
|
||||
|
|
|
|||
|
|
@ -34,8 +34,15 @@ inline void HandleGeneralError(const std::string& message) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
inline void HandleInternalError(const std::string& message, const std::string& place) {
|
||||
std::cerr << "\x1b[1;31mInternal Error:\x1b[0m " << message << " at " << place << ".\n";
|
||||
inline void HandleInternalError(const std::string& message,
|
||||
const std::string& place,
|
||||
std::optional<const interpreter::tokens::BaseNode*> node) {
|
||||
std::cerr << "\x1b[1;31mInternal Error:\x1b[0m " << message << " at " << place;
|
||||
if (node.has_value()) {
|
||||
std::cerr << ", at ";
|
||||
PrintPosition(std::cerr, node.value()->start_position, node.value()->end_position);
|
||||
}
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,8 +24,10 @@ public:
|
|||
type_context_manager_(type_context_manager),
|
||||
context_manager_(context_manager) {}
|
||||
|
||||
void VisitSourceFile(SourceFile*) override {
|
||||
error_handling::HandleInternalError("VisitSourceFile unavailible", "ExecuteVisitor.VisitSourceFile");
|
||||
void VisitSourceFile(SourceFile* node) override {
|
||||
error_handling::HandleInternalError("VisitSourceFile unavailible",
|
||||
"ExecuteVisitor.VisitSourceFile",
|
||||
&node->base);
|
||||
};
|
||||
|
||||
void ExecutePartition(interpreter::tokens::PartitionStatement* partition) {
|
||||
|
|
@ -157,6 +159,13 @@ private:
|
|||
}
|
||||
return maybe_internal_value_info.value();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
bool HandleBuiltinFunctionCall(FunctionCallExpression* node);
|
||||
|
||||
bool HandleBuiltinTypeclassFunctionCall(FunctionCallExpression* node);
|
||||
|
||||
private:
|
||||
info::GlobalInfo& global_info_;
|
||||
info::TypeclassGraph& typeclass_graph_;
|
||||
|
|
|
|||
|
|
@ -523,6 +523,9 @@ struct FunctionCallExpression {
|
|||
// only one from two is present
|
||||
std::optional<utils::IdType> function_id_;
|
||||
std::optional<utils::IdType> graph_id_; // for type or typeclass
|
||||
|
||||
std::optional<std::string> abstract_type_name_; // for typeclasses
|
||||
|
||||
bool is_method_of_first_argument_ = false;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -162,6 +162,13 @@ private:
|
|||
FunctionCallExpression* node,
|
||||
info::definition::AnyType* defined_type,
|
||||
bool is_method);
|
||||
|
||||
//
|
||||
|
||||
bool HandleBuiltinFunctionCall(FunctionCallExpression* node);
|
||||
|
||||
bool HandleBuiltinTypeclassFunctionCall(FunctionCallExpression* node);
|
||||
|
||||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::GlobalInfo& global_info_;
|
||||
|
|
|
|||
|
|
@ -44,6 +44,10 @@ public:
|
|||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
const std::string& GetName() {
|
||||
return name_;
|
||||
}
|
||||
|
||||
bool HasTypeclass(utils::IdType graph_id) {
|
||||
return requirement_graph_ids_.count(graph_id) != 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue