Add semantic passes

This commit is contained in:
2024-12-23 13:54:11 +01:00
parent f080b75c52
commit 40306ac986
27 changed files with 1220 additions and 325 deletions

View File

@ -0,0 +1,24 @@
#pragma once
#include "elna/source/ast.h"
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "tree-iterator.h"
namespace elna
{
namespace gcc
{
class generic_visitor final : public source::empty_visitor
{
tree current_statements{ NULL_TREE };
public:
void visit(source::program *program) override;
void visit(source::call_statement *statement) override;
};
}
}

View File

@ -1,10 +1,12 @@
#pragma once
#include <memory>
#include "elna/source/result.hpp"
#include "elna/source/types.hpp"
#include "elna/source/result.h"
#include "elna/source/types.h"
namespace elna::source
namespace elna
{
namespace source
{
enum class binary_operator
{
@ -481,3 +483,4 @@ namespace elna::source
unary_operator operation() const noexcept;
};
}
}

View File

@ -1,10 +1,12 @@
#pragma once
#include <list>
#include "elna/source/ast.hpp"
#include "elna/source/ast.h"
#include "location.hh"
namespace elna::source
namespace elna
{
namespace source
{
position make_position(const yy::location& location);
@ -33,3 +35,4 @@ namespace elna::source
const std::list<std::unique_ptr<struct error>>& errors() const noexcept;
};
}
}

View File

@ -2,9 +2,11 @@
#include <cstddef>
#include <filesystem>
#include "elna/source/types.hpp"
#include "elna/source/types.h"
namespace elna::source
namespace elna
{
namespace source
{
/**
* Position in the source text.
@ -100,3 +102,4 @@ namespace elna::source
operation kind;
};
}
}

View File

@ -1,10 +1,12 @@
#pragma once
#include <list>
#include "elna/source/ast.hpp"
#include "elna/source/symbol_table.hpp"
#include "elna/source/ast.h"
#include "elna/source/symbol_table.h"
namespace elna::source
namespace elna
{
namespace source
{
class name_analysis_visitor final : public empty_visitor
{
@ -92,3 +94,4 @@ namespace elna::source
void visit(assign_statement *statement) override;
};
}
}

View File

@ -5,9 +5,18 @@
#include <string>
#include <memory>
namespace elna::source
namespace elna
{
namespace source
{
class symbol_table;
class type_info;
class typed_info;
class constant_info;
class variable_info;
class parameter_info;
class intrinsic_info;
class procedure_info;
/**
* Generic language entity information.
@ -17,6 +26,14 @@ namespace elna::source
public:
virtual ~info() = 0;
virtual type_info *is_type_info() noexcept;
virtual typed_info *is_typed_info() noexcept;
virtual constant_info *is_constant_info() noexcept;
virtual variable_info *is_variable_info() noexcept;
virtual parameter_info *is_parameter_info() noexcept;
virtual intrinsic_info *is_intrinsic_info() noexcept;
virtual procedure_info *is_procedure_info() noexcept;
protected:
info();
};
@ -26,11 +43,13 @@ namespace elna::source
*/
class type_info final : public info
{
std::shared_ptr<class type> m_type;
std::shared_ptr<const class type> m_type;
public:
explicit type_info(const class type& type);
explicit type_info(std::shared_ptr<class type> type);
~type_info() override;
virtual type_info *is_type_info() noexcept override;
std::shared_ptr<const class type> type() const noexcept;
};
@ -47,6 +66,7 @@ namespace elna::source
public:
~typed_info() override;
virtual typed_info *is_typed_info() noexcept override;
std::shared_ptr<const class type> type() const noexcept;
};
@ -60,6 +80,7 @@ namespace elna::source
public:
constant_info(std::shared_ptr<const class type> type, const std::int32_t value);
virtual constant_info *is_constant_info() noexcept override;
std::int32_t value() const noexcept;
};
@ -72,6 +93,8 @@ namespace elna::source
std::ptrdiff_t offset{ 0 };
explicit variable_info(std::shared_ptr<const class type> type);
virtual variable_info *is_variable_info() noexcept override;
};
/**
@ -83,6 +106,8 @@ namespace elna::source
std::ptrdiff_t offset{ 0 };
explicit parameter_info(std::shared_ptr<const class type> type);
virtual parameter_info *is_parameter_info() noexcept override;
};
/**
@ -98,6 +123,7 @@ namespace elna::source
std::shared_ptr<const class procedure_type> type() const noexcept;
std::size_t parameter_stack_size() const noexcept;
virtual intrinsic_info *is_intrinsic_info() noexcept override;
};
/**
@ -116,6 +142,7 @@ namespace elna::source
std::shared_ptr<symbol_table> scope();
std::size_t stack_size() const noexcept;
virtual procedure_info *is_procedure_info() noexcept override;
};
/**
@ -158,4 +185,11 @@ namespace elna::source
*/
std::shared_ptr<symbol_table> scope();
};
/**
* Creates a symbol table with predefined symbols.
*
* \return A symbol table with predefined symbols.
*/
std::shared_ptr<source::symbol_table> add_builtin_symbols();}
}

View File

@ -4,12 +4,18 @@
#include <string>
#include <vector>
namespace elna::source
namespace elna
{
namespace source
{
class primitive_type;
class pointer_type;
class procedure_type;
/**
* Type representation.
*/
class type
class type
{
const std::size_t byte_size;
@ -27,6 +33,13 @@ namespace elna::source
*/
virtual std::size_t size() const noexcept;
/**
* \return Unique type representation.
*/
virtual std::string type_name() const = 0;
virtual const pointer_type *is_pointer_type() const;
friend bool operator==(const type& lhs, const type& rhs) noexcept;
friend bool operator!=(const type& lhs, const type& rhs) noexcept;
};
@ -34,11 +47,12 @@ namespace elna::source
/**
* Built-in type representation.
*/
struct primitive_type : public type
class primitive_type final : public type
{
/// Type name.
const std::string type_name;
const std::string m_type_name;
public:
/**
* Constructor.
*
@ -47,14 +61,13 @@ namespace elna::source
*/
primitive_type(const std::string& type_name, const std::size_t byte_size);
bool operator==(const primitive_type& that) const noexcept;
bool operator!=(const primitive_type& that) const noexcept;
virtual std::string type_name() const override;
};
/**
* Typed pointer.
*/
struct pointer_type : public type
struct pointer_type final : public type
{
/// Pointer target type.
std::shared_ptr<const type> base_type;
@ -67,14 +80,15 @@ namespace elna::source
*/
pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size);
bool operator==(const pointer_type& that) const noexcept;
bool operator!=(const pointer_type& that) const noexcept;
virtual std::string type_name() const override;
virtual const pointer_type *is_pointer_type() const override;
};
/**
* Type of a procedure.
*/
struct procedure_type : public type
struct procedure_type final : public type
{
/// Argument types.
std::vector<std::shared_ptr<const type>> arguments;
@ -87,13 +101,13 @@ namespace elna::source
*/
procedure_type(std::vector<std::shared_ptr<const type>> arguments, const std::size_t byte_size);
bool operator==(const procedure_type& that) const noexcept;
bool operator!=(const procedure_type& that) const noexcept;
virtual std::string type_name() const override;
};
bool operator==(const type& lhs, const type& rhs) noexcept;
bool operator!=(const type& lhs, const type& rhs) noexcept;
inline const primitive_type boolean_type{ "Boolean", 1 };
inline const primitive_type int_type{ "Int", 4 };
extern const primitive_type boolean_type;
extern const primitive_type int_type;
}
}