Introduce float type

This commit is contained in:
2024-12-31 18:10:34 +01:00
parent 75a691134f
commit 8176da5f9b
8 changed files with 100 additions and 60 deletions

View File

@ -95,7 +95,11 @@ namespace source
{
}
void empty_visitor::visit(integer_literal *number)
void empty_visitor::visit(number_literal<std::int32_t> *number)
{
}
void empty_visitor::visit(number_literal<double> *number)
{
}
@ -214,7 +218,7 @@ namespace source
}
constant_definition::constant_definition(const struct position position, const std::string& identifier,
std::unique_ptr<integer_literal>&& body)
std::unique_ptr<number_literal<std::int32_t>>&& body)
: definition(position, identifier), m_body(std::move(body))
{
}
@ -224,7 +228,7 @@ namespace source
visitor->visit(this);
}
integer_literal& constant_definition::body()
number_literal<std::int32_t>& constant_definition::body()
{
return *m_body;
}
@ -290,21 +294,6 @@ namespace source
visitor->visit(this);
}
integer_literal::integer_literal(const struct position position, const std::int32_t value)
: expression(position), m_number(value)
{
}
void integer_literal::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
std::int32_t integer_literal::number() const noexcept
{
return m_number;
}
boolean_literal::boolean_literal(const struct position position, const bool value)
: expression(position), m_boolean(value)
{

View File

@ -73,6 +73,9 @@ false {
[0-9]+ {
return yy::parser::make_NUMBER(strtol(yytext, NULL, 10), this->location);
}
[0-9]+\.[0-9] {
return yy::parser::make_FLOAT(strtof(yytext, NULL), this->location);
}
\( {
return yy::parser::make_LEFT_PAREN(this->location);
}

View File

@ -58,6 +58,7 @@
%token <std::string> IDENTIFIER "identifier"
%token <std::int32_t> NUMBER "number"
%token <float> FLOAT "float"
%token <bool> BOOLEAN
%token IF WHILE DO
%token CONST VAR PROCEDURE
@ -70,7 +71,8 @@
%precedence THEN
%precedence ELSE
%type <std::unique_ptr<elna::source::integer_literal>> integer_literal;
%type <std::unique_ptr<elna::source::number_literal<std::int32_t>>> integer_literal;
%type <std::unique_ptr<elna::source::number_literal<double>>> float_literal;
%type <std::unique_ptr<elna::source::boolean_literal>> boolean_literal;
%type <std::unique_ptr<elna::source::constant_definition>> constant_definition;
%type <std::vector<std::unique_ptr<elna::source::constant_definition>>> constant_definition_part constant_definitions;
@ -141,7 +143,11 @@ procedure_definition_part:
| procedure_definitions { std::swap($$, $1); }
integer_literal: NUMBER
{
$$ = std::make_unique<elna::source::integer_literal>(elna::source::make_position(@1), $1);
$$ = std::make_unique<elna::source::number_literal<std::int32_t>>(elna::source::make_position(@1), $1);
};
float_literal: FLOAT
{
$$ = std::make_unique<elna::source::number_literal<double>>(elna::source::make_position(@1), $1);
};
boolean_literal: BOOLEAN
{
@ -179,6 +185,7 @@ if_statement:
}
pointer:
integer_literal { $$ = std::move($1); }
| float_literal { $$ = std::move($1); }
| boolean_literal { $$ = std::move($1); }
| variable_expression { $$ = std::move($1); }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }