2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* MathParser.ypp, part of LatAnalyze 3
|
|
|
|
*
|
2016-04-06 20:11:23 +01:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-02-06 18:52:13 +00:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-09-15 18:39:22 +01:00
|
|
|
%{
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Global.hpp>
|
2019-02-10 00:23:36 +00:00
|
|
|
#include <LatAnalyze/Core/MathInterpreter.hpp>
|
2013-09-15 18:39:22 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
%}
|
|
|
|
|
|
|
|
%pure-parser
|
2016-04-06 20:04:23 +01:00
|
|
|
%name-prefix "_math_"
|
2013-09-15 18:39:22 +01:00
|
|
|
%locations
|
|
|
|
%defines
|
|
|
|
%error-verbose
|
2014-02-06 18:07:27 +00:00
|
|
|
%parse-param { Latan::MathInterpreter::MathParserState *state }
|
2013-09-15 18:39:22 +01:00
|
|
|
%initial-action {yylloc.last_column = 0;}
|
|
|
|
%lex-param { void* scanner }
|
|
|
|
|
|
|
|
%union
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
double val_double;
|
|
|
|
char val_char;
|
|
|
|
char val_str[MAXIDLENGTH];
|
2014-02-18 18:11:08 +00:00
|
|
|
Latan::ExprNode *val_node;
|
2013-09-15 18:39:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
%token <val_char> ERR
|
|
|
|
%token <val_str> FLOAT
|
|
|
|
%token <val_str> ID
|
2014-02-04 17:33:58 +00:00
|
|
|
%token RETURN
|
2013-09-15 18:39:22 +01:00
|
|
|
|
2014-01-23 14:08:18 +00:00
|
|
|
%left '='
|
2013-09-15 18:39:22 +01:00
|
|
|
%left '+' '-'
|
|
|
|
%left '*' '/'
|
|
|
|
%nonassoc UMINUS
|
2014-02-20 20:21:17 +00:00
|
|
|
%left '^'
|
2013-09-15 18:39:22 +01:00
|
|
|
|
2014-02-04 17:33:58 +00:00
|
|
|
%type <val_node> stmt stmt_list expr func_args
|
2013-09-15 18:39:22 +01:00
|
|
|
|
|
|
|
%{
|
2014-01-23 14:08:18 +00:00
|
|
|
int _math_lex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
|
2013-09-15 18:39:22 +01:00
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
void _math_error(YYLTYPE *locp, MathInterpreter::MathParserState *state,
|
2014-01-23 14:08:18 +00:00
|
|
|
const char *err)
|
2013-09-15 18:39:22 +01:00
|
|
|
{
|
|
|
|
stringstream buf;
|
|
|
|
|
2014-01-23 14:08:18 +00:00
|
|
|
buf << *(state->streamName) << ":" << locp->first_line << ":"\
|
2013-09-15 18:39:22 +01:00
|
|
|
<< locp->first_column << ": " << err;
|
2014-01-23 14:08:18 +00:00
|
|
|
LATAN_ERROR(Parsing, buf.str());
|
2013-09-15 18:39:22 +01:00
|
|
|
}
|
2014-02-18 18:11:08 +00:00
|
|
|
|
|
|
|
void _math_warning(YYLTYPE *locp, MathInterpreter::MathParserState *state,
|
|
|
|
const char *err)
|
|
|
|
{
|
|
|
|
stringstream buf;
|
|
|
|
|
|
|
|
buf << *(state->streamName) << ":" << locp->first_line << ":"\
|
|
|
|
<< locp->first_column << ": " << err;
|
|
|
|
LATAN_WARNING(buf.str());
|
|
|
|
}
|
2013-09-15 18:39:22 +01:00
|
|
|
|
|
|
|
#define scanner state->scanner
|
|
|
|
%}
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
program:
|
|
|
|
/* empty string */
|
2014-02-13 19:23:39 +00:00
|
|
|
| stmt_list {(*(state->data)).reset($1);}
|
2013-09-15 18:39:22 +01:00
|
|
|
;
|
|
|
|
|
2014-02-04 17:33:58 +00:00
|
|
|
stmt:
|
|
|
|
';'
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new SemicolonNode(";");}
|
2014-02-04 17:33:58 +00:00
|
|
|
| expr ';'
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = nullptr; _math_warning(&yylloc, state, "useless statement removed");}
|
2014-02-04 17:33:58 +00:00
|
|
|
| ID '=' expr ';'
|
2015-06-15 13:59:10 +01:00
|
|
|
{$$ = new AssignNode("="); $$->pushArg(new VarNode($1)); $$->pushArg($3);}
|
2014-02-04 17:33:58 +00:00
|
|
|
| RETURN expr ';'
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new ReturnNode("return"); $$->pushArg($2);}
|
2014-02-04 17:33:58 +00:00
|
|
|
| '{' stmt_list '}'
|
|
|
|
{$$ = $2;}
|
|
|
|
;
|
|
|
|
|
|
|
|
stmt_list:
|
|
|
|
stmt
|
|
|
|
{$$ = $1;}
|
|
|
|
| stmt_list stmt
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new SemicolonNode(";"); $$->pushArg($1); $$->pushArg($2);}
|
2014-02-04 17:33:58 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
expr:
|
|
|
|
FLOAT
|
2015-06-15 13:59:10 +01:00
|
|
|
{$$ = new CstNode($1);}
|
2014-02-04 17:33:58 +00:00
|
|
|
| ID
|
2015-06-15 13:59:10 +01:00
|
|
|
{$$ = new VarNode($1);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| '-' expr %prec UMINUS
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new MathOpNode("-"); $$->pushArg($2);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '+' expr
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new MathOpNode("+"); $$->pushArg($1); $$->pushArg($3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '-' expr
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new MathOpNode("-"); $$->pushArg($1); $$->pushArg($3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '*' expr
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new MathOpNode("*"); $$->pushArg($1); $$->pushArg($3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '/' expr
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new MathOpNode("/"); $$->pushArg($1); $$->pushArg($3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '^' expr
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new MathOpNode("^"); $$->pushArg($1); $$->pushArg($3);}
|
2014-01-23 14:08:18 +00:00
|
|
|
| '(' expr ')'
|
|
|
|
{$$ = $2;}
|
2014-02-04 17:33:58 +00:00
|
|
|
| ID '(' func_args ')'
|
2015-06-15 13:59:10 +01:00
|
|
|
{$$ = $3; $$->setName($1);}
|
2013-09-15 18:39:22 +01:00
|
|
|
;
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-02-04 17:33:58 +00:00
|
|
|
func_args:
|
2014-01-30 19:28:30 +00:00
|
|
|
/* empty string */
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new FuncNode("");}
|
2014-01-30 19:28:30 +00:00
|
|
|
| expr
|
2014-02-18 18:11:08 +00:00
|
|
|
{$$ = new FuncNode(""); $$->pushArg($1);}
|
2014-02-04 17:33:58 +00:00
|
|
|
| func_args ',' expr
|
2014-01-30 19:28:30 +00:00
|
|
|
{$$ = $1; $$->pushArg($3);}
|
|
|
|
;
|
|
|
|
|