From ef312564ca84a2b49fc291434d8fb2f8501bb618 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Tue, 15 Nov 2016 13:00:01 +0100 Subject: Initial commit --- expressionParser.mly | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 expressionParser.mly (limited to 'expressionParser.mly') diff --git a/expressionParser.mly b/expressionParser.mly new file mode 100755 index 0000000..a4d0716 --- /dev/null +++ b/expressionParser.mly @@ -0,0 +1,102 @@ +%{ + open ScTypes + module F = Functions + + let u = UTF8.from_utf8string + + let extractColumnNameFromNum (fixed, (str, value)) = (fixed, value) + +%} + +%token REAL +%token NUM +%token STR + +%token LETTERS + +%token DOLLAR + +%token LPAREN +%token RPAREN +%token PLUS +%token TIMES +%token DIVIDE +%token MINUS +%token EQ NEQ +%token LT LE GT GE +%token EOF +%token SEMICOLON +%token COLON +%token POW + +%nonassoc EQ NEQ LT LE GT GE +%left PLUS MINUS +%left TIMES DIVIDE +%left POW + +%start value +%start content + +%% + +value: + | EQ expr EOF {$2} + +content: + | basic EOF {$1} + +basic: + | num {Num ((snd $1), Some (u(fst $1)))} + | MINUS num {Num (Num.minus_num (snd $2), Some (u("-" ^(fst $2)) ))} + | NUM DIVIDE NUM DIVIDE NUM { + Date (Tools.Date.get_julian_day + (Num.int_of_num @@ snd $1) + (Num.int_of_num @@ snd $3) + (Num.int_of_num @@ snd $5) + )} + +expr: + | num {Value (Num ((snd $1), Some (u(fst $1))))} + | MINUS num {Value (Num (Num.minus_num (snd $2), Some (u("-" ^(fst $2)) )))} + + | LETTERS ident LPAREN separated_list(SEMICOLON, expr) RPAREN { Call (u($1 ^ $2), $4) } + + + | cell {Ref (Cell $1)} + | cell COLON cell {Ref (Range ($1, $3))} + + + | LPAREN expr RPAREN {Expression $2} + | STR {Value (Str (u $1))} + + (* Mathematical operators *) + | expr MINUS expr {Call (F.sub, [$1; $3])} + | expr DIVIDE expr {Call (F.div, [$1; $3])} + | expr TIMES expr {Call (F.mul, [$1; $3])} + | expr PLUS expr {Call (F.add, [$1; $3])} + | expr POW expr {Call (F.pow, [$1; $3])} + + (* Comparaison *) + | expr EQ expr {Call (F.eq, [$1; $3])} + | expr NEQ expr {Call (F.neq, [$1; $3])} + | expr LT expr {Call (F.lt, [$1; $3])} + | expr GT expr {Call (F.gt, [$1; $3])} + | expr LE expr {Call (F.le, [$1; $3])} + | expr GE expr {Call (F.ge, [$1; $3])} + +%inline cell: + | LETTERS NUM { Cell.from_string (false, $1) (false, snd $2) } + | DOLLAR LETTERS NUM { Cell.from_string (true, $2) (false, snd $3) } + | LETTERS DOLLAR NUM { Cell.from_string (false, $1) (true, snd $3) } + | DOLLAR LETTERS DOLLAR NUM { Cell.from_string (true, $2) (true, snd $4) } + +num: + | REAL {$1} + | NUM {$1} + +ident: + | text* { String.concat "" $1 } + +text: + | LETTERS { $1 } + | NUM { fst $1 } -- cgit v1.2.3