update: token

This commit is contained in:
Eatswap 2022-12-10 00:54:16 +08:00
parent a34a05d0d0
commit 08c51a0232
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 38 additions and 5 deletions

View File

@ -7,6 +7,10 @@
%code requires {
#include <typeinfo>
#include "token.h"
namespace DragonLisp {
class DLDriver;
class DLScanner;
@ -48,6 +52,8 @@ namespace DragonLisp {
%token END 0 "EOF"
%token <int64_t> NUMBER "number"
%type <DragonLisp::Token> 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"); }
;
%%

View File

@ -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;

15
token.h Normal file
View File

@ -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__