Skip to content

Commit

Permalink
Added AST and token printers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goubermouche committed Dec 18, 2023
1 parent ad6fe45 commit 0fec30b
Show file tree
Hide file tree
Showing 25 changed files with 214 additions and 98 deletions.
3 changes: 2 additions & 1 deletion source/abstract_syntax_tree/abstract_syntax_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace sigma {
public:
abstract_syntax_tree();

void add_node(handle<node> node);
void traverse(std::function<void(handle<node>, u16)>&& function) const;

void add_node(handle<node> node);

auto allocate_node_list(u64 count) -> utility::slice<handle<node>>;

auto get_nodes() -> utility::contiguous_container<handle<node>>&;
Expand Down
2 changes: 1 addition & 1 deletion source/abstract_syntax_tree/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace sigma {
auto node_type::to_string() const->std::string {
switch(type) {
case UNKNOWN: return "UNKNOWN";
case FUNCTION: return "FUNCTION";
case FUNCTION_DECLARATION: return "FUNCTION_DECLARATION";
case FUNCTION_CALL: return "FUNCTION_CALL";

case RETURN: return "RETURN";
Expand Down
18 changes: 7 additions & 11 deletions source/abstract_syntax_tree/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace sigma {
enum underlying {
UNKNOWN,

FUNCTION,
FUNCTION_DECLARATION,
FUNCTION_CALL,

RETURN,
Expand All @@ -22,6 +22,7 @@ namespace sigma {

VARIABLE_DECLARATION,
VARIABLE_ACCESS,
// first child is the variable, second child is the assigned value
VARIABLE_ASSIGNMENT,

OPERATOR_ADD,
Expand Down Expand Up @@ -51,18 +52,18 @@ namespace sigma {
utility::slice<named_data_type> parameter_types;
bool has_var_args = false;

utility::symbol_table_key identifier_key;
utility::string_table_key identifier_key;
};

struct function_call {
utility::symbol_table_key callee_identifier_key;
utility::string_table_key callee_identifier_key;
bool is_external;
};

struct return_statement {};

struct literal {
utility::symbol_table_key value_key;
utility::string_table_key value_key;
data_type dt;
};

Expand All @@ -71,12 +72,7 @@ namespace sigma {
};

struct variable {
utility::symbol_table_key identifier_key;
data_type dt;
};

struct variable_access {
utility::symbol_table_key identifier_key;
utility::string_table_key identifier_key;
data_type dt;
};

Expand All @@ -88,7 +84,7 @@ namespace sigma {
// - children[0 - n] = statements

using node_properties = utility::property<
function, return_statement, literal, function_call, variable, variable_access, bool_literal
function, return_statement, literal, function_call, variable, bool_literal
>;

struct node : node_properties {
Expand Down
85 changes: 85 additions & 0 deletions source/compiler/compiler/compilation_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "compilation_context.h"
#include <utility/string_helper.h>

namespace sigma {
void compilation_context::print_tokens() const {
for(const auto& info : tokens) {
std::string symbol_value;
if(strings.contains(info.symbol_key)) {
symbol_value = utility::detail::escape_string(strings.get(info.symbol_key));
}

utility::console::println("{:<20} {}", info.tok.to_string(), symbol_value);
}
}

void compilation_context::print_ast() const {
ast.traverse([&](const handle<node>& node, u16 depth) {
utility::console::print(
"{}{} ", std::string(static_cast<u64>(depth * 2), ' '), node->type.to_string()
);

switch (node->type) {
case node_type::FUNCTION_DECLARATION: {
const auto& property = node->get<function>();

utility::console::print(
"['{} {} (", property.return_type.to_string(), strings.get(property.identifier_key)
);

for (u64 i = 0; i < property.parameter_types.get_size(); ++i) {
utility::console::print("{}", property.parameter_types[i].type.to_string());

if(i + 1 != property.parameter_types.get_size()) {
utility::console::print(", ");
}
}
utility::console::print(")']");
break;
}

case node_type::VARIABLE_DECLARATION: {
const auto& property = node->get<variable>();

utility::console::print(
"[{} '{}']", strings.get(property.identifier_key), property.dt.to_string()
);
break;
}
case node_type::VARIABLE_ACCESS: {
const auto& property = node->get<variable>();
utility::console::print("[{}]", strings.get(property.identifier_key));
break;
}

case node_type::NUMERICAL_LITERAL: {
const auto& property = node->get<literal>();
utility::console::print(
"['{}' {}]", property.dt.to_string(), strings.get(property.value_key)
);

break;
}
case node_type::STRING_LITERAL: {
const auto& property = node->get<literal>();
utility::console::print(
"[\"{}\"]", utility::detail::escape_string(strings.get(property.value_key))
);

break;
}
case node_type::BOOL_LITERAL: {
const auto& property = node->get<bool_literal>();
utility::console::print(
"[{}]", property.value ? "true" : "false"
);

break;
}
}

utility::console::println();
});
}
} // namespace sigma

7 changes: 5 additions & 2 deletions source/compiler/compiler/compilation_context.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once
#include <abstract_syntax_tree/abstract_syntax_tree.h>
#include <utility/containers/symbol_table.h>
#include <utility/containers/string_table.h>
#include <tokenizer/token_buffer.h>

namespace sigma {
struct compilation_context {
utility::symbol_table symbols;
void print_tokens() const;
void print_ast() const;

utility::string_table strings;
abstract_syntax_tree ast;
token_buffer tokens;
};
Expand Down
7 changes: 5 additions & 2 deletions source/compiler/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ namespace sigma {
auto [tokens, symbols] = tokenizer::tokenize(file);

compilation_context context {
.symbols = symbols,
.strings = symbols,
.tokens = tokens
};

// context.print_tokens();

// parser
parser_timer.start();
context.ast = parser::parse(context);
context.print_ast();

// type checker
type_checker_timer.start();
Expand All @@ -62,7 +65,7 @@ namespace sigma {
const char* active_format = object_formats[static_cast<u8>(target.get_system())];
auto object_path = path.parent_path() / (std::string("a") + active_format);

//utility::file::write(object_file, object_path);
utility::file::write(object_file, object_path);
}

void compiler::verify_file(const filepath& path) {
Expand Down
10 changes: 8 additions & 2 deletions source/compiler/test/main.s
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
// - check for memory oversteps
// - IR translator
// - dynamic programming impl?
// - Fix all errors that DEBUG catches, run with fsanitize=address,undefined

i32 test(i32 a) {
ret 0;
}

i32 main() {
if(true) {
bool value = true;
value = false;

if(value) {
printf("true\n");
}
else {
Expand Down
30 changes: 15 additions & 15 deletions source/ir_translator/ir_translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace sigma {
compilation_context& context, ir::target target
) : m_context(context), m_module(target), m_builder(m_module),
m_functions(m_builder), m_variables(m_builder) {
m_functions.register_external_function(m_context.symbols.insert("printf"), {
m_functions.register_external_function(m_context.strings.insert("printf"), {
.identifier = "printf",
.parameters = { PTR_TYPE },
.returns = { I32_TYPE },
.has_var_args = true
});

m_functions.register_external_function(m_context.symbols.insert("puts"), {
m_functions.register_external_function(m_context.strings.insert("puts"), {
.identifier = "puts",
.parameters = { PTR_TYPE },
.returns = { I32_TYPE },
Expand All @@ -29,7 +29,7 @@ namespace sigma {

handle<ir::node> ir_translator::translate_node(handle<node> ast_node) {
switch (ast_node->type) {
case node_type::FUNCTION: translate_function_declaration(ast_node); break;
case node_type::FUNCTION_DECLARATION: translate_function_declaration(ast_node); break;
case node_type::FUNCTION_CALL: return translate_function_call(ast_node);

// // flow control
Expand All @@ -38,21 +38,21 @@ namespace sigma {

case node_type::VARIABLE_DECLARATION: translate_variable_declaration(ast_node); break;
case node_type::VARIABLE_ACCESS: return translate_variable_access(ast_node);
// case node_type::VARIABLE_ASSIGNMENT: return translate_variable_assignment(ast_node);
case node_type::VARIABLE_ASSIGNMENT: return translate_variable_assignment(ast_node);

// // operators:
// case node_type::OPERATOR_ADD:
// case node_type::OPERATOR_SUBTRACT:
// case node_type::OPERATOR_MULTIPLY:
// case node_type::OPERATOR_DIVIDE:
// case node_type::OPERATOR_MODULO: return translate_binary_math_operator(ast_node);
case node_type::OPERATOR_ADD:
case node_type::OPERATOR_SUBTRACT:
case node_type::OPERATOR_MULTIPLY:
case node_type::OPERATOR_DIVIDE:
case node_type::OPERATOR_MODULO: return translate_binary_math_operator(ast_node);

// // literals
case node_type::NUMERICAL_LITERAL: return translate_numerical_literal(ast_node);
case node_type::STRING_LITERAL: return translate_string_literal(ast_node);
case node_type::BOOL_LITERAL: return translate_bool_literal(ast_node);
// default: NOT_IMPLEMENTED();
default: std::cout << ast_node->type.to_string() << '\n';
default: PANIC("irgen for node '{}' is not implemented", ast_node->type.to_string());
}

return nullptr;
Expand All @@ -69,7 +69,7 @@ namespace sigma {

// TODO: the IR system should inherit our symbol table system
const ir::function_signature signature {
.identifier = m_context.symbols.get(prop.identifier_key), // TEMP
.identifier = m_context.strings.get(prop.identifier_key), // TEMP
.parameters = parameter_types,
.returns = { data_type_to_ir(prop.return_type) },
.has_var_args = false
Expand Down Expand Up @@ -175,7 +175,7 @@ namespace sigma {
auto ir_translator::translate_string_literal(
handle<node> string_literal_node
) const -> handle<ir::node> {
const std::string& value = m_context.symbols.get(string_literal_node->get<literal>().value_key);
const std::string& value = m_context.strings.get(string_literal_node->get<literal>().value_key);
return m_builder.create_string(value);
}

Expand Down Expand Up @@ -220,7 +220,7 @@ namespace sigma {
}

auto ir_translator::translate_variable_access(handle<node> access_node) -> handle<ir::node> {
const auto& prop = access_node->get<variable_access>();
const auto& prop = access_node->get<variable>();

handle<ir::node> load = m_variables.create_load(
prop.identifier_key, data_type_to_ir(prop.dt), prop.dt.get_byte_width()
Expand All @@ -233,7 +233,7 @@ namespace sigma {
auto ir_translator::translate_variable_assignment(
handle<node> assignment_node
) -> handle<ir::node> {
const auto& var = assignment_node->children[0]->get<variable_access>();
const auto& var = assignment_node->children[0]->get<variable>();
const handle<ir::node> value = translate_node(assignment_node->children[1]);

// assign the variable
Expand All @@ -242,7 +242,7 @@ namespace sigma {
}

auto ir_translator::literal_to_ir(literal& literal) const -> handle<ir::node> {
const std::string& value = m_context.symbols.get(literal.value_key);
const std::string& value = m_context.strings.get(literal.value_key);

// handle pointers separately
if (literal.dt.pointer_level > 0) {
Expand Down
6 changes: 3 additions & 3 deletions source/ir_translator/values/function_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace sigma::detail {
function_registry::function_registry(ir::builder& builder) : m_builder(builder) {}

void function_registry::register_function(
utility::symbol_table_key identifier_key, const ir::function_signature& function_sig
utility::string_table_key identifier_key, const ir::function_signature& function_sig
) {
m_functions[identifier_key] = m_builder.create_function(function_sig, ir::linkage::PUBLIC);
}

void function_registry::register_external_function(
utility::symbol_table_key identifier_key, const ir::function_signature& function_sig
utility::string_table_key identifier_key, const ir::function_signature& function_sig
) {
m_external_functions[identifier_key] = external_function{
.external = m_builder.create_external(function_sig, ir::linkage::SO_LOCAL),
Expand All @@ -19,7 +19,7 @@ namespace sigma::detail {
}

auto function_registry::create_call(
utility::symbol_table_key identifier_key, const std::vector<handle<ir::node>>& parameters
utility::string_table_key identifier_key, const std::vector<handle<ir::node>>& parameters
) -> handle<ir::node> {
// attempt to call a local function
const auto func_it = m_functions.find(identifier_key);
Expand Down
12 changes: 6 additions & 6 deletions source/ir_translator/values/function_registry.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <intermediate_representation/builder.h>
#include <utility/containers/symbol_table.h>
#include <utility/containers/string_table.h>

namespace sigma::detail {
using namespace utility::types;
Expand All @@ -20,7 +20,7 @@ namespace sigma::detail {
* \param function_sig Function to declare
*/
void register_function(
utility::symbol_table_key identifier_key, const ir::function_signature& function_sig
utility::string_table_key identifier_key, const ir::function_signature& function_sig
);

/**
Expand All @@ -29,7 +29,7 @@ namespace sigma::detail {
* \param function_sig Function signature of the external function
*/
void register_external_function(
utility::symbol_table_key identifier_key, const ir::function_signature& function_sig
utility::string_table_key identifier_key, const ir::function_signature& function_sig
);

/**
Expand All @@ -39,13 +39,13 @@ namespace sigma::detail {
* \return Callee return value.
*/
[[nodiscard]] auto create_call(
utility::symbol_table_key identifier_key, const std::vector<handle<ir::node>>& parameters
utility::string_table_key identifier_key, const std::vector<handle<ir::node>>& parameters
) -> handle<ir::node>;
private:
ir::builder& m_builder;

// TODO: the key should take parameter data types into account
std::unordered_map<utility::symbol_table_key, handle<ir::function>> m_functions;
std::unordered_map<utility::symbol_table_key, external_function> m_external_functions;
std::unordered_map<utility::string_table_key, handle<ir::function>> m_functions;
std::unordered_map<utility::string_table_key, external_function> m_external_functions;
};
} // sigma::detail
Loading

0 comments on commit 0fec30b

Please sign in to comment.