Implement external functions
This commit is contained in:
@ -18,11 +18,14 @@ namespace source
|
||||
|
||||
void empty_visitor::visit(procedure_definition *definition)
|
||||
{
|
||||
for (auto parameter : definition->parameters())
|
||||
for (auto parameter : definition->parameters)
|
||||
{
|
||||
parameter->accept(this);
|
||||
}
|
||||
definition->body().accept(this);
|
||||
if (definition->body() != nullptr)
|
||||
{
|
||||
definition->body()->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(type_definition *definition)
|
||||
@ -65,11 +68,14 @@ namespace source
|
||||
|
||||
void empty_visitor::visit(block *block)
|
||||
{
|
||||
for (const auto& definition : block->value_definitions)
|
||||
for (const auto definition : block->value_definitions)
|
||||
{
|
||||
definition->accept(this);
|
||||
}
|
||||
block->body().accept(this);
|
||||
for (const auto body_statement : block->body)
|
||||
{
|
||||
body_statement->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(program *program)
|
||||
@ -407,20 +413,18 @@ namespace source
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
block& procedure_definition::body()
|
||||
block *procedure_definition::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
std::vector<variable_declaration *>& procedure_definition::parameters()
|
||||
{
|
||||
return m_parameters;
|
||||
return m_body;
|
||||
}
|
||||
|
||||
procedure_definition::~procedure_definition()
|
||||
{
|
||||
delete m_body;
|
||||
for (auto parameter : m_parameters)
|
||||
if (m_body != nullptr)
|
||||
{
|
||||
delete m_body;
|
||||
}
|
||||
for (auto parameter : parameters)
|
||||
{
|
||||
delete parameter;
|
||||
}
|
||||
@ -448,8 +452,8 @@ namespace source
|
||||
}
|
||||
|
||||
block::block(const struct position position, std::vector<definition *>&& value_definitions,
|
||||
statement *body)
|
||||
: node(position), m_body(std::move(body)), value_definitions(std::move(value_definitions))
|
||||
std::vector<statement *>&& body)
|
||||
: node(position), value_definitions(std::move(value_definitions)), body(std::move(body))
|
||||
{
|
||||
}
|
||||
|
||||
@ -458,24 +462,22 @@ namespace source
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
statement& block::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
block::~block()
|
||||
{
|
||||
for (auto definition : value_definitions)
|
||||
for (auto definition : this->value_definitions)
|
||||
{
|
||||
delete definition;
|
||||
}
|
||||
delete m_body;
|
||||
for (auto body_statement : this->body)
|
||||
{
|
||||
delete body_statement;
|
||||
}
|
||||
}
|
||||
|
||||
program::program(const struct position position,
|
||||
std::vector<definition *>&& type_definitions,
|
||||
std::vector<definition *>&& value_definitions, statement *body)
|
||||
: block(position, std::move(value_definitions), body),
|
||||
std::vector<definition *>&& value_definitions, std::vector<statement *>&& body)
|
||||
: block(position, std::move(value_definitions), std::move(body)),
|
||||
type_definitions(std::move(type_definitions))
|
||||
{
|
||||
}
|
||||
|
@ -55,6 +55,9 @@ begin {
|
||||
end {
|
||||
return yy::parser::make_END_BLOCK(this->location);
|
||||
}
|
||||
extern {
|
||||
return yy::parser::make_EXTERN(this->location);
|
||||
}
|
||||
const {
|
||||
return yy::parser::make_CONST(this->location);
|
||||
}
|
||||
|
@ -64,7 +64,7 @@
|
||||
%token <bool> BOOLEAN
|
||||
%token IF WHILE DO
|
||||
%token CONST VAR PROCEDURE ARRAY OF TYPE RECORD
|
||||
%token BEGIN_BLOCK END_BLOCK
|
||||
%token BEGIN_BLOCK END_BLOCK EXTERN
|
||||
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
|
||||
%token AND OR NOT
|
||||
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
|
||||
@ -100,7 +100,7 @@
|
||||
%type <std::vector<std::pair<std::string, elna::source::type_expression *>>> field_list;
|
||||
%%
|
||||
program:
|
||||
type_part constant_part procedure_part variable_part compound_statement DOT
|
||||
type_part constant_part procedure_part variable_part BEGIN_BLOCK optional_statements END_BLOCK DOT
|
||||
{
|
||||
std::vector<elna::source::definition *> definitions($1.size() + $3.size());
|
||||
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
|
||||
@ -124,11 +124,11 @@ program:
|
||||
*value_definition++ = variable;
|
||||
}
|
||||
auto tree = new elna::source::program(elna::source::position{},
|
||||
std::move(definitions), std::move(value_definitions), std::move($5));
|
||||
std::move(definitions), std::move(value_definitions), std::move($6));
|
||||
|
||||
driver.tree.reset(tree);
|
||||
}
|
||||
block: constant_part variable_part statement
|
||||
block: constant_part variable_part BEGIN_BLOCK optional_statements END_BLOCK
|
||||
{
|
||||
std::vector<elna::source::definition *> definitions($1.size() + $2.size());
|
||||
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
|
||||
@ -142,15 +142,21 @@ block: constant_part variable_part statement
|
||||
*definition++ = variable;
|
||||
}
|
||||
$$ = new elna::source::block(elna::source::position{},
|
||||
std::move(definitions), std::move($3));
|
||||
};
|
||||
std::move(definitions), std::move($4));
|
||||
}
|
||||
procedure_definition:
|
||||
PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON block SEMICOLON
|
||||
{
|
||||
$$ = new elna::source::procedure_definition(elna::source::position{},
|
||||
std::move($2), $5);
|
||||
std::swap($$->parameters(), $3);
|
||||
};
|
||||
std::swap($$->parameters, $3);
|
||||
}
|
||||
| PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON EXTERN SEMICOLON
|
||||
{
|
||||
$$ = new elna::source::procedure_definition(elna::source::position{},
|
||||
std::move($2));
|
||||
std::swap($$->parameters, $3);
|
||||
}
|
||||
procedure_definitions:
|
||||
procedure_definition procedure_definitions
|
||||
{
|
||||
|
Reference in New Issue
Block a user