use separated list

This commit is contained in:
Mylloon 2022-12-23 05:03:40 +01:00
parent 51150269cc
commit 1305a867f8
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 13 additions and 50 deletions

View file

@ -30,3 +30,6 @@ archive:
symlinks: symlinks:
ln -s _build/*.cmi . ln -s _build/*.cmi .
menhir-debug:
menhir parser.mly --explain --infer

View file

@ -18,17 +18,10 @@
%left Ladd Lsub Lmul Ldiv Lrem Lseq Lsge Lsgt Lsle Lslt Lsne %left Ladd Lsub Lmul Ldiv Lrem Lseq Lsge Lsgt Lsle Lslt Lsne
%left Land Lor %left Land Lor
%left Lbracedeb Lparfin Lbracefin Lreturn %type <Ast.Syntax.prog> prog
%left Ltype Lbool Lint Lvar Lstr
%left Lif Lwhile
%start prog %start prog
%type <Ast.Syntax.block> block
%type <Ast.Syntax.prog> prog
%type <Ast.Syntax.args> args_ident
%type <Ast.Syntax.expr list> args_expr
%% %%
prog: prog:
@ -38,42 +31,19 @@ prog:
| Lend { [] } | Lend { [] }
def: def:
/* Définition fonction : type fonction (args) block */ /* Définition fonction : type fonction (...) block */
| t = Ltype | t = Ltype
; f = Lvar ; f = Lvar
; a = args_ident ; Lpardeb
; a = separated_list(Lcomma, arg)
; Lparfin
; b = block { ; b = block {
[ Func { func = f ; type_t = t ; args = a ; code = b ; pos = $startpos(f) } ] [ Func { func = f ; type_t = t ; args = a ; code = b ; pos = $startpos(f) } ]
} }
/* Définition fonction : type fonction () block */ arg:
| t = Ltype /* type a */
; f = Lvar | t = Ltype ; a = Lvar { Arg { type_t = t ; name = a } }
; Lpardeb
; Lparfin
; b = block {
[ Func { func = f ; type_t = t ; args = [] ; code = b ; pos = $startpos(f) } ]
}
args_ident:
/* ( */
| Lpardeb ; s = args_ident { s }
/* type a, ... */
| t = Ltype ; a = arg_ident ; Lcomma ; s = args_ident { Arg { type_t = t
; name = a
} :: s }
/* type c) */
| t = Ltype ; a = arg_ident ; Lparfin { [ Arg { type_t = t
; name = a } ] }
/* ) */
| Lparfin { [] }
arg_ident:
/* Argument */
| a = Lvar { a }
block: block:
/* { */ /* { */
@ -223,18 +193,8 @@ expr:
Call { func = "%or" ; args = [ a ; b ] ; pos = $startpos($2) } Call { func = "%or" ; args = [ a ; b ] ; pos = $startpos($2) }
} }
/* function(a */ /* function(...) */
| f = Lvar ; Lpardeb ; a = args_expr { | f = Lvar ; Lpardeb ; a = separated_list(Lcomma, expr) ; Lparfin {
Call { func = f ; args = a ; pos = $startpos(a) } Call { func = f ; args = a ; pos = $startpos(a) }
} }
; ;
args_expr:
/* a, ... */
| a = expr ; Lcomma ; s = args_expr { a :: s }
/* c) */
| a = expr ; Lparfin { [ a ] }
/* ) */
| Lparfin { [] }