Split result into symboltable and types

This commit is contained in:
2024-05-01 10:39:18 +02:00
parent aa5579f234
commit 052dda78f8
21 changed files with 400 additions and 1258 deletions

View File

@ -1,28 +0,0 @@
#pragma once
#include <boost/core/noncopyable.hpp>
#include <string>
#include <list>
namespace elna
{
struct editor_history : public boost::noncopyable
{
using const_iterator = std::list<std::string>::const_iterator;
editor_history();
void push(const std::string& entry);
void clear();
const_iterator next() noexcept;
const_iterator prev() noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_iterator current() const noexcept;
private:
std::list<std::string> commands;
std::list<std::string>::const_iterator current_pointer;
};
}

View File

@ -1,89 +0,0 @@
#pragma once
#include <boost/core/noncopyable.hpp>
#include <stdexcept>
#include <string>
#include <optional>
#include "elna/shell/state.hpp"
#include "elna/shell/history.hpp"
#define BOOST_PROCESS_USE_STD_FS
namespace elna
{
/**
* Runtime exception for non recoverable errors.
*/
struct interactive_exception : public std::runtime_error
{
/**
* Constructor.
*/
interactive_exception();
};
/**
* Main loop.
*/
void loop();
/**
* Reads the next line.
* Returns no value upon receiving end of file.
*
* \param history Shell history.
* \return The read line.
*/
std::optional<editor_state> read_line(editor_history& history);
/**
* Runs a built-in or a command.
*
* \param line The command and arguments.
* \return Whether the input shoud continued (no exit requested).
*/
bool execute(const std::string& line);
/**
* Runs the binary specified in its argument.
*
* \param program Program name in PATH.
*/
void launch(const std::string& program);
/**
* Enables the raw mode.
*
* \return Whether the operation was successful.
*/
bool enable_raw_mode();
/**
* Disables the raw mode.
*/
void disable_raw_mode();
/**
* Reads a key.
*
* \return Read character.
*/
key read_key();
/**
* Calls autocompletion.
*
* \param User input.
* \param Cursor position in the user input.
* \return Selected item.
*/
std::string complete(const std::string& input, const std::size_t position);
/**
* Prints the message from the exception.
*
* \param exception The exception to print.
*/
void print_exception(const std::exception& exception);
}

View File

@ -1,170 +0,0 @@
#include <cstdint>
#include <cstring>
#include <string>
#include <variant>
namespace elna
{
constexpr const char *erase_line = "\x1b[2K";
constexpr const char *start_kitty_keybaord = "\x1b[>1u";
constexpr const char *end_kitty_keybaord = "\x1b[<u";
std::string cursor_to_column(const std::uint16_t position = 1);
enum class special_key
{
arrow_left,
arrow_right,
arrow_up,
arrow_down,
page_up,
page_down,
home_key,
end_key,
};
/**
* Key modifiers.
*/
enum class modifier : std::uint8_t
{
shift = 0b1, ///< 1
alt = 0b10, ///< 2
ctrl = 0b100, ///< 4
super = 0b1000, ///< 8
hyper = 0b10000, ///< 16
meta = 0b100000, ///< 32
caps_lock = 0b1000000, ///< 64
num_lock = 0b10000000, ///< 128
};
struct key
{
using char_t = std::uint32_t;
key();
explicit key(const char_t c, const std::uint8_t modifiers = 0);
explicit key(const char_t c, const modifier modifiers);
explicit key(const special_key control, const std::uint8_t modifiers = 0);
explicit key(const special_key control, const modifier modifiers);
bool operator==(const char_t c) const;
friend bool operator==(const char_t c, const key& that);
bool operator==(const special_key control) const;
friend bool operator==(const special_key control, const key& that);
bool operator==(const key& that) const;
bool operator!=(const char_t c) const;
friend bool operator!=(const char_t c, const key& that);
bool operator!=(const special_key control) const;
friend bool operator!=(const special_key control, const key& that);
bool operator!=(const key& that) const;
char_t character() const;
special_key control() const;
bool modified(const std::uint8_t modifiers) const noexcept;
bool modified(const modifier modifiers) const noexcept;
bool is_character() const noexcept;
bool is_control() const noexcept;
bool empty() const noexcept;
private:
std::variant<special_key, char_t> store;
std::uint8_t modifiers;
};
/**
* The action should be performed after updating the editor state.
*/
enum class action
{
redraw, ///< Redraw everything.
write, ///< Write a single character.
finalize, ///< Finalize command input.
move, ///< Move the cursor.
ignore, ///< Do nothing.
complete, ///< Complete the input.
history, ///< Navigate the history.
};
/**
* The line editor with its state.
*/
class editor_state
{
std::string input;
std::string prompt = "> ";
std::size_t position{ 1 };
public:
editor_state();
/**
* Returns the current input gathered by the lin editor.
*
* \return User input.
*/
const std::string& command_line() const noexcept;
/**
* Processes the next keypress by changing the line editor state.
*
* \param key Pressed key.
* \return Action to do after the key was pressed.
*/
action process_keypress(const key& press);
/**
* Clears editors working area and writes drawing sequences into the
* buffer.
*
* \param T Output range for bytes.
* \param output Output buffer.
*/
template<typename T>
void draw(T output) const
{
std::string code;
std::copy(erase_line, erase_line + strlen(erase_line), output);
code = cursor_to_column();
std::copy(std::cbegin(code), std::cend(code), output);
std::copy(std::cbegin(this->prompt), std::cend(this->prompt), output);
std::copy(std::cbegin(this->input), std::cend(this->input), output);
code = cursor_to_column(absolute_position());
std::copy(std::cbegin(code), std::cend(code), output);
}
/**
* Computes the position in the terminal line.
*
* \return Position in the terminal line.
*/
std::size_t absolute_position() const noexcept;
/**
* Cursor position relative to the start of the user input.
*
* \returns Cursor position.
*/
std::size_t relative_position() const noexcept;
/**
* Inserts text at cursor position.
*
* \param text Text to insert.
*/
void insert(const std::string& text);
void insert(const key::char_t text);
/**
* Replaces the user input with the given string.
*
* \param text New user input string.
*/
void load(const std::string& text);
};
}

View File

@ -1,6 +1,8 @@
#pragma once
#include <unordered_map>
#include "elna/source/parser.hpp"
#include "elna/source/symbol_table.hpp"
namespace elna::source
{

View File

@ -1,15 +1,12 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <variant>
#include <list>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>
namespace elna::source
{
@ -126,181 +123,6 @@ namespace elna::source
std::string what() const override;
};
class symbol_table;
/**
* Type representation.
*/
struct type
{
const std::size_t byte_size;
protected:
explicit type(const std::size_t byte_size);
};
/**
* Built-in type representation.
*/
struct primitive_type : public type
{
const std::string type_name;
primitive_type(const std::string& type_name, const std::size_t byte_size);
};
/**
* Typed pointer.
*/
struct pointer_type : public type
{
std::shared_ptr<const type> base_type;
pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size);
};
inline const primitive_type boolean_type{ "Boolean", 1 };
inline const primitive_type int_type{ "Int", 4 };
/**
* Generic language entity information.
*/
class info
{
public:
virtual ~info() = 0;
protected:
info();
};
/**
* Type information.
*/
class type_info final : public info
{
std::shared_ptr<class type> m_type;
public:
explicit type_info(const class type& type);
~type_info() override;
std::shared_ptr<const class type> type() const noexcept;
};
/**
* Constant information.
*/
class constant_info final : public info
{
std::int32_t m_value;
public:
constant_info(const std::int32_t value);
~constant_info() override;
std::int32_t value() const noexcept;
};
/**
* Variable information.
*/
class variable_info final : public info
{
std::shared_ptr<const class type> m_type;
public:
std::ptrdiff_t offset{ 0 };
explicit variable_info(std::shared_ptr<const class type> type);
~variable_info() override;
std::shared_ptr<const class type> type() noexcept;
};
/**
* Procedure parameter information.
*/
class parameter_info final : public info
{
std::shared_ptr<const class type> m_type;
public:
std::ptrdiff_t offset{ 0 };
explicit parameter_info(std::shared_ptr<const class type> type);
~parameter_info() override;
std::shared_ptr<const class type> type() const noexcept;
};
/**
* Intrinsic and external procedure information.
*/
class intrinsic_info : public info
{
public:
std::vector<parameter_info> parameter_infos;
~intrinsic_info() override;
std::size_t parameter_stack_size() const noexcept;
};
/**
* Procedure information.
*/
class procedure_info final : public intrinsic_info
{
std::shared_ptr<symbol_table> local_table;
public:
std::size_t local_stack_size{ 0 };
std::size_t argument_stack_size{ 0 };
explicit procedure_info(std::shared_ptr<symbol_table> outer_scope);
~procedure_info() override;
std::shared_ptr<symbol_table> scope();
std::size_t stack_size() const noexcept;
};
/**
* Symbol table.
*/
class symbol_table
{
std::unordered_map<std::string, std::shared_ptr<info>> entries;
std::shared_ptr<symbol_table> outer_scope;
public:
/**
* Constructs a new symbol with an optional outer scope.
*
* \param scope Outer scope.
*/
explicit symbol_table(std::shared_ptr<symbol_table> scope = nullptr);
/**
* Looks for symbol in the table by name. Returns nullptr if the symbol
* can not be found.
*
* \param name Symbol name.
* \return Symbol from the table if found.
*/
std::shared_ptr<info> lookup(const std::string& name);
/**
* Registers new symbol.
*
* \param name Symbol name.
* \param entry Symbol information.
*/
void enter(const std::string& name, std::shared_ptr<info> entry);
/**
* Returns the outer scope or nullptr if the this is the global scope.
*
* \return Outer scope.
*/
std::shared_ptr<symbol_table> scope();
};
template<typename T>
struct writer
{

View File

@ -1,6 +1,7 @@
#pragma once
#include "elna/source/parser.hpp"
#include "elna/source/symbol_table.hpp"
namespace elna::source
{

View File

@ -0,0 +1,151 @@
#pragma once
#include <cstdint>
#include <unordered_map>
#include <string>
#include <memory>
#include <vector>
namespace elna::source
{
class symbol_table;
/**
* Generic language entity information.
*/
class info
{
public:
virtual ~info() = 0;
protected:
info();
};
/**
* Type information.
*/
class type_info final : public info
{
std::shared_ptr<class type> m_type;
public:
explicit type_info(const class type& type);
~type_info() override;
std::shared_ptr<const class type> type() const noexcept;
};
/**
* Constant information.
*/
class constant_info final : public info
{
std::int32_t m_value;
public:
constant_info(const std::int32_t value);
~constant_info() override;
std::int32_t value() const noexcept;
};
/**
* Variable information.
*/
class variable_info final : public info
{
std::shared_ptr<const class type> m_type;
public:
std::ptrdiff_t offset{ 0 };
explicit variable_info(std::shared_ptr<const class type> type);
~variable_info() override;
std::shared_ptr<const class type> type() noexcept;
};
/**
* Procedure parameter information.
*/
class parameter_info final : public info
{
std::shared_ptr<const class type> m_type;
public:
std::ptrdiff_t offset{ 0 };
explicit parameter_info(std::shared_ptr<const class type> type);
~parameter_info() override;
std::shared_ptr<const class type> type() const noexcept;
};
/**
* Intrinsic and external procedure information.
*/
class intrinsic_info : public info
{
public:
std::vector<parameter_info> parameter_infos;
~intrinsic_info() override;
std::size_t parameter_stack_size() const noexcept;
};
/**
* Procedure information.
*/
class procedure_info final : public intrinsic_info
{
std::shared_ptr<symbol_table> local_table;
public:
std::size_t local_stack_size{ 0 };
std::size_t argument_stack_size{ 0 };
explicit procedure_info(std::shared_ptr<symbol_table> outer_scope);
~procedure_info() override;
std::shared_ptr<symbol_table> scope();
std::size_t stack_size() const noexcept;
};
/**
* Symbol table.
*/
class symbol_table
{
std::unordered_map<std::string, std::shared_ptr<info>> entries;
std::shared_ptr<symbol_table> outer_scope;
public:
/**
* Constructs a new symbol with an optional outer scope.
*
* \param scope Outer scope.
*/
explicit symbol_table(std::shared_ptr<symbol_table> scope = nullptr);
/**
* Looks for symbol in the table by name. Returns nullptr if the symbol
* can not be found.
*
* \param name Symbol name.
* \return Symbol from the table if found.
*/
std::shared_ptr<info> lookup(const std::string& name);
/**
* Registers new symbol.
*
* \param name Symbol name.
* \param entry Symbol information.
*/
void enter(const std::string& name, std::shared_ptr<info> entry);
/**
* Returns the outer scope or nullptr if the this is the global scope.
*
* \return Outer scope.
*/
std::shared_ptr<symbol_table> scope();
};
}

View File

@ -0,0 +1,41 @@
#pragma once
#include <memory>
#include <string>
namespace elna::source
{
/**
* Type representation.
*/
struct type
{
const std::size_t byte_size;
protected:
explicit type(const std::size_t byte_size);
};
/**
* Built-in type representation.
*/
struct primitive_type : public type
{
const std::string type_name;
primitive_type(const std::string& type_name, const std::size_t byte_size);
};
/**
* Typed pointer.
*/
struct pointer_type : public type
{
std::shared_ptr<const type> base_type;
pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size);
};
inline const primitive_type boolean_type{ "Boolean", 1 };
inline const primitive_type int_type{ "Int", 4 };
}

View File

@ -48,6 +48,7 @@ namespace elna
boost::asio::readable_pipe& read_pipe);
test_result run_for_output(boost::asio::io_context& context, const std::uint8_t stream_number,
const std::filesystem::path& binary, std::initializer_list<boost::string_view> arguments);
std::string find_object(const std::vector<std::filesystem::path>& environment, const std::string& object);
test_result build_test(boost::asio::io_context& context, const std::filesystem::directory_entry& test_entry);
test_result run_test(boost::asio::io_context& context, const std::filesystem::directory_entry& test_entry);
void print_result(const std::filesystem::directory_entry& test_entry, const test_result& result);