From b9e173c39bc1a9776a3664981388b6ba957c73e5 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sun, 11 Dec 2022 19:41:09 +0800 Subject: [PATCH] fix: complete tokens & syntax --- DragonLisp.l | 23 +++++++++++++++++++++++ DragonLisp.y | 37 +++++++++++++++++++++++++++++-------- token.h | 3 +++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/DragonLisp.l b/DragonLisp.l index a31890a..1b21d8f 100644 --- a/DragonLisp.l +++ b/DragonLisp.l @@ -47,6 +47,7 @@ defun (defun|DEFUN) print (print|PRINT) loop (loop|LOOP) setq (setq|SETQ) +setf (setf|SETF) quote (quote|QUOTE) for (for|FOR) in (in|IN) @@ -55,6 +56,8 @@ to (to|TO) dotimes (dotimes|DOTIMES) dolist (dolist|DOLIST) do (do|DO) +aref (aref|AREF) +makearray (make-array|MAKE-ARRAY) defconstant (defconstant|DEFCONSTANT) %% @@ -274,6 +277,11 @@ defconstant (defconstant|DEFCONSTANT) return token::TOKEN_SETQ; }; +{setf} { + std::printf("Scanned setf\n"); + return token::TOKEN_SETF; +}; + {quote} { std::printf("Scanned quote\n"); return token::TOKEN_QUOTE; @@ -314,6 +322,21 @@ defconstant (defconstant|DEFCONSTANT) return token::TOKEN_DO; }; +{aref} { + std::printf("Scanned aref\n"); + return token::TOKEN_AREF; +}; + +{makearray} { + std::printf("Scanned makearray\n"); + return token::TOKEN_MAKE_ARRAY; +}; + +{defconstant} { + std::printf("Scanned defconstant\n"); + return token::TOKEN_DEFCONSTANT; +}; + {id} { std::printf("Scanned identifier: %s\n", yytext); yylval->emplace(std::string(yytext, yyleng)); diff --git a/DragonLisp.y b/DragonLisp.y index 8728b30..64e87b5 100644 --- a/DragonLisp.y +++ b/DragonLisp.y @@ -74,6 +74,7 @@ namespace DragonLisp { PRINT "print" LOOP "loop" SETQ "setq" + SETF "setf" QUOTE "quote" FOR "for" IN "in" @@ -82,6 +83,8 @@ namespace DragonLisp { DOTIMES "dotimes" DOLIST "dolist" DO "do" + AREF "aref" + MAKE_ARRAY "make-array" DEFCONSTANT "defconstant" ; @@ -115,6 +118,14 @@ S-Exprs | S-Expr { std::printf("Parsed S-Exprs -> S-Expr\n"); } ; +array-ref + : LPAREN AREF IDENTIFIER R-Value RPAREN { std::printf("Parsed array-ref -> ( AREF IDENTIFIER R-Value )\n"); } +; + +L-Value + : IDENTIFIER + | array-ref + R-Value : IDENTIFIER { std::printf("Parsed R-Value -> IDENTIFIER\n"); } | S-Expr { std::printf("Parsed R-Value -> S-Expr\n"); } @@ -129,12 +140,13 @@ R-Value-list ; S-Expr - : LPAREN S-Expr-helper RPAREN { std::printf("Parsed S-Expr -> LPAREN S-Expr-helper RPAREN\n"); } - | LPAREN RPAREN { std::printf("Parsed S-Expr -> LPAREN RPAREN\n"); } + : LPAREN S-Expr-helper RPAREN { std::printf("Parsed S-Expr -> ( S-Expr-helper )\n"); } + | LPAREN RPAREN { std::printf("Parsed S-Expr -> ()\n"); } ; S-Expr-helper : S-Expr-var-op { std::printf("Parsed S-Expr-helper -> S-Expr-var-op\n"); } + | S-Expr-Lval-op { std::printf("Parsed S-Expr-helper -> S-Expr-Lval-op\n"); } | S-Expr-unary { std::printf("Parsed S-Expr-helper -> S-Expr-unary\n"); } | S-Expr-binary { std::printf("Parsed S-Expr-helper -> S-Expr-binary\n"); } | S-Expr-list { std::printf("Parsed S-Expr-helper -> S-Expr-list\n"); } @@ -155,13 +167,24 @@ var-op-tokens | DEFCONSTANT { std::printf("Parsed var-op-tokens -> DEFCONSTANT\n"); } ; +S-Expr-Lval-op + : lval-op-tokens L-Value R-Value { std::printf("Parsed S-Expr-Lval-op -> lval-op-tokens L-Value R-Value\n"); } +; + +lval-op-tokens + : SETF { std::printf("Parsed lval-op-tokens -> SETF\n"); } + | INCF { std::printf("Parsed lval-op-tokens -> INCF\n"); } + | DECF { std::printf("Parsed lval-op-tokens -> DECF\n"); } +; + S-Expr-unary : unary-tokens R-Value { std::printf("Parsed S-Expr-unary -> unary-tokens R-Value\n"); } ; unary-tokens - : NOT { std::printf("Parsed unary-tokens -> NOT\n"); } - | PRINT { std::printf("Parsed unary-tokens -> PRINT\n"); } + : NOT { std::printf("Parsed unary-tokens -> NOT\n"); } + | PRINT { std::printf("Parsed unary-tokens -> PRINT\n"); } + | MAKE_ARRAY { std::printf("Parsed unary-tokens -> MAKE_ARRAY\n"); } ; S-Expr-binary @@ -206,14 +229,12 @@ S-Expr-if S-Expr-loop : LOOP S-Exprs { std::printf("Parsed S-Expr-loop -> LOOP S-Exprs\n"); } - | LOOP FOR IDENTIFIER IN S-Expr DO S-Exprs { std::printf("Parsed S-Expr-loop -> LOOP FOR IDENTIFIER IN S-Expr DO S-Exprs\n"); } | LOOP FOR IDENTIFIER FROM S-Expr TO S-Expr DO S-Exprs { std::printf("Parsed S-Expr-loop -> LOOP FOR IDENTIFIER FROM S-Expr TO S-Expr DO S-Exprs\n"); } - | DOTIMES LPAREN IDENTIFIER S-Expr RPAREN S-Exprs { std::printf("Parsed S-Expr-loop -> DOTIMES LPAREN IDENTIFIER S-Expr RPAREN S-Exprs\n"); } - | DOLIST LPAREN IDENTIFIER S-Expr RPAREN S-Exprs { std::printf("Parsed S-Expr-loop -> DOLIST LPAREN IDENTIFIER S-Expr RPAREN S-Exprs\n"); } + | DOTIMES LPAREN IDENTIFIER S-Expr RPAREN S-Exprs { std::printf("Parsed S-Expr-loop -> DOTIMES ( IDENTIFIER S-Expr ) S-Exprs\n"); } ; func-def - : LPAREN DEFUN IDENTIFIER LPAREN identifier-list RPAREN ignored-func-doc S-Exprs RPAREN { std::printf("Parsed func-def -> DEFUN IDENTIFIER LPAREN identifier-list RPAREN ignored-func-doc S-Exprs\n"); } + : LPAREN DEFUN IDENTIFIER LPAREN identifier-list RPAREN ignored-func-doc S-Exprs RPAREN { std::printf("Parsed func-def -> DEFUN IDENTIFIER ( identifier-list ) ignored-func-doc S-Exprs\n"); } ; ignored-func-doc diff --git a/token.h b/token.h index 4ae873e..9793721 100644 --- a/token.h +++ b/token.h @@ -40,6 +40,7 @@ enum Token { PRINT, LOOP, SETQ, + SETF, QUOTE, FOR, IN, @@ -48,6 +49,8 @@ enum Token { DOTIMES, DOLIST, DO, + AREF, + MAKE_ARRAY, DEFCONSTANT, };