Added duration literals but comparison buggy

This commit is contained in:
Denis Merigoux 2020-12-10 11:51:50 +01:00
parent 902c3f8d7d
commit 5004ee12d2
9 changed files with 91 additions and 7 deletions

View File

@ -93,9 +93,45 @@ let rec translate_expr (scope : Scopelang.Ast.ScopeName.t)
| MoneyAmount i ->
Scopelang.Ast.ELit
(Dcalc.Ast.LMoney Z.((i.money_amount_units * of_int 100) + i.money_amount_cents))
| Number ((Int _, _), Some ((Year | Month | Day), _))
| Number ((Int i, i_pos), Some (Year, _)) ->
Scopelang.Ast.ELit
(Dcalc.Ast.LDuration
(ODuration.make ~forward:true
~year:
( try Z.to_int i
with Failure _ ->
Errors.raise_spanned_error
"This duration is too big to fit in Catala's duration computation \
engine"
i_pos )
()))
| Number ((Int i, i_pos), Some (Month, _)) ->
Scopelang.Ast.ELit
(Dcalc.Ast.LDuration
(ODuration.make ~forward:true
~month:
( try Z.to_int i
with Failure _ ->
Errors.raise_spanned_error
"This duration is too big to fit in Catala's duration computation \
engine"
i_pos )
()))
| Number ((Int i, i_pos), Some (Day, _)) ->
Scopelang.Ast.ELit
(Dcalc.Ast.LDuration
(ODuration.make ~forward:true
~day:
( try Z.to_int i
with Failure _ ->
Errors.raise_spanned_error
"This duration is too big to fit in Catala's duration computation \
engine"
i_pos )
()))
| Number ((Dec (_, _), _), Some ((Year | Month | Day), _)) ->
Name_resolution.raise_unsupported_feature "literal" pos
Errors.raise_spanned_error
"Impossible to specify decimal amounts of days, months or years" pos
| Date date -> (
let date =
ODate.Unix.make

View File

@ -102,6 +102,8 @@ let token_list : (string * token) list =
("not", NOT);
("number", CARDINAL);
("year", YEAR);
("month", MONTH);
("day", DAY);
("true", TRUE);
("false", FALSE);
]
@ -283,6 +285,12 @@ let rec lex_code (lexbuf : lexbuf) : token =
| "year" ->
update_acc lexbuf;
YEAR
| "month" ->
update_acc lexbuf;
MONTH
| "day" ->
update_acc lexbuf;
DAY
| 0x24, Star white_space, '0' .. '9', Star ('0' .. '9' | ','), Opt ('.', Rep ('0' .. '9', 0 .. 2))
->
let extract_parts = R.regexp "([0-9]([0-9,]*[0-9]|))(.([0-9]{0,2})|)" in

View File

@ -70,6 +70,8 @@ let token_list_en : (string * token) list =
("not", NOT);
("number", CARDINAL);
("year", YEAR);
("month", MONTH);
("day", DAY);
("true", TRUE);
("false", FALSE);
]
@ -251,6 +253,12 @@ let rec lex_code_en (lexbuf : lexbuf) : token =
| "year" ->
L.update_acc lexbuf;
YEAR
| "month" ->
L.update_acc lexbuf;
MONTH
| "day" ->
L.update_acc lexbuf;
DAY
| 0x24, Star white_space, '0' .. '9', Star ('0' .. '9' | ','), Opt ('.', Rep ('0' .. '9', 0 .. 2))
->
let extract_parts = R.regexp "([0-9]([0-9,]*[0-9]|))(.([0-9]{0,2})|)" in

View File

@ -70,6 +70,8 @@ let token_list_fr : (string * token) list =
("non", NOT);
("nombre", CARDINAL);
("an", YEAR);
("mois", MONTH);
("jour", DAY);
("vrai", TRUE);
("faux", FALSE);
]
@ -257,6 +259,12 @@ let rec lex_code_fr (lexbuf : lexbuf) : token =
| "an" ->
L.update_acc lexbuf;
YEAR
| "mois" ->
L.update_acc lexbuf;
MONTH
| "jour" ->
L.update_acc lexbuf;
DAY
| ( '0' .. '9',
Star ('0' .. '9' | white_space),
Opt (',', Rep ('0' .. '9', 0 .. 2)),

View File

@ -49,7 +49,7 @@
%token LESSER_DURATION GREATER_DURATION LESSER_EQUAL_DURATION GREATER_EQUAL_DURATION
%token EXISTS IN SUCH THAT NOW
%token DOT AND OR LPAREN RPAREN OPTIONAL EQUAL
%token CARDINAL ASSERTION FIXED BY YEAR
%token CARDINAL ASSERTION FIXED BY YEAR MONTH DAY
%token PLUS MINUS MULT DIV
%token PLUSDEC MINUSDEC MULTDEC DIVDEC
%token PLUSMONEY MINUSMONEY MULTMONEY DIVMONEY
@ -160,6 +160,8 @@ num_literal:
unit_literal:
| PERCENT { (Percent, $sloc) }
| YEAR { (Year, $sloc)}
| MONTH { (Month, $sloc) }
| DAY { (Day, $sloc) }
date_int:
| d = INT_LITERAL { (Z.to_int d, $sloc) }

View File

@ -111,10 +111,10 @@ let format_binop (fmt : Format.formatter) (op : binop Pos.marked) : unit =
| Or -> Format.fprintf fmt "%s" "||"
| Eq -> Format.fprintf fmt "%s" "=="
| Neq -> Format.fprintf fmt "%s" "!="
| Lt _ -> Format.fprintf fmt "%s" "<"
| Lte _ -> Format.fprintf fmt "%s" "<="
| Gt _ -> Format.fprintf fmt "%s" ">"
| Gte _ -> Format.fprintf fmt "%s" ">="
| Lt k -> Format.fprintf fmt "%s%a" "<" format_op_kind k
| Lte k -> Format.fprintf fmt "%s%a" "<=" format_op_kind k
| Gt k -> Format.fprintf fmt "%s%a" ">" format_op_kind k
| Gte k -> Format.fprintf fmt "%s%a" ">=" format_op_kind k
let format_unop (fmt : Format.formatter) (op : unop Pos.marked) : unit =
Format.fprintf fmt "%s"

View File

@ -0,0 +1,15 @@
@Article@
/*
new scope A:
param x content date
param y content date
param z content bool
param z2 content bool
scope A:
def x := |01/01/2019|
def y := |30/09/2002|
def z := x -@ y >=^ 16 year
def z2 := x -@ y <^ 20 year
*/

View File

@ -0,0 +1,4 @@
[RESULT] x -> 2019-01-01
[RESULT] y -> 2002-09-30
[RESULT] z -> true
[RESULT] z2 -> false

View File

@ -0,0 +1,3 @@
[RESULT] x -> 2019-01-01
[RESULT] y -> 2002-09-30
[RESULT] z -> 16 years or 195 months of 5937 days