Handle array variable declaration

This commit is contained in:
2025-01-06 15:08:23 +01:00
parent e8747a803f
commit ed1bb621d6
6 changed files with 178 additions and 77 deletions

View File

@ -25,6 +25,7 @@ namespace gcc
tree_chain variable_chain;
tree build_label_decl(const char *name, location_t loc);
tree build_type(source::type_expression& type);
void enter_scope();
tree_symbol_mapping leave_scope();

View File

@ -47,6 +47,7 @@ namespace source
class binary_expression;
class unary_expression;
class basic_type_expression;
class array_type_expression;
class variable_expression;
template<typename T>
class number_literal;
@ -71,6 +72,7 @@ namespace source
virtual void visit(binary_expression *) = 0;
virtual void visit(unary_expression *) = 0;
virtual void visit(basic_type_expression *) = 0;
virtual void visit(array_type_expression *) = 0;
virtual void visit(variable_expression *) = 0;
virtual void visit(number_literal<std::int32_t> *) = 0;
virtual void visit(number_literal<double> *) = 0;
@ -97,6 +99,7 @@ namespace source
virtual void visit(binary_expression *expression) override;
virtual void visit(unary_expression *expression) override;
virtual void visit(basic_type_expression *) override;
virtual void visit(array_type_expression *) override;
virtual void visit(variable_expression *) override;
virtual void visit(number_literal<std::int32_t> *) override;
virtual void visit(number_literal<double> *) override;
@ -111,7 +114,7 @@ namespace source
struct operand
{
public:
virtual ~operand() noexcept = 0;
virtual ~operand() = 0;
};
struct integer_operand final : public operand
@ -121,7 +124,7 @@ namespace source
public:
explicit integer_operand(const std::int32_t value);
std::int32_t value() const noexcept;
std::int32_t value() const;
};
class variable_operand final : public operand
@ -131,7 +134,7 @@ namespace source
public:
explicit variable_operand(const std::string& name);
const std::string& name() const noexcept;
const std::string& name() const;
};
struct temporary_variable final : public operand
@ -141,7 +144,7 @@ namespace source
public:
explicit temporary_variable(const std::size_t counter);
std::size_t counter() const noexcept;
std::size_t counter() const;
};
struct label_operand final : public operand
@ -151,7 +154,7 @@ namespace source
public:
explicit label_operand(const std::size_t counter);
std::size_t counter() const noexcept;
std::size_t counter() const;
};
/**
@ -168,13 +171,13 @@ namespace source
explicit node(const position position);
public:
virtual ~node() noexcept = default;
virtual ~node() = default;
virtual void accept(parser_visitor *) = 0;
/**
* \return Node position in the source code.
*/
const struct position& position() const noexcept;
const struct position& position() const;
};
class statement : public node
@ -219,7 +222,7 @@ namespace source
/**
* \return Definition name.
*/
std::string& identifier() noexcept;
std::string& identifier();
};
/**
@ -228,7 +231,9 @@ namespace source
class type_expression : public node
{
public:
virtual const std::string& base_name() = 0;
virtual basic_type_expression *is_basic();
virtual array_type_expression *is_array();
protected:
type_expression(const struct position position);
@ -239,7 +244,7 @@ namespace source
*/
class basic_type_expression : public type_expression
{
std::string m_base;
const std::string m_name;
public:
/**
@ -249,14 +254,40 @@ namespace source
basic_type_expression(const struct position position, const std::string& name);
virtual void accept(parser_visitor *visitor) override;
/**
* \return Name of the base type.
*/
const std::string& base() const noexcept;
const std::string& base_name() override;
basic_type_expression *is_basic() override;
};
class array_type_expression : public type_expression
{
type_expression *m_base;
public:
/**
* Array size.
*/
const std::uint32_t size;
/**
* \param position Source code position.
* \param name Array base type.
* \param name Array size.
*/
array_type_expression(const struct position position, type_expression *base, const std::uint32_t size);
virtual void accept(parser_visitor *visitor) override;
/**
* \return Array base type.
*/
type_expression& base();
const std::string& base_name() override;
array_type_expression *is_array() override;
virtual ~array_type_expression() override;
};
/**
* Variable declaration.
*/
@ -276,7 +307,7 @@ namespace source
type_expression *type);
virtual void accept(parser_visitor *visitor) override;
type_expression& type() noexcept;
type_expression& type();
virtual ~declaration() override;
};
@ -322,7 +353,7 @@ namespace source
virtual void accept(parser_visitor *visitor) override;
block& body();
std::vector<declaration *>& parameters() noexcept;
std::vector<declaration *>& parameters();
virtual ~procedure_definition() override;
};
@ -343,8 +374,8 @@ namespace source
call_statement(const struct position position, const std::string& name);
virtual void accept(parser_visitor *visitor) override;
std::string& name() noexcept;
std::vector<expression *>& arguments() noexcept;
std::string& name();
std::vector<expression *>& arguments();
virtual ~call_statement() override;
};
@ -377,7 +408,7 @@ namespace source
expression *rvalue);
virtual void accept(parser_visitor *visitor) override;
std::string& lvalue() noexcept;
std::string& lvalue();
expression& rvalue();
virtual ~assign_statement() override;
@ -447,8 +478,8 @@ namespace source
virtual void accept(parser_visitor *visitor) override;
statement& body();
std::vector<definition *>& definitions() noexcept;
std::vector<declaration *>& declarations() noexcept;
std::vector<definition *>& definitions();
std::vector<declaration *>& declarations();
virtual ~block() override;
};
@ -478,7 +509,7 @@ namespace source
visitor->visit(this);
}
T number() const noexcept
T number() const
{
return m_number;
}
@ -492,7 +523,7 @@ namespace source
char_literal(const struct position position, const unsigned char value);
virtual void accept(parser_visitor *visitor) override;
unsigned char character() const noexcept;
unsigned char character() const;
};
class string_literal : public expression
@ -503,7 +534,7 @@ namespace source
string_literal(const struct position position, const std::string& value);
virtual void accept(parser_visitor *visitor) override;
const std::string& string() const noexcept;
const std::string& string() const;
};
class variable_expression : public expression
@ -514,7 +545,7 @@ namespace source
variable_expression(const struct position position, const std::string& name);
virtual void accept(parser_visitor *visitor) override;
const std::string& name() const noexcept;
const std::string& name() const;
};
class binary_expression : public expression
@ -530,7 +561,7 @@ namespace source
virtual void accept(parser_visitor *visitor) override;
expression& lhs();
expression& rhs();
binary_operator operation() const noexcept;
binary_operator operation() const;
virtual ~binary_expression() override;
};
@ -546,7 +577,7 @@ namespace source
virtual void accept(parser_visitor *visitor) override;
expression& operand();
unary_operator operation() const noexcept;
unary_operator operation() const;
virtual ~unary_expression() override;
};