From e3ff931c5d4bd118036bbcfa6ec87dd4c4790341 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Tue, 13 Dec 2022 18:18:46 +0800 Subject: [PATCH] add: option to mute noisy debug info --- DragonLisp.l | 120 ++++++++++++++++-------------- DragonLisp.y | 172 +++++++++++++++++++++++-------------------- DragonLispDriver.cpp | 8 +- Makefile | 2 +- 4 files changed, 164 insertions(+), 138 deletions(-) diff --git a/DragonLisp.l b/DragonLisp.l index 213a244..f49c39a 100644 --- a/DragonLisp.l +++ b/DragonLisp.l @@ -15,6 +15,16 @@ using token = DragonLisp::DLParser::token; #define YY_USER_ACTION loc->columns(yyleng); +#ifdef DLDEBUG +#define PRINT_FUNC std::printf +#else // DLDEBUG +template +void printNothing(T _ign1, Ts... _ign2) { ; } +template +void printNothing(T _ign1) { ; } +#define PRINT_FUNC printNothing +#endif // DLDEBUG + %} %option yyclass="DragonLisp::DLScanner" @@ -72,14 +82,14 @@ defconstant [dD][eE][fF][cC][oO][nN][sS][tT][aA][nN][tT] {blank}+ { loc->step(); - // std::printf("Skipping blank\n"); + // PRINT_FUNC("Skipping blank\n"); // return token::TOKEN_SPACE; }; \n+ { loc->lines(yyleng); loc->step(); - // std::printf("Skipping newline\n"); + // PRINT_FUNC("Skipping newline\n"); // return token::TOKEN_SPACE; } @@ -92,7 +102,7 @@ defconstant [dD][eE][fF][cC][oO][nN][sS][tT][aA][nN][tT] if (seq_end_ptr - yytext < yyleng) throw DragonLisp::DLParser::syntax_error(*loc, "Invalid float scanned: [" + std::string(yytext, seq_end_ptr) + "], but provided [" + std::string(yytext) + "]"); yylval->emplace(n); - std::printf("Scanned float: %lf\n", n); + PRINT_FUNC("Scanned float: %lf\n", n); return token::TOKEN_FLOAT; } @@ -105,264 +115,264 @@ defconstant [dD][eE][fF][cC][oO][nN][sS][tT][aA][nN][tT] if (seq_end_ptr - yytext < yyleng) throw DragonLisp::DLParser::syntax_error(*loc, "Invalid integer scanned: [" + std::string(yytext, seq_end_ptr) + "], but provided [" + std::string(yytext) + "]"); yylval->emplace(n); - std::printf("Scanned integer: %lld\n", n); + PRINT_FUNC("Scanned integer: %lld\n", n); return token::TOKEN_INTEGER; }; "<=" { - std::printf("Scanned <=\n"); + PRINT_FUNC("Scanned <=\n"); return token::TOKEN_LESS_EQUAL; }; ">=" { - std::printf("Scanned >=\n"); + PRINT_FUNC("Scanned >=\n"); return token::TOKEN_GREATER_EQUAL; }; "<" { - std::printf("Scanned <\n"); + PRINT_FUNC("Scanned <\n"); return token::TOKEN_LESS; }; ">" { - std::printf("Scanned >\n"); + PRINT_FUNC("Scanned >\n"); return token::TOKEN_GREATER; }; "/=" { - std::printf("Scanned !=\n"); + PRINT_FUNC("Scanned !=\n"); return token::TOKEN_NOT_EQUAL; }; "=" { - std::printf("Scanned =\n"); + PRINT_FUNC("Scanned =\n"); return token::TOKEN_EQUAL; }; "(" { - std::printf("Scanned (\n"); + PRINT_FUNC("Scanned (\n"); return token::TOKEN_LPAREN; }; ")" { - std::printf("Scanned )\n"); + PRINT_FUNC("Scanned )\n"); return token::TOKEN_RPAREN; }; "+" { - std::printf("Scanned +\n"); + PRINT_FUNC("Scanned +\n"); return token::TOKEN_PLUS; }; "-" { - std::printf("Scanned -\n"); + PRINT_FUNC("Scanned -\n"); return token::TOKEN_MINUS; }; "*" { - std::printf("Scanned *\n"); + PRINT_FUNC("Scanned *\n"); return token::TOKEN_MULTIPLY; }; "/" { - std::printf("Scanned /\n"); + PRINT_FUNC("Scanned /\n"); return token::TOKEN_DIVIDE; }; {and} { - std::printf("Scanned and\n"); + PRINT_FUNC("Scanned and\n"); return token::TOKEN_AND; }; {or} { - std::printf("Scanned or\n"); + PRINT_FUNC("Scanned or\n"); return token::TOKEN_OR; }; {not} { - std::printf("Scanned not\n"); + PRINT_FUNC("Scanned not\n"); return token::TOKEN_NOT; }; {max} { - std::printf("Scanned max\n"); + PRINT_FUNC("Scanned max\n"); return token::TOKEN_MAX; }; {min} { - std::printf("Scanned min\n"); + PRINT_FUNC("Scanned min\n"); return token::TOKEN_MIN; }; {if} { - std::printf("Scanned if\n"); + PRINT_FUNC("Scanned if\n"); return token::TOKEN_IF; }; {logand} { - std::printf("Scanned logand\n"); + PRINT_FUNC("Scanned logand\n"); return token::TOKEN_LOGAND; }; {logior} { - std::printf("Scanned logior\n"); + PRINT_FUNC("Scanned logior\n"); return token::TOKEN_LOGIOR; }; {logxor} { - std::printf("Scanned logxor\n"); + PRINT_FUNC("Scanned logxor\n"); return token::TOKEN_LOGXOR; }; {lognor} { - std::printf("Scanned lognor\n"); + PRINT_FUNC("Scanned lognor\n"); return token::TOKEN_LOGNOR; }; {logeqv} { - std::printf("Scanned logeqv\n"); + PRINT_FUNC("Scanned logeqv\n"); return token::TOKEN_LOGEQV; }; {mod} { - std::printf("Scanned mod\n"); + PRINT_FUNC("Scanned mod\n"); return token::TOKEN_MOD; }; {rem} { - std::printf("Scanned rem\n"); + PRINT_FUNC("Scanned rem\n"); return token::TOKEN_REM; }; {incf} { - std::printf("Scanned incf\n"); + PRINT_FUNC("Scanned incf\n"); return token::TOKEN_INCF; }; {decf} { - std::printf("Scanned decf\n"); + PRINT_FUNC("Scanned decf\n"); return token::TOKEN_DECF; }; {defvar} { - std::printf("Scanned defvar\n"); + PRINT_FUNC("Scanned defvar\n"); return token::TOKEN_DEFVAR; }; {defun} { - std::printf("Scanned defun\n"); + PRINT_FUNC("Scanned defun\n"); return token::TOKEN_DEFUN; }; {print} { - std::printf("Scanned print\n"); + PRINT_FUNC("Scanned print\n"); return token::TOKEN_PRINT; }; {loop} { - std::printf("Scanned loop\n"); + PRINT_FUNC("Scanned loop\n"); return token::TOKEN_LOOP; }; {setq} { - std::printf("Scanned setq\n"); + PRINT_FUNC("Scanned setq\n"); return token::TOKEN_SETQ; }; {setf} { - std::printf("Scanned setf\n"); + PRINT_FUNC("Scanned setf\n"); return token::TOKEN_SETF; }; {quote} { - std::printf("Scanned quote\n"); + PRINT_FUNC("Scanned quote\n"); return token::TOKEN_QUOTE; }; {for} { - std::printf("Scanned for\n"); + PRINT_FUNC("Scanned for\n"); return token::TOKEN_FOR; }; {in} { - std::printf("Scanned in\n"); + PRINT_FUNC("Scanned in\n"); return token::TOKEN_IN; }; {from} { - std::printf("Scanned from\n"); + PRINT_FUNC("Scanned from\n"); return token::TOKEN_FROM; }; {to} { - std::printf("Scanned to\n"); + PRINT_FUNC("Scanned to\n"); return token::TOKEN_TO; }; {dotimes} { - std::printf("Scanned dotimes\n"); + PRINT_FUNC("Scanned dotimes\n"); return token::TOKEN_DOTIMES; }; {dolist} { - std::printf("Scanned dolist\n"); + PRINT_FUNC("Scanned dolist\n"); return token::TOKEN_DOLIST; }; {do} { - std::printf("Scanned do\n"); + PRINT_FUNC("Scanned do\n"); return token::TOKEN_DO; }; {aref} { - std::printf("Scanned aref\n"); + PRINT_FUNC("Scanned aref\n"); return token::TOKEN_AREF; }; {t} { - std::printf("Scanned t\n"); + PRINT_FUNC("Scanned t\n"); return token::TOKEN_T; }; {nil} { - std::printf("Scanned nil\n"); + PRINT_FUNC("Scanned nil\n"); return token::TOKEN_NIL; }; {return} { - std::printf("Scanned return\n"); + PRINT_FUNC("Scanned return\n"); return token::TOKEN_RETURN; }; {returnfrom} { - std::printf("Scanned returnfrom\n"); + PRINT_FUNC("Scanned returnfrom\n"); return token::TOKEN_RETURN_FROM; }; {makearray} { - std::printf("Scanned makearray\n"); + PRINT_FUNC("Scanned makearray\n"); return token::TOKEN_MAKE_ARRAY; }; {defconstant} { - std::printf("Scanned defconstant\n"); + PRINT_FUNC("Scanned defconstant\n"); return token::TOKEN_DEFCONSTANT; }; {string} { loc->step(); - std::printf("Scanned string: %s\n", yytext); + PRINT_FUNC("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); + PRINT_FUNC("Scanned comment: %s\n", yytext); loc->step(); } {id} { - std::printf("Scanned identifier: %s\n", yytext); + PRINT_FUNC("Scanned identifier: %s\n", yytext); yylval->emplace(std::string(yytext, yyleng)); return token::TOKEN_IDENTIFIER; }; diff --git a/DragonLisp.y b/DragonLisp.y index e6f2b1e..9bcd121 100644 --- a/DragonLisp.y +++ b/DragonLisp.y @@ -34,6 +34,16 @@ namespace DragonLisp { #undef yylex #define yylex scanner.yylex +#ifdef DLDEBUG +#define PRINT_FUNC std::printf +#else // DLDEBUG +template +void printNothing(T _ign1, Ts... _ign2) { ; } +template +void printNothing(T _ign1) { ; } +#define PRINT_FUNC printNothing +#endif // DLDEBUG + } %locations @@ -140,167 +150,167 @@ namespace DragonLisp { %% S - : END { std::printf("Parsed S -> END\n"); } - | statements END { std::printf("Parsed S -> statements END\n"); } + : END { PRINT_FUNC("Parsed S -> END\n"); } + | statements END { PRINT_FUNC("Parsed S -> statements END\n"); } ; statements - : statement { std::printf("Parsed statements -> statement\n"); drv.execute($1); } - | statements statement { std::printf("Parsed statements -> statements statement\n"); drv.execute($2); } + : statement { PRINT_FUNC("Parsed statements -> statement\n"); drv.execute($1); } + | statements statement { PRINT_FUNC("Parsed statements -> statements statement\n"); drv.execute($2); } ; statement - : R-Value { std::printf("Parsed statement -> R-Value\n"); $$ = $1; } - | func-def { std::printf("Parsed statement -> func-def\n"); $$ = $1; } + : R-Value { PRINT_FUNC("Parsed statement -> R-Value\n"); $$ = $1; } + | func-def { PRINT_FUNC("Parsed statement -> func-def\n"); $$ = $1; } ; array-ref - : LPAREN AREF IDENTIFIER R-Value RPAREN { std::printf("Parsed array-ref -> ( AREF IDENTIFIER R-Value )\n"); $$ = drv.constructLValueAST($3, $4); } + : LPAREN AREF IDENTIFIER R-Value RPAREN { PRINT_FUNC("Parsed array-ref -> ( AREF IDENTIFIER R-Value )\n"); $$ = drv.constructLValueAST($3, $4); } ; return-expr - : LPAREN RETURN R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN R-Value )\n"); $$ = drv.constructReturnAST($3); } - | LPAREN RETURN_FROM IDENTIFIER R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN_FROM IDENTIFIER R-Value )\n"); $$ = drv.constructReturnAST($4, $3); } + : LPAREN RETURN R-Value RPAREN { PRINT_FUNC("Parsed return-expr -> ( RETURN R-Value )\n"); $$ = drv.constructReturnAST($3); } + | LPAREN RETURN_FROM IDENTIFIER R-Value RPAREN { PRINT_FUNC("Parsed return-expr -> ( RETURN_FROM IDENTIFIER R-Value )\n"); $$ = drv.constructReturnAST($4, $3); } ; func-body-expr - : return-expr { std::printf("Parsed func-body -> return-expr\n"); $$ = $1; } - | R-Value { std::printf("Parsed func-body -> R-Value\n"); $$ = $1; } + : return-expr { PRINT_FUNC("Parsed func-body -> return-expr\n"); $$ = $1; } + | R-Value { PRINT_FUNC("Parsed func-body -> R-Value\n"); $$ = $1; } ; func-body - : func-body-expr { std::printf("Parsed func-body -> func-body-expr\n"); $$ = { $1 }; } - | func-body func-body-expr { std::printf("Parsed func-body -> func-body func-body-expr\n"); $1.push_back($2); $$ = $1; } + : func-body-expr { PRINT_FUNC("Parsed func-body -> func-body-expr\n"); $$ = { $1 }; } + | func-body func-body-expr { PRINT_FUNC("Parsed func-body -> func-body func-body-expr\n"); $1.push_back($2); $$ = $1; } ; L-Value - : IDENTIFIER { std::printf("Parsed L-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); } - | array-ref { std::printf("Parsed L-Value -> array-ref\n"); $$ = $1; } + : IDENTIFIER { PRINT_FUNC("Parsed L-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); } + | array-ref { PRINT_FUNC("Parsed L-Value -> array-ref\n"); $$ = $1; } ; R-Value - : IDENTIFIER { std::printf("Parsed R-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); } - | S-Expr { std::printf("Parsed R-Value -> S-Expr\n"); $$ = $1; } - | INTEGER { std::printf("Parsed R-Value -> INTEGER\n"); $$ = drv.constructLiteralAST($1); } - | FLOAT { std::printf("Parsed R-Value -> FLOAT\n"); $$ = drv.constructLiteralAST($1); } - | STRING { std::printf("Parsed R-Value -> STRING\n"); $$ = drv.constructLiteralAST($1); } - | array-ref { std::printf("Parsed R-Value -> array-ref\n"); $$ = $1; } - | NIL { std::printf("Parsed R-Value -> NIL\n"); $$ = drv.constructLiteralAST(false); } - | T { std::printf("Parsed R-Value -> T\n"); $$ = drv.constructLiteralAST(true); } + : IDENTIFIER { PRINT_FUNC("Parsed R-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); } + | S-Expr { PRINT_FUNC("Parsed R-Value -> S-Expr\n"); $$ = $1; } + | INTEGER { PRINT_FUNC("Parsed R-Value -> INTEGER\n"); $$ = drv.constructLiteralAST($1); } + | FLOAT { PRINT_FUNC("Parsed R-Value -> FLOAT\n"); $$ = drv.constructLiteralAST($1); } + | STRING { PRINT_FUNC("Parsed R-Value -> STRING\n"); $$ = drv.constructLiteralAST($1); } + | array-ref { PRINT_FUNC("Parsed R-Value -> array-ref\n"); $$ = $1; } + | NIL { PRINT_FUNC("Parsed R-Value -> NIL\n"); $$ = drv.constructLiteralAST(false); } + | T { PRINT_FUNC("Parsed R-Value -> T\n"); $$ = drv.constructLiteralAST(true); } ; R-Value-list - : R-Value { std::printf("Parsed R-Value-list -> R-Value\n"); $$ = { $1 }; } - | R-Value-list R-Value { std::printf("Parsed R-Value-list -> R-Value-list R-Value\n"); $1.push_back($2); $$ = $1; } + : R-Value { PRINT_FUNC("Parsed R-Value-list -> R-Value\n"); $$ = { $1 }; } + | R-Value-list R-Value { PRINT_FUNC("Parsed R-Value-list -> R-Value-list R-Value\n"); $1.push_back($2); $$ = $1; } ; S-Expr - : LPAREN S-Expr-helper RPAREN { std::printf("Parsed S-Expr -> ( S-Expr-helper )\n"); $$ = $2; } - | LPAREN RPAREN { std::printf("Parsed S-Expr -> ()\n"); $$ = drv.constructLiteralAST(false); } + : LPAREN S-Expr-helper RPAREN { PRINT_FUNC("Parsed S-Expr -> ( S-Expr-helper )\n"); $$ = $2; } + | LPAREN RPAREN { PRINT_FUNC("Parsed S-Expr -> ()\n"); $$ = drv.constructLiteralAST(false); } ; S-Expr-helper - : S-Expr-var-op { std::printf("Parsed S-Expr-helper -> S-Expr-var-op\n"); $$ = $1; } - | S-Expr-Lval-op { std::printf("Parsed S-Expr-helper -> S-Expr-Lval-op\n"); $$ = $1; } - | S-Expr-unary { std::printf("Parsed S-Expr-helper -> S-Expr-unary\n"); $$ = $1; } - | S-Expr-binary { std::printf("Parsed S-Expr-helper -> S-Expr-binary\n"); $$ = $1; } - | S-Expr-list { std::printf("Parsed S-Expr-helper -> S-Expr-list\n"); $$ = $1; } - | S-Expr-if { std::printf("Parsed S-Expr-helper -> S-Expr-if\n"); $$ = $1; } - | S-Expr-loop { std::printf("Parsed S-Expr-helper -> S-Expr-loop\n"); $$ = $1; } - | S-Expr-func-call { std::printf("Parsed S-Expr-helper -> S-Expr-func-call\n"); $$ = $1; } + : S-Expr-var-op { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-var-op\n"); $$ = $1; } + | S-Expr-Lval-op { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-Lval-op\n"); $$ = $1; } + | S-Expr-unary { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-unary\n"); $$ = $1; } + | S-Expr-binary { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-binary\n"); $$ = $1; } + | S-Expr-list { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-list\n"); $$ = $1; } + | S-Expr-if { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-if\n"); $$ = $1; } + | S-Expr-loop { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-loop\n"); $$ = $1; } + | S-Expr-func-call { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-func-call\n"); $$ = $1; } ; S-Expr-var-op - : var-op-tokens IDENTIFIER R-Value { std::printf("Parsed S-Expr-var-op -> var-op-tokens IDENTIFIER R-Value\n"); $$ = drv.constructVarOpAST($2, $3, $1); } + : var-op-tokens IDENTIFIER R-Value { PRINT_FUNC("Parsed S-Expr-var-op -> var-op-tokens IDENTIFIER R-Value\n"); $$ = drv.constructVarOpAST($2, $3, $1); } ; var-op-tokens - : DEFVAR { std::printf("Parsed var-op-tokens -> DEFVAR\n"); $$ = DragonLisp::Token::DEFVAR; } - | SETQ { std::printf("Parsed var-op-tokens -> SETQ\n"); $$ = DragonLisp::Token::SETQ; } + : DEFVAR { PRINT_FUNC("Parsed var-op-tokens -> DEFVAR\n"); $$ = DragonLisp::Token::DEFVAR; } + | SETQ { PRINT_FUNC("Parsed var-op-tokens -> SETQ\n"); $$ = DragonLisp::Token::SETQ; } ; 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"); $$ = drv.constructLValOpAST($2, $3, $1); } + : lval-op-tokens L-Value R-Value { PRINT_FUNC("Parsed S-Expr-Lval-op -> lval-op-tokens L-Value R-Value\n"); $$ = drv.constructLValOpAST($2, $3, $1); } ; lval-op-tokens - : SETF { std::printf("Parsed lval-op-tokens -> SETF\n"); $$ = DragonLisp::Token::SETF; } - | INCF { std::printf("Parsed lval-op-tokens -> INCF\n"); $$ = DragonLisp::Token::INCF; } - | DECF { std::printf("Parsed lval-op-tokens -> DECF\n"); $$ = DragonLisp::Token::DECF; } + : SETF { PRINT_FUNC("Parsed lval-op-tokens -> SETF\n"); $$ = DragonLisp::Token::SETF; } + | INCF { PRINT_FUNC("Parsed lval-op-tokens -> INCF\n"); $$ = DragonLisp::Token::INCF; } + | DECF { PRINT_FUNC("Parsed lval-op-tokens -> DECF\n"); $$ = DragonLisp::Token::DECF; } ; S-Expr-unary - : unary-tokens R-Value { std::printf("Parsed S-Expr-unary -> unary-tokens R-Value\n"); $$ = drv.constructUnaryExprAST($2, $1); } + : unary-tokens R-Value { PRINT_FUNC("Parsed S-Expr-unary -> unary-tokens R-Value\n"); $$ = drv.constructUnaryExprAST($2, $1); } ; unary-tokens - : NOT { std::printf("Parsed unary-tokens -> NOT\n"); $$ = DragonLisp::Token::NOT; } - | PRINT { std::printf("Parsed unary-tokens -> PRINT\n"); $$ = DragonLisp::Token::PRINT; } - | MAKE_ARRAY { std::printf("Parsed unary-tokens -> MAKE_ARRAY\n"); $$ = DragonLisp::Token::MAKE_ARRAY; } + : NOT { PRINT_FUNC("Parsed unary-tokens -> NOT\n"); $$ = DragonLisp::Token::NOT; } + | PRINT { PRINT_FUNC("Parsed unary-tokens -> PRINT\n"); $$ = DragonLisp::Token::PRINT; } + | MAKE_ARRAY { PRINT_FUNC("Parsed unary-tokens -> MAKE_ARRAY\n"); $$ = DragonLisp::Token::MAKE_ARRAY; } ; S-Expr-binary - : binary-tokens R-Value R-Value { std::printf("Parsed S-Expr-binary -> binary-tokens R-Value R-Value\n"); $$ = drv.constructBinaryExprAST($2, $3, $1); } + : binary-tokens R-Value R-Value { PRINT_FUNC("Parsed S-Expr-binary -> binary-tokens R-Value R-Value\n"); $$ = drv.constructBinaryExprAST($2, $3, $1); } ; binary-tokens - : LESS { std::printf("Parsed binary-tokens -> LESS\n"); $$ = DragonLisp::Token::LESS; } - | LESS_EQUAL { std::printf("Parsed binary-tokens -> LESS_EQUAL\n"); $$ = DragonLisp::Token::LESS_EQUAL; } - | GREATER { std::printf("Parsed binary-tokens -> GREATER\n"); $$ = DragonLisp::Token::GREATER; } - | GREATER_EQUAL { std::printf("Parsed binary-tokens -> GREATER_EQUAL\n"); $$ = DragonLisp::Token::GREATER_EQUAL; } - | LOGNOR { std::printf("Parsed binary-tokens -> LOGNOR\n"); $$ = DragonLisp::Token::LOGNOR; } - | MOD { std::printf("Parsed binary-tokens -> MOD\n"); $$ = DragonLisp::Token::MOD; } - | REM { std::printf("Parsed binary-tokens -> REM\n"); $$ = DragonLisp::Token::MOD; /* This is the same as MOD. */ } + : LESS { PRINT_FUNC("Parsed binary-tokens -> LESS\n"); $$ = DragonLisp::Token::LESS; } + | LESS_EQUAL { PRINT_FUNC("Parsed binary-tokens -> LESS_EQUAL\n"); $$ = DragonLisp::Token::LESS_EQUAL; } + | GREATER { PRINT_FUNC("Parsed binary-tokens -> GREATER\n"); $$ = DragonLisp::Token::GREATER; } + | GREATER_EQUAL { PRINT_FUNC("Parsed binary-tokens -> GREATER_EQUAL\n"); $$ = DragonLisp::Token::GREATER_EQUAL; } + | LOGNOR { PRINT_FUNC("Parsed binary-tokens -> LOGNOR\n"); $$ = DragonLisp::Token::LOGNOR; } + | MOD { PRINT_FUNC("Parsed binary-tokens -> MOD\n"); $$ = DragonLisp::Token::MOD; } + | REM { PRINT_FUNC("Parsed binary-tokens -> REM\n"); $$ = DragonLisp::Token::MOD; /* This is the same as MOD. */ } ; S-Expr-list - : list-tokens R-Value-list { std::printf("Parsed S-Expr-list -> list-tokens R-Value-list\n"); $$ = drv.constructListExprAST($2, $1); } + : list-tokens R-Value-list { PRINT_FUNC("Parsed S-Expr-list -> list-tokens R-Value-list\n"); $$ = drv.constructListExprAST($2, $1); } ; list-tokens - : EQUAL { std::printf("Parsed list-tokens -> EQUAL\n"); $$ = DragonLisp::Token::EQUAL; } - | NOT_EQUAL { std::printf("Parsed list-tokens -> NOT_EQUAL\n"); $$ = DragonLisp::Token::NOT_EQUAL; } - | AND { std::printf("Parsed list-tokens -> AND\n"); $$ = DragonLisp::Token::AND; } - | OR { std::printf("Parsed list-tokens -> OR\n"); $$ = DragonLisp::Token::OR; } - | MAX { std::printf("Parsed list-tokens -> MAX\n"); $$ = DragonLisp::Token::MAX; } - | MIN { std::printf("Parsed list-tokens -> MIN\n"); $$ = DragonLisp::Token::MIN; } - | LOGAND { std::printf("Parsed list-tokens -> LOGAND\n"); $$ = DragonLisp::Token::LOGAND; } - | LOGIOR { std::printf("Parsed list-tokens -> LOGIOR\n"); $$ = DragonLisp::Token::LOGIOR; } - | LOGXOR { std::printf("Parsed list-tokens -> LOGXOR\n"); $$ = DragonLisp::Token::LOGXOR; } - | LOGEQV { std::printf("Parsed list-tokens -> LOGEQV\n"); $$ = DragonLisp::Token::LOGEQV; } - | PLUS { std::printf("Parsed list-tokens -> PLUS\n"); $$ = DragonLisp::Token::PLUS; } - | MINUS { std::printf("Parsed list-tokens -> MINUS\n"); $$ = DragonLisp::Token::MINUS; } - | MULTIPLY { std::printf("Parsed list-tokens -> MULTIPLY\n"); $$ = DragonLisp::Token::MULTIPLY; } - | DIVIDE { std::printf("Parsed list-tokens -> DIVIDE\n"); $$ = DragonLisp::Token::DIVIDE; } + : EQUAL { PRINT_FUNC("Parsed list-tokens -> EQUAL\n"); $$ = DragonLisp::Token::EQUAL; } + | NOT_EQUAL { PRINT_FUNC("Parsed list-tokens -> NOT_EQUAL\n"); $$ = DragonLisp::Token::NOT_EQUAL; } + | AND { PRINT_FUNC("Parsed list-tokens -> AND\n"); $$ = DragonLisp::Token::AND; } + | OR { PRINT_FUNC("Parsed list-tokens -> OR\n"); $$ = DragonLisp::Token::OR; } + | MAX { PRINT_FUNC("Parsed list-tokens -> MAX\n"); $$ = DragonLisp::Token::MAX; } + | MIN { PRINT_FUNC("Parsed list-tokens -> MIN\n"); $$ = DragonLisp::Token::MIN; } + | LOGAND { PRINT_FUNC("Parsed list-tokens -> LOGAND\n"); $$ = DragonLisp::Token::LOGAND; } + | LOGIOR { PRINT_FUNC("Parsed list-tokens -> LOGIOR\n"); $$ = DragonLisp::Token::LOGIOR; } + | LOGXOR { PRINT_FUNC("Parsed list-tokens -> LOGXOR\n"); $$ = DragonLisp::Token::LOGXOR; } + | LOGEQV { PRINT_FUNC("Parsed list-tokens -> LOGEQV\n"); $$ = DragonLisp::Token::LOGEQV; } + | PLUS { PRINT_FUNC("Parsed list-tokens -> PLUS\n"); $$ = DragonLisp::Token::PLUS; } + | MINUS { PRINT_FUNC("Parsed list-tokens -> MINUS\n"); $$ = DragonLisp::Token::MINUS; } + | MULTIPLY { PRINT_FUNC("Parsed list-tokens -> MULTIPLY\n"); $$ = DragonLisp::Token::MULTIPLY; } + | DIVIDE { PRINT_FUNC("Parsed list-tokens -> DIVIDE\n"); $$ = DragonLisp::Token::DIVIDE; } ; S-Expr-if - : IF R-Value func-body-expr func-body-expr { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr func-body-expr\n"); $$ = drv.constructIfAST($2, $3, $4); } - | IF R-Value func-body-expr { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr\n"); $$ = drv.constructIfAST($2, $3, nullptr); } + : IF R-Value func-body-expr func-body-expr { PRINT_FUNC("Parsed S-Expr-if -> IF R-Value func-body-expr func-body-expr\n"); $$ = drv.constructIfAST($2, $3, $4); } + | IF R-Value func-body-expr { PRINT_FUNC("Parsed S-Expr-if -> IF R-Value func-body-expr\n"); $$ = drv.constructIfAST($2, $3, nullptr); } ; S-Expr-loop - : LOOP func-body { std::printf("Parsed S-Expr-loop -> LOOP func-body\n"); $$ = drv.constructLoopAST($2); } - | LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body { std::printf("Parsed S-Expr-loop -> LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body\n"); $$ = drv.constructLoopAST($3, $5, $7, $9); } - | DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body { std::printf("Parsed S-Expr-loop -> DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body\n"); $$ = drv.constructLoopAST($3, $4, $6); } + : LOOP func-body { PRINT_FUNC("Parsed S-Expr-loop -> LOOP func-body\n"); $$ = drv.constructLoopAST($2); } + | LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body { PRINT_FUNC("Parsed S-Expr-loop -> LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body\n"); $$ = drv.constructLoopAST($3, $5, $7, $9); } + | DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body { PRINT_FUNC("Parsed S-Expr-loop -> DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body\n"); $$ = drv.constructLoopAST($3, $4, $6); } ; func-def - : LPAREN DEFUN IDENTIFIER func-arg-list func-body RPAREN { std::printf("Parsed func-def -> ( DEFUN IDENTIFIER func-arg-list func-body )\n"); $$ = drv.constructFuncDefAST($3, $4, $5); } + : LPAREN DEFUN IDENTIFIER func-arg-list func-body RPAREN { PRINT_FUNC("Parsed func-def -> ( DEFUN IDENTIFIER func-arg-list func-body )\n"); $$ = drv.constructFuncDefAST($3, $4, $5); } ; func-arg-list - : LPAREN RPAREN { std::printf("Parsed func-arg-list -> ( )\n"); $$ = {}; } - | LPAREN identifier-list RPAREN { std::printf("Parsed func-arg-list -> ( identifier-list )\n"); $$ = $2; } + : LPAREN RPAREN { PRINT_FUNC("Parsed func-arg-list -> ( )\n"); $$ = {}; } + | LPAREN identifier-list RPAREN { PRINT_FUNC("Parsed func-arg-list -> ( identifier-list )\n"); $$ = $2; } identifier-list - : identifier-list IDENTIFIER { std::printf("Parsed identifier-list -> identifier-list IDENTIFIER\n"); $1.push_back($2); $$ = $1; } - | IDENTIFIER { std::printf("Parsed identifier-list -> IDENTIFIER\n"); $$ = { $1 }; } + : identifier-list IDENTIFIER { PRINT_FUNC("Parsed identifier-list -> identifier-list IDENTIFIER\n"); $1.push_back($2); $$ = $1; } + | IDENTIFIER { PRINT_FUNC("Parsed identifier-list -> IDENTIFIER\n"); $$ = { $1 }; } ; S-Expr-func-call - : IDENTIFIER R-Value-list { std::printf("Parsed S-Expr-func-call -> IDENTIFIER R-Value-list\n"); $$ = drv.constructFuncCallAST($1, $2); } - | IDENTIFIER { std::printf("Parsed S-Expr-func-call -> IDENTIFIER\n"); $$ = drv.constructFuncCallAST($1, {}); } + : IDENTIFIER R-Value-list { PRINT_FUNC("Parsed S-Expr-func-call -> IDENTIFIER R-Value-list\n"); $$ = drv.constructFuncCallAST($1, $2); } + | IDENTIFIER { PRINT_FUNC("Parsed S-Expr-func-call -> IDENTIFIER\n"); $$ = drv.constructFuncCallAST($1, {}); } ; %% diff --git a/DragonLispDriver.cpp b/DragonLispDriver.cpp index 2cffaf2..7448bf9 100644 --- a/DragonLispDriver.cpp +++ b/DragonLispDriver.cpp @@ -34,7 +34,13 @@ int DLDriver::parse(std::istream& in, const std::string& s) { delete this->context; this->context = new Context(nullptr); - this->parser->set_debug_level(1); + this->parser->set_debug_level( +#ifdef DLDEBUG + 1 +#else + 0 +#endif + ); return this->parser->parse(); } diff --git a/Makefile b/Makefile index 11389b2..20a9e34 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ compile: lexer_compile parser_compile misc_compile $(CXX) $(CXXFLAGS) -o $(OUTPUT) $(OBJS) parser.o lexer.o $(LIBS) compile_debug: lexer parser - $(CXX) $(CXXFLAGS) -o $(OUTPUT) \ + $(CXX) $(CXXFLAGS) -DDLDEBUG -o $(OUTPUT) \ main.cpp \ DragonLispDriver.cpp \ AST.cpp \