Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ add_library(korka_lib
include/korka/compiler/parser.hpp
include/korka/shared.hpp
include/korka/compiler/ast_walker.hpp
include/korka/compiler/error.hpp
include/korka/shared/error.hpp
include/korka/compiler/lex_token.hpp
include/korka/utils/const_format.hpp
include/korka/compiler/compiler.hpp
include/korka/utils/overloaded.hpp
include/korka/shared/types.hpp
include/korka/shared/flat_map.hpp
)

target_include_directories(korka_lib
Expand All @@ -57,20 +60,20 @@ add_executable(pxkorka main.cpp)
target_link_libraries(pxkorka PRIVATE korka_lib)

# --- TESTS ---
if (ENABLE_TESTS)
enable_testing()

add_executable(pxkorka_tests
test/lexer.cpp
test/bytecode_builder.cpp
test/parser.cpp
)

target_link_libraries(pxkorka_tests
PRIVATE
korka_lib
Catch2WithMain
)

catch_discover_tests(pxkorka_tests)
endif ()
#if (ENABLE_TESTS)
# enable_testing()
#
# add_executable(pxkorka_tests
# test/lexer.cpp
# test/bytecode_builder.cpp
# test/parser.cpp
# )
#
# target_link_libraries(pxkorka_tests
# PRIVATE
# korka_lib
# Catch2WithMain
# )
#
# catch_discover_tests(pxkorka_tests)
#endif ()
52 changes: 25 additions & 27 deletions include/korka/compiler/ast_walker.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#pragma once
#include "parser.hpp"
#include "korka/utils/overloaded.hpp"
#include <format>
#include <span>

namespace korka {
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

struct ast_walker {
const std::span<const parser::node> &pool;
parser::index_t index;
const std::span<const nodes::node> &pool;
nodes::index_t index;
int indent = 0;
};
}
Expand All @@ -21,7 +19,7 @@ struct std::formatter<korka::ast_walker> {
auto format(const korka::ast_walker& w, std::format_context& ctx) const {
auto out = ctx.out();

if (w.index == korka::parser::empty_node) {
if (w.index == korka::nodes::empty_node) {
return std::format_to(out, "{}<null>", std::string(w.indent * 2, ' '));
}

Expand All @@ -32,7 +30,7 @@ struct std::formatter<korka::ast_walker> {
const auto& node = w.pool[w.index];
std::string spaces(w.indent * 2, ' ');

auto fmt_child = [&](std::string_view label, korka::parser::index_t child_idx) {
auto fmt_child = [&](std::string_view label, korka::nodes::index_t child_idx) {
std::format_to(out, "\n{}{}:", spaces, label);
std::format_to(out, "\n{}", korka::ast_walker{w.pool, child_idx, w.indent + 1});
};
Expand All @@ -41,7 +39,7 @@ struct std::formatter<korka::ast_walker> {

using namespace korka;
std::visit(overloaded{
[&](const parser::expr_literal& lit) {
[&](const nodes::expr_literal& lit) {
std::visit(overloaded{
[&](std::monostate) {
out = std::format_to(out, "null");
Expand All @@ -51,70 +49,70 @@ struct std::formatter<korka::ast_walker> {
}
}, lit);
},
[&](const parser::expr_var& v) {
[&](const nodes::expr_var& v) {
out = std::format_to(out, "Var '{}'", v.name);
},
[&](const parser::expr_unary& v) {
[&](const nodes::expr_unary& v) {
out = std::format_to(out, "Unary '{}'", v.op);
fmt_child("child", v.child);
},
[&](const parser::expr_binary& v) {
[&](const nodes::expr_binary& v) {
out = std::format_to(out, "Binary '{}'", v.op);
fmt_child("L", v.left);
fmt_child("R", v.right);
},
[&](const parser::expr_call& v) {
[&](const nodes::expr_call& v) {
out = std::format_to(out, "Call '{}'", v.name);
if (v.args_head != parser::empty_node) {
if (v.args_head != nodes::empty_node) {
fmt_child("args", v.args_head);
}
},
[&](const parser::stmt_block& v) {
[&](const nodes::stmt_block& v) {
out = std::format_to(out, "Block");
if (v.children_head != parser::empty_node) {
if (v.children_head != nodes::empty_node) {
fmt_child("body", v.children_head);
}
},
[&](const parser::stmt_if& v) {
[&](const nodes::stmt_if& v) {
out = std::format_to(out, "If");
fmt_child("cond", v.condition);
fmt_child("then", v.then_branch);
if (v.else_branch != parser::empty_node) {
if (v.else_branch != nodes::empty_node) {
fmt_child("else", v.else_branch);
}
},
[&](const parser::stmt_while& v) {
[&](const nodes::stmt_while& v) {
out = std::format_to(out, "While");
fmt_child("cond", v.condition);
fmt_child("body", v.body);
},
[&](const parser::stmt_return& v) {
[&](const nodes::stmt_return& v) {
out = std::format_to(out, "Return");
if (v.expr != parser::empty_node) fmt_child("val", v.expr);
if (v.expr != nodes::empty_node) fmt_child("val", v.expr);
},
[&](const parser::stmt_expr& v) {
[&](const nodes::stmt_expr& v) {
out = std::format_to(out, "ExprStmt");
fmt_child("expr", v.expr);
},
[&](const parser::decl_var& v) {
[&](const nodes::decl_var& v) {
out = std::format_to(out, "DeclVar '{} {}'", v.type_name, v.var_name);
if (v.init_expr != parser::empty_node) {
if (v.init_expr != nodes::empty_node) {
fmt_child("init", v.init_expr);
}
},
[&](const parser::decl_function& v) {
[&](const nodes::decl_function& v) {
out = std::format_to(out, "Function '{} {}'", v.ret_type, v.name);
if (v.params_head != parser::empty_node) fmt_child("params", v.params_head);
if (v.params_head != nodes::empty_node) fmt_child("params", v.params_head);
fmt_child("body", v.body);
},
[&](const parser::decl_program& v) {
[&](const nodes::decl_program& v) {
out = std::format_to(out, "Program");
fmt_child("roots", v.external_declarations_head);
}
}, node.data);


if (node.next != parser::empty_node) {
if (node.next != nodes::empty_node) {
std::format_to(out, "\n{}", korka::ast_walker{w.pool, node.next, w.indent});
}

Expand Down
Loading
Loading