Implement parameterless prcoedures

This commit is contained in:
Eugen Wissner 2025-01-11 13:32:37 +01:00
parent 35c32fcf3f
commit 0fcf29c17e
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
6 changed files with 260 additions and 113 deletions

81
example.elna Normal file
View File

@ -0,0 +1,81 @@
type
T = array 5 of Int,
R = record
x: Int;
y: Int
end;
const t = 5;
proc test_string();
var s: String;
begin
s := "Test string.";
writei("");
writei(s)
end;
proc test_array();
var a: T, x_3: Int;
begin
a[0] := 2;
a[1] := 5;
writei("");
writei("Test array:");
x_3 := 0;
while x_3 < 2 do
begin
writei(a[x_3]);
x_3 := x_3 + 1
end
end;
proc test_pointer();
var x_2: Int, p: ^Int;
begin
x_2 := 5;
p := @x_2;
writei("");
writei("Test pointer:");
writei(p);
writei(p^)
end;
proc test_record();
var r: R;
begin
writei("");
writei("Test record:");
r.x := 4;
r.y := 8;
writei(r.y)
end;
var x_1: Int, y: Bool, z: Float, c: Char;
begin
z := 8.2;
x_1 := t;
y := false;
c := 'x';
if y then
z := z + 3.0
else
z := z + 2.0;
writei(z);
writei(x_1);
writei(c);
test_string();
test_array();
test_pointer();
test_record()
end.

View File

@ -8,6 +8,7 @@
#include "diagnostic.h"
#include "realmpfr.h"
#include "stor-layout.h"
#include "varasm.h"
#include <set>
namespace elna
@ -16,81 +17,100 @@ namespace gcc
{
void generic_visitor::visit(source::call_statement *statement)
{
if (statement->name() != "writei")
if (statement->name() == "writei")
{
error_at(get_location(&statement->position()),
"procedure '%s' not declared",
statement->name().c_str());
return;
}
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);
auto argument_type = TREE_TYPE(this->current_expression);
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);
auto argument_type = TREE_TYPE(this->current_expression);
const char *format_number{ nullptr };
if (argument_type == integer_type_node)
{
format_number = "%d\n";
}
else if (argument_type == double_type_node)
{
format_number = "%f\n";
}
else if (argument_type == elna_char_type_node)
{
format_number = "%c\n";
}
else if (is_string_type(argument_type))
{
format_number = "%s\n";
}
else if (is_pointer_type(argument_type))
{
format_number = "%p\n";
const char *format_number{ nullptr };
if (argument_type == integer_type_node)
{
format_number = "%d\n";
}
else if (argument_type == double_type_node)
{
format_number = "%f\n";
}
else if (argument_type == elna_char_type_node)
{
format_number = "%c\n";
}
else if (is_string_type(argument_type))
{
format_number = "%s\n";
}
else if (is_pointer_type(argument_type))
{
format_number = "%p\n";
}
else
{
error_at(get_location(&argument->position()),
"invalid argument of type %s for procedure %s",
print_type(argument_type), statement->name().c_str());
this->current_expression = error_mark_node;
return;
}
tree args[] = {
build_string_literal(strlen(format_number) + 1, format_number),
this->current_expression
};
tree fndecl_type_param[] = {
build_pointer_type(build_qualified_type(char_type_node, TYPE_QUAL_CONST)) /* const char* */
};
tree fndecl_type = build_varargs_function_type_array(integer_type_node, 1, fndecl_type_param);
tree printf_fn_decl = build_fn_decl("printf", fndecl_type);
DECL_EXTERNAL(printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_array(integer_type_node, printf_fn, 2, args);
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
}
else
{
error_at(get_location(&argument->position()),
"invalid argument of type %s for procedure %s",
print_type(argument_type), statement->name().c_str());
this->current_expression = error_mark_node;
return;
tree fndecl_type = build_function_type_list(integer_type_node, NULL_TREE);
tree printf_fn_decl = build_fn_decl(statement->name().c_str(), fndecl_type);
DECL_EXTERNAL(printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_nary(integer_type_node, printf_fn, 0);
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
/*
error_at(get_location(&statement->position()),
"procedure '%s' not declared",
statement->name().c_str()); */
}
tree args[] = {
build_string_literal(strlen(format_number) + 1, format_number),
this->current_expression
};
tree fndecl_type_param[] = {
build_pointer_type(build_qualified_type(char_type_node, TYPE_QUAL_CONST)) /* const char* */
};
tree fndecl_type = build_varargs_function_type_array(integer_type_node, 1, fndecl_type_param);
tree printf_fn_decl = build_fn_decl("printf", fndecl_type);
DECL_EXTERNAL(printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_array(integer_type_node, printf_fn, 2, args);
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
}
void generic_visitor::visit(source::program *program)
{
tree main_fndecl_type_param[] = {
for (const auto& constant : program->definitions())
{
constant->accept(this);
}
tree parameter_types[] = {
integer_type_node,
build_pointer_type(build_pointer_type(char_type_node))
};
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 declaration_type = build_function_type_array(integer_type_node, 2, parameter_types);
this->main_fndecl = build_fn_decl("main", declaration_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;
@ -100,7 +120,47 @@ namespace gcc
enter_scope();
empty_visitor::visit(program);
for (const auto& definition : program->value_definitions)
{
definition->accept(this);
}
program->body().accept(this);
append_to_statement_list(return_stmt, &this->current_statements);
tree_symbol_mapping mapping = leave_scope();
BLOCK_SUPERCONTEXT(mapping.block()) = this->main_fndecl;
DECL_INITIAL(this->main_fndecl) = mapping.block();
DECL_SAVED_TREE(this->main_fndecl) = mapping.bind_expression();
DECL_EXTERNAL(this->main_fndecl) = 0;
DECL_PRESERVE_P(this->main_fndecl) = 1;
gimplify_function_tree(this->main_fndecl);
cgraph_node::finalize_function(this->main_fndecl, true);
}
void generic_visitor::visit(source::procedure_definition *definition)
{
tree *parameter_types = reinterpret_cast<tree *>(xmalloc(definition->parameters().size() * sizeof(tree)));
for (std::size_t i = 0; i < definition->parameters().size(); ++i)
{
parameter_types[i] = build_type(definition->parameters().at(i)->type());
}
tree declaration_type = build_function_type_array(void_type_node,
definition->parameters().size(), parameter_types);
this->main_fndecl = build_fn_decl(definition->identifier().c_str(), declaration_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));
tree return_stmt = build1(RETURN_EXPR, void_type_node, set_result);
enter_scope();
definition->body().accept(this);
append_to_statement_list(return_stmt, &this->current_statements);
tree_symbol_mapping mapping = leave_scope();
@ -115,6 +175,8 @@ namespace gcc
gimplify_function_tree(this->main_fndecl);
cgraph_node::finalize_function(this->main_fndecl, true);
free(parameter_types);
}
void generic_visitor::enter_scope()
@ -426,7 +488,7 @@ namespace gcc
return NULL_TREE;
}
void generic_visitor::visit(source::declaration *declaration)
void generic_visitor::visit(source::variable_declaration *declaration)
{
tree declaration_type = build_type(declaration->type());
gcc_assert(declaration_type != NULL_TREE);

View File

@ -32,6 +32,7 @@ namespace gcc
public:
void visit(source::program *program) override;
void visit(source::procedure_definition *definition) override;
void visit(source::call_statement *statement) override;
void visit(source::number_literal<std::int32_t> *literal) override;
void visit(source::number_literal<double> *literal) override;
@ -42,7 +43,7 @@ namespace gcc
void visit(source::unary_expression *expression) override;
void visit(source::constant_definition *definition) override;
void visit(source::type_definition *definition) override;
void visit(source::declaration *declaration) override;
void visit(source::variable_declaration *declaration) override;
void visit(source::variable_expression *expression) override;
void visit(source::array_access_expression *expression) override;
void visit(source::field_access_expression *expression) override;

View File

@ -33,7 +33,7 @@ namespace source
reference
};
class declaration;
class variable_declaration;
class constant_definition;
class procedure_definition;
class type_definition;
@ -64,7 +64,7 @@ namespace source
*/
struct parser_visitor
{
virtual void visit(declaration *) = 0;
virtual void visit(variable_declaration *) = 0;
virtual void visit(constant_definition *) = 0;
virtual void visit(procedure_definition *) = 0;
virtual void visit(type_definition *) = 0;
@ -97,7 +97,7 @@ namespace source
*/
struct empty_visitor : parser_visitor
{
virtual void visit(declaration *) override;
virtual void visit(variable_declaration *) override;
virtual void visit(constant_definition *definition) override;
virtual void visit(procedure_definition *definition) override;
virtual void visit(type_definition *definition) override;
@ -331,7 +331,7 @@ namespace source
/**
* Variable declaration.
*/
class declaration : public definition
class variable_declaration : public definition
{
type_expression *m_type;
@ -343,13 +343,13 @@ namespace source
* \param identifier Definition name.
* \param type Declared type.
*/
declaration(const struct position position, const std::string& identifier,
variable_declaration(const struct position position, const std::string& identifier,
type_expression *type);
virtual void accept(parser_visitor *visitor) override;
type_expression& type();
virtual ~declaration() override;
virtual ~variable_declaration() override;
};
/**
@ -380,7 +380,7 @@ namespace source
class procedure_definition : public definition
{
block *m_body;
std::vector<declaration *> m_parameters;
std::vector<variable_declaration *> m_parameters;
public:
/**
@ -393,7 +393,7 @@ namespace source
virtual void accept(parser_visitor *visitor) override;
block& body();
std::vector<declaration *>& parameters();
std::vector<variable_declaration *>& parameters();
virtual ~procedure_definition() override;
};
@ -597,18 +597,18 @@ namespace source
class block : public node
{
std::vector<definition *> m_definitions;
std::vector<declaration *> m_declarations;
statement *m_body;
public:
std::vector<definition *> value_definitions;
block(const struct position position, std::vector<definition *>&& definitions,
std::vector<declaration *>&& declarations,
std::vector<definition *>&& value_definitions,
statement *body);
virtual void accept(parser_visitor *visitor) override;
statement& body();
std::vector<definition *>& definitions();
std::vector<declaration *>& declarations();
virtual ~block() override;
};
@ -617,8 +617,7 @@ namespace source
{
public:
program(const struct position position, std::vector<definition *>&& definitions,
std::vector<declaration *>&& declarations,
statement *body);
std::vector<definition *>&& value_definitions, statement *body);
virtual void accept(parser_visitor *visitor) override;
};

View File

@ -7,7 +7,7 @@ namespace elna
{
namespace source
{
void empty_visitor::visit(declaration *)
void empty_visitor::visit(variable_declaration *)
{
}
@ -69,9 +69,9 @@ namespace source
{
constant->accept(this);
}
for (const auto& block_declaration : block->declarations())
for (const auto& definition : block->value_definitions)
{
block_declaration->accept(this);
definition->accept(this);
}
block->body().accept(this);
}
@ -344,23 +344,23 @@ namespace source
}
}
declaration::declaration(const struct position position, const std::string& identifier,
variable_declaration::variable_declaration(const struct position position, const std::string& identifier,
type_expression *type)
: definition(position, identifier), m_type(type)
{
}
declaration::~declaration()
variable_declaration::~variable_declaration()
{
delete m_type;
}
void declaration::accept(parser_visitor *visitor)
void variable_declaration::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
type_expression& declaration::type()
type_expression& variable_declaration::type()
{
return *m_type;
}
@ -412,7 +412,7 @@ namespace source
return *m_body;
}
std::vector<declaration *>& procedure_definition::parameters()
std::vector<variable_declaration *>& procedure_definition::parameters()
{
return m_parameters;
}
@ -448,10 +448,10 @@ namespace source
}
block::block(const struct position position, std::vector<definition *>&& definitions,
std::vector<declaration *>&& declarations,
std::vector<definition *>&& value_definitions,
statement *body)
: node(position), m_definitions(std::move(definitions)),
m_declarations(std::move(declarations)), m_body(std::move(body))
m_body(std::move(body)), value_definitions(std::move(value_definitions))
{
}
@ -460,19 +460,14 @@ namespace source
visitor->visit(this);
}
statement& block::body()
{
return *m_body;
}
std::vector<definition *>& block::definitions()
{
return m_definitions;
}
std::vector<declaration *>& block::declarations()
statement& block::body()
{
return m_declarations;
return *m_body;
}
block::~block()
@ -481,17 +476,16 @@ namespace source
{
delete definition;
}
for (auto declaration : m_declarations)
for (auto definition : value_definitions)
{
delete declaration;
delete definition;
}
delete m_body;
}
program::program(const struct position position, std::vector<definition *>&& definitions,
std::vector<declaration *>&& declarations,
statement *body)
: block(position, std::move(definitions), std::move(declarations), body)
std::vector<definition *>&& value_definitions, statement *body)
: block(position, std::move(definitions), std::move(value_definitions), body)
{
}

View File

@ -80,8 +80,8 @@
%type <elna::source::string_literal *> string_literal;
%type <elna::source::constant_definition *> constant_definition;
%type <std::vector<elna::source::constant_definition *>> constant_part constant_definitions;
%type <elna::source::declaration *> variable_declaration;
%type <std::vector<elna::source::declaration *>> variable_declarations variable_part
%type <elna::source::variable_declaration *> variable_declaration;
%type <std::vector<elna::source::variable_declaration *>> variable_declarations variable_part
formal_parameter_list;
%type <elna::source::type_expression *> type_expression;
%type <elna::source::expression *> expression pointer summand factor comparand;
@ -105,35 +105,45 @@
program:
type_part constant_part procedure_part variable_part compound_statement DOT
{
std::vector<elna::source::definition *> definitions($1.size() + $2.size() + $3.size());
std::vector<elna::source::definition *> definitions($1.size() + $3.size());
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
std::vector<elna::source::definition *> value_definitions($2.size() + $4.size());
std::vector<elna::source::definition *>::iterator value_definition = value_definitions.begin();
for (auto& type : $1)
for (auto type : $1)
{
*definition++ = type;
}
for (auto& constant : $2)
for (auto constant : $2)
{
*definition++ = constant;
*value_definition++ = constant;
}
for (auto& procedure : $3)
for (auto procedure : $3)
{
*definition++ = procedure;
}
for (auto variable : $4)
{
*value_definition++ = variable;
}
driver.tree = std::make_unique<elna::source::program>(elna::source::position{},
std::move(definitions), std::move($4), std::move($5));
std::move(definitions), std::move(value_definitions), std::move($5));
}
block: constant_part variable_part statement
{
std::vector<elna::source::definition *> definitions($1.size());
std::vector<elna::source::definition *> definitions($1.size() + $2.size());
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
for (auto& constant : $1)
for (auto constant : $1)
{
*definition++ = constant;
}
for (auto variable : $2)
{
*definition++ = variable;
}
$$ = new elna::source::block(elna::source::position{},
std::move(definitions), std::move($2), std::move($3));
{}, std::move(definitions), std::move($3));
};
procedure_definition:
PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON block SEMICOLON
@ -335,7 +345,7 @@ type_expression:
}
variable_declaration: IDENTIFIER COLON type_expression
{
$$ = new elna::source::declaration(elna::source::make_position(@1), $1, $3);
$$ = new elna::source::variable_declaration(elna::source::make_position(@1), $1, $3);
};
variable_declarations:
variable_declaration COMMA variable_declarations