Add string type

This commit is contained in:
2025-01-03 22:18:35 +01:00
parent 660c774327
commit a7b0c53d23
11 changed files with 202 additions and 76 deletions

View File

@ -7,7 +7,7 @@ namespace elna
{
namespace source
{
void empty_visitor::visit(declaration *declaration)
void empty_visitor::visit(declaration *)
{
}
@ -87,27 +87,31 @@ namespace source
expression->operand().accept(this);
}
void empty_visitor::visit(type_expression *variable)
void empty_visitor::visit(type_expression *)
{
}
void empty_visitor::visit(variable_expression *variable)
void empty_visitor::visit(variable_expression *)
{
}
void empty_visitor::visit(number_literal<std::int32_t> *number)
void empty_visitor::visit(number_literal<std::int32_t> *)
{
}
void empty_visitor::visit(number_literal<double> *number)
void empty_visitor::visit(number_literal<double> *)
{
}
void empty_visitor::visit(number_literal<bool> *character)
void empty_visitor::visit(number_literal<bool> *)
{
}
void empty_visitor::visit(char_literal *character)
void empty_visitor::visit(char_literal *)
{
}
void empty_visitor::visit(string_literal *)
{
}
@ -313,6 +317,21 @@ namespace source
return m_character;
}
string_literal::string_literal(const struct position position, const std::string& value)
: expression(position), m_string(value)
{
}
void string_literal::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
const std::string& string_literal::string() const noexcept
{
return m_string;
}
variable_expression::variable_expression(const struct position position, const std::string& name)
: expression(position), m_name(name)
{
@ -550,6 +569,7 @@ namespace source
case binary_operator::greater_equal:
return ">=";
}
__builtin_unreachable();
};
}
}

View File

@ -80,6 +80,10 @@ false {
return yy::parser::make_CHARACTER(
std::string(yytext, 1, strlen(yytext) - 2), this->location);
}
\"[^\"]*\" {
return yy::parser::make_STRING(
std::string(yytext, 1, strlen(yytext) - 2), this->location);
}
\( {
return yy::parser::make_LEFT_PAREN(this->location);
}

View File

@ -60,6 +60,7 @@
%token <std::int32_t> INTEGER "integer"
%token <float> FLOAT "float"
%token <std::string> CHARACTER "character"
%token <std::string> STRING "string"
%token <bool> BOOLEAN
%token IF WHILE DO
%token CONST VAR PROCEDURE
@ -76,6 +77,7 @@
%type <std::unique_ptr<elna::source::number_literal<double>>> float_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::string_literal>> string_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;
@ -153,7 +155,11 @@ float_literal: FLOAT
};
character_literal: CHARACTER
{
$$ = std::make_unique<elna::source::char_literal>(elna::source::make_position(@1), $1[0]);
$$ = std::make_unique<elna::source::char_literal>(elna::source::make_position(@1), $1.at(0));
};
string_literal: STRING
{
$$ = std::make_unique<elna::source::string_literal>(elna::source::make_position(@1), $1);
};
boolean_literal: BOOLEAN
{
@ -194,6 +200,7 @@ pointer:
| float_literal { $$ = std::move($1); }
| boolean_literal { $$ = std::move($1); }
| character_literal { $$ = std::move($1); }
| string_literal { $$ = std::move($1); }
| variable_expression { $$ = std::move($1); }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
summand: