From 2f05f7ba09558a3d5d23f8d419d2d5e17b92f157 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 21 Oct 2023 02:43:18 +0200 Subject: [PATCH] fmt and some rewrite of expressions --- flap/src/hopix/hopixParser.mly | 258 ++++++++++++++++----------------- 1 file changed, 125 insertions(+), 133 deletions(-) diff --git a/flap/src/hopix/hopixParser.mly b/flap/src/hopix/hopixParser.mly index b48a835..c5aebe8 100644 --- a/flap/src/hopix/hopixParser.mly +++ b/flap/src/hopix/hopixParser.mly @@ -36,9 +36,13 @@ program: definition: /* Définition de types */ -// Manque le 'type_variable located list' ici, on met une liste vide en attendant -| TYPE tc=located(type_constructor) EQUAL td=tdefinition { - DefineType (tc, [], td) +| TYPE tc=located(type_constructor) tvl=optionlist(definition_typevariablelist) + EQUAL td=tdefinition { + DefineType (tc, tvl, td) + } +// La tdefinition peut être optionnel, dans ce cas on utilise c'est abstrait +| TYPE tc=located(type_constructor) tvl=optionlist(definition_typevariablelist) { + DefineType (tc, tvl, Abstract) } /* Valeurs externes */ | EXTERN id=located(identifier) ts=located(type_scheme) { @@ -49,11 +53,16 @@ definition: DefineValue v } +definition_typevariablelist: +| INFERIOR l=separated_nonempty_list(COMMA, located(type_variable)) SUPERIOR { + l + } + tdefinition: /* Type sommes */ /* la définition étant assez compliqué, on va utilisé d'autre terme pour réduire la taille */ -| option(PIPE) l=separated_nonempty_list(PIPE,list_constructor_and_ty) { +| option(PIPE) l=separated_nonempty_list(PIPE, list_constructor_and_their_ty) { DefineSumType(l) } /* Type produit étiqueté */ @@ -61,14 +70,16 @@ tdefinition: DefineRecordType(lt) } -list_constructor_and_ty: c=located(constructor) t=list_ty{ - (c,t) -} - -list_ty: LPAREN l=separated_nonempty_list(COMMA,located(ty)) RPAREN { - l -} +list_constructor_and_their_ty: +// TODO: C'est pas sensé être en option list_ty ici? +| c=located(constructor) t=list_ty { + (c, t) + } +list_ty: +| LPAREN l=separated_nonempty_list(COMMA, located(ty)) RPAREN { + l + } label_with_type: | l=located(label) COLON t=located(ty) { @@ -86,7 +97,7 @@ vdefinition: * - fun : int f a = 1 * - fun f a = 1 and : int g a = 2 */ | FUN fl=separated_nonempty_list(AND_KW, fundef) { - RecFunctions(fl) + RecFunctions(fl) } vdef_type_scheme: @@ -96,7 +107,8 @@ vdef_type_scheme: fundef: -| COLON t=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression) { +| COLON t=option(located(type_scheme)) i=located(identifier) p=located(pattern) + EQUAL e=located(expression) { i, t, FunctionDefinition(p, e) } | i=located(identifier) p=located(pattern) EQUAL e=located(expression) { @@ -110,10 +122,7 @@ fundef: * TODO : y'a environ 50 warnings ici, surtout au niveau du POr et PAnd */ branches: -| b=separated_nonempty_list(PIPE, located(branch)) { - b - } -| PIPE b=separated_nonempty_list(PIPE, located(branch)) { +| option(PIPE) b=separated_nonempty_list(PIPE, located(branch)) { b } @@ -148,24 +157,37 @@ simple_pattern: | const=located(constructor) { PTaggedValue(const, None, []) } -| const=located(constructor) INFERIOR liste_ty=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { +| const=located(constructor) + INFERIOR liste_ty=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { PTaggedValue(const, liste_ty, []) } -| const=located(constructor) LPAREN liste_pattern=separated_nonempty_list(COMMA, located(pattern)) RPAREN { +| const=located(constructor) + LPAREN liste_pattern=separated_nonempty_list(COMMA, located(pattern)) RPAREN { PTaggedValue(const, None, liste_pattern) } -| const=located(constructor) INFERIOR liste_ty=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR LPAREN liste_pattern=separated_nonempty_list(COMMA, located(pattern)) RPAREN { +| const=located(constructor) + INFERIOR liste_ty=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR + LPAREN liste_pattern=separated_nonempty_list(COMMA, located(pattern)) RPAREN { PTaggedValue(const, liste_ty, liste_pattern) } /* Enregistrement */ /* à refaire */ -| LBRACE l=separated_nonempty_list(COMMA, separated_pair(located(label), EQUAL, located(pattern))) RBRACE { +| LBRACE l=separated_nonempty_list( + COMMA, + separated_pair(located(label), EQUAL, located(pattern)) + ) RBRACE { PRecord(l, None) } -| LBRACE l=separated_nonempty_list(COMMA, separated_pair(located(label), EQUAL, located(pattern))) RBRACE INFERIOR SUPERIOR { +| LBRACE l=separated_nonempty_list( + COMMA, + separated_pair(located(label), EQUAL, located(pattern)) + ) RBRACE INFERIOR SUPERIOR { PRecord(l, None) } -| LBRACE l=separated_nonempty_list(COMMA, separated_pair(located(label), EQUAL, located(pattern))) RBRACE INFERIOR liste_ty=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { +| LBRACE l=separated_nonempty_list( + COMMA, + separated_pair(located(label), EQUAL, located(pattern)) + ) RBRACE INFERIOR liste_ty=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { PRecord(l, liste_ty) } /* Disjonction */ @@ -182,7 +204,8 @@ pattern_and: | p1=simple_pattern{ p1 } -| p1=located(simple_pattern) AND p_list=separated_nonempty_list(AND, located(simple_pattern)) { +| p1=located(simple_pattern) + AND p_list=separated_nonempty_list(AND, located(simple_pattern)) { PAnd(p1 :: p_list) } @@ -207,7 +230,9 @@ simple_ty: | tc=type_constructor { TyCon(tc, []) } -| tc=type_constructor INFERIOR liste_ty=separated_nonempty_list(COMMA, located(ty)) SUPERIOR { +/* 'liste_ty' doit etre optionnel => gérer par le cas au dessus */ +| tc=type_constructor + INFERIOR liste_ty=separated_nonempty_list(COMMA, located(ty)) SUPERIOR { TyCon(tc, liste_ty) } /* Variables de type */ @@ -237,7 +262,8 @@ ty: type_scheme: /* Il faut peut être modifié le séparateur */ -| LBRACK liste_typevar=separated_list(COMMA, located(type_variable)) RBRACK ty=located(ty) { +| LBRACK liste_typevar=separated_list(COMMA, located(type_variable)) + RBRACK ty=located(ty) { ForallTy(liste_typevar, ty) } | ty=located(ty) { @@ -247,167 +273,134 @@ type_scheme: /********************************* EXPRESSION *********************************/ -/* De manière générale, il faudrait au mieux revoir le code, pour le factoriser et le rendre plus propre */ -/* (il y a même moyen que ça le soit obligatoire pour pas avoir des conflits éventuel) */ -/* Exemple : TAgged et Record, trop de cas différent alors qu'on pourrait en faire en 2 fois au moins voir 1 */ simple_expression: /* Simple litteral */ | l=located(literal) { Literal l } /* Variable */ -| i=located(identifier) { - Variable(i, None) - } -| i=located(identifier) INFERIOR SUPERIOR { - Variable(i, None) - } -| i=located(identifier) INFERIOR t_list=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { - Variable(i, t_list) +| i=located(identifier) tl=option(type_list) { + Variable(i, tl) } -/* Tuple n = 0 and n > 1 */ +/* Tuple n = 0 and n > 1 - Construction d'un 0-uplet */ | LPAREN RPAREN { Tuple([]) } -| LPAREN e=located(expression) COMMA e_list=separated_nonempty_list(COMMA, located(expression)) RPAREN { - Tuple(e::e_list) +/* Tuple n > 1 - Construction d'un n-uplet (n > 1) */ +| el=expr_list { + Tuple(el) } - -/* Tagged Value*/ -/* K */ -| const=located(constructor) { - Tagged(const, None, []) +/* Tagged Value - Construction d'une donnée */ +| const=located(constructor) tl=option(type_list) el=optionlist(expr_list) { + Tagged(const, tl, el) } -/* K < > */ -| const=located(constructor) INFERIOR SUPERIOR { - Tagged(const, None, []) +/* Record - Construction d'un enregistrement */ +| LBRACE l=separated_nonempty_list( + COMMA, + separated_pair(located(label), EQUAL, located(expression)) + ) RBRACE tl=option(type_list) { + Record(l, tl) } -/* K < > (e1, ..., en) */ -| const=located(constructor) INFERIOR SUPERIOR LPAREN e_list=separated_nonempty_list(COMMA, located(expression)) RPAREN { - Tagged(const, None, e_list) - } -/* K */ -| const=located(constructor) INFERIOR t_list=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { - Tagged(const, t_list, []) - } -/* K (e1, ..., en) */ -| const=located(constructor) INFERIOR t_list=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR LPAREN e_list=separated_nonempty_list(COMMA, located(expression)) RPAREN { - Tagged(const, t_list, e_list) - } - -/* Record */ -| LBRACE l=separated_nonempty_list(COMMA, separated_pair(located(label), EQUAL, located(expression))) RBRACE { - Record(l, None) - } -| LBRACE l=separated_nonempty_list(COMMA, separated_pair(located(label), EQUAL, located(expression))) RBRACE INFERIOR SUPERIOR { - Record(l, None) - } -| LBRACE l=separated_nonempty_list(COMMA, separated_pair(located(label), EQUAL, located(expression))) RBRACE INFERIOR t_list=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { - Record(l, t_list) - } - -/* Lecture de variable */ -/* ! expr */ +/* Lecture de variable + * !expr */ | EXCLA e=located(simple_expression) { Read(e) } +type_list: +| INFERIOR tl=optionlist(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { + tl + } + +expr_list: +| LPAREN el=separated_nonempty_list(COMMA, located(expression)) RPAREN { + el + } expression: | e=simple_expression { e } -/* Field */ - -/* e.l */ -| e=located(expression) DOT l=located(label) { - Field(e, l, None) +/* Field - Projection d’un champ */ +| e=located(expression) DOT l=located(label) tl=option(type_list) { + Field(e, l, tl) } -/* e.l < > */ -| e=located(expression) DOT l=located(label) INFERIOR SUPERIOR { - Field(e, l, None) +/* Sequence - Séquencement * + * Pas sûr, voir s'il ne fuat pas une troisième couche d'expression */ +| e=located(simple_expression) + SEMICOLON e_list=separated_nonempty_list(SEMICOLON, located(simple_expression)) { + Sequence(e :: e_list) } -/* e.l */ -| e=located(expression) DOT l=located(label) INFERIOR t_list=option(separated_nonempty_list(COMMA, located(ty))) SUPERIOR { - Field(e, l, t_list) - } -/* Sequence */ -/* Pas sûr, voir s'il ne fuat pas une troisième couche d'expression */ -| e=located(simple_expression) SEMICOLON e_list=separated_nonempty_list(SEMICOLON, located(simple_expression)) { - Sequence(e::e_list) - } - /* Definition locale */ | vd=vdefinition SEMICOLON e=located(expression) { Define(vd, e) } - /* Fonction anonyme */ | BACKSLASH p=located(pattern) ARROW e=located(expression) { Fun(FunctionDefinition(p, e)) } - /* Application */ | e1=located(expression) e2=located(expression) { Apply(e1, e2) } - - -/* Match (exp) {| ...| ... | ...} */ +/* Operateurs binaires - Application infixe */ +/* | e1=located(expression) b=binop e2=located(expression) { + Apply(Apply(b, e1), e2) + } */ +/* Analyse de motifs + * match (exp) {| ...| ... | ...} */ | MATCH LPAREN e=located(expression) RPAREN LBRACE b=branches RBRACE { Case(e, b) } - -/* TODO if ( exp ) then { expr } j'ai RIEN COMPRIS */ -/* | IF LPAREN e=located(expression) RPAREN +/* Conditionnelle (1) + * if ( expr ) then { expr } */ +| IF LPAREN e1=located(expression) RPAREN THEN LBRACE e2=located(expression) RBRACE { - IfThenElse(e, e2, None) - } */ - -/* if ( expr ) then { expr } else { expr } */ -| IF LPAREN e=located(expression) RPAREN + (* else { () } aka le 0-uplet *) + IfThenElse(e1, e2, Position.unknown_pos (Tuple [])) + } +/* Conditionnelle (2) + * if ( expr ) then { expr } else { expr } */ +| IF LPAREN e1=located(expression) RPAREN THEN LBRACE e2=located(expression) RBRACE ELSE LBRACE e3=located(expression) RBRACE { - IfThenElse(e, e2, e3) + IfThenElse(e1, e2, e3) } - -/* Reference ref expr */ +/* Reference - Allocation + * ref expr */ | REF e=located(expression) { Ref(e) } - /* Affectation * expr := expr */ | e1=located(expression) ASSIGN e2=located(expression) { Assign(e1, e2) } - -/* While */ -/* while ( expr ) { expr } */ -| WHILE LPAREN e=located(expression) RPAREN +/* While - Boucle non bornée + * while ( expr ) { expr } */ +| WHILE LPAREN e1=located(expression) RPAREN LBRACE e2=located(expression) RBRACE { - While(e, e2) + While(e1, e2) } - -/* Do while +/* Do while - Boucle non bornée et non vide * do { expr } until ( expr ) */ -/* TODO */ - -/* Boucle for +| DO LBRACE e1=located(expression) RBRACE UNTIL LPAREN e2=located(expression) RPAREN { + Sequence([e2 ; Position.unknown_pos (While(e1, e2))]) + } +/* Boucle for - Boucle bornée * for x in (e1 to e2) { expr } */ -| FOR x=located(identifier) +| FOR var=located(identifier) FROM LPAREN e1=located(expression) RPAREN TO LPAREN e2=located(expression) RPAREN LBRACE e3=located(expression) RBRACE { - For(x, e1, e2, e3) + For(var, e1, e2, e3) } - -/* Parenthésage pas sûr mais je vois pas sinon */ +/* Parenthésage + * Pas sûr mais je vois pas sinon */ | LPAREN e=expression RPAREN { e } - /* Annotation de type * (e : ty) */ | LPAREN e=located(expression) COLON t=located(ty) RPAREN { @@ -415,14 +408,6 @@ expression: } -/* Operateurs binaires */ -/* | e1=located(expression) b=binop e2=located(expression) { - Apply(Apply(b,e1),e2) - } */ - - - - /******************************** BASIC TYPES *********************************/ @@ -451,12 +436,15 @@ label: literal: +/* Entier positif */ | i=INT { LInt i } +/* Caractère */ | c=CHAR { LChar c } +/* Chaîne de caractères */ | s=STRING { LString s } @@ -469,7 +457,7 @@ identifier: %inline binop: -/*| loc=located(PLUS) { ("`+`",loc) }*/ +/* | loc=located(PLUS) { ("`+`",loc) } */ | MINUS { "`-`" } | STAR { "`*`" } @@ -485,3 +473,7 @@ identifier: %inline located(X): x=X { Position.with_poss $startpos $endpos x } + +%inline optionlist(X): x=option(X) { + match x with | Some l -> l | None -> [] +}