add: option to mute noisy debug info

This commit is contained in:
Eatswap 2022-12-13 18:18:46 +08:00
parent d079d8073c
commit e3ff931c5d
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
4 changed files with 164 additions and 138 deletions

View File

@ -15,6 +15,16 @@ using token = DragonLisp::DLParser::token;
#define YY_USER_ACTION loc->columns(yyleng); #define YY_USER_ACTION loc->columns(yyleng);
#ifdef DLDEBUG
#define PRINT_FUNC std::printf
#else // DLDEBUG
template<typename T, typename... Ts>
void printNothing(T _ign1, Ts... _ign2) { ; }
template<typename T>
void printNothing(T _ign1) { ; }
#define PRINT_FUNC printNothing
#endif // DLDEBUG
%} %}
%option yyclass="DragonLisp::DLScanner" %option yyclass="DragonLisp::DLScanner"
@ -72,14 +82,14 @@ defconstant [dD][eE][fF][cC][oO][nN][sS][tT][aA][nN][tT]
{blank}+ { {blank}+ {
loc->step(); loc->step();
// std::printf("Skipping blank\n"); // PRINT_FUNC("Skipping blank\n");
// return token::TOKEN_SPACE; // return token::TOKEN_SPACE;
}; };
\n+ { \n+ {
loc->lines(yyleng); loc->lines(yyleng);
loc->step(); loc->step();
// std::printf("Skipping newline\n"); // PRINT_FUNC("Skipping newline\n");
// return token::TOKEN_SPACE; // 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) 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) + "]"); throw DragonLisp::DLParser::syntax_error(*loc, "Invalid float scanned: [" + std::string(yytext, seq_end_ptr) + "], but provided [" + std::string(yytext) + "]");
yylval->emplace<double>(n); yylval->emplace<double>(n);
std::printf("Scanned float: %lf\n", n); PRINT_FUNC("Scanned float: %lf\n", n);
return token::TOKEN_FLOAT; 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) 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) + "]"); throw DragonLisp::DLParser::syntax_error(*loc, "Invalid integer scanned: [" + std::string(yytext, seq_end_ptr) + "], but provided [" + std::string(yytext) + "]");
yylval->emplace<int64_t>(n); yylval->emplace<int64_t>(n);
std::printf("Scanned integer: %lld\n", n); PRINT_FUNC("Scanned integer: %lld\n", n);
return token::TOKEN_INTEGER; return token::TOKEN_INTEGER;
}; };
"<=" { "<=" {
std::printf("Scanned <=\n"); PRINT_FUNC("Scanned <=\n");
return token::TOKEN_LESS_EQUAL; return token::TOKEN_LESS_EQUAL;
}; };
">=" { ">=" {
std::printf("Scanned >=\n"); PRINT_FUNC("Scanned >=\n");
return token::TOKEN_GREATER_EQUAL; return token::TOKEN_GREATER_EQUAL;
}; };
"<" { "<" {
std::printf("Scanned <\n"); PRINT_FUNC("Scanned <\n");
return token::TOKEN_LESS; return token::TOKEN_LESS;
}; };
">" { ">" {
std::printf("Scanned >\n"); PRINT_FUNC("Scanned >\n");
return token::TOKEN_GREATER; return token::TOKEN_GREATER;
}; };
"/=" { "/=" {
std::printf("Scanned !=\n"); PRINT_FUNC("Scanned !=\n");
return token::TOKEN_NOT_EQUAL; return token::TOKEN_NOT_EQUAL;
}; };
"=" { "=" {
std::printf("Scanned =\n"); PRINT_FUNC("Scanned =\n");
return token::TOKEN_EQUAL; return token::TOKEN_EQUAL;
}; };
"(" { "(" {
std::printf("Scanned (\n"); PRINT_FUNC("Scanned (\n");
return token::TOKEN_LPAREN; return token::TOKEN_LPAREN;
}; };
")" { ")" {
std::printf("Scanned )\n"); PRINT_FUNC("Scanned )\n");
return token::TOKEN_RPAREN; return token::TOKEN_RPAREN;
}; };
"+" { "+" {
std::printf("Scanned +\n"); PRINT_FUNC("Scanned +\n");
return token::TOKEN_PLUS; return token::TOKEN_PLUS;
}; };
"-" { "-" {
std::printf("Scanned -\n"); PRINT_FUNC("Scanned -\n");
return token::TOKEN_MINUS; return token::TOKEN_MINUS;
}; };
"*" { "*" {
std::printf("Scanned *\n"); PRINT_FUNC("Scanned *\n");
return token::TOKEN_MULTIPLY; return token::TOKEN_MULTIPLY;
}; };
"/" { "/" {
std::printf("Scanned /\n"); PRINT_FUNC("Scanned /\n");
return token::TOKEN_DIVIDE; return token::TOKEN_DIVIDE;
}; };
{and} { {and} {
std::printf("Scanned and\n"); PRINT_FUNC("Scanned and\n");
return token::TOKEN_AND; return token::TOKEN_AND;
}; };
{or} { {or} {
std::printf("Scanned or\n"); PRINT_FUNC("Scanned or\n");
return token::TOKEN_OR; return token::TOKEN_OR;
}; };
{not} { {not} {
std::printf("Scanned not\n"); PRINT_FUNC("Scanned not\n");
return token::TOKEN_NOT; return token::TOKEN_NOT;
}; };
{max} { {max} {
std::printf("Scanned max\n"); PRINT_FUNC("Scanned max\n");
return token::TOKEN_MAX; return token::TOKEN_MAX;
}; };
{min} { {min} {
std::printf("Scanned min\n"); PRINT_FUNC("Scanned min\n");
return token::TOKEN_MIN; return token::TOKEN_MIN;
}; };
{if} { {if} {
std::printf("Scanned if\n"); PRINT_FUNC("Scanned if\n");
return token::TOKEN_IF; return token::TOKEN_IF;
}; };
{logand} { {logand} {
std::printf("Scanned logand\n"); PRINT_FUNC("Scanned logand\n");
return token::TOKEN_LOGAND; return token::TOKEN_LOGAND;
}; };
{logior} { {logior} {
std::printf("Scanned logior\n"); PRINT_FUNC("Scanned logior\n");
return token::TOKEN_LOGIOR; return token::TOKEN_LOGIOR;
}; };
{logxor} { {logxor} {
std::printf("Scanned logxor\n"); PRINT_FUNC("Scanned logxor\n");
return token::TOKEN_LOGXOR; return token::TOKEN_LOGXOR;
}; };
{lognor} { {lognor} {
std::printf("Scanned lognor\n"); PRINT_FUNC("Scanned lognor\n");
return token::TOKEN_LOGNOR; return token::TOKEN_LOGNOR;
}; };
{logeqv} { {logeqv} {
std::printf("Scanned logeqv\n"); PRINT_FUNC("Scanned logeqv\n");
return token::TOKEN_LOGEQV; return token::TOKEN_LOGEQV;
}; };
{mod} { {mod} {
std::printf("Scanned mod\n"); PRINT_FUNC("Scanned mod\n");
return token::TOKEN_MOD; return token::TOKEN_MOD;
}; };
{rem} { {rem} {
std::printf("Scanned rem\n"); PRINT_FUNC("Scanned rem\n");
return token::TOKEN_REM; return token::TOKEN_REM;
}; };
{incf} { {incf} {
std::printf("Scanned incf\n"); PRINT_FUNC("Scanned incf\n");
return token::TOKEN_INCF; return token::TOKEN_INCF;
}; };
{decf} { {decf} {
std::printf("Scanned decf\n"); PRINT_FUNC("Scanned decf\n");
return token::TOKEN_DECF; return token::TOKEN_DECF;
}; };
{defvar} { {defvar} {
std::printf("Scanned defvar\n"); PRINT_FUNC("Scanned defvar\n");
return token::TOKEN_DEFVAR; return token::TOKEN_DEFVAR;
}; };
{defun} { {defun} {
std::printf("Scanned defun\n"); PRINT_FUNC("Scanned defun\n");
return token::TOKEN_DEFUN; return token::TOKEN_DEFUN;
}; };
{print} { {print} {
std::printf("Scanned print\n"); PRINT_FUNC("Scanned print\n");
return token::TOKEN_PRINT; return token::TOKEN_PRINT;
}; };
{loop} { {loop} {
std::printf("Scanned loop\n"); PRINT_FUNC("Scanned loop\n");
return token::TOKEN_LOOP; return token::TOKEN_LOOP;
}; };
{setq} { {setq} {
std::printf("Scanned setq\n"); PRINT_FUNC("Scanned setq\n");
return token::TOKEN_SETQ; return token::TOKEN_SETQ;
}; };
{setf} { {setf} {
std::printf("Scanned setf\n"); PRINT_FUNC("Scanned setf\n");
return token::TOKEN_SETF; return token::TOKEN_SETF;
}; };
{quote} { {quote} {
std::printf("Scanned quote\n"); PRINT_FUNC("Scanned quote\n");
return token::TOKEN_QUOTE; return token::TOKEN_QUOTE;
}; };
{for} { {for} {
std::printf("Scanned for\n"); PRINT_FUNC("Scanned for\n");
return token::TOKEN_FOR; return token::TOKEN_FOR;
}; };
{in} { {in} {
std::printf("Scanned in\n"); PRINT_FUNC("Scanned in\n");
return token::TOKEN_IN; return token::TOKEN_IN;
}; };
{from} { {from} {
std::printf("Scanned from\n"); PRINT_FUNC("Scanned from\n");
return token::TOKEN_FROM; return token::TOKEN_FROM;
}; };
{to} { {to} {
std::printf("Scanned to\n"); PRINT_FUNC("Scanned to\n");
return token::TOKEN_TO; return token::TOKEN_TO;
}; };
{dotimes} { {dotimes} {
std::printf("Scanned dotimes\n"); PRINT_FUNC("Scanned dotimes\n");
return token::TOKEN_DOTIMES; return token::TOKEN_DOTIMES;
}; };
{dolist} { {dolist} {
std::printf("Scanned dolist\n"); PRINT_FUNC("Scanned dolist\n");
return token::TOKEN_DOLIST; return token::TOKEN_DOLIST;
}; };
{do} { {do} {
std::printf("Scanned do\n"); PRINT_FUNC("Scanned do\n");
return token::TOKEN_DO; return token::TOKEN_DO;
}; };
{aref} { {aref} {
std::printf("Scanned aref\n"); PRINT_FUNC("Scanned aref\n");
return token::TOKEN_AREF; return token::TOKEN_AREF;
}; };
{t} { {t} {
std::printf("Scanned t\n"); PRINT_FUNC("Scanned t\n");
return token::TOKEN_T; return token::TOKEN_T;
}; };
{nil} { {nil} {
std::printf("Scanned nil\n"); PRINT_FUNC("Scanned nil\n");
return token::TOKEN_NIL; return token::TOKEN_NIL;
}; };
{return} { {return} {
std::printf("Scanned return\n"); PRINT_FUNC("Scanned return\n");
return token::TOKEN_RETURN; return token::TOKEN_RETURN;
}; };
{returnfrom} { {returnfrom} {
std::printf("Scanned returnfrom\n"); PRINT_FUNC("Scanned returnfrom\n");
return token::TOKEN_RETURN_FROM; return token::TOKEN_RETURN_FROM;
}; };
{makearray} { {makearray} {
std::printf("Scanned makearray\n"); PRINT_FUNC("Scanned makearray\n");
return token::TOKEN_MAKE_ARRAY; return token::TOKEN_MAKE_ARRAY;
}; };
{defconstant} { {defconstant} {
std::printf("Scanned defconstant\n"); PRINT_FUNC("Scanned defconstant\n");
return token::TOKEN_DEFCONSTANT; return token::TOKEN_DEFCONSTANT;
}; };
{string} { {string} {
loc->step(); loc->step();
std::printf("Scanned string: %s\n", yytext); PRINT_FUNC("Scanned string: %s\n", yytext);
yylval->emplace<std::string>(std::string(yytext + 1, yyleng - 2)); yylval->emplace<std::string>(std::string(yytext + 1, yyleng - 2));
return token::TOKEN_STRING; return token::TOKEN_STRING;
} }
{comment} { {comment} {
std::printf("Scanned comment: %s\n", yytext); PRINT_FUNC("Scanned comment: %s\n", yytext);
loc->step(); loc->step();
} }
{id} { {id} {
std::printf("Scanned identifier: %s\n", yytext); PRINT_FUNC("Scanned identifier: %s\n", yytext);
yylval->emplace<std::string>(std::string(yytext, yyleng)); yylval->emplace<std::string>(std::string(yytext, yyleng));
return token::TOKEN_IDENTIFIER; return token::TOKEN_IDENTIFIER;
}; };

View File

@ -34,6 +34,16 @@ namespace DragonLisp {
#undef yylex #undef yylex
#define yylex scanner.yylex #define yylex scanner.yylex
#ifdef DLDEBUG
#define PRINT_FUNC std::printf
#else // DLDEBUG
template<typename T, typename... Ts>
void printNothing(T _ign1, Ts... _ign2) { ; }
template<typename T>
void printNothing(T _ign1) { ; }
#define PRINT_FUNC printNothing
#endif // DLDEBUG
} }
%locations %locations
@ -140,167 +150,167 @@ namespace DragonLisp {
%% %%
S S
: END { std::printf("Parsed S -> END\n"); } : END { PRINT_FUNC("Parsed S -> END\n"); }
| statements END { std::printf("Parsed S -> statements END\n"); } | statements END { PRINT_FUNC("Parsed S -> statements END\n"); }
; ;
statements statements
: statement { std::printf("Parsed statements -> statement\n"); drv.execute($1); } : statement { PRINT_FUNC("Parsed statements -> statement\n"); drv.execute($1); }
| statements statement { std::printf("Parsed statements -> statements statement\n"); drv.execute($2); } | statements statement { PRINT_FUNC("Parsed statements -> statements statement\n"); drv.execute($2); }
; ;
statement statement
: R-Value { std::printf("Parsed statement -> R-Value\n"); $$ = $1; } : R-Value { PRINT_FUNC("Parsed statement -> R-Value\n"); $$ = $1; }
| func-def { std::printf("Parsed statement -> func-def\n"); $$ = $1; } | func-def { PRINT_FUNC("Parsed statement -> func-def\n"); $$ = $1; }
; ;
array-ref 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 return-expr
: LPAREN RETURN R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN R-Value )\n"); $$ = drv.constructReturnAST($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 { std::printf("Parsed return-expr -> ( RETURN_FROM IDENTIFIER R-Value )\n"); $$ = drv.constructReturnAST($4, $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 func-body-expr
: return-expr { std::printf("Parsed func-body -> return-expr\n"); $$ = $1; } : return-expr { PRINT_FUNC("Parsed func-body -> return-expr\n"); $$ = $1; }
| R-Value { std::printf("Parsed func-body -> R-Value\n"); $$ = $1; } | R-Value { PRINT_FUNC("Parsed func-body -> R-Value\n"); $$ = $1; }
; ;
func-body func-body
: func-body-expr { std::printf("Parsed func-body -> func-body-expr\n"); $$ = { $1 }; } : func-body-expr { PRINT_FUNC("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 func-body-expr { PRINT_FUNC("Parsed func-body -> func-body func-body-expr\n"); $1.push_back($2); $$ = $1; }
; ;
L-Value L-Value
: IDENTIFIER { std::printf("Parsed L-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); } : IDENTIFIER { PRINT_FUNC("Parsed L-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); }
| array-ref { std::printf("Parsed L-Value -> array-ref\n"); $$ = $1; } | array-ref { PRINT_FUNC("Parsed L-Value -> array-ref\n"); $$ = $1; }
; ;
R-Value R-Value
: IDENTIFIER { std::printf("Parsed R-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); } : IDENTIFIER { PRINT_FUNC("Parsed R-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); }
| S-Expr { std::printf("Parsed R-Value -> S-Expr\n"); $$ = $1; } | S-Expr { PRINT_FUNC("Parsed R-Value -> S-Expr\n"); $$ = $1; }
| INTEGER { std::printf("Parsed R-Value -> INTEGER\n"); $$ = drv.constructLiteralAST($1); } | INTEGER { PRINT_FUNC("Parsed R-Value -> INTEGER\n"); $$ = drv.constructLiteralAST($1); }
| FLOAT { std::printf("Parsed R-Value -> FLOAT\n"); $$ = drv.constructLiteralAST($1); } | FLOAT { PRINT_FUNC("Parsed R-Value -> FLOAT\n"); $$ = drv.constructLiteralAST($1); }
| STRING { std::printf("Parsed R-Value -> STRING\n"); $$ = drv.constructLiteralAST($1); } | STRING { PRINT_FUNC("Parsed R-Value -> STRING\n"); $$ = drv.constructLiteralAST($1); }
| array-ref { std::printf("Parsed R-Value -> array-ref\n"); $$ = $1; } | array-ref { PRINT_FUNC("Parsed R-Value -> array-ref\n"); $$ = $1; }
| NIL { std::printf("Parsed R-Value -> NIL\n"); $$ = drv.constructLiteralAST(false); } | NIL { PRINT_FUNC("Parsed R-Value -> NIL\n"); $$ = drv.constructLiteralAST(false); }
| T { std::printf("Parsed R-Value -> T\n"); $$ = drv.constructLiteralAST(true); } | T { PRINT_FUNC("Parsed R-Value -> T\n"); $$ = drv.constructLiteralAST(true); }
; ;
R-Value-list R-Value-list
: R-Value { std::printf("Parsed R-Value-list -> R-Value\n"); $$ = { $1 }; } : R-Value { PRINT_FUNC("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-list R-Value { PRINT_FUNC("Parsed R-Value-list -> R-Value-list R-Value\n"); $1.push_back($2); $$ = $1; }
; ;
S-Expr S-Expr
: LPAREN S-Expr-helper RPAREN { std::printf("Parsed S-Expr -> ( S-Expr-helper )\n"); $$ = $2; } : LPAREN S-Expr-helper RPAREN { PRINT_FUNC("Parsed S-Expr -> ( S-Expr-helper )\n"); $$ = $2; }
| LPAREN RPAREN { std::printf("Parsed S-Expr -> ()\n"); $$ = drv.constructLiteralAST(false); } | LPAREN RPAREN { PRINT_FUNC("Parsed S-Expr -> ()\n"); $$ = drv.constructLiteralAST(false); }
; ;
S-Expr-helper S-Expr-helper
: S-Expr-var-op { std::printf("Parsed S-Expr-helper -> S-Expr-var-op\n"); $$ = $1; } : S-Expr-var-op { PRINT_FUNC("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-Lval-op { PRINT_FUNC("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-unary { PRINT_FUNC("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-binary { PRINT_FUNC("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-list { PRINT_FUNC("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-if { PRINT_FUNC("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-loop { PRINT_FUNC("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-func-call { PRINT_FUNC("Parsed S-Expr-helper -> S-Expr-func-call\n"); $$ = $1; }
; ;
S-Expr-var-op 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 var-op-tokens
: DEFVAR { std::printf("Parsed var-op-tokens -> DEFVAR\n"); $$ = DragonLisp::Token::DEFVAR; } : DEFVAR { PRINT_FUNC("Parsed var-op-tokens -> DEFVAR\n"); $$ = DragonLisp::Token::DEFVAR; }
| SETQ { std::printf("Parsed var-op-tokens -> SETQ\n"); $$ = DragonLisp::Token::SETQ; } | SETQ { PRINT_FUNC("Parsed var-op-tokens -> SETQ\n"); $$ = DragonLisp::Token::SETQ; }
; ;
S-Expr-Lval-op 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 lval-op-tokens
: SETF { std::printf("Parsed lval-op-tokens -> SETF\n"); $$ = DragonLisp::Token::SETF; } : SETF { PRINT_FUNC("Parsed lval-op-tokens -> SETF\n"); $$ = DragonLisp::Token::SETF; }
| INCF { std::printf("Parsed lval-op-tokens -> INCF\n"); $$ = DragonLisp::Token::INCF; } | INCF { PRINT_FUNC("Parsed lval-op-tokens -> INCF\n"); $$ = DragonLisp::Token::INCF; }
| DECF { std::printf("Parsed lval-op-tokens -> DECF\n"); $$ = DragonLisp::Token::DECF; } | DECF { PRINT_FUNC("Parsed lval-op-tokens -> DECF\n"); $$ = DragonLisp::Token::DECF; }
; ;
S-Expr-unary 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 unary-tokens
: NOT { std::printf("Parsed unary-tokens -> NOT\n"); $$ = DragonLisp::Token::NOT; } : NOT { PRINT_FUNC("Parsed unary-tokens -> NOT\n"); $$ = DragonLisp::Token::NOT; }
| PRINT { std::printf("Parsed unary-tokens -> PRINT\n"); $$ = DragonLisp::Token::PRINT; } | PRINT { PRINT_FUNC("Parsed unary-tokens -> PRINT\n"); $$ = DragonLisp::Token::PRINT; }
| MAKE_ARRAY { std::printf("Parsed unary-tokens -> MAKE_ARRAY\n"); $$ = DragonLisp::Token::MAKE_ARRAY; } | MAKE_ARRAY { PRINT_FUNC("Parsed unary-tokens -> MAKE_ARRAY\n"); $$ = DragonLisp::Token::MAKE_ARRAY; }
; ;
S-Expr-binary 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 binary-tokens
: LESS { std::printf("Parsed binary-tokens -> LESS\n"); $$ = DragonLisp::Token::LESS; } : LESS { PRINT_FUNC("Parsed binary-tokens -> LESS\n"); $$ = DragonLisp::Token::LESS; }
| LESS_EQUAL { std::printf("Parsed binary-tokens -> LESS_EQUAL\n"); $$ = DragonLisp::Token::LESS_EQUAL; } | LESS_EQUAL { PRINT_FUNC("Parsed binary-tokens -> LESS_EQUAL\n"); $$ = DragonLisp::Token::LESS_EQUAL; }
| GREATER { std::printf("Parsed binary-tokens -> GREATER\n"); $$ = DragonLisp::Token::GREATER; } | GREATER { PRINT_FUNC("Parsed binary-tokens -> GREATER\n"); $$ = DragonLisp::Token::GREATER; }
| GREATER_EQUAL { std::printf("Parsed binary-tokens -> GREATER_EQUAL\n"); $$ = DragonLisp::Token::GREATER_EQUAL; } | GREATER_EQUAL { PRINT_FUNC("Parsed binary-tokens -> GREATER_EQUAL\n"); $$ = DragonLisp::Token::GREATER_EQUAL; }
| LOGNOR { std::printf("Parsed binary-tokens -> LOGNOR\n"); $$ = DragonLisp::Token::LOGNOR; } | LOGNOR { PRINT_FUNC("Parsed binary-tokens -> LOGNOR\n"); $$ = DragonLisp::Token::LOGNOR; }
| MOD { std::printf("Parsed binary-tokens -> MOD\n"); $$ = DragonLisp::Token::MOD; } | MOD { PRINT_FUNC("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. */ } | REM { PRINT_FUNC("Parsed binary-tokens -> REM\n"); $$ = DragonLisp::Token::MOD; /* This is the same as MOD. */ }
; ;
S-Expr-list 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 list-tokens
: EQUAL { std::printf("Parsed list-tokens -> EQUAL\n"); $$ = DragonLisp::Token::EQUAL; } : EQUAL { PRINT_FUNC("Parsed list-tokens -> EQUAL\n"); $$ = DragonLisp::Token::EQUAL; }
| NOT_EQUAL { std::printf("Parsed list-tokens -> NOT_EQUAL\n"); $$ = DragonLisp::Token::NOT_EQUAL; } | NOT_EQUAL { PRINT_FUNC("Parsed list-tokens -> NOT_EQUAL\n"); $$ = DragonLisp::Token::NOT_EQUAL; }
| AND { std::printf("Parsed list-tokens -> AND\n"); $$ = DragonLisp::Token::AND; } | AND { PRINT_FUNC("Parsed list-tokens -> AND\n"); $$ = DragonLisp::Token::AND; }
| OR { std::printf("Parsed list-tokens -> OR\n"); $$ = DragonLisp::Token::OR; } | OR { PRINT_FUNC("Parsed list-tokens -> OR\n"); $$ = DragonLisp::Token::OR; }
| MAX { std::printf("Parsed list-tokens -> MAX\n"); $$ = DragonLisp::Token::MAX; } | MAX { PRINT_FUNC("Parsed list-tokens -> MAX\n"); $$ = DragonLisp::Token::MAX; }
| MIN { std::printf("Parsed list-tokens -> MIN\n"); $$ = DragonLisp::Token::MIN; } | MIN { PRINT_FUNC("Parsed list-tokens -> MIN\n"); $$ = DragonLisp::Token::MIN; }
| LOGAND { std::printf("Parsed list-tokens -> LOGAND\n"); $$ = DragonLisp::Token::LOGAND; } | LOGAND { PRINT_FUNC("Parsed list-tokens -> LOGAND\n"); $$ = DragonLisp::Token::LOGAND; }
| LOGIOR { std::printf("Parsed list-tokens -> LOGIOR\n"); $$ = DragonLisp::Token::LOGIOR; } | LOGIOR { PRINT_FUNC("Parsed list-tokens -> LOGIOR\n"); $$ = DragonLisp::Token::LOGIOR; }
| LOGXOR { std::printf("Parsed list-tokens -> LOGXOR\n"); $$ = DragonLisp::Token::LOGXOR; } | LOGXOR { PRINT_FUNC("Parsed list-tokens -> LOGXOR\n"); $$ = DragonLisp::Token::LOGXOR; }
| LOGEQV { std::printf("Parsed list-tokens -> LOGEQV\n"); $$ = DragonLisp::Token::LOGEQV; } | LOGEQV { PRINT_FUNC("Parsed list-tokens -> LOGEQV\n"); $$ = DragonLisp::Token::LOGEQV; }
| PLUS { std::printf("Parsed list-tokens -> PLUS\n"); $$ = DragonLisp::Token::PLUS; } | PLUS { PRINT_FUNC("Parsed list-tokens -> PLUS\n"); $$ = DragonLisp::Token::PLUS; }
| MINUS { std::printf("Parsed list-tokens -> MINUS\n"); $$ = DragonLisp::Token::MINUS; } | MINUS { PRINT_FUNC("Parsed list-tokens -> MINUS\n"); $$ = DragonLisp::Token::MINUS; }
| MULTIPLY { std::printf("Parsed list-tokens -> MULTIPLY\n"); $$ = DragonLisp::Token::MULTIPLY; } | MULTIPLY { PRINT_FUNC("Parsed list-tokens -> MULTIPLY\n"); $$ = DragonLisp::Token::MULTIPLY; }
| DIVIDE { std::printf("Parsed list-tokens -> DIVIDE\n"); $$ = DragonLisp::Token::DIVIDE; } | DIVIDE { PRINT_FUNC("Parsed list-tokens -> DIVIDE\n"); $$ = DragonLisp::Token::DIVIDE; }
; ;
S-Expr-if 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 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 { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr\n"); $$ = drv.constructIfAST($2, $3, nullptr); } | 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 S-Expr-loop
: LOOP func-body { std::printf("Parsed S-Expr-loop -> LOOP func-body\n"); $$ = drv.constructLoopAST($2); } : 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 { 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); } | 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 { std::printf("Parsed S-Expr-loop -> DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body\n"); $$ = drv.constructLoopAST($3, $4, $6); } | 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 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 func-arg-list
: LPAREN RPAREN { std::printf("Parsed func-arg-list -> ( )\n"); $$ = {}; } : LPAREN RPAREN { PRINT_FUNC("Parsed func-arg-list -> ( )\n"); $$ = {}; }
| LPAREN identifier-list RPAREN { std::printf("Parsed func-arg-list -> ( identifier-list )\n"); $$ = $2; } | LPAREN identifier-list RPAREN { PRINT_FUNC("Parsed func-arg-list -> ( identifier-list )\n"); $$ = $2; }
identifier-list identifier-list
: identifier-list IDENTIFIER { std::printf("Parsed identifier-list -> identifier-list IDENTIFIER\n"); $1.push_back($2); $$ = $1; } : identifier-list IDENTIFIER { PRINT_FUNC("Parsed identifier-list -> identifier-list IDENTIFIER\n"); $1.push_back($2); $$ = $1; }
| IDENTIFIER { std::printf("Parsed identifier-list -> IDENTIFIER\n"); $$ = { $1 }; } | IDENTIFIER { PRINT_FUNC("Parsed identifier-list -> IDENTIFIER\n"); $$ = { $1 }; }
; ;
S-Expr-func-call 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 R-Value-list { PRINT_FUNC("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 { PRINT_FUNC("Parsed S-Expr-func-call -> IDENTIFIER\n"); $$ = drv.constructFuncCallAST($1, {}); }
; ;
%% %%

View File

@ -34,7 +34,13 @@ int DLDriver::parse(std::istream& in, const std::string& s) {
delete this->context; delete this->context;
this->context = new Context(nullptr); 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(); return this->parser->parse();
} }

View File

@ -46,7 +46,7 @@ compile: lexer_compile parser_compile misc_compile
$(CXX) $(CXXFLAGS) -o $(OUTPUT) $(OBJS) parser.o lexer.o $(LIBS) $(CXX) $(CXXFLAGS) -o $(OUTPUT) $(OBJS) parser.o lexer.o $(LIBS)
compile_debug: lexer parser compile_debug: lexer parser
$(CXX) $(CXXFLAGS) -o $(OUTPUT) \ $(CXX) $(CXXFLAGS) -DDLDEBUG -o $(OUTPUT) \
main.cpp \ main.cpp \
DragonLispDriver.cpp \ DragonLispDriver.cpp \
AST.cpp \ AST.cpp \