Add char literals

This commit is contained in:
Nikita Lisitsa 2025-12-18 16:39:01 +03:00
parent f22cd63a2a
commit 2f5ad00b6d
2 changed files with 11 additions and 0 deletions

View file

@ -88,6 +88,14 @@ f64 { return bp::make_f64(ctx.location); }
[0-9]+\.[0-9]+ { return bp::make_lit_f32(yytext, ctx.location); } [0-9]+\.[0-9]+ { return bp::make_lit_f32(yytext, ctx.location); }
[0-9]+\.[0-9]+l { return bp::make_lit_f64(yytext, ctx.location); } [0-9]+\.[0-9]+l { return bp::make_lit_f64(yytext, ctx.location); }
'.' { return bp::make_lit_char8(yytext[1], ctx.location); }
'\\n' { return bp::make_lit_char8(10, ctx.location); }
'\\t' { return bp::make_lit_char8(11, ctx.location); }
'\\r' { return bp::make_lit_char8(13, ctx.location); }
'\\\"' { return bp::make_lit_char8(22, ctx.location); }
'\\'' { return bp::make_lit_char8(27, ctx.location); }
'\\\\' { return bp::make_lit_char8(134, ctx.location); }
<<EOF>> { return bp::make_end(ctx.location); } <<EOF>> { return bp::make_end(ctx.location); }
. { throw ::pslang::parser::parse_error("unexpected character \"" + std::string(yytext) + "\"", ctx.location); } . { throw ::pslang::parser::parse_error("unexpected character \"" + std::string(yytext) + "\"", ctx.location); }

View file

@ -101,6 +101,8 @@ template <typename T>
%token <std::string> lit_f32 %token <std::string> lit_f32
%token <std::string> lit_f64 %token <std::string> lit_f64
%token <char> lit_char8
%token <std::string> name %token <std::string> name
%token const %token const
@ -310,6 +312,7 @@ literal
| lit_u64 { $$ = parse_numeric_literal<std::uint64_t>($1, ctx.location); } | lit_u64 { $$ = parse_numeric_literal<std::uint64_t>($1, ctx.location); }
| lit_f32 { $$ = parse_numeric_literal<float>($1, ctx.location); } | lit_f32 { $$ = parse_numeric_literal<float>($1, ctx.location); }
| lit_f64 { $$ = parse_numeric_literal<double>($1, ctx.location); } | lit_f64 { $$ = parse_numeric_literal<double>($1, ctx.location); }
| lit_char8 { $$ = ast::u8_literal{static_cast<std::uint8_t>($1), ctx.location}; }
; ;
%% %%