From 263b868a4582e2d0f1f5d529c030084b8b8282f8 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 19 Mar 2024 09:35:50 +0100 Subject: [PATCH] Replace ! with a function call writei --- TODO | 2 - backend/riscv.cpp | 86 +++++++++++------- backend/target.cpp | 155 +++++++++++++++++++++----------- include/elna/backend/riscv.hpp | 9 +- include/elna/backend/target.hpp | 53 +++++++---- include/elna/source/lexer.hpp | 3 +- include/elna/source/parser.hpp | 16 ++-- include/elna/source/result.hpp | 48 +++++++++- source/lexer.cpp | 20 +++-- source/parser.cpp | 30 ++++--- source/result.cpp | 10 +++ tests/7_member_sum.eln | 2 +- tests/const_list.eln | 2 +- tests/declare_variable.eln | 2 +- tests/divide.eln | 2 +- tests/if_condition.eln | 14 +-- tests/left_nested_sum.eln | 2 +- tests/multiline_output.eln | 4 +- tests/multiply.eln | 2 +- tests/multiply_3.eln | 2 +- tests/plus_minus.eln | 2 +- tests/print_number.eln | 2 +- tests/subtraction.eln | 2 +- tests/sum.eln | 2 +- tests/sums.eln | 2 +- 25 files changed, 315 insertions(+), 159 deletions(-) diff --git a/TODO b/TODO index c34f3a9..1b08cbb 100644 --- a/TODO +++ b/TODO @@ -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. diff --git a/backend/riscv.cpp b/backend/riscv.cpp index 8a871ae..604aa2b 100644 --- a/backend/riscv.cpp +++ b/backend/riscv.cpp @@ -76,9 +76,8 @@ namespace elna::riscv return reinterpret_cast(&this->representation) + sizeof(this->representation); } - visitor::visitor(std::function write_text, - std::function write_read_only) - : write_text(write_text), write_read_only(write_read_only) + visitor::visitor(std::shared_ptr 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("%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(this->instructions.data()), + this->instructions.size() * sizeof(instruction)); + + this->instructions.clear(); visit(dynamic_cast(program)); - write_text("main", reinterpret_cast(this->instructions.data()), + this->writer->sink("main", reinterpret_cast(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("%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("%d\n\0"), 4); + auto format_string = this->writer->sink(reinterpret_cast("%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) diff --git a/backend/target.cpp b/backend/target.cpp index 6b46b25..da9ddf1 100644 --- a/backend/target.cpp +++ b/backend/target.cpp @@ -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>()), - sizes(std::make_shared>()) + 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(data), size); + labels.push_back(label); + sizes.push_back(size); + m_section->append_data(reinterpret_cast(data), size); } - std::string_view elfio_writer::operator()(const std::byte *data, std::size_t size) + std::pair 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(labels.back(), true); } - return found->label; + return std::pair(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(text->get_data())); + return elfio_section_writer::iterator(labels.cbegin(), sizes.cbegin(), + reinterpret_cast(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(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(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; } } diff --git a/include/elna/backend/riscv.hpp b/include/elna/backend/riscv.hpp index 80ff833..d67c3af 100644 --- a/include/elna/backend/riscv.hpp +++ b/include/elna/backend/riscv.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "elna/source/parser.hpp" namespace elna::riscv @@ -160,8 +159,7 @@ namespace elna::riscv class visitor final : public source::parser_visitor { - std::function write_text; - std::function write_read_only; + std::shared_ptr writer; public: std::vector instructions; @@ -170,12 +168,11 @@ namespace elna::riscv std::vector references; std::shared_ptr table; - visitor(std::function write_text, - std::function write_read_only); + visitor(std::shared_ptr 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; diff --git a/include/elna/backend/target.hpp b/include/elna/backend/target.hpp index 72f60ac..648595f 100644 --- a/include/elna/backend/target.hpp +++ b/include/elna/backend/target.hpp @@ -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 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> labels; - std::shared_ptr> sizes; - ELFIO::section *text; + std::vector labels; + std::vector 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); } diff --git a/include/elna/source/lexer.hpp b/include/elna/source/lexer.hpp index e72e666..e250f6e 100644 --- a/include/elna/source/lexer.hpp +++ b/include/elna/source/lexer.hpp @@ -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; diff --git a/include/elna/source/parser.hpp b/include/elna/source/parser.hpp index 5697191..615d52d 100644 --- a/include/elna/source/parser.hpp +++ b/include/elna/source/parser.hpp @@ -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 m_body; public: - bang_statement(std::unique_ptr&& body); + call_statement(const std::string& name, std::unique_ptr&& 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 parse_definition(); std::unique_ptr parse_declaration(); std::unique_ptr parse_statement(); - std::unique_ptr parse_bang_statement(); + std::unique_ptr parse_call_statement(); std::unique_ptr parse_question_mark_statement(); std::unique_ptr parse_compound_statement(); std::unique_ptr parse_assign_statement(); diff --git a/include/elna/source/result.hpp b/include/elna/source/result.hpp index 8cb3513..b19feb9 100644 --- a/include/elna/source/result.hpp +++ b/include/elna/source/result.hpp @@ -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> entries; public: - symbol_table() = default; + symbol_table(); std::shared_ptr lookup(const std::string& name); void enter(const std::string& name, std::shared_ptr 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; + }; } diff --git a/source/lexer.cpp b/source/lexer.cpp index e642e6a..a1f7b6b 100644 --- a/source/lexer.cpp +++ b/source/lexer.cpp @@ -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()); diff --git a/source/parser.cpp b/source/parser.cpp index ebf089e..fadf5e1 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -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&& body) - : m_body(std::move(body)) + call_statement::call_statement(const std::string& name, std::unique_ptr&& 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 parser::parse_bang_statement() + std::unique_ptr 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(std::move(bang_body)); + return std::make_unique(function_name->get().identifier(), std::move(bang_body)); } return nullptr; } diff --git a/source/result.cpp b/source/result.cpp index 0d989d1..397233e 100644 --- a/source/result.cpp +++ b/source/result.cpp @@ -27,6 +27,12 @@ namespace elna::source return "Name '" + name + "' was already defined"; } + symbol_table::symbol_table() + { + enter("writei", std::make_shared()); + enter("writeb", std::make_shared()); + } + std::shared_ptr 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() + { + } } diff --git a/tests/7_member_sum.eln b/tests/7_member_sum.eln index 287c06f..8c56a1b 100644 --- a/tests/7_member_sum.eln +++ b/tests/7_member_sum.eln @@ -1,2 +1,2 @@ -! 3 + 4 + 5 + 1 + 2 + 4 + 3 +writei(3 + 4 + 5 + 1 + 2 + 4 + 3) . diff --git a/tests/const_list.eln b/tests/const_list.eln index 375627f..e65a53b 100644 --- a/tests/const_list.eln +++ b/tests/const_list.eln @@ -1,3 +1,3 @@ const a = 1, b = 2; -! a + b +writei(a + b) . diff --git a/tests/declare_variable.eln b/tests/declare_variable.eln index 3fb05d1..24d7359 100644 --- a/tests/declare_variable.eln +++ b/tests/declare_variable.eln @@ -1,5 +1,5 @@ var x: Int; begin x := 5; - ! x + writei(x) end. diff --git a/tests/divide.eln b/tests/divide.eln index cc7173a..2a1343b 100644 --- a/tests/divide.eln +++ b/tests/divide.eln @@ -1 +1 @@ -! 6 / 3 +writei(6 / 3) diff --git a/tests/if_condition.eln b/tests/if_condition.eln index 2c0669e..51cccd0 100644 --- a/tests/if_condition.eln +++ b/tests/if_condition.eln @@ -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. diff --git a/tests/left_nested_sum.eln b/tests/left_nested_sum.eln index aae2a77..cbec749 100644 --- a/tests/left_nested_sum.eln +++ b/tests/left_nested_sum.eln @@ -1,2 +1,2 @@ -! (3 + 4) + 1 +writei((3 + 4) + 1) . diff --git a/tests/multiline_output.eln b/tests/multiline_output.eln index d7a381f..30ddf27 100644 --- a/tests/multiline_output.eln +++ b/tests/multiline_output.eln @@ -1,4 +1,4 @@ begin - ! 5; - ! 7 + writei(5); + writei(7) end. diff --git a/tests/multiply.eln b/tests/multiply.eln index 1161c66..08ff32b 100644 --- a/tests/multiply.eln +++ b/tests/multiply.eln @@ -1,2 +1,2 @@ -! 5 * 2 +writei(5 * 2) . diff --git a/tests/multiply_3.eln b/tests/multiply_3.eln index e488b8f..6b93f7c 100644 --- a/tests/multiply_3.eln +++ b/tests/multiply_3.eln @@ -1,2 +1,2 @@ -! 3 * 4 * 2 +writei(3 * 4 * 2) . diff --git a/tests/plus_minus.eln b/tests/plus_minus.eln index 06bd367..5c27988 100644 --- a/tests/plus_minus.eln +++ b/tests/plus_minus.eln @@ -1,2 +1,2 @@ -! 2 + 3 - 1 +writei(2 + 3 - 1) . diff --git a/tests/print_number.eln b/tests/print_number.eln index 51b5ca0..cab7751 100644 --- a/tests/print_number.eln +++ b/tests/print_number.eln @@ -1,2 +1,2 @@ -! 3 +writei(3) . diff --git a/tests/subtraction.eln b/tests/subtraction.eln index 00917c6..cfad17e 100644 --- a/tests/subtraction.eln +++ b/tests/subtraction.eln @@ -1,2 +1,2 @@ -! 5 - 4 +write(5 - 4) . diff --git a/tests/sum.eln b/tests/sum.eln index 6b4e720..19bcd0b 100644 --- a/tests/sum.eln +++ b/tests/sum.eln @@ -1,2 +1,2 @@ -! 1 + 7 +writei(1 + 7) . diff --git a/tests/sums.eln b/tests/sums.eln index 59586e9..924c0aa 100644 --- a/tests/sums.eln +++ b/tests/sums.eln @@ -1,2 +1,2 @@ -! 1 + (3 + 4) +writei(1 + (3 + 4)) .