support list patterns [p1; p2; ...; pn]

This commit is contained in:
Gabriel Scherer 2020-12-20 21:46:55 +01:00
parent c93dcaac1d
commit bf1daa2a59
4 changed files with 35 additions and 2 deletions

View File

@ -204,6 +204,14 @@
(expr_no_semi) : (cons $1 #nil)
(semi_separated_expr_list SEMICOLON expr_no_semi) : (cons $3 $1))
(semi_separated_pattern_list_opt
(semi_separated_pattern_list) : (reverse $1)
(semi_separated_pattern_list SEMICOLON) : (reverse $1))
(semi_separated_pattern_list
(pattern) : (cons $1 #nil)
(semi_separated_pattern_list SEMICOLON pattern) : (cons $3 $1))
(type_ignore
( ) : '()
(STAR type_ignore) : '()
@ -310,6 +318,8 @@
(lident_ext) : (if (equal? $1 "_") (list 'PWild) (list 'PVar $1))
(longident_constr) : (list 'PConstr $1 #nil)
(LBRACK RBRACK) : (lid->pconstr "[]" #nil)
(LBRACK semi_separated_pattern_list_opt RBRACK) :
(fold-right (lambda (p r) (lid->pconstr "::" (list p r))) (lid->pconstr "[]" #nil) $2)
(LPAREN pattern COLON type_ignore RPAREN) : $2
(LPAREN RPAREN) : (list 'PInt 0)
(LPAREN pattern RPAREN) : $2

View File

@ -1 +1 @@
Bytecode size: 4142 bytes
Bytecode size: 5374 bytes

View File

@ -11,6 +11,27 @@ let print_list l =
let () = print_list [1; 2; 3; 4; 5; 6; 7; 8; 9]
let () = print_newline ()
let rec iter_sep f sep l =
match l with
| [] -> ()
| [x] ->
f x
(* this function is an excuse to test literral patterns
[p1; p2; ...; pn] *)
| [x0; x1] ->
f x0; sep (); f x1
| x :: l ->
f x; sep (); iter_sep f sep l
let print_list l =
print "["; iter_sep print_int (fun () -> print ";") l; print "]"
let () = print_list [1; 2; 3; 4; 5; 6; 7; 8; 9]
let () = print_newline ()
let rec map f l =
match l with
| [] -> []

View File

@ -1,2 +1,4 @@
Lists:
[ 1 2 3 4 5 6 7 8 9][ 2 3 4 5 6 7 8 9 10][ 2 4 6]
[ 1 2 3 4 5 6 7 8 9]
[ 1; 2; 3; 4; 5; 6; 7; 8; 9]
[ 2; 3; 4; 5; 6; 7; 8; 9; 10][ 2; 4; 6]