From ecf948e382752e8135aac60e4c4ead30e5e0385b Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sun, 11 Dec 2022 13:37:02 +0800 Subject: [PATCH] add: more syntax --- DragonLisp.l | 36 +++++++++++++++ DragonLisp.y | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++- token.h | 1 + 3 files changed, 164 insertions(+), 1 deletion(-) diff --git a/DragonLisp.l b/DragonLisp.l index 3c20fe1..2696156 100644 --- a/DragonLisp.l +++ b/DragonLisp.l @@ -54,6 +54,7 @@ from (from|FROM) to (to|TO) dotimes (dotimes|DOTIMES) dolist (dolist|DOLIST) +do (do|DO) defconstant (defconstant|DEFCONSTANT) %% @@ -278,6 +279,41 @@ defconstant (defconstant|DEFCONSTANT) return token::TOKEN_QUOTE; }; +{for} { + std::printf("Scanned for\n"); + return token::TOKEN_FOR; +}; + +{in} { + std::printf("Scanned in\n"); + return token::TOKEN_IN; +}; + +{from} { + std::printf("Scanned from\n"); + return token::TOKEN_FROM; +}; + +{to} { + std::printf("Scanned to\n"); + return token::TOKEN_TO; +}; + +{dotimes} { + std::printf("Scanned dotimes\n"); + return token::TOKEN_DOTIMES; +}; + +{dolist} { + std::printf("Scanned dolist\n"); + return token::TOKEN_DOLIST; +}; + +{do} { + std::printf("Scanned do\n"); + return token::TOKEN_DO; +}; + {id} { std::printf("Scanned identifier: %s\n", yytext); yylval->emplace(std::string(yytext, yyleng)); diff --git a/DragonLisp.y b/DragonLisp.y index fbe0d89..59f68f4 100644 --- a/DragonLisp.y +++ b/DragonLisp.y @@ -81,6 +81,7 @@ namespace DragonLisp { TO "to" DOTIMES "dotimes" DOLIST "dolist" + DO "do" DEFCONSTANT "defconstant" ; @@ -95,7 +96,132 @@ namespace DragonLisp { %% S - : END + : END + | S-Exprs END +; + +S-Exprs + : S-Exprs empty S-Expr + | S-Expr +; + +spaces + : SPACE + | spaces SPACE +; + +empty + : + | spaces +; + +R-Value + : IDENTIFIER + | literal + | S-Expr + +S-Expr + : LPAREN empty S-Expr-helper empty RPAREN + | LPAREN empty RPAREN +; + +literal + : INTEGER + | FLOAT + | STRING +; + +S-Expr-helper + : S-Expr-var-op + | S-Expr-unary + | S-Expr-binary + | S-Expr-list + | S-Expr-if + | S-Expr-loop + | S-Expr-func-def + | S-Expr-func-call +; + +S-Expr-var-op + : var-op-tokens spaces IDENTIFIER spaces R-Value +; + +var-op-tokens + : DEFVAR + | SETQ + | INCF + | DECF + | DEFCONSTANT +; + +S-Expr-unary + : unary-tokens spaces R-Value +; + +unary-tokens + : NOT + | PRINT +; + +S-Expr-binary + : binary-tokens spaces S-Expr empty S-Expr +; + +binary-tokens + : LESS + | LESS_EQUAL + | GREATER + | GREATER_EQUAL + | LOGNOR + | MOD + | REM +; + +S-Expr-list + : list-tokens spaces S-Exprs +; + +list-tokens + : EQUAL + | NOT_EQUAL + | AND + | OR + | MAX + | MIN + | LOGAND + | LOGIOR + | LOGXOR + | LOGEQV + | PLUS + | MINUS + | MULTIPLY + | DIVIDE +; + +S-Expr-if + : IF spaces S-Expr empty S-Expr empty S-Expr +; + +S-Expr-loop + : LOOP empty S-Exprs + | LOOP spaces FOR spaces IDENTIFIER spaces IN empty S-Expr empty DO empty S-Exprs + | LOOP spaces FOR spaces IDENTIFIER spaces FROM empty S-Expr empty TO empty S-Expr empty DO empty S-Exprs + | DOTIMES empty LPAREN empty IDENTIFIER empty S-Expr empty RPAREN empty S-Exprs + | DOLIST empty LPAREN empty IDENTIFIER empty S-Expr empty RPAREN empty S-Exprs +; + +S-Expr-func-def + : DEFUN spaces IDENTIFIER empty LPAREN empty identifier-list empty RPAREN ignored-func-doc S-Exprs +; + +ignored-func-doc + : empty STRING empty +; + +identifier-list + : identifier-list IDENTIFIER + | IDENTIFIER +; %% diff --git a/token.h b/token.h index 0e4be3a..4ae873e 100644 --- a/token.h +++ b/token.h @@ -47,6 +47,7 @@ enum Token { TO, DOTIMES, DOLIST, + DO, DEFCONSTANT, };