-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: import statement first implementation
- Loading branch information
1 parent
c13ada4
commit 1c80503
Showing
13 changed files
with
193 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <pl/core/parser_manager.hpp> | ||
#include <pl/core/preprocessor.hpp> | ||
#include <pl/core/validator.hpp> | ||
#include <pl/core/parser.hpp> | ||
#include <pl/core/lexer.hpp> | ||
|
||
#include <wolv/utils/string.hpp> | ||
|
||
using namespace pl::core; | ||
|
||
CompileResult<std::vector<std::shared_ptr<ast::ASTNode>>> | ||
ParserManager::parse(api::Source *source, const std::string &namespacePrefix) { | ||
using result_t = CompileResult<std::vector<std::shared_ptr<ast::ASTNode>>>; | ||
|
||
if (this->m_astCache.contains(source->source)) { | ||
return result_t::good(this->m_astCache[source->source]); | ||
} | ||
|
||
if(m_onceIncluded.contains(source)) | ||
return result_t::good({}); | ||
|
||
auto parser = Parser(); | ||
|
||
std::vector<std::string> namespaces; | ||
if (!namespacePrefix.empty()) { | ||
namespaces = wolv::util::splitString(namespacePrefix, "::"); | ||
} | ||
|
||
auto preprocessor = Preprocessor(); | ||
|
||
preprocessor.addPragmaHandler("namespace", [&](auto&, const std::string& value) { | ||
if(namespacePrefix.empty()) | ||
namespaces = wolv::util::splitString(value, "::"); | ||
return true; | ||
}); | ||
|
||
auto lexer = Lexer(); | ||
auto validator = Validator(); | ||
|
||
auto [preprocessedCode, preprocessorErrors] = preprocessor.preprocess(this->m_patternLanguage, source); | ||
if (!preprocessorErrors.empty()) { | ||
return result_t::err(preprocessorErrors); | ||
} | ||
|
||
source->content = preprocessedCode.value(); // update source object with preprocessed code | ||
|
||
if(preprocessor.shouldOnlyIncludeOnce()) | ||
m_onceIncluded.insert(source); | ||
|
||
auto [tokens, lexerErrors] = lexer.lex(source); | ||
|
||
if (!lexerErrors.empty()) { | ||
return result_t::err(lexerErrors); | ||
} | ||
|
||
parser.addNamespace(namespaces); | ||
parser.setParserManager(this); | ||
|
||
auto result = parser.parse(source->content, tokens.value()); | ||
if (result.has_errs()) | ||
return result; | ||
|
||
if (!validator.validate(source->content, *result.ok, true, true)) { | ||
return result_t::err({}); | ||
} | ||
|
||
this->m_astCache[source->source] = result.ok.value(); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include "test_pattern.hpp" | ||
|
||
namespace pl::test { | ||
|
||
class TestPatternImport : public TestPattern { | ||
public: | ||
TestPatternImport() : TestPattern("Import") { | ||
} | ||
~TestPatternImport() override = default; | ||
|
||
[[nodiscard]] std::string getSourceCode() const override { | ||
return R"( | ||
import IA as A; | ||
fn main() { | ||
A::a(); | ||
B::b(); | ||
C::c(); | ||
}; | ||
)"; | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters