add: string, comment

This commit is contained in:
Eatswap 2022-12-10 17:07:25 +08:00
parent ae30e35334
commit db863b806e
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 20 additions and 8 deletions

View File

@ -23,6 +23,8 @@ using token = DragonLisp::DLParser::token;
id [a-zA-Z_][a-zA-Z_0-9]* id [a-zA-Z_][a-zA-Z_0-9]*
int [0-9]+ int [0-9]+
blank [ \t\v\r] blank [ \t\v\r]
comment ;.*$
string \"[^\r\n]*\"
%% %%
@ -30,6 +32,18 @@ blank [ \t\v\r]
yylval = lval; yylval = lval;
%} %}
{string} {
loc->step();
std::printf("Scanned string: %s\n", yytext);
yylval->emplace<std::string>(std::string(yytext + 1, yyleng - 2));
return token::TOKEN_STRING;
}
{comment} {
std::printf("Scanned comment: %s\n", yytext);
loc->step();
}
{blank}+ { {blank}+ {
loc->step(); loc->step();
std::printf("Skipping blank\n"); std::printf("Skipping blank\n");

View File

@ -57,6 +57,7 @@ namespace DragonLisp {
%token END 0 "EOF" %token END 0 "EOF"
%token <int64_t> NUMBER "number" %token <int64_t> NUMBER "number"
%token <std::string> STRING "string"
%type <DragonLisp::Token> basic_operator %type <DragonLisp::Token> basic_operator
%type <DragonLisp::Token> arithmetic_operator %type <DragonLisp::Token> arithmetic_operator
@ -72,6 +73,7 @@ S
R R
: :
| R S-Expr | R S-Expr
| STRING { std::cout << "Scanned string -> " << $1 << std::endl; }
S-Expr S-Expr
: LPAREN basic_operator NUMBER NUMBER RPAREN { : LPAREN basic_operator NUMBER NUMBER RPAREN {
@ -82,7 +84,6 @@ S-Expr
basic_operator basic_operator
: arithmetic_operator { $$ = $1; } : arithmetic_operator { $$ = $1; }
| boolean_operator { $$ = $1; }
| comparsion_operator { $$ = $1; } | comparsion_operator { $$ = $1; }
; ;
@ -93,10 +94,6 @@ arithmetic_operator
| SLASH { $$ = Token::DIVIDE; std::printf("I am slash /\n"); } | SLASH { $$ = Token::DIVIDE; std::printf("I am slash /\n"); }
; ;
boolean_operator
:
;
comparsion_operator comparsion_operator
: LE { $$ = Token::LE; } : LE { $$ = Token::LE; }
| LT { $$ = Token::LT; } | LT { $$ = Token::LT; }

View File

@ -14,6 +14,7 @@ enum Token {
MINUS, MINUS,
MULTIPLY, MULTIPLY,
DIVIDE, DIVIDE,
STRING,
}; };
} }