From db863b806eba12b75c3525f2ead4ed2fcdc37c65 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 10 Dec 2022 17:07:25 +0800 Subject: [PATCH] add: string, comment --- DragonLisp.l | 20 +++++++++++++++++--- DragonLisp.y | 7 ++----- token.h | 1 + 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/DragonLisp.l b/DragonLisp.l index 3db8a47..cced544 100644 --- a/DragonLisp.l +++ b/DragonLisp.l @@ -23,6 +23,8 @@ using token = DragonLisp::DLParser::token; id [a-zA-Z_][a-zA-Z_0-9]* int [0-9]+ blank [ \t\v\r] +comment ;.*$ +string \"[^\r\n]*\" %% @@ -30,23 +32,35 @@ blank [ \t\v\r] yylval = lval; %} +{string} { + loc->step(); + std::printf("Scanned string: %s\n", yytext); + yylval->emplace(std::string(yytext + 1, yyleng - 2)); + return token::TOKEN_STRING; +} + +{comment} { + std::printf("Scanned comment: %s\n", yytext); + loc->step(); +} + {blank}+ { loc->step(); std::printf("Skipping blank\n"); }; -\n+ { +\n+ { loc->lines(yyleng); loc->step(); std::printf("Skipping newline\n"); } -"<=" { +"<=" { std::printf("Scanned <=\n"); return token::TOKEN_LE; }; -">=" { +">=" { std::printf("Scanned >=\n"); return token::TOKEN_GE; }; diff --git a/DragonLisp.y b/DragonLisp.y index dd94a94..77b791d 100644 --- a/DragonLisp.y +++ b/DragonLisp.y @@ -57,6 +57,7 @@ namespace DragonLisp { %token END 0 "EOF" %token NUMBER "number" +%token STRING "string" %type basic_operator %type arithmetic_operator @@ -72,6 +73,7 @@ S R : | R S-Expr + | STRING { std::cout << "Scanned string -> " << $1 << std::endl; } S-Expr : LPAREN basic_operator NUMBER NUMBER RPAREN { @@ -82,7 +84,6 @@ S-Expr basic_operator : arithmetic_operator { $$ = $1; } - | boolean_operator { $$ = $1; } | comparsion_operator { $$ = $1; } ; @@ -93,10 +94,6 @@ arithmetic_operator | SLASH { $$ = Token::DIVIDE; std::printf("I am slash /\n"); } ; -boolean_operator - : -; - comparsion_operator : LE { $$ = Token::LE; } | LT { $$ = Token::LT; } diff --git a/token.h b/token.h index d644ac7..4fb9e0f 100644 --- a/token.h +++ b/token.h @@ -14,6 +14,7 @@ enum Token { MINUS, MULTIPLY, DIVIDE, + STRING, }; }