Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
Program::resolveEnvironment now integrate used modules in parent envi…
Browse files Browse the repository at this point in the history
…ronment
  • Loading branch information
Gashmob committed Sep 5, 2023
1 parent 8d78582 commit ce1be78
Show file tree
Hide file tree
Showing 54 changed files with 652 additions and 214 deletions.
2 changes: 1 addition & 1 deletion src/lib/FilCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace filc {
}

try {
program->resolveEnvironment(collector);
program->resolveEnvironment(collector, _modules);
} catch (std::exception &e) {
collector->addError(new filc::message::BasicError(filc::message::ERROR,
"Error when resolve module " + module.first +
Expand Down
15 changes: 14 additions & 1 deletion src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "MessageCollector.h"
#include <string>
#include <vector>
#include <map>

namespace filc::ast {
class Program {
Expand All @@ -49,7 +50,11 @@ namespace filc::ast {

auto setFilename(const std::string &filename) -> void;

auto resolveEnvironment(filc::message::MessageCollector *collector) -> void;
auto resolveEnvironment(filc::message::MessageCollector *collector,
const std::map<const std::string, Program *> &modules) -> void;

[[nodiscard]] auto getPublicEnvironment(const filc::environment::Environment *parent) const
-> filc::environment::Environment *;

private:
std::string _module;
Expand Down Expand Up @@ -85,6 +90,8 @@ namespace filc::ast {
filc::message::MessageCollector *collector,
AbstractType *preferred_type = nullptr) -> void;

virtual auto addNameToEnvironment(filc::environment::Environment *environment) const -> void;

private:
bool _exported{false};
filc::utils::Position *_position{nullptr};
Expand All @@ -108,6 +115,8 @@ namespace filc::ast {
filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void override;

auto addNameToEnvironment(filc::environment::Environment *environment) const -> void override;

private:
std::string _name;
};
Expand Down Expand Up @@ -187,6 +196,8 @@ namespace filc::ast {
auto resolveType(filc::environment::Environment *environment, filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void override;

auto addNameToEnvironment(filc::environment::Environment *environment) const -> void override;

private:
bool _constant;
Identifier *_identifier;
Expand Down Expand Up @@ -529,6 +540,8 @@ namespace filc::ast {
filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void override;

auto addNameToEnvironment(filc::environment::Environment *environment) const -> void override;

private:
Identifier *_name;
};
Expand Down
7 changes: 7 additions & 0 deletions src/lib/ast/AbstractExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
#include "AST.h"
#include "Error.h"
#include "tools.h"

namespace filc::ast {
AbstractExpression::~AbstractExpression() {
Expand Down Expand Up @@ -62,4 +63,10 @@ namespace filc::ast {
getPosition())
);
}

auto AbstractExpression::addNameToEnvironment(filc::environment::Environment *environment) const -> void {
auto name = filc::utils::joinString(filc::utils::splitString(environment->getModule(), '.'), "_")
+ "_" + std::to_string(getPosition()->getLine());
environment->addName(name, getExpressionType());
}
}
2 changes: 1 addition & 1 deletion src/lib/ast/ForI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace filc::ast {
auto ForI::resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void {
_body_environment = new filc::environment::Environment(environment);
_body_environment = new filc::environment::Environment("", environment);

if (_declaration != nullptr) {
_declaration->resolveType(_body_environment, collector, nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ast/ForIter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace filc::ast {
auto ForIter::resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void {
_body_environment = new filc::environment::Environment(environment);
_body_environment = new filc::environment::Environment("", environment);

_array->resolveType(environment, collector);
auto *array_type = _array->getExpressionType();
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ast/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ namespace filc::ast {

environment->addName(_name->getName(), getExpressionType());
}

auto Function::addNameToEnvironment(filc::environment::Environment *environment) const -> void {
environment->addName(_name->getName(), getExpressionType());
}
}
4 changes: 4 additions & 0 deletions src/lib/ast/Identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ namespace filc::ast {
setExpressionType(name->getType());
}
}

auto Identifier::addNameToEnvironment(filc::environment::Environment *environment) const -> void {
environment->addName(_name, getExpressionType());
}
}
2 changes: 1 addition & 1 deletion src/lib/ast/Lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace filc::ast {
auto Lambda::resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void {
_body_environment = new filc::environment::Environment(environment);
_body_environment = new filc::environment::Environment("", environment);

std::vector<AbstractType *> parameters_types;
for (const auto &parameter: _parameters) {
Expand Down
28 changes: 25 additions & 3 deletions src/lib/ast/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,35 @@ namespace filc::ast {
}
}

auto Program::resolveEnvironment(filc::message::MessageCollector *collector) -> void {
_environment = new filc::environment::Environment(filc::environment::Environment::getGlobalEnvironment());
auto Program::resolveEnvironment(filc::message::MessageCollector *collector,
const std::map<const std::string, Program *> &modules) -> void {
const auto *parent = filc::environment::Environment::getGlobalEnvironment();
for (const auto &item: _imports) {
auto module = modules.find(item);
if (module == modules.end()) {
throw std::logic_error("Module " + item + " not found");
}

// TODO : add imports env to _environment
parent = module->second->getPublicEnvironment(parent);
}

_environment = new filc::environment::Environment(_module, parent);

for (const auto &expression: _expressions) {
expression->resolveType(_environment, collector);
}
}

auto Program::getPublicEnvironment(const filc::environment::Environment *parent) const
-> filc::environment::Environment * {
auto *environment = new filc::environment::Environment(_module, parent);

for (const auto &expression: _expressions) {
if (expression->isExported()) {
expression->addNameToEnvironment(environment);
}
}

return environment;
}
}
4 changes: 4 additions & 0 deletions src/lib/ast/VariableDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ namespace filc::ast {
environment->addName(_identifier->getName(), _type);
setExpressionType(_type);
}

auto VariableDeclaration::addNameToEnvironment(filc::environment::Environment *environment) const -> void {
environment->addName(_identifier->getName(), getExpressionType());
}
}
Loading

0 comments on commit ce1be78

Please sign in to comment.