mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-08 07:51:43 +03:00
Added duration literals but comparison buggy
This commit is contained in:
parent
902c3f8d7d
commit
5004ee12d2
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)),
|
||||
|
@ -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) }
|
||||
|
@ -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"
|
||||
|
15
tests/test_date/durations.catala
Normal file
15
tests/test_date/durations.catala
Normal 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
|
||||
*/
|
4
tests/test_date/durations.catala.A.out
Normal file
4
tests/test_date/durations.catala.A.out
Normal file
@ -0,0 +1,4 @@
|
||||
[RESULT] x -> 2019-01-01
|
||||
[RESULT] y -> 2002-09-30
|
||||
[RESULT] z -> true
|
||||
[RESULT] z2 -> false
|
3
tests/test_date/simple.catala.A.out
Normal file
3
tests/test_date/simple.catala.A.out
Normal 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
|
Loading…
Reference in New Issue
Block a user