From 08c51a023284d6e5c75ce72eaac56c2db73416ed Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 10 Dec 2022 00:54:16 +0800 Subject: [PATCH] update: token --- DragonLisp.y | 21 ++++++++++++++++----- DragonLispDriver.hh | 7 +++++++ token.h | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 token.h diff --git a/DragonLisp.y b/DragonLisp.y index 3fb428a..de32373 100644 --- a/DragonLisp.y +++ b/DragonLisp.y @@ -7,6 +7,10 @@ %code requires { +#include + +#include "token.h" + namespace DragonLisp { class DLDriver; class DLScanner; @@ -48,6 +52,8 @@ namespace DragonLisp { %token END 0 "EOF" %token NUMBER "number" +%type operator + %define parse.error verbose %% @@ -60,13 +66,18 @@ R | R S-Expr S-Expr - : LPAREN operator NUMBER NUMBER RPAREN { std::printf("This is S-Expr!\n"); } + : LPAREN operator NUMBER NUMBER RPAREN { + std::printf("Operator -> %d, LHS -> %lld, RHS -> %lld\n", int($2), $3, $4); + std::printf("This is S-Expr!\n"); + } +; operator - : PLUS { std::printf("I am plus +\n"); } - | MINUS { std::printf("I am minus -\n"); } - | STAR { std::printf("I am star *\n"); } - | SLASH { std::printf("I am slash /\n"); } + : PLUS { $$ = Token::PLUS; std::printf("I am plus +\n"); } + | MINUS { $$ = Token::MINUS; std::printf("I am minus -\n"); } + | STAR { $$ = Token::MULTIPLY; std::printf("I am star *\n"); } + | SLASH { $$ = Token::DIVIDE; std::printf("I am slash /\n"); } +; %% diff --git a/DragonLispDriver.hh b/DragonLispDriver.hh index 4ff9968..a358421 100644 --- a/DragonLispDriver.hh +++ b/DragonLispDriver.hh @@ -20,6 +20,13 @@ public: void error(const DLParser::location_type& l, const std::string& m); void error(const std::string& m); + enum Operator { + PLUS, + MINUS, + MULTIPLY, + DIVIDE + }; + private: DLParser* parser = nullptr; DLScanner* scanner = nullptr; diff --git a/token.h b/token.h new file mode 100644 index 0000000..d6e7b6d --- /dev/null +++ b/token.h @@ -0,0 +1,15 @@ +#ifndef __DRAGON_LISP_TOKEN_H__ +#define __DRAGON_LISP_TOKEN_H__ + +namespace DragonLisp { + +enum Token { + PLUS, + MINUS, + MULTIPLY, + DIVIDE +}; + +} + +#endif // __DRAGON_LISP_TOKEN_H__ \ No newline at end of file