Allow negative literals

This commit is contained in:
Denis Merigoux 2022-07-21 14:14:22 +02:00
parent dcf80f4bec
commit 100c84a741
No known key found for this signature in database
GPG Key ID: EE99DCFA365C3EE3
6 changed files with 271 additions and 315 deletions

View File

@ -314,9 +314,9 @@ type builtin_expression =
visitors { variety = "iter"; name = "builtin_expression_iter"; nude = true }]
type literal_date = {
literal_date_day : (int[@opaque]) Marked.pos;
literal_date_month : (int[@opaque]) Marked.pos;
literal_date_year : (int[@opaque]) Marked.pos;
literal_date_day : (int[@opaque]);
literal_date_month : (int[@opaque]);
literal_date_year : (int[@opaque]);
}
[@@deriving
visitors

View File

@ -221,21 +221,17 @@ let rec translate_expr
Errors.raise_spanned_error pos
"Impossible to specify decimal amounts of days, months or years"
| LDate date ->
if Marked.unmark date.literal_date_month > 12 then
Errors.raise_spanned_error
(Marked.get_mark date.literal_date_month)
if date.literal_date_month > 12 then
Errors.raise_spanned_error pos
"There is an error in this date: the month number is bigger than 12";
if Marked.unmark date.literal_date_day > 31 then
Errors.raise_spanned_error
(Marked.get_mark date.literal_date_day)
if date.literal_date_day > 31 then
Errors.raise_spanned_error pos
"There is an error in this date: the day number is bigger than 31";
Desugared.Ast.ELit
(Dcalc.Ast.LDate
(try
Runtime.date_of_numbers
(Marked.unmark date.literal_date_year)
(Marked.unmark date.literal_date_month)
(Marked.unmark date.literal_date_day)
Runtime.date_of_numbers date.literal_date_year
date.literal_date_month date.literal_date_day
with Runtime.ImpossibleDate ->
Errors.raise_spanned_error pos
"There is an error in this date, it does not correspond to a \

View File

@ -588,10 +588,26 @@ let rec lex_code (lexbuf : lexbuf) : token =
Buffer.add_string cents (String.make (2 - Buffer.length cents) '0');
L.update_acc lexbuf;
MONEY_AMOUNT (Buffer.contents units, Buffer.contents cents)
| Plus digit, MC_DECIMAL_SEPARATOR, Star digit ->
| Rep (digit, 4), '-', Rep (digit, 2), '-', Rep (digit, 2) ->
let rex =
Re.(compile @@ whole_string @@ seq [
group (rep1 digit);
group (repn digit 4 None);
char '-';
group (repn digit 2 None);
char '-';
group (repn digit 2 None);
])
in
let date_parts = R.get_substring (R.exec ~rex (Utf8.lexeme lexbuf)) in
DATE_LITERAL (
int_of_string (date_parts 1),
int_of_string (date_parts 2),
int_of_string (date_parts 3)
)
| Opt '-', Plus digit, MC_DECIMAL_SEPARATOR, Star digit ->
let rex =
Re.(compile @@ whole_string @@ seq [
group (seq [opt (char '-') ; rep1 digit]);
char MC_DECIMAL_SEPARATOR;
group (rep digit)
]) in
@ -766,7 +782,7 @@ let rec lex_code (lexbuf : lexbuf) : token =
(* Name of variable *)
L.update_acc lexbuf;
IDENT (Utf8.lexeme lexbuf)
| Plus digit ->
| Opt '-', Plus digit ->
(* Integer literal*)
L.update_acc lexbuf;
INT_LITERAL (Utf8.lexeme lexbuf)

File diff suppressed because it is too large Load Diff

View File

@ -126,9 +126,6 @@ unit_literal:
| MONTH { (Month, Pos.from_lpos $sloc) }
| DAY { (Day, Pos.from_lpos $sloc) }
date_int:
| d = INT_LITERAL { (int_of_string d, Pos.from_lpos $sloc) }
literal:
| l = num_literal u = option(unit_literal) {
(LNumber (l, u), Pos.from_lpos $sloc)
@ -140,7 +137,8 @@ literal:
money_amount_cents = cents;
}, Pos.from_lpos $sloc)
}
| VERTICAL y = date_int MINUS m = date_int MINUS d = date_int VERTICAL {
| VERTICAL d = DATE_LITERAL VERTICAL {
let (y,m,d) = d in
(LDate {
literal_date_year = y;
literal_date_month = m;

View File

@ -32,6 +32,7 @@
%token<string> CONSTRUCTOR IDENT
%token<string> END_CODE
%token<string> INT_LITERAL
%token<int * int * int> DATE_LITERAL
%token TRUE FALSE
%token<string * string> DECIMAL_LITERAL
%token<string * string> MONEY_AMOUNT