From 824f2987d47e87d58ee2a4a96d7be417aad6aeab Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Wed, 31 Jan 2018 13:20:20 +0100 Subject: API refactoring : made the GADT abstract, provide contructor for each case, and deported the expression with evaluation with module functors --- src/expressionParser.mly | 66 +++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'src/expressionParser.mly') diff --git a/src/expressionParser.mly b/src/expressionParser.mly index 473797f..1c2769c 100755 --- a/src/expressionParser.mly +++ b/src/expressionParser.mly @@ -1,11 +1,19 @@ %{ open ScTypes + open ScTypes.Result module S = Symbols let u = UTF8.from_utf8string let extractColumnNameFromNum (fixed, (str, value)) = (fixed, value) + let build_call ident = function + | [] -> Expr.call0 ident + | [p1] -> Expr.call1 ident p1 + | [p1;p2] -> Expr.call2 ident p1 p2 + | [p1;p2;p3] -> Expr.call3 ident p1 p2 p3 + | n -> Expr.callN ident n + %} %token REAL @@ -34,8 +42,8 @@ %left TIMES DIVIDE %left POW -%start value -%start content +%start value +%start content %% @@ -46,18 +54,18 @@ content: | basic EOF {$1} basic: - | PLUS num {Result (number $2)} - | MINUS num {Result (number (DataType.Num.neg $2))} - | num {Result (number $1)} - | NUM DIVIDE NUM DIVIDE NUM {Result ( - date ( - DataType.Date.get_julian_day + | PLUS num {Ok (Type.number $2)} + | MINUS num {Ok (Type.number (DataType.Num.neg $2))} + | num {Ok (Type.number $1)} + | NUM DIVIDE NUM DIVIDE NUM {Ok ( + Type.date ( + DataType.Date.get_julian_day (int_of_string $1) (int_of_string $3) (int_of_string $5) ))} - | NUM COLON NUM COLON NUM {Result ( - date ( + | NUM COLON NUM COLON NUM {Ok ( + Type.date ( let nhour = DataType.Num.div (DataType.Num.of_int @@ int_of_string $1) (DataType.Num.of_int 24) and nmin = DataType.Num.div (DataType.Num.of_int @@ int_of_string $3) (DataType.Num.of_int 1440) and nsec = DataType.Num.div (DataType.Num.of_int @@ int_of_string $5) (DataType.Num.of_int 86400) @@ -66,34 +74,34 @@ basic: )} expr: - | num {Value (number ($1))} - | MINUS expr {Call (S.sub, [$2])} - | PLUS expr {Call (S.add, [$2])} + | num {Expr.value (Type.number ($1))} + | MINUS expr {Expr.call1 S.sub $2} + | PLUS expr {Expr.call1 S.add $2} - | LETTERS ident LPAREN separated_list(SEMICOLON, expr) RPAREN { Call (u($1 ^ $2), $4) } + | LETTERS ident LPAREN separated_list(SEMICOLON, expr) RPAREN { build_call (u($1 ^ $2)) $4 } - | cell {Ref (Cell $1)} - | cell COLON cell {Ref (Range ($1, $3))} + | cell {Expr.ref (Refs.cell $1)} + | cell COLON cell {Expr.ref (Refs.range $1 $3)} - | LPAREN expr RPAREN {Expression $2} - | STR {Value (string (u $1))} + | LPAREN expr RPAREN {Expr.expression $2} + | STR {Expr.value (Type.string (u $1))} (* Mathematical operators *) - | expr MINUS expr {Call (S.sub, [$1; $3])} - | expr DIVIDE expr {Call (S.div, [$1; $3])} - | expr TIMES expr {Call (S.mul, [$1; $3])} - | expr PLUS expr {Call (S.add, [$1; $3])} - | expr POW expr {Call (S.pow, [$1; $3])} + | expr MINUS expr {Expr.call2 S.sub $1 $3} + | expr DIVIDE expr {Expr.call2 S.div $1 $3} + | expr TIMES expr {Expr.call2 S.mul $1 $3} + | expr PLUS expr {Expr.call2 S.add $1 $3} + | expr POW expr {Expr.call2 S.pow $1 $3} (* Comparaison *) - | expr EQ expr {Call (S.eq, [$1; $3])} - | expr NEQ expr {Call (S.neq, [$1; $3])} - | expr LT expr {Call (S.lt, [$1; $3])} - | expr GT expr {Call (S.gt, [$1; $3])} - | expr LE expr {Call (S.le, [$1; $3])} - | expr GE expr {Call (S.ge, [$1; $3])} + | expr EQ expr {Expr.call2 S.eq $1 $3} + | expr NEQ expr {Expr.call2 S.neq $1 $3} + | expr LT expr {Expr.call2 S.lt $1 $3} + | expr GT expr {Expr.call2 S.gt $1 $3} + | expr LE expr {Expr.call2 S.le $1 $3} + | expr GE expr {Expr.call2 S.ge $1 $3} %inline cell: | LETTERS NUM { Cell.from_string (false, $1) (false, int_of_string $2) } -- cgit v1.2.3