Implement simple if conditions
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include "elna/source/parser.hpp"
|
||||
|
||||
namespace elna::riscv
|
||||
@ -144,9 +146,11 @@ namespace elna::riscv
|
||||
instruction(base_opcode opcode);
|
||||
|
||||
instruction& i(x_register rd, funct3_t funct3, x_register rs1, std::uint32_t immediate);
|
||||
instruction& s(std::uint32_t imm1, funct3_t funct3, x_register rs1, x_register rs2);
|
||||
instruction& s(std::uint32_t imm, funct3_t funct3, x_register rs1, x_register rs2);
|
||||
instruction& b(std::uint32_t imm, funct3_t funct3, x_register rs1, x_register rs2);
|
||||
instruction& r(x_register rd, funct3_t funct3, x_register rs1, x_register rs2, funct7_t funct7 = funct7_t::none);
|
||||
instruction& u(x_register rd, std::uint32_t imm);
|
||||
instruction& j(x_register rd, std::uint32_t imm);
|
||||
|
||||
const std::byte *cbegin() const;
|
||||
const std::byte *cend() const;
|
||||
@ -155,6 +159,160 @@ namespace elna::riscv
|
||||
std::uint32_t representation{ 0 };
|
||||
};
|
||||
|
||||
/**
|
||||
* Assigns sequentially numbered labels to text strings.
|
||||
*/
|
||||
template<char... prefix>
|
||||
struct read_only_table
|
||||
{
|
||||
private:
|
||||
constexpr static const char get_prefix[] = { prefix... };
|
||||
constexpr static const std::size_t prefix_length = sizeof(get_prefix) / sizeof(char);
|
||||
|
||||
public:
|
||||
/**
|
||||
* An iterator over label and string pairs.
|
||||
*/
|
||||
struct const_iterator
|
||||
{
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = std::pair<std::string, std::string_view>;
|
||||
using pointer = const value_type *;
|
||||
using reference = const value_type&;
|
||||
|
||||
reference operator*() const noexcept
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
|
||||
pointer operator->() const noexcept
|
||||
{
|
||||
return &payload;
|
||||
}
|
||||
|
||||
const_iterator& operator++()
|
||||
{
|
||||
++index;
|
||||
++iterator;
|
||||
this->payload = std::pair<const std::string, std::string_view>(label(), *iterator);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator& operator++(int)
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const const_iterator& that) const
|
||||
{
|
||||
return this->index == that.index;
|
||||
}
|
||||
|
||||
bool operator!=(const const_iterator& that) const
|
||||
{
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::string>::const_iterator iterator;
|
||||
std::size_t index;
|
||||
value_type payload;
|
||||
|
||||
const_iterator(std::size_t index, std::vector<std::string>::const_iterator iterator)
|
||||
: iterator(iterator), index(index), payload({ label(), *iterator })
|
||||
{
|
||||
}
|
||||
|
||||
const_iterator(std::size_t index, std::vector<std::string>::const_iterator iterator,
|
||||
std::string_view value)
|
||||
: iterator(iterator), index(index), payload({ get_prefix, value })
|
||||
{
|
||||
}
|
||||
|
||||
std::string label() const
|
||||
{
|
||||
return get_prefix + std::to_string(this->index);
|
||||
}
|
||||
|
||||
friend read_only_table;
|
||||
};
|
||||
|
||||
const_iterator begin()
|
||||
{
|
||||
if (payload.empty())
|
||||
{
|
||||
return end();
|
||||
}
|
||||
else
|
||||
{
|
||||
return read_only_table::const_iterator(0, payload.cbegin());
|
||||
}
|
||||
}
|
||||
|
||||
const_iterator end() const
|
||||
{
|
||||
return read_only_table::const_iterator(size(), payload.cend(), "");
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return payload.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up \a needle in the string storage and returns a label for it
|
||||
* or create a new one.
|
||||
*
|
||||
* \param needle A string to search for.
|
||||
* \return Label name.
|
||||
*/
|
||||
const_iterator label(std::string_view needle)
|
||||
{
|
||||
auto format_string = std::find(this->payload.cbegin(), this->payload.cend(), needle);
|
||||
|
||||
if (format_string == this->payload.cend())
|
||||
{
|
||||
format_string = this->payload.emplace(format_string, needle);
|
||||
}
|
||||
auto read_only_index = std::distance(this->payload.cbegin(), format_string);
|
||||
|
||||
return read_only_table::const_iterator(read_only_index, format_string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(std::string_view needle)
|
||||
{
|
||||
if (needle.size() <= prefix_length)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
auto needle_middle = needle.cbegin() + prefix_length;
|
||||
auto needle_prefix = std::string_view(needle.cbegin(), prefix_length);
|
||||
|
||||
std::size_t counter;
|
||||
auto [position, char_error] = std::from_chars(needle_middle, needle.cend(), counter);
|
||||
if (char_error != std::errc{} || position != needle.cend()
|
||||
|| needle_prefix != get_prefix || counter >= size())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::string> payload;
|
||||
};
|
||||
|
||||
class visitor final : public source::parser_visitor
|
||||
{
|
||||
public:
|
||||
@ -163,15 +321,20 @@ namespace elna::riscv
|
||||
std::uint32_t variable_counter = 1;
|
||||
std::vector<reference> references;
|
||||
std::shared_ptr<source::symbol_table> table;
|
||||
read_only_table<'.', 'C', 'L'> read_only;
|
||||
|
||||
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::question_mark_statement *statement) override;
|
||||
virtual void visit(source::compound_statement *statement) override;
|
||||
virtual void visit(source::assign_statement *statement) override;
|
||||
virtual void visit(source::if_statement *statement) override;
|
||||
virtual void visit(source::while_statement *statement) override;
|
||||
virtual void visit(source::block *block) override;
|
||||
virtual void visit(source::variable_expression *variable) override;
|
||||
virtual void visit(source::integer_literal *number) override;
|
||||
virtual void visit(source::binary_expression *expression) override;
|
||||
virtual void visit(source::integer_literal *number) override;
|
||||
virtual void visit(source::boolean_literal *number) override;
|
||||
};
|
||||
}
|
||||
|
@ -53,6 +53,7 @@ namespace elna::source
|
||||
enum class type : std::uint16_t
|
||||
{
|
||||
number,
|
||||
boolean,
|
||||
term_operator,
|
||||
let,
|
||||
identifier,
|
||||
@ -68,7 +69,13 @@ namespace elna::source
|
||||
eof,
|
||||
begin,
|
||||
end,
|
||||
assignment
|
||||
assignment,
|
||||
colon,
|
||||
question_mark,
|
||||
when,
|
||||
then,
|
||||
_while,
|
||||
_do
|
||||
};
|
||||
|
||||
/**
|
||||
@ -107,6 +114,7 @@ namespace elna::source
|
||||
elna::source::position m_position;
|
||||
|
||||
bool has_identifier() const noexcept;
|
||||
bool is_numeric() const noexcept;
|
||||
};
|
||||
|
||||
class unexpected_character final : public error
|
||||
|
@ -17,24 +17,32 @@ namespace elna::source
|
||||
class declaration;
|
||||
class definition;
|
||||
class bang_statement;
|
||||
class question_mark_statement;
|
||||
class compound_statement;
|
||||
class assign_statement;
|
||||
class if_statement;
|
||||
class while_statement;
|
||||
class block;
|
||||
class binary_expression;
|
||||
class variable_expression;
|
||||
class integer_literal;
|
||||
class boolean_literal;
|
||||
|
||||
struct parser_visitor
|
||||
{
|
||||
virtual void visit(declaration *) = 0;
|
||||
virtual void visit(definition *) = 0;
|
||||
virtual void visit(bang_statement *) = 0;
|
||||
virtual void visit(question_mark_statement *) = 0;
|
||||
virtual void visit(compound_statement *) = 0;
|
||||
virtual void visit(assign_statement *) = 0;
|
||||
virtual void visit(if_statement *) = 0;
|
||||
virtual void visit(while_statement *) = 0;
|
||||
virtual void visit(block *) = 0;
|
||||
virtual void visit(binary_expression *) = 0;
|
||||
virtual void visit(variable_expression *) = 0;
|
||||
virtual void visit(integer_literal *) = 0;
|
||||
virtual void visit(boolean_literal *) = 0;
|
||||
};
|
||||
|
||||
struct empty_visitor : parser_visitor
|
||||
@ -42,12 +50,16 @@ 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(question_mark_statement *statement) override;
|
||||
virtual void visit(compound_statement *statement) override;
|
||||
virtual void visit(assign_statement *statement) override;
|
||||
virtual void visit(if_statement *) override;
|
||||
virtual void visit(while_statement *) override;
|
||||
virtual void visit(block *block) override;
|
||||
virtual void visit(binary_expression *expression) override;
|
||||
virtual void visit(variable_expression *variable) override;
|
||||
virtual void visit(integer_literal *number) override;
|
||||
virtual void visit(boolean_literal *boolean) override;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -67,6 +79,10 @@ namespace elna::source
|
||||
{
|
||||
};
|
||||
|
||||
class condition : public node
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* Variable declaration.
|
||||
*/
|
||||
@ -108,6 +124,17 @@ namespace elna::source
|
||||
expression& body();
|
||||
};
|
||||
|
||||
class question_mark_statement : public statement
|
||||
{
|
||||
std::unique_ptr<condition> m_body;
|
||||
|
||||
public:
|
||||
question_mark_statement(std::unique_ptr<condition>&& body);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
|
||||
condition& body();
|
||||
};
|
||||
|
||||
class compound_statement : public statement
|
||||
{
|
||||
std::vector<std::unique_ptr<statement>> m_statements;
|
||||
@ -133,6 +160,32 @@ namespace elna::source
|
||||
expression& rvalue();
|
||||
};
|
||||
|
||||
class if_statement : public statement
|
||||
{
|
||||
std::unique_ptr<condition> m_prerequisite;
|
||||
std::unique_ptr<statement> m_body;
|
||||
|
||||
public:
|
||||
if_statement(std::unique_ptr<condition>&& prerequisite, std::unique_ptr<statement>&& body);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
|
||||
condition& prerequisite();
|
||||
statement& body();
|
||||
};
|
||||
|
||||
class while_statement : public statement
|
||||
{
|
||||
std::unique_ptr<condition> m_prerequisite;
|
||||
std::unique_ptr<statement> m_body;
|
||||
|
||||
public:
|
||||
while_statement(std::unique_ptr<condition>&& prerequisite, std::unique_ptr<statement>&& body);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
|
||||
condition& prerequisite();
|
||||
statement& body();
|
||||
};
|
||||
|
||||
/**
|
||||
* Block.
|
||||
*/
|
||||
@ -166,6 +219,17 @@ namespace elna::source
|
||||
std::int32_t number() const noexcept;
|
||||
};
|
||||
|
||||
class boolean_literal : public condition
|
||||
{
|
||||
bool m_boolean;
|
||||
|
||||
public:
|
||||
boolean_literal(const bool value);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
|
||||
bool boolean() const noexcept;
|
||||
};
|
||||
|
||||
class variable_expression : public expression
|
||||
{
|
||||
std::string m_name;
|
||||
@ -198,12 +262,16 @@ namespace elna::source
|
||||
std::unique_ptr<expression> parse_factor();
|
||||
std::unique_ptr<expression> parse_term();
|
||||
std::unique_ptr<expression> parse_expression();
|
||||
std::unique_ptr<condition> parse_condition();
|
||||
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<question_mark_statement> parse_question_mark_statement();
|
||||
std::unique_ptr<compound_statement> parse_compound_statement();
|
||||
std::unique_ptr<assign_statement> parse_assign_statement();
|
||||
std::unique_ptr<if_statement> parse_if_statement();
|
||||
std::unique_ptr<while_statement> parse_while_statement();
|
||||
std::vector<std::unique_ptr<definition>> parse_definitions();
|
||||
std::vector<std::unique_ptr<declaration>> parse_declarations();
|
||||
std::unique_ptr<block> parse_block();
|
||||
|
Reference in New Issue
Block a user