Add string type

This commit is contained in:
2025-01-03 22:18:35 +01:00
parent 660c774327
commit a7b0c53d23
11 changed files with 202 additions and 76 deletions

View File

@ -25,7 +25,7 @@ namespace gcc
if (statement->arguments().size() != 1)
{
error_at(get_location(&statement->position()),
"procedure '%s' expects 1 argument, %i given",
"procedure '%s' expects 1 argument, %lu given",
statement->name().c_str(), statement->arguments().size());
return;
}
@ -46,6 +46,10 @@ namespace gcc
{
format_number = "%c\n";
}
else if (is_string_type(argument_type))
{
format_number = "%s\n";
}
else
{
error_at(get_location(&argument->position()),
@ -83,6 +87,7 @@ namespace gcc
tree main_fndecl_type = build_function_type_array(integer_type_node, 2, main_fndecl_type_param);
this->main_fndecl = build_fn_decl("main", main_fndecl_type);
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl;
tree set_result = build2(INIT_EXPR, void_type_node, DECL_RESULT(main_fndecl),
build_int_cst_type(integer_type_node, 0));
@ -139,6 +144,11 @@ namespace gcc
this->current_expression = build_int_cstu(elna_char_type_node, character->character());
}
void generic_visitor::visit(source::string_literal *string)
{
this->current_expression = build_string_literal(string->string().size() + 1, string->string().c_str());
}
void generic_visitor::visit(source::binary_expression *expression)
{
expression->lhs().accept(this);
@ -180,6 +190,8 @@ namespace gcc
operator_code = MULT_EXPR;
target_type = left_type;
break;
default:
break;
}
if (operator_code != ERROR_MARK) // An arithmetic operation.
{
@ -219,6 +231,8 @@ namespace gcc
operator_code = GE_EXPR;
target_type = boolean_type_node;
break;
default:
break;
}
gcc_assert(operator_code != ERROR_MARK);
gcc_assert(target_type != error_mark_node);
@ -275,6 +289,10 @@ namespace gcc
{
declaration_type = elna_char_type_node;
}
else if (declaration->type().base() == "String")
{
declaration_type = elna_string_type_node;
}
else
{
error_at(get_location(&declaration->type().position()),
@ -286,6 +304,7 @@ namespace gcc
get_identifier(declaration->identifier().c_str()), declaration_type);
auto result = this->symbol_map.insert({ declaration->identifier(), declaration_tree });
// DECL_CONTEXT(declaration_tree) = this->main_fndecl;
if (result.second)
{
auto declaration_statement = build1_loc(declaration_location, DECL_EXPR,