Cast expressions

This commit is contained in:
2025-01-28 11:21:02 +01:00
parent 18d8ded239
commit be1a56a557
9 changed files with 183 additions and 136 deletions

View File

@ -20,40 +20,21 @@ namespace gcc
this->symbol_map = symbol_table;
}
void generic_visitor::visit(source::call_expression *statement)
void generic_visitor::visit(source::call_expression *expression)
{
auto symbol = this->symbol_map->lookup(statement->name());
if (statement->name() == "Int" || statement->name() == "Word" || statement->name() == "Bool"
|| statement->name() == "Float" || statement->name() == "Char")
{
if (statement->arguments().size() != 1)
{
error_at(get_location(&statement->position()),
"procedure '%s' expects 1 argument, %lu given",
statement->name().c_str(), statement->arguments().size());
return;
}
auto& argument = statement->arguments().at(0);
argument->accept(this);
tree argument_type = TREE_TYPE(this->current_expression);
this->current_expression = build1_loc(get_location(&argument->position()), CONVERT_EXPR,
symbol->payload, this->current_expression);
}
else if (symbol)
if (auto symbol = this->symbol_map->lookup(expression->name()))
{
tree return_type = TREE_TYPE(TREE_TYPE(symbol->payload));
tree fndecl_type = build_function_type(return_type, TYPE_ARG_TYPES(symbol->payload));
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), symbol->payload);
std::vector<tree> arguments(statement->arguments().size());
for (std::size_t i = 0; i < statement->arguments().size(); ++i)
std::vector<tree> arguments(expression->arguments().size());
for (std::size_t i = 0; i < expression->arguments().size(); ++i)
{
statement->arguments().at(i)->accept(this);
expression->arguments().at(i)->accept(this);
arguments[i] = this->current_expression;
}
tree stmt = build_call_array_loc(get_location(&statement->position()),
tree stmt = build_call_array_loc(get_location(&expression->position()),
return_type, printf_fn, arguments.size(), arguments.data());
if (return_type == void_type_node)
@ -68,12 +49,24 @@ namespace gcc
}
else
{
error_at(get_location(&statement->position()),
error_at(get_location(&expression->position()),
"procedure '%s' not declared",
statement->name().c_str());
expression->name().c_str());
this->current_expression = error_mark_node;
}
}
void generic_visitor::visit(source::cast_expression *expression)
{
tree cast_target = build_type(expression->target());
gcc_assert(cast_target != NULL_TREE);
expression->value().accept(this);
this->current_expression = build1_loc(get_location(&expression->position()), CONVERT_EXPR,
cast_target, this->current_expression);
}
void generic_visitor::visit(source::program *program)
{
for (const auto definition : program->value_definitions)