2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* MathParser.ypp, part of LatAnalyze 3
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 - 2014 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/>.
|
|
|
|
*/
|
|
|
|
|
2013-09-15 18:39:22 +01:00
|
|
|
%{
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstring>
|
|
|
|
#include <latan/Global.hpp>
|
2014-02-06 18:07:27 +00:00
|
|
|
#include <latan/MathInterpreter.hpp>
|
2013-09-15 18:39:22 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
%}
|
|
|
|
|
|
|
|
%pure-parser
|
2014-01-22 15:57:47 +00: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
|
|
|
|
{
|
|
|
|
double val_double;
|
|
|
|
char val_char;
|
|
|
|
char val_str[MAXIDLENGTH];
|
2014-01-23 14:08:18 +00:00
|
|
|
Latan::MathNode *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 '*' '/'
|
|
|
|
%left '^'
|
|
|
|
%nonassoc UMINUS
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#define scanner state->scanner
|
|
|
|
%}
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
program:
|
|
|
|
/* empty string */
|
2014-02-04 17:33:58 +00:00
|
|
|
| stmt_list {*(state->data) = $1;}
|
2013-09-15 18:39:22 +01:00
|
|
|
;
|
|
|
|
|
2014-02-04 17:33:58 +00:00
|
|
|
stmt:
|
|
|
|
';'
|
|
|
|
{$$ = new MathNode(";", MathNode::Type::op, 0);}
|
|
|
|
| expr ';'
|
|
|
|
{$$ = $1;}
|
|
|
|
| ID '=' expr ';'
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
|
|
|
$$ = new MathNode("=", MathNode::Type::op, 2, \
|
|
|
|
new MathNode($ID,MathNode::Type::var), $3);
|
|
|
|
}
|
2014-02-04 17:33:58 +00:00
|
|
|
| RETURN expr ';'
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("return", MathNode::Type::keyw, 1, $2);}
|
2014-02-04 17:33:58 +00:00
|
|
|
| '{' stmt_list '}'
|
|
|
|
{$$ = $2;}
|
|
|
|
;
|
|
|
|
|
|
|
|
stmt_list:
|
|
|
|
stmt
|
|
|
|
{$$ = $1;}
|
|
|
|
| stmt_list stmt
|
|
|
|
{$$ = new MathNode(";", MathNode::Type::op, 2, $1, $2);}
|
|
|
|
;
|
|
|
|
|
|
|
|
expr:
|
|
|
|
FLOAT
|
|
|
|
{$$ = new MathNode($FLOAT, MathNode::Type::cst);}
|
|
|
|
| ID
|
|
|
|
{$$ = new MathNode($ID, MathNode::Type::var);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| '-' expr %prec UMINUS
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("-", MathNode::Type::op, 1, $2);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '+' expr
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("+", MathNode::Type::op, 2, $1, $3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '-' expr
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("-", MathNode::Type::op, 2, $1, $3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '*' expr
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("*", MathNode::Type::op, 2, $1, $3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '/' expr
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("/", MathNode::Type::op, 2, $1, $3);}
|
2014-01-22 15:57:47 +00:00
|
|
|
| expr '^' expr
|
2014-01-23 14:08:18 +00:00
|
|
|
{$$ = new MathNode("^", MathNode::Type::op, 2, $1, $3);}
|
|
|
|
| '(' expr ')'
|
|
|
|
{$$ = $2;}
|
2014-02-04 17:33:58 +00:00
|
|
|
| ID '(' func_args ')'
|
2014-01-30 19:28:30 +00:00
|
|
|
{$$ = $3; $$->setName($ID);}
|
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 */
|
|
|
|
{$$ = new MathNode("", MathNode::Type::func);}
|
|
|
|
| expr
|
|
|
|
{$$ = new MathNode("", MathNode::Type::func, 1, $1);}
|
2014-02-04 17:33:58 +00:00
|
|
|
| func_args ',' expr
|
2014-01-30 19:28:30 +00:00
|
|
|
{$$ = $1; $$->pushArg($3);}
|
|
|
|
;
|
|
|
|
|