Skip to content

Commit

Permalink
chore: ParseTreeModuleLoader and safer types
Browse files Browse the repository at this point in the history
  • Loading branch information
jahan-addison committed Dec 14, 2024
1 parent 1b76b82 commit 814bd39
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ repos:
- id: clang-format
args: [--style=file]
- id: cppcheck
# example: https://github.com/ScintillaOrg/lexilla/blob/master/cppcheck.suppress
args: [--enable=all, --language=c++, -I., --suppressions-list=./cppcheck.suppress]
# - id: clang-tidy
# - id: include-what-you-use

6 changes: 3 additions & 3 deletions roxas/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <iostream>
#include <roxas/parse_tree.h>

int main(int argc, char* argv[])
int main(int argc, const char* argv[])
{
if (argc < 4) {
if (argc < 2) {
Expand All @@ -33,8 +33,8 @@ int main(int argc, char* argv[])
try {
// ~/.cache/pypoetry/virtualenvs/xion-I1_4RUhc-py3.11/lib/python3.11/site-packages
auto parse_tree =
argc == 5 ? roxas::ParseTreeLoader(argv[2], argv[3], argv[4])
: roxas::ParseTreeLoader(argv[2], argv[3]);
argc == 5 ? roxas::ParseTreeModuleLoader(argv[2], argv[3], argv[4])
: roxas::ParseTreeModuleLoader(argv[2], argv[3]);
std::cout << parse_tree.get_parse_tree_as_string_from_module(argv[1])
<< std::endl;
} catch (std::runtime_error& e) {
Expand Down
41 changes: 19 additions & 22 deletions roxas/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <filesystem>
#include <fstream>
#include <iostream>
#include <roxas/parse_tree.h>
#include <sstream>
#include <stdexcept>
Expand Down Expand Up @@ -46,7 +45,7 @@ std::string read_source_file(fs::path path)
}

/**
* @brief ParseTreeLoader constructor
* @brief ParseTreeModuleLoader constructor
*
* Constructs object that interfaces with a compiler frontend in python
*
Expand All @@ -55,17 +54,16 @@ std::string read_source_file(fs::path path)
* @param env_path an optional absolute path to a venv directory where
* dependecies are installed
*/
ParseTreeLoader::ParseTreeLoader(const char* module_path,
const char* file_path,
const char* env_path)
ParseTreeModuleLoader::ParseTreeModuleLoader(std::string const& module_path,
std::string const& file_path,
std::string const& env_path)
: module_path_(std::move(module_path))
, file_path_(std::move(file_path))
{
std::ostringstream python_path;
tree_.location = std::string{ file_path };
module_path_ = std::string{ module_path };
python_path << "sys.path.append(\"" << module_path_ << "\")";

python_path << "sys.path.append(\"" << module_path << "\")";

if (env_path != nullptr) {
if (not env_path.empty()) {
python_path << std::endl << "sys.path.append(\"" << env_path << "\")";
}

Expand All @@ -77,23 +75,21 @@ ParseTreeLoader::ParseTreeLoader(const char* module_path,
}

/**
* @brief ParseTreeLoader constructor
* @brief
*
* Constructs object that interfaces with a compiler frontend in python
* Parse a source program and provides the parse tree as a string
*
* @param module_path an absolute path to the frontend python module
* @param file_path an absolute path to the source file to parse
* @param env_path an optional absolute path to a venv directory where
* dependecies are installed
* @param module_name a python frontend compiler for B
* @return std::string parse tree as a readable string
*/
std::string ParseTreeLoader::get_parse_tree_as_string_from_module(
std::string_view module_name)
std::string ParseTreeModuleLoader::get_parse_tree_as_string_from_module(
std::string const& module_name)
{
std::string ret{};
PyObject *pModule, *pDict, *pFunc, *pValue, *pArgs, *vModule;
PyObject *pModule, *pDict, *pFunc, *vModule;

// Load the module object
pModule = PyImport_ImportModule(module_name.data());
pModule = PyImport_ImportModule(module_name.c_str());

if (pModule == NULL) {
throw std::runtime_error("memory error: failed to allocate pModule");
Expand Down Expand Up @@ -126,11 +122,12 @@ std::string ParseTreeLoader::get_parse_tree_as_string_from_module(
}

if (PyCallable_Check(pFunc)) {
PyObject *pValue, *pArgs;
pArgs = PyTuple_New(1);
PyTuple_SetItem(
pArgs,
0,
PyUnicode_FromString(read_source_file(tree_.location).c_str()));
PyUnicode_FromString(read_source_file(file_path_).c_str()));
pValue = PyObject_CallObject(pFunc, pArgs);
if (pValue != NULL) {
ret = std::string{ PyUnicode_AsUTF8(pValue) };
Expand All @@ -153,7 +150,7 @@ std::string ParseTreeLoader::get_parse_tree_as_string_from_module(
* @brief clean up
*
*/
ParseTreeLoader::~ParseTreeLoader()
ParseTreeModuleLoader::~ParseTreeModuleLoader()
{
Py_Finalize();
}
Expand Down
38 changes: 10 additions & 28 deletions roxas/parse_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,23 @@

#pragma once

#include <cstddef>
#include <optional>
#include <roxas/config.h>
#include <string>

namespace roxas {

namespace detail {

/**
* @brief
*
* Parse tree data structure.
*
*/
struct ParseTree
{
std::string source{};
std::string location{};
};

} // namespace detail

/**
* @brief
*
* Parse tree loader via python interface to the python compiler frontend
* Module loader via libpython interface to the compiler frontend module
*
*/
class ParseTreeLoader
class ParseTreeModuleLoader
{
public:
ParseTreeLoader(ParseTreeLoader const&) = delete;
ParseTreeModuleLoader(ParseTreeModuleLoader const&) = delete;
/**
* @brief ParseTreeLoader constructor
* @brief ParseTreeModuleLoader constructor
*
* Constructs object that interfaces with a compiler frontend in python
*
Expand All @@ -59,15 +41,15 @@ class ParseTreeLoader
* @param env_path an optional absolute path to a venv directory where
* dependecies are installed
*/
ParseTreeLoader(const char* module_path,
const char* file_path,
const char* env_path = nullptr);
ParseTreeModuleLoader(std::string const& module_path,
std::string const& file_path,
std::string const& env_path = "");

/**
* @brief clean up
*
*/
~ParseTreeLoader();
~ParseTreeModuleLoader();

public:
/**
Expand All @@ -79,11 +61,11 @@ class ParseTreeLoader
* @return std::string parse tree as a readable string
*/
std::string get_parse_tree_as_string_from_module(
std::string_view module_name);
std::string const& module_name);

private:
std::string module_path_;
detail::ParseTree tree_;
std::string file_path_;
};

} // namespace roxas

0 comments on commit 814bd39

Please sign in to comment.