Skip to content

Commit

Permalink
clean up cst nodes and base struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Ze7111 committed Jul 5, 2024
1 parent 0e9e88f commit 1f494ef
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 242 deletions.
17 changes: 11 additions & 6 deletions source/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

#include <chrono>
#include <memory>
#include <vector>

#include "cli/include/cli.hh"
#include "controllers/include/file_system.hh"
#include "core/utils/hx_print"
#include "parser/cst/include/cst.hh"
#include "parser/cst/include/nodes.hh"
#include "parser/preprocessor/include/preprocessor.hh"
#include "lexer/include/lexer.hh"
Expand All @@ -41,23 +43,26 @@ int main(int argc, char **argv) {
Preprocessor(tokens, "main").parse();

// end the timer and calculate the duration
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;

// preprocessor::import_tree->print_tree(preprocessor::import_tree->get_root());

// print the preprocessed tokens
// print_tokens(tokens);

auto node =
std::make_unique<cst::Parentheses<cst::StringLiteral>>(std::make_shared<TokenList>(tokens));
std::make_shared<cst::Parentheses<cst::StringLiteral>>(std::make_shared<TokenList>(tokens));

auto tmp = node->parse();

print(node->to_string());
cst::CSTNodeList<> list;

list.push_back(node);

auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;

print(node->to_json());
print(tokens.to_json());
/// PRINT THE NODES AND THE NODES WITH
///

// Print the time taken in nanoseconds and milliseconds
print("time taken: ", diff.count() * 1e+9, " ns");
Expand Down
229 changes: 16 additions & 213 deletions source/parser/cst/include/cst.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,197 +26,6 @@
#include "token/include/token.hh"
#include "token/include/token_list.hh"

/*
something like: 2 * (3 + 4) / 5
would break down to:
BinaryOperation(
left: BinaryOperation(
left: Number(2),
op: '*',
right: BinaryOperation(
left: Number(3),
op: '+',
right: Number(4)
)
),
op: '/',
right: Number(5)
)
or something like: let a: i32 = some_function(5, 6);
would break down to:
VariableDeclaration(
name: "a",
type: "i32",
value: FunctionCall(
name: "some_function",
arguments: [
Number(5),
Number(6)
]
)
)
or something like: "Hello, world!" if true else "Goodbye, world!"
would break down to:
TernaryOperation(
condition: Boolean(true),
if_true: String("Hello, world!"),
if_false: String("Goodbye, world!")
)
or something like: print("Hello, world!")
would break down to:
FunctionCall(
name: "print",
arguments: [
String("Hello, world!")
]
)
or something like:
```
fn this_adds_two_numbers(a: i32, b: i32) -> i32 {
return a + b;
}
let result: i32 = this_adds_two_numbers(5, 6);
```
would break down to:
FunctionDeclaration(
name: "this_adds_two_numbers",
arguments: [
Argument(name: "a", type: "i32"),
Argument(name: "b", type: "i32")
],
return_type: "i32",
body: [
Return(
value: BinaryOperation(
left: Variable("a"),
op: '+',
right: Variable("b")
)
)
]
),
VariableDeclaration(
name: "result",
type: "i32",
value: FunctionCall(
name: "this_adds_two_numbers",
arguments: [
Number(5),
Number(6)
]
)
)
root node for
```
let a: i32 = 5;
let b: i32 = 6;
print("Hello, world!");
```
is as so:
Program(
body: [
VariableDeclaration(
name: "a",
type: "i32",
value: Number(5)
),
VariableDeclaration(
name: "b",
type: "i32",
value: Number(6)
),
FunctionCall(
name: "print",
arguments: [
String("Hello, world!")
]
)
],
file: "example.hlx",
comments: [
...
]
)
each node would also contain location information (line, column, etc.) for error reporting and
comments
*/

/*
the following parsed and translated:
a: i16; // i16 a();
a: i16 = 5; // i16 a = 5;
a: i16? = null; // std::optional<i16> a = std::nullopt;
a: ptr<i16>; // i16* a = std::make_unique<i16>();
b: i16 = ref<a>; // i16 b = &a;
c: i16 = ref<ref<a>>; // i16 c = &&a;
ref<a> parsed as:
Generic(
name: "ref",
arguments: [
Identifier("a")
]
)
fn add<T: Addable>(a: T, b: T) -> T {
return a + b;
}
add(5, 6) parsed as:
FunctionCall(
name: "add",
arguments: [
Number(5),
Number(6)
]
)
fn add<T: Addable>(a: T, b: T) -> T parsed as:
FunctionDeclaration(
name: "add",
arguments: [
Variable(name: "a", type: "T"),
Variable(name: "b", type: "T")
],
return_type: "T",
generics: [
Generic(
name: "add",
arguments: [
Variable(
name: "T",
type: "Addable"
)
]
)
],
body: [
Return(
value: BinaryOperation(
left: Variable("a"),
op: '+',
right: Variable("b")
)
)
]
)
*/

namespace parser::cst {
inline string get_indent(u16 depth) noexcept { return string(static_cast<u16>(depth * 4), ' '); };

Expand All @@ -236,45 +45,39 @@ struct CSTBase;
template <>
struct CSTBase<void> {
// virtual std::expected<std::span<Token>,AstError> parse(std::span<Token> tokens) = 0;
virtual ~CSTBase() = default;
CSTBase() = default;
CSTBase(CSTBase &&) = default;
CSTBase(const CSTBase &) = default;
CSTBase &operator=(CSTBase &&) = default;
CSTBase() = default;
CSTBase(CSTBase &&) = default;
CSTBase(const CSTBase &) = default;
CSTBase &operator=(CSTBase &&) = default;
CSTBase &operator=(const CSTBase &) = delete;
~CSTBase() = default;

[[nodiscard]] virtual ParseResult parse() = 0;
[[nodiscard]] virtual std::string to_string(u32 depth = 0) const = 0;
[[nodiscard]] virtual std::string to_json(u32 depth = 0) const = 0;
};

template <typename T>
struct CSTBase {
virtual ~CSTBase() = default;
CSTBase() = default;
struct CSTBase : public CSTBase<void> {
explicit CSTBase(TokenListRef parse_tokens);
CSTBase(CSTBase &&) = default;
CSTBase(const CSTBase &) = default;
CSTBase &operator=(CSTBase &&) = default;
CSTBase() = default;
CSTBase(CSTBase &&) = default;
CSTBase(const CSTBase &) = default;
CSTBase &operator=(CSTBase &&) = default;
CSTBase &operator=(const CSTBase &) = delete;

[[nodiscard]] virtual ParseResult parse() = 0;
[[nodiscard]] virtual std::string to_string(u32 depth = 0) const = 0;
~CSTBase() = default;
};

template <typename T>
concept CSTNode = std::derived_from<T, CSTBase<T>>;

template <typename T = void>
using CSTNodePtr = std::unique_ptr<CSTBase<T>>;
concept CSTNode = std::derived_from<T, CSTBase<T>>;

template <typename T = void>
using CSTNodeList = std::vector<CSTNodePtr<T>>;
using CSTNodePtr = std::shared_ptr<CSTBase<T>>;

template <typename T = void>
using CSTNodeRef = std::reference_wrapper<CSTBase<T>>;
using CSTNodeList = std::vector<CSTNodePtr<T>>;

template <typename T = void>
using CSTSlice = const std::reference_wrapper<CSTNodeList<T>>;
using CSTSlice = const std::reference_wrapper<CSTNodeList<T>>;

} // namespace parser::cst
#endif // __CST_HH__
Loading

0 comments on commit 1f494ef

Please sign in to comment.