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 "diagnostic.h"
#include "realmpfr.h" #include "realmpfr.h"
#include "stor-layout.h" #include "stor-layout.h"
#include "varasm.h"
#include <set> #include <set>
namespace elna namespace elna
@ -16,13 +17,8 @@ namespace gcc
{ {
void generic_visitor::visit(source::call_statement *statement) 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) if (statement->arguments().size() != 1)
{ {
error_at(get_location(&statement->position()), error_at(get_location(&statement->position()),
@ -82,15 +78,39 @@ namespace gcc
append_to_statement_list(stmt, &this->current_statements); append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE; this->current_expression = NULL_TREE;
} }
else
{
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()); */
}
}
void generic_visitor::visit(source::program *program) 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, integer_type_node,
build_pointer_type(build_pointer_type(char_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); tree declaration_type = build_function_type_array(integer_type_node, 2, parameter_types);
this->main_fndecl = build_fn_decl("main", main_fndecl_type); this->main_fndecl = build_fn_decl("main", declaration_type);
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node); tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_CONTEXT(resdecl) = this->main_fndecl; DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl; DECL_RESULT(this->main_fndecl) = resdecl;
@ -100,7 +120,47 @@ namespace gcc
enter_scope(); 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); append_to_statement_list(return_stmt, &this->current_statements);
tree_symbol_mapping mapping = leave_scope(); tree_symbol_mapping mapping = leave_scope();
@ -115,6 +175,8 @@ namespace gcc
gimplify_function_tree(this->main_fndecl); gimplify_function_tree(this->main_fndecl);
cgraph_node::finalize_function(this->main_fndecl, true); cgraph_node::finalize_function(this->main_fndecl, true);
free(parameter_types);
} }
void generic_visitor::enter_scope() void generic_visitor::enter_scope()
@ -426,7 +488,7 @@ namespace gcc
return NULL_TREE; 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()); tree declaration_type = build_type(declaration->type());
gcc_assert(declaration_type != NULL_TREE); gcc_assert(declaration_type != NULL_TREE);

View File

@ -32,6 +32,7 @@ namespace gcc
public: public:
void visit(source::program *program) override; void visit(source::program *program) override;
void visit(source::procedure_definition *definition) override;
void visit(source::call_statement *statement) override; void visit(source::call_statement *statement) override;
void visit(source::number_literal<std::int32_t> *literal) override; void visit(source::number_literal<std::int32_t> *literal) override;
void visit(source::number_literal<double> *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::unary_expression *expression) override;
void visit(source::constant_definition *definition) override; void visit(source::constant_definition *definition) override;
void visit(source::type_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::variable_expression *expression) override;
void visit(source::array_access_expression *expression) override; void visit(source::array_access_expression *expression) override;
void visit(source::field_access_expression *expression) override; void visit(source::field_access_expression *expression) override;

View File

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

View File

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

View File

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