Skip to content

Commit

Permalink
feat(ast): initial ast and node definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
jahan-addison committed Dec 18, 2024
1 parent af835f9 commit 5ed004a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cppcheck.suppress
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ missingIncludeSystem
unusedStructMember
unmatchedSuppression

// Ignore debug methods
unusedFunction

// libpython uses this a lot, unfortunately
cstyleCast

Expand Down
22 changes: 22 additions & 0 deletions roxas/ast.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <roxas/ast.h>

namespace roxas {

namespace ast {

node::node(std::string const& type)
: type_(std::move(type))
, literal_({ literal::UNKNOWN, "unknown" })
, children_({})
{
}

void node::print()
{
std::cout << "node type: " << type_;
}

} // namespace ast

} // namespace roxas
61 changes: 61 additions & 0 deletions roxas/ast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <optional>
#include <string>
#include <tuple>
#include <vector>

namespace roxas {

namespace ast {

class node;
struct definition;

} // namespace ast

using AST = std::vector<ast::definition>;

AST parse_tree_to_ast(std::string const& parse_tree);

namespace ast {

enum class literal : int
{
NUMBER,
CONSTANT,
STRING,
UNKNOWN
};

using literal_datatype = std::tuple<literal, std::string>;

struct definition
{
enum class type : int
{
FUNCTION,
VECTOR
};

type type{ type::FUNCTION };
std::vector<node> children{};
};

class node
{
public:
node(node const&) = delete;
node& operator=(node const&) = delete;

explicit node(std::string const& type);
void print();

private:
std::string type_;
std::optional<literal_datatype> literal_;
std::optional<std::vector<node>> children_;
};

} // namespace ast
} // namespace roxas
1 change: 1 addition & 0 deletions roxas/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstdlib>
#include <iostream>
#include <roxas/ast.h>
#include <roxas/parse_tree.h>

int main(int argc, const char* argv[])
Expand Down

0 comments on commit 5ed004a

Please sign in to comment.