Add a char type

This commit is contained in:
2025-01-01 23:02:19 +01:00
parent 8176da5f9b
commit 45eb6a3b84
10 changed files with 70 additions and 25 deletions

View File

@ -103,7 +103,11 @@ namespace source
{
}
void empty_visitor::visit(boolean_literal *boolean)
void empty_visitor::visit(number_literal<bool> *character)
{
}
void empty_visitor::visit(char_literal *character)
{
}
@ -294,19 +298,19 @@ namespace source
visitor->visit(this);
}
boolean_literal::boolean_literal(const struct position position, const bool value)
: expression(position), m_boolean(value)
char_literal::char_literal(const struct position position, const unsigned char value)
: expression(position), m_character(value)
{
}
void boolean_literal::accept(parser_visitor *visitor)
void char_literal::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
bool boolean_literal::boolean() const noexcept
unsigned char char_literal::character() const noexcept
{
return m_boolean;
return m_character;
}
variable_expression::variable_expression(const struct position position, const std::string& name)

View File

@ -71,11 +71,15 @@ false {
return yy::parser::make_IDENTIFIER(yytext, this->location);
}
[0-9]+ {
return yy::parser::make_NUMBER(strtol(yytext, NULL, 10), this->location);
return yy::parser::make_INTEGER(strtol(yytext, NULL, 10), this->location);
}
[0-9]+\.[0-9] {
return yy::parser::make_FLOAT(strtof(yytext, NULL), this->location);
}
'[^']' {
return yy::parser::make_CHARACTER(
std::string(yytext, 1, strlen(yytext) - 2), this->location);
}
\( {
return yy::parser::make_LEFT_PAREN(this->location);
}

View File

@ -57,8 +57,9 @@
%start program;
%token <std::string> IDENTIFIER "identifier"
%token <std::int32_t> NUMBER "number"
%token <std::int32_t> INTEGER "integer"
%token <float> FLOAT "float"
%token <std::string> CHARACTER "character"
%token <bool> BOOLEAN
%token IF WHILE DO
%token CONST VAR PROCEDURE
@ -73,7 +74,8 @@
%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::number_literal<bool>>> boolean_literal;
%type <std::unique_ptr<elna::source::char_literal>> character_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;
%type <std::unique_ptr<elna::source::declaration>> variable_declaration;
@ -141,7 +143,7 @@ procedure_definitions:
procedure_definition_part:
/* no procedure definitions */ {}
| procedure_definitions { std::swap($$, $1); }
integer_literal: NUMBER
integer_literal: INTEGER
{
$$ = std::make_unique<elna::source::number_literal<std::int32_t>>(elna::source::make_position(@1), $1);
};
@ -149,9 +151,13 @@ float_literal: FLOAT
{
$$ = std::make_unique<elna::source::number_literal<double>>(elna::source::make_position(@1), $1);
};
character_literal: CHARACTER
{
$$ = std::make_unique<elna::source::char_literal>(elna::source::make_position(@1), $1[0]);
};
boolean_literal: BOOLEAN
{
$$ = std::make_unique<elna::source::boolean_literal>(elna::source::make_position(@1), $1);
$$ = std::make_unique<elna::source::number_literal<bool>>(elna::source::make_position(@1), $1);
};
compound_statement: BEGIN_BLOCK optional_statements END_BLOCK
{
@ -187,6 +193,7 @@ pointer:
integer_literal { $$ = std::move($1); }
| float_literal { $$ = std::move($1); }
| boolean_literal { $$ = std::move($1); }
| character_literal { $$ = std::move($1); }
| variable_expression { $$ = std::move($1); }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
summand: