Replace ! with a function call writei

This commit is contained in:
Eugen Wissner 2024-03-19 09:35:50 +01:00
parent bfabdb1d08
commit 263b868a45
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
25 changed files with 315 additions and 159 deletions

2
TODO
View File

@ -14,8 +14,6 @@
- Support immediates greater than 12 bits.
- It seems instructions are correctly encoded only if the compiler is running
on a little endian architecture.
- Save static symbols during the code generation, don't hardcode them in the
object generator.
# Shell
- Persist the history.

View File

@ -76,9 +76,8 @@ namespace elna::riscv
return reinterpret_cast<const std::byte *>(&this->representation) + sizeof(this->representation);
}
visitor::visitor(std::function<void(const std::string&, const std::byte *, std::size_t)> write_text,
std::function<std::string_view(const std::byte *, std::size_t)> write_read_only)
: write_text(write_text), write_read_only(write_read_only)
visitor::visitor(std::shared_ptr<source::writer> writer)
: writer(writer)
{
}
@ -127,40 +126,67 @@ namespace elna::riscv
void visitor::visit(source::program *program)
{
this->writer->sink("printf");
auto format_string = this->writer->sink(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::sp, funct3_t::addi, x_register::sp, -8));
this->instructions.push_back(instruction(base_opcode::store)
.s(4, funct3_t::sw, x_register::sp, x_register::s0));
this->instructions.push_back(instruction(base_opcode::store)
.s(0, funct3_t::sw, x_register::sp, x_register::ra));
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::s0, funct3_t::addi, x_register::sp, -8));
this->references.push_back(reference());
this->references.back().name = format_string;
this->references.back().offset = writer->size() + instructions.size() * 4;
this->references.back().target = address_t::high20;
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
this->references.push_back(reference());
this->references.back().name = format_string;
this->references.back().offset = writer->size() + instructions.size() * 4;
this->references.back().target = address_t::lower12i;
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
this->references.push_back(reference());
this->references.back().name = "printf";
this->references.back().offset = writer->size() + instructions.size() * 4;
this->references.back().target = address_t::text;
this->instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
this->instructions.push_back(instruction(base_opcode::jalr)
.i(x_register::ra, funct3_t::jalr, x_register::ra, 0));
this->instructions.push_back(instruction(base_opcode::load)
.i(x_register::s0, funct3_t::lw, x_register::sp, 4));
this->instructions.push_back(instruction(base_opcode::load)
.i(x_register::ra, funct3_t::lw, x_register::sp, 0));
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::sp, funct3_t::addi, x_register::sp, 8));
this->instructions.push_back(instruction(base_opcode::jalr)
.i(x_register::zero, funct3_t::jalr, x_register::ra, 0));
this->writer->sink("writei", reinterpret_cast<const std::byte *>(this->instructions.data()),
this->instructions.size() * sizeof(instruction));
this->instructions.clear();
visit(dynamic_cast<source::block *>(program));
write_text("main", reinterpret_cast<const std::byte *>(this->instructions.data()),
this->writer->sink("main", reinterpret_cast<const std::byte *>(this->instructions.data()),
this->instructions.size() * sizeof(instruction));
}
void visitor::visit(source::bang_statement *statement)
void visitor::visit(source::call_statement *statement)
{
statement->body().accept(this);
statement->arguments().accept(this);
// Print the result.
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::a0, 0));
auto format_string = write_read_only(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
this->references.push_back(reference());
this->references.back().name = format_string;
this->references.back().offset = instructions.size() * 4;
this->references.back().target = address_t::high20;
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
this->references.push_back(reference());
this->references.back().name = format_string;
this->references.back().offset = instructions.size() * 4;
this->references.back().target = address_t::lower12i;
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
this->references.push_back(reference());
this->references.back().name = "printf";
this->references.back().offset = instructions.size() * 4;
this->references.back().target = address_t::text;
this->instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
this->instructions.push_back(instruction(base_opcode::jalr)
.i(x_register::ra, funct3_t::jalr, x_register::ra, 0));
.i(x_register::ra, funct3_t::jalr, x_register::ra, -instructions.size() * 4 - 40));
}
void visitor::visit(source::question_mark_statement *statement)
@ -171,23 +197,23 @@ namespace elna::riscv
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::a0, 0));
auto format_string = write_read_only(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
auto format_string = this->writer->sink(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
this->references.push_back(reference());
this->references.back().name = format_string;
this->references.back().offset = instructions.size() * 4;
this->references.back().offset = writer->size() + instructions.size() * 4;
this->references.back().target = address_t::high20;
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
this->references.push_back(reference());
this->references.back().name = format_string;
this->references.back().offset = instructions.size() * 4;
this->references.back().offset = writer->size() + instructions.size() * 4;
this->references.back().target = address_t::lower12i;
this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
this->references.push_back(reference());
this->references.back().name = "printf";
this->references.back().offset = instructions.size() * 4;
this->references.back().offset = writer->size() + instructions.size() * 4;
this->references.back().target = address_t::text;
this->instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
this->instructions.push_back(instruction(base_opcode::jalr)
@ -224,7 +250,7 @@ namespace elna::riscv
instructions.push_back(instruction(base_opcode::branch));
statement->body().accept(this);
instructions[before_branch]
.b((instructions.size() - before_branch) * 4 - 3, funct3_t::beq, x_register::zero, free_register);
.b((instructions.size() - before_branch) * 4, funct3_t::beq, x_register::zero, free_register);
}
void visitor::visit(source::while_statement *statement)

View File

@ -4,17 +4,17 @@
namespace elna::riscv
{
elfio_writer::iterator::reference elfio_writer::iterator::operator*() const noexcept
elfio_section_writer::iterator::reference elfio_section_writer::iterator::operator*() const noexcept
{
return payload;
}
elfio_writer::iterator::pointer elfio_writer::iterator::operator->() const noexcept
elfio_section_writer::iterator::pointer elfio_section_writer::iterator::operator->() const noexcept
{
return &payload;
}
elfio_writer::iterator& elfio_writer::iterator::operator++()
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++()
{
this->payload.data += *this->sizes;
this->payload.label = *(++this->labels);
@ -23,70 +23,127 @@ namespace elna::riscv
return *this;
}
elfio_writer::iterator& elfio_writer::iterator::operator++(int)
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++(int)
{
auto tmp = *this;
++(*this);
return *this;
}
bool elfio_writer::iterator::operator==(const iterator& that) const
bool elfio_section_writer::iterator::operator==(const iterator& that) const
{
return this->labels == that.labels;
}
bool elfio_writer::iterator::operator!=(const iterator& that) const
bool elfio_section_writer::iterator::operator!=(const iterator& that) const
{
return !(*this == that);
}
elfio_writer::elfio_writer(ELFIO::section *text)
: text(text), labels(std::make_shared<std::vector<std::string>>()),
sizes(std::make_shared<std::vector<std::size_t>>())
elfio_section_writer::elfio_section_writer(ELFIO::section *section)
: m_section(section)
{
}
void elfio_writer::operator()(const std::string& label, const std::byte *data, std::size_t size)
void elfio_section_writer::operator()(const std::string& label, const std::byte *data, std::size_t size)
{
labels->push_back(label + '\0');
sizes->push_back(size);
text->append_data(reinterpret_cast<const char *>(data), size);
labels.push_back(label);
sizes.push_back(size);
m_section->append_data(reinterpret_cast<const char *>(data), size);
}
std::string_view elfio_writer::operator()(const std::byte *data, std::size_t size)
std::pair<std::string_view, bool> elfio_section_writer::operator()(const std::byte *data, std::size_t size)
{
auto found = std::find_if(begin(), end(),
[data, size](elfio_writer::entry entry) {
[data, size](elfio_section_writer::entry entry) {
return size == entry.size && std::memcmp(entry.data, data, size) == 0;
});
if (found == end())
{
(*this)(".CL" + std::to_string(labels->size()), data, size);
return labels->back();
(*this)(".CL" + std::to_string(labels.size()), data, size);
return std::pair<std::string_view, bool>(labels.back(), true);
}
return found->label;
return std::pair<std::string_view, bool>(found->label, false);
}
elfio_writer::iterator elfio_writer::begin() const
elfio_section_writer::iterator elfio_section_writer::begin() const
{
return elfio_writer::iterator(labels->cbegin(), sizes->cbegin(),
reinterpret_cast<const std::byte *>(text->get_data()));
return elfio_section_writer::iterator(labels.cbegin(), sizes.cbegin(),
reinterpret_cast<const std::byte *>(m_section->get_data()));
}
elfio_writer::iterator elfio_writer::end() const
elfio_section_writer::iterator elfio_section_writer::end() const
{
return elfio_writer::iterator(labels->cend(), sizes->cend());
return elfio_section_writer::iterator(labels.cend(), sizes.cend());
}
std::ptrdiff_t elfio_writer::lookup(const std::string& label)
ELFIO::section *elfio_section_writer::section() noexcept
{
auto found = std::find(labels->cbegin(), labels->cend(), label);
return m_section;
}
if (found == labels->cend())
elfio_writer::elfio_writer(ELFIO::section *text, ELFIO::section *read_only,
ELFIO::symbol_section_accessor symbol_accessor, ELFIO::string_section_accessor string_accessor)
: text(text), read_only(elfio_section_writer(read_only)),
symbol_accessor(symbol_accessor), string_accessor(string_accessor)
{
}
std::size_t elfio_writer::sink(const std::string& label, const std::byte *data, std::size_t size)
{
auto offset = text->get_size();
text->append_data(reinterpret_cast<const char *>(data), size);
this->symbol_accessor.add_symbol(this->string_accessor, label.data(), offset, label.size(),
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text->get_index());
return text->get_size();
}
std::string_view elfio_writer::sink(const std::byte *data, std::size_t size)
{
auto offset = read_only.section()->get_size();
auto [result, inserted] = read_only(data, size);
if (inserted)
{
return -1;
this->symbol_accessor.add_symbol(this->string_accessor, result.data(), offset,
result.size(), ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0,
read_only.section()->get_index());
}
return std::distance(labels->cbegin(), found);
return result;
}
void elfio_writer::sink(const std::string& label)
{
this->symbol_accessor.add_symbol(this->string_accessor, "printf", 0x00000000, 0,
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
}
std::size_t elfio_writer::size() const
{
return this->text->get_size();
}
std::ptrdiff_t lookup(ELFIO::symbol_section_accessor symbol_accessor, const std::string& label)
{
for (ptrdiff_t j = 0; j < symbol_accessor.get_symbols_num(); ++j)
{
std::string name;
ELFIO::Elf64_Addr value;
ELFIO::Elf_Xword size;
unsigned char bind;
unsigned char type;
ELFIO::Elf_Half section_index;
unsigned char other;
symbol_accessor.get_symbol(j, name, value, size, bind, type, section_index, other);
if (name == label)
{
return j;
}
}
return -1;
}
void riscv32_elf(source::program *ast, const std::filesystem::path& out_file)
@ -135,46 +192,36 @@ namespace elna::riscv
ro_sec->set_flags(ELFIO::SHF_ALLOC);
ro_sec->set_addr_align(0x4);
elfio_writer text_writer{ text_sec };
elfio_writer read_only_writer{ ro_sec };
visitor _visitor{ text_writer, read_only_writer };
_visitor.visit(ast);
// Create symbol relocation table writers
ELFIO::symbol_section_accessor syma(writer, sym_sec);
ELFIO::relocation_section_accessor rela(writer, rel_sec);
ELFIO::Elf_Word digit_symbol;
auto _writer = std::make_shared<elfio_writer>(text_sec, ro_sec, syma, stra);
for (auto symbol : read_only_writer)
{
syma.add_symbol(stra, symbol.label.data(), 0x00000000,
symbol.label.size(), ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0, ro_sec->get_index());
}
ELFIO::Elf_Word printf_symbol = syma.add_symbol(stra, "printf", 0x00000000, 0,
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
visitor _visitor{ _writer };
_visitor.visit(ast);
syma.arrange_local_symbols();
for (auto symbol : text_writer)
{
syma.add_symbol(stra, symbol.label.data(), 0x00000000, symbol.label.size(),
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text_sec->get_index());
}
for (auto& reference : _visitor.references)
{
ELFIO::Elf_Word relocated_symbol{ 0 };
switch (reference.target)
{
case address_t::high20:
digit_symbol = read_only_writer.lookup(reference.name) + 1;
rela.add_entry(reference.offset, digit_symbol, 26 /* ELFIO::R_RISCV_HI20 */);
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
relocated_symbol = lookup(syma, reference.name);
rela.add_entry(reference.offset, relocated_symbol, 26 /* ELFIO::R_RISCV_HI20 */);
rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break;
case address_t::lower12i:
digit_symbol = read_only_writer.lookup(reference.name) + 1;
rela.add_entry(reference.offset, digit_symbol, 27 /* ELFIO::R_RISCV_LO12_I */);
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
relocated_symbol = lookup(syma, reference.name);
rela.add_entry(reference.offset, relocated_symbol, 27 /* ELFIO::R_RISCV_LO12_I */);
rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break;
case address_t::text:
rela.add_entry(reference.offset, printf_symbol, 18 /* ELFIO::R_RISCV_CALL */);
rela.add_entry(reference.offset, printf_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
relocated_symbol = lookup(syma, "printf");
rela.add_entry(reference.offset, relocated_symbol, 18 /* ELFIO::R_RISCV_CALL */);
rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break;
}
}

View File

@ -1,7 +1,6 @@
#pragma once
#include <cstdint>
#include <functional>
#include "elna/source/parser.hpp"
namespace elna::riscv
@ -160,8 +159,7 @@ namespace elna::riscv
class visitor final : public source::parser_visitor
{
std::function<void(const std::string&, const std::byte *, std::size_t)> write_text;
std::function<std::string_view(const std::byte *, std::size_t)> write_read_only;
std::shared_ptr<source::writer> writer;
public:
std::vector<instruction> instructions;
@ -170,12 +168,11 @@ namespace elna::riscv
std::vector<reference> references;
std::shared_ptr<source::symbol_table> table;
visitor(std::function<void(const std::string&, const std::byte *, std::size_t)> write_text,
std::function<std::string_view(const std::byte *, std::size_t)> write_read_only);
visitor(std::shared_ptr<source::writer> writer);
virtual void visit(source::declaration *declaration) override;
virtual void visit(source::definition *definition) override;
virtual void visit(source::bang_statement *statement) override;
virtual void visit(source::call_statement *statement) override;
virtual void visit(source::question_mark_statement *statement) override;
virtual void visit(source::compound_statement *statement) override;
virtual void visit(source::assign_statement *statement) override;

View File

@ -4,7 +4,10 @@
namespace elna::riscv
{
struct elfio_writer
/**
* Writer to a single label.
*/
struct elfio_section_writer
{
struct entry
{
@ -51,30 +54,50 @@ namespace elna::riscv
{
}
friend elfio_writer;
friend elfio_section_writer;
};
explicit elfio_writer(ELFIO::section *text);
explicit elfio_section_writer(ELFIO::section *section);
void operator()(const std::string& label, const std::byte *data, std::size_t size);
std::string_view operator()(const std::byte *data, std::size_t size);
std::pair<std::string_view, bool> operator()(const std::byte *data, std::size_t size);
iterator begin() const;
iterator end() const;
/**
* Searches the content by label and returns its index or -1 when the
* label does not exist.
*
* \param needle Label name.
* \return Data index.
*/
std::ptrdiff_t lookup(const std::string& label);
ELFIO::section *section() noexcept;
private:
std::shared_ptr<std::vector<std::string>> labels;
std::shared_ptr<std::vector<std::size_t>> sizes;
ELFIO::section *text;
std::vector<std::string> labels;
std::vector<std::size_t> sizes;
ELFIO::section *m_section;
};
class elfio_writer final : public source::writer
{
ELFIO::section *text;
elfio_section_writer read_only;
ELFIO::symbol_section_accessor symbol_accessor;
ELFIO::string_section_accessor string_accessor;
public:
elfio_writer(ELFIO::section *text, ELFIO::section *read_only, ELFIO::symbol_section_accessor symbol_accessor,
ELFIO::string_section_accessor string_accessor);
std::size_t sink(const std::string& label, const std::byte *data, std::size_t size) override;
std::string_view sink(const std::byte *data, std::size_t size) override;
void sink(const std::string& label) override;
std::size_t size() const override;
};
/**
* Searches the content by label and returns its index or -1 when the
* label does not exist.
*
* \param symbol_accessor Object accessor.
* \param needle Label name.
* \return Data index.
*/
std::ptrdiff_t lookup(ELFIO::symbol_section_accessor symbol_accessor, const std::string& label);
void riscv32_elf(source::program *ast, const std::filesystem::path& out_file);
}

View File

@ -62,7 +62,6 @@ namespace elna::source
semicolon,
left_paren,
right_paren,
bang,
dot,
comma,
factor_operator,
@ -110,7 +109,7 @@ namespace elna::source
private:
type m_type;
value m_value;
value m_value{};
elna::source::position m_position;
bool has_identifier() const noexcept;

View File

@ -16,7 +16,7 @@ namespace elna::source
class declaration;
class definition;
class bang_statement;
class call_statement;
class question_mark_statement;
class compound_statement;
class assign_statement;
@ -33,7 +33,7 @@ namespace elna::source
{
virtual void visit(declaration *) = 0;
virtual void visit(definition *) = 0;
virtual void visit(bang_statement *) = 0;
virtual void visit(call_statement *) = 0;
virtual void visit(question_mark_statement *) = 0;
virtual void visit(compound_statement *) = 0;
virtual void visit(assign_statement *) = 0;
@ -51,7 +51,7 @@ namespace elna::source
{
virtual void visit(declaration *declaration) override;
virtual void visit(definition *definition) override;
virtual void visit(bang_statement *statement) override;
virtual void visit(call_statement *statement) override;
virtual void visit(question_mark_statement *statement) override;
virtual void visit(compound_statement *statement) override;
virtual void visit(assign_statement *statement) override;
@ -116,15 +116,17 @@ namespace elna::source
integer_literal& body();
};
class bang_statement : public statement
class call_statement : public statement
{
std::string m_name;
std::unique_ptr<expression> m_body;
public:
bang_statement(std::unique_ptr<expression>&& body);
call_statement(const std::string& name, std::unique_ptr<expression>&& body);
virtual void accept(parser_visitor *visitor) override;
expression& body();
std::string& name() noexcept;
expression& arguments();
};
class question_mark_statement : public statement
@ -275,7 +277,7 @@ namespace elna::source
std::unique_ptr<definition> parse_definition();
std::unique_ptr<declaration> parse_declaration();
std::unique_ptr<statement> parse_statement();
std::unique_ptr<bang_statement> parse_bang_statement();
std::unique_ptr<call_statement> parse_call_statement();
std::unique_ptr<question_mark_statement> parse_question_mark_statement();
std::unique_ptr<compound_statement> parse_compound_statement();
std::unique_ptr<assign_statement> parse_assign_statement();

View File

@ -140,6 +140,9 @@ namespace elna::source
~variable_info() override;
};
/**
* Procedure information.
*/
class procedure_info final : public info
{
std::size_t local_stack_size{ 0 };
@ -151,6 +154,15 @@ namespace elna::source
std::size_t stack_size() const noexcept;
};
/**
* Intrinsic and external procedure information.
*/
class intrinsic_info final : public info
{
public:
~intrinsic_info() override;
};
/**
* Symbol table.
*/
@ -159,9 +171,43 @@ namespace elna::source
std::unordered_map<std::string, std::shared_ptr<info>> entries;
public:
symbol_table() = default;
symbol_table();
std::shared_ptr<info> lookup(const std::string& name);
void enter(const std::string& name, std::shared_ptr<info> entry);
};
struct writer
{
/**
* Writes data to the table and saves it under the given label.
*
* \param label Symbol name.
* \param data Data to be written.
* \param size Data size.
*
* \return New size of the table.
*/
virtual std::size_t sink(const std::string& label, const std::byte *data, std::size_t size) = 0;
/**
* Writes data and returns a label under that the data can be accessed.
*
* \param data Data to be written.
* \param size Data size.
*
* \return Label for the symbol.
*/
virtual std::string_view sink(const std::byte *data, std::size_t size) = 0;
/**
* Creates an external symbol.
*/
virtual void sink(const std::string& label) = 0;
/**
* \return Actual size of the text section.
*/
virtual std::size_t size() const = 0;
};
}

View File

@ -108,20 +108,18 @@ namespace elna::source
}
token::token(const token& that)
: m_type(that.of()), m_position(that.position())
{
*this = that;
}
token::token(token&& that)
: m_type(that.of()), m_position(that.position())
{
*this = std::move(that);
}
token::~token()
{
if (m_type == type::identifier || m_type == type::term_operator || m_type == type::factor_operator)
if (has_identifier())
{
m_value.identifier.~basic_string();
}
@ -129,11 +127,15 @@ namespace elna::source
token& token::operator=(const token& that)
{
if (has_identifier())
{
m_value.identifier.~basic_string();
}
m_type = that.of();
m_position = that.position();
if (that.has_identifier())
{
m_value.identifier = that.identifier();
new((void *) &m_value.identifier) std::string(that.identifier());
}
else if (that.is_numeric())
{
@ -148,11 +150,15 @@ namespace elna::source
token& token::operator=(token&& that)
{
if (has_identifier())
{
m_value.identifier.~basic_string();
}
m_type = that.of();
m_position = that.position();
if (that.has_identifier())
{
m_value.identifier = std::move(that.identifier());
new((void *) &m_value.identifier) std::string(std::move(that.identifier()));
}
else if (that.is_numeric())
{
@ -344,10 +350,6 @@ namespace elna::source
{
tokens.emplace_back(token::type::comma, iterator.position());
}
else if (*iterator == '!')
{
tokens.emplace_back(token::type::bang, iterator.position());
}
else if (*iterator == '?')
{
tokens.emplace_back(token::type::question_mark, iterator.position());

View File

@ -12,9 +12,9 @@ namespace elna::source
definition->body().accept(this);
}
void empty_visitor::visit(bang_statement *statement)
void empty_visitor::visit(call_statement *statement)
{
statement->body().accept(this);
statement->arguments().accept(this);
}
void empty_visitor::visit(question_mark_statement *statement)
@ -259,17 +259,22 @@ namespace elna::source
return m_operator;
}
bang_statement::bang_statement(std::unique_ptr<expression>&& body)
: m_body(std::move(body))
call_statement::call_statement(const std::string& name, std::unique_ptr<expression>&& body)
: m_name(name), m_body(std::move(body))
{
}
void bang_statement::accept(parser_visitor *visitor)
void call_statement::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
expression& bang_statement::body()
std::string& call_statement::name() noexcept
{
return m_name;
}
expression& call_statement::arguments()
{
return *m_body;
}
@ -510,9 +515,9 @@ namespace elna::source
{
return parse_assign_statement();
}
else if (iterator.current(token::type::bang))
else if (iterator.current(token::type::identifier) && iterator.look_ahead(token::type::left_paren))
{
return parse_bang_statement();
return parse_call_statement();
}
else if (iterator.current(token::type::question_mark))
{
@ -534,17 +539,18 @@ namespace elna::source
return nullptr;
}
std::unique_ptr<bang_statement> parser::parse_bang_statement()
std::unique_ptr<call_statement> parser::parse_call_statement()
{
if (!iterator.advance(token::type::bang))
auto function_name = iterator.advance(token::type::identifier);
if (function_name.has_value() && !iterator.skip(token::type::left_paren))
{
return nullptr;
}
auto bang_body = parse_expression();
if (bang_body != nullptr)
if (bang_body != nullptr && iterator.skip(token::type::right_paren))
{
return std::make_unique<bang_statement>(std::move(bang_body));
return std::make_unique<call_statement>(function_name->get().identifier(), std::move(bang_body));
}
return nullptr;
}

View File

@ -27,6 +27,12 @@ namespace elna::source
return "Name '" + name + "' was already defined";
}
symbol_table::symbol_table()
{
enter("writei", std::make_shared<intrinsic_info>());
enter("writeb", std::make_shared<intrinsic_info>());
}
std::shared_ptr<info> symbol_table::lookup(const std::string& name)
{
auto entry = entries.find(name);
@ -84,4 +90,8 @@ namespace elna::source
{
this->local_stack_size = size;
}
intrinsic_info::~intrinsic_info()
{
}
}

View File

@ -1,2 +1,2 @@
! 3 + 4 + 5 + 1 + 2 + 4 + 3
writei(3 + 4 + 5 + 1 + 2 + 4 + 3)
.

View File

@ -1,3 +1,3 @@
const a = 1, b = 2;
! a + b
writei(a + b)
.

View File

@ -1,5 +1,5 @@
var x: Int;
begin
x := 5;
! x
writei(x)
end.

View File

@ -1 +1 @@
! 6 / 3
writei(6 / 3)

View File

@ -1,9 +1,9 @@
begin
if True then ! 8;
if False then
begin
! 5;
! 5;
! 5
end
if True then writei(8);
if False then
begin
writei(5);
writei(5);
writei(5)
end
end.

View File

@ -1,2 +1,2 @@
! (3 + 4) + 1
writei((3 + 4) + 1)
.

View File

@ -1,4 +1,4 @@
begin
! 5;
! 7
writei(5);
writei(7)
end.

View File

@ -1,2 +1,2 @@
! 5 * 2
writei(5 * 2)
.

View File

@ -1,2 +1,2 @@
! 3 * 4 * 2
writei(3 * 4 * 2)
.

View File

@ -1,2 +1,2 @@
! 2 + 3 - 1
writei(2 + 3 - 1)
.

View File

@ -1,2 +1,2 @@
! 3
writei(3)
.

View File

@ -1,2 +1,2 @@
! 5 - 4
write(5 - 4)
.

View File

@ -1,2 +1,2 @@
! 1 + 7
writei(1 + 7)
.

View File

@ -1,2 +1,2 @@
! 1 + (3 + 4)
writei(1 + (3 + 4))
.