1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-09-20 05:25:37 +01:00
LatAnalyze/lib/MathParser.ypp

153 lines
3.9 KiB
Plaintext
Raw Normal View History

/*
* MathParser.ypp, part of LatAnalyze 3
*
2015-06-11 14:04:54 +01:00
* Copyright (C) 2013 - 2015 Antonin Portelli
*
* 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/>.
*/
%{
#include <iostream>
#include <sstream>
#include <cstring>
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/Global.hpp>
#include <LatAnalyze/MathInterpreter.hpp>
2015-05-23 14:44:43 +01:00
#if (defined __INTEL_COMPILER)
#pragma warning disable 1682 2259
#elif (defined __GNUC__)||(defined __clang__)
2015-02-24 16:59:22 +00:00
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
2015-05-23 14:44:43 +01:00
#endif
2015-02-24 16:59:22 +00:00
using namespace std;
using namespace Latan;
%}
%pure-parser
2015-06-15 13:59:10 +01:00
%name-prefix = "_math_"
%locations
%defines
%error-verbose
%parse-param { Latan::MathInterpreter::MathParserState *state }
%initial-action {yylloc.last_column = 0;}
%lex-param { void* scanner }
%union
{
double val_double;
char val_char;
char val_str[MAXIDLENGTH];
Latan::ExprNode *val_node;
}
%token <val_char> ERR
%token <val_str> FLOAT
%token <val_str> ID
%token RETURN
%left '='
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
2014-02-20 20:21:17 +00:00
%left '^'
%type <val_node> stmt stmt_list expr func_args
%{
int _math_lex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
void _math_error(YYLTYPE *locp, MathInterpreter::MathParserState *state,
const char *err)
{
stringstream buf;
buf << *(state->streamName) << ":" << locp->first_line << ":"\
<< locp->first_column << ": " << err;
LATAN_ERROR(Parsing, buf.str());
}
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());
}
#define scanner state->scanner
%}
%%
program:
/* empty string */
| stmt_list {(*(state->data)).reset($1);}
;
stmt:
';'
{$$ = new SemicolonNode(";");}
| expr ';'
{$$ = nullptr; _math_warning(&yylloc, state, "useless statement removed");}
| ID '=' expr ';'
2015-06-15 13:59:10 +01:00
{$$ = new AssignNode("="); $$->pushArg(new VarNode($1)); $$->pushArg($3);}
| RETURN expr ';'
{$$ = new ReturnNode("return"); $$->pushArg($2);}
| '{' stmt_list '}'
{$$ = $2;}
;
stmt_list:
stmt
{$$ = $1;}
| stmt_list stmt
{$$ = new SemicolonNode(";"); $$->pushArg($1); $$->pushArg($2);}
;
expr:
FLOAT
2015-06-15 13:59:10 +01:00
{$$ = new CstNode($1);}
| ID
2015-06-15 13:59:10 +01:00
{$$ = new VarNode($1);}
| '-' expr %prec UMINUS
{$$ = new MathOpNode("-"); $$->pushArg($2);}
| expr '+' expr
{$$ = new MathOpNode("+"); $$->pushArg($1); $$->pushArg($3);}
| expr '-' expr
{$$ = new MathOpNode("-"); $$->pushArg($1); $$->pushArg($3);}
| expr '*' expr
{$$ = new MathOpNode("*"); $$->pushArg($1); $$->pushArg($3);}
| expr '/' expr
{$$ = new MathOpNode("/"); $$->pushArg($1); $$->pushArg($3);}
| expr '^' expr
{$$ = new MathOpNode("^"); $$->pushArg($1); $$->pushArg($3);}
| '(' expr ')'
{$$ = $2;}
| ID '(' func_args ')'
2015-06-15 13:59:10 +01:00
{$$ = $3; $$->setName($1);}
;
func_args:
/* empty string */
{$$ = new FuncNode("");}
| expr
{$$ = new FuncNode(""); $$->pushArg($1);}
| func_args ',' expr
{$$ = $1; $$->pushArg($3);}
;