Add GCC backend glue

This commit is contained in:
2024-12-27 10:51:46 +01:00
parent 40306ac986
commit 51f5603c4a
24 changed files with 601 additions and 329 deletions

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/ast.h"
namespace elna

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/driver.h"
namespace elna
@ -13,7 +16,7 @@ namespace source
}
syntax_error::syntax_error(const std::string& message,
const std::filesystem::path& input_file, const yy::location& location)
const char *input_file, const yy::location& location)
: error(input_file, make_position(location)), message(message)
{
}
@ -23,7 +26,7 @@ namespace source
return message;
}
driver::driver(const std::filesystem::path& input_file)
driver::driver(const char *input_file)
: input_file(input_file)
{
}

View File

@ -1,3 +1,8 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
%{
#define YY_NO_UNISTD_H
#define YY_USER_ACTION this->location.columns(yyleng);

View File

@ -1,3 +1,8 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
%require "3.2"
%language "c++"
@ -70,7 +75,7 @@
%type <std::vector<std::unique_ptr<elna::source::declaration>>> variable_declarations variable_declaration_part
formal_parameter_list;
%type <std::unique_ptr<elna::source::type_expression>> type_expression;
%type <std::unique_ptr<elna::source::expression>> expression pointer summand factor address;
%type <std::unique_ptr<elna::source::expression>> expression pointer summand factor address comparand;
%type <std::vector<std::unique_ptr<elna::source::expression>>> expressions actual_parameter_list;
%type <std::unique_ptr<elna::source::variable_expression>> variable_expression;
%type <std::unique_ptr<elna::source::compound_statement>> compound_statement;
@ -81,24 +86,25 @@
%type <std::unique_ptr<elna::source::statement>> statement;
%type <std::vector<std::unique_ptr<elna::source::statement>>> statements optional_statements;
%type <std::unique_ptr<elna::source::procedure_definition>> procedure_definition;
%type <std::vector<std::unique_ptr<elna::source::procedure_definition>>> procedure_definitions;
%type <std::vector<std::unique_ptr<elna::source::procedure_definition>>> procedure_definitions
procedure_definition_part;
%type <std::unique_ptr<elna::source::block>> block;
%%
program: constant_definition_part variable_declaration_part procedure_definitions statement DOT
program: constant_definition_part procedure_definition_part variable_declaration_part statement DOT
{
std::vector<std::unique_ptr<elna::source::definition>> definitions($1.size() + $3.size());
std::vector<std::unique_ptr<elna::source::definition>> definitions($1.size() + $2.size());
std::vector<std::unique_ptr<elna::source::definition>>::iterator definition = definitions.begin();
for (auto& constant : $1)
{
*definition++ = std::move(constant);
}
for (auto& constant : $3)
for (auto& procedure : $2)
{
*definition++ = std::move(constant);
*definition++ = std::move(procedure);
}
driver.tree = std::make_unique<elna::source::program>(elna::source::position{},
std::move(definitions), std::move($2),
std::move(definitions), std::move($3),
std::move($4));
}
block: constant_definition_part variable_declaration_part statement
@ -127,6 +133,9 @@ procedure_definitions:
$$.emplace($$.cbegin(), std::move($1));
}
| procedure_definition { $$.emplace_back(std::move($1)); }
procedure_definition_part:
/* no procedure definitions */ {}
| procedure_definitions { std::swap($$, $1); }
integer_literal: NUMBER
{
$$ = std::make_unique<elna::source::integer_literal>(elna::source::make_position(@1), $1);
@ -191,38 +200,50 @@ factor:
std::move($2), '@');
}
| address { $$ = std::move($1); }
comparand:
summand PLUS summand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '+');
}
| summand MINUS summand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '-');
}
| summand { $$ = std::move($1); }
expression:
summand EQUALS summand
comparand EQUALS comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '=');
}
| summand NOT_EQUAL summand
| comparand NOT_EQUAL comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), 'n');
}
| summand LESS_THAN summand
| comparand LESS_THAN comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '<');
}
| summand GREATER_THAN summand
| comparand GREATER_THAN comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '>');
}
| summand LESS_EQUAL summand
| comparand LESS_EQUAL comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '<');
}
| summand GREATER_EQUAL summand
| comparand GREATER_EQUAL comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '>');
}
| summand { $$ = std::move($1); }
| comparand { $$ = std::move($1); }
expressions:
expression COMMA expressions
{

View File

@ -1,11 +1,14 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/result.h"
namespace elna
{
namespace source
{
error::error(const std::filesystem::path& path, const position position)
: m_position(position), m_path(path)
error::error(const char *path, const position position)
: m_position(position), path(path)
{
}
@ -19,12 +22,7 @@ namespace source
return this->m_position.column;
}
const std::filesystem::path& error::path() const noexcept
{
return this->m_path;
}
name_collision::name_collision(const std::string& name, const std::filesystem::path& path,
name_collision::name_collision(const std::string& name, const char *path,
const position current, const position previous)
: error(path, current), name(name), previous(previous)
{
@ -36,7 +34,7 @@ namespace source
}
type_mismatch::type_mismatch(std::shared_ptr<const type> got, operation kind,
const std::filesystem::path& path, const struct position position)
const char *path, const struct position position)
: error(path, position), kind(kind), got(got)
{
}

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/semantic.h"
#include "elna/source/result.h"
#include <cstdlib>
@ -7,7 +10,7 @@ namespace elna
namespace source
{
name_analysis_visitor::name_analysis_visitor(std::shared_ptr<symbol_table> table,
const std::filesystem::path& filename, const std::size_t target_pointer_size)
const char *filename, const std::size_t target_pointer_size)
: table(table), filename(filename), pointer_size(target_pointer_size)
{
}
@ -133,7 +136,7 @@ namespace source
}
type_analysis_visitor::type_analysis_visitor(std::shared_ptr<symbol_table> table,
const std::filesystem::path& filename, const std::size_t target_pointer_size)
const char *filename, const std::size_t target_pointer_size)
: table(table), filename(filename), pointer_size(target_pointer_size)
{
}

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/types.h"
#include "elna/source/symbol_table.h"

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include <elna/source/types.h>
namespace elna