This commit is contained in:
Mylloon 2023-10-24 23:09:45 +02:00
parent f596c5c64d
commit 88efc86649
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -17,24 +17,16 @@
%start<HopixAST.t> program %start<HopixAST.t> program
/* TODO: Résoudre tout les shift/reduce conflits */
%right lparen1
%left LPAREN %left LPAREN
%left let1
/* %left FUN */
%left STRING %left STRING
%left INT CID CHAR /* WHILE */ %left INT CID CHAR
%left ID %left ID
/* %right REF DO */
/* %left LET MATCH IF FOR */
%right ARROW %right ARROW
%right SEMICOLON %right SEMICOLON
%left DOT %left DOT
%left ASSIGN %left ASSIGN
%left LBRACE %left LBRACE
/* %left BACKSLASH */
%left EXCLA COLON %left EXCLA COLON
@ -46,8 +38,6 @@
%left SLASH STAR %left SLASH STAR
%left local_def1 %left local_def1
%left ref1
/* %left ref1 */
%left fun1 %left fun1
%left app1 %left app1
@ -74,7 +64,6 @@ definition:
DefineType (tc, tvl, td) DefineType (tc, tvl, td)
} }
// La tdefinition peut être optionnel, dans ce cas on utilise c'est abstrait // La tdefinition peut être optionnel, dans ce cas on utilise c'est abstrait
| TYPE tc=located(type_constructor) tvl=optionlist(definition_typevariablelist) { | TYPE tc=located(type_constructor) tvl=optionlist(definition_typevariablelist) {
DefineType (tc, tvl, Abstract) DefineType (tc, tvl, Abstract)
} }
@ -121,9 +110,8 @@ label_with_type:
l, t l, t
} }
/* /* vdefinition et vdefinition_local font sensiblement la même chose,
vdefinition et vdefinition_local font sensiblement la même chose, seulement l'ordre de priorité est différent * seulement l'ordre de priorité est différent */
*/
vdefinition: vdefinition:
/* Valeur simple */ /* Valeur simple */
| LET i=located(var_identifier) ts=option(colon_type_scheme) | LET i=located(var_identifier) ts=option(colon_type_scheme)
@ -131,7 +119,6 @@ vdefinition:
SimpleValue(i, ts, e) SimpleValue(i, ts, e)
} }
/* Fonction(s) */ /* Fonction(s) */
| FUN fl=separated_nonempty_list(AND_KW, fundef) { | FUN fl=separated_nonempty_list(AND_KW, fundef) {
RecFunctions(fl) RecFunctions(fl)
} }
@ -156,10 +143,6 @@ fundef:
/********************************** PATTERN ***********************************/ /********************************** PATTERN ***********************************/
/* à revoir éventuellement : PTaggedValue (et PRecord) est réécrite 4 fois, mais
* peut être qu'en utilisant des option, on pourrait diminuer le nombre de répétition.
* TODO : y'a environ 50 warnings ici, surtout au niveau du POr et PAnd */
branches: branches:
| option(PIPE) b=separated_nonempty_list(PIPE, located(branch)) { | option(PIPE) b=separated_nonempty_list(PIPE, located(branch)) {
b b
@ -180,14 +163,14 @@ simple_pattern:
| WILDCARD { | WILDCARD {
PWildcard PWildcard
} }
/* N-uplets ou parenthésage */ /* Parenthésage */
| LPAREN RPAREN { | LPAREN RPAREN {
PTuple([]) PTuple([])
} }
/* N-uplets */
| l=pattern_list { | l=pattern_list {
match l with | [alone] -> Position.value alone | _ -> PTuple(l) match l with | [alone] -> Position.value alone | _ -> PTuple(l)
} }
/* Annotation de type */ /* Annotation de type */
| p=located(simple_pattern) COLON ty=located(ty) { | p=located(simple_pattern) COLON ty=located(ty) {
PTypeAnnotation(p, ty) PTypeAnnotation(p, ty)
@ -239,7 +222,6 @@ pattern_list:
* ty -> ty * [ ty ] * ty -> ty * [ ty ]
*/ */
simple_ty: simple_ty:
/* Application d'un constructeur de type */ /* Application d'un constructeur de type */
| tc=type_constructor { | tc=type_constructor {
@ -303,13 +285,11 @@ simple_expression:
| i=located(var_identifier) tl=option(type_list) { | i=located(var_identifier) tl=option(type_list) {
Variable(i, tl) Variable(i, tl)
} }
/* Annotation de type /* Annotation de type
* ( expr : type ) */ * ( expr : type ) */
| LPAREN e=located(expression) COLON t=located(ty) RPAREN { | LPAREN e=located(expression) COLON t=located(ty) RPAREN {
TypeAnnotation(e, t) TypeAnnotation(e, t)
} }
/* Tuple n = 0 - Construction d'un 0-uplet */ /* Tuple n = 0 - Construction d'un 0-uplet */
| LPAREN RPAREN { | LPAREN RPAREN {
Tuple([]) Tuple([])
@ -348,17 +328,14 @@ mid_expression:
| e=simple_expression { | e=simple_expression {
e e
} }
/* Field record */ /* Field record */
| e=located(mid_expression) DOT l=located(label_identifier) tl=option(type_list) { | e=located(mid_expression) DOT l=located(label_identifier) tl=option(type_list) {
Field(e, l, tl) Field(e, l, tl)
} }
/* Tagged Value - Construction d'une donnée */ /* Tagged Value - Construction d'une donnée */
| const=located(constructor) tl=option(type_list) el=optionlist(expr_list) { | const=located(constructor) tl=option(type_list) el=optionlist(expr_list) {
Tagged(const, tl, el) Tagged(const, tl, el)
} }
/* Application */ /* Application */
| e1=located(mid_expression) e2=located(mid_expression) %prec app1 { | e1=located(mid_expression) e2=located(mid_expression) %prec app1 {
Apply(e1, e2) Apply(e1, e2)
@ -370,12 +347,10 @@ expression:
| e=mid_expression { | e=mid_expression {
e e
} }
/* Sequence - Séquencement */ /* Sequence - Séquencement */
| e=located(expression) SEMICOLON e2=located(expression) { | e=located(expression) SEMICOLON e2=located(expression) {
Sequence([e; e2]) Sequence([e; e2])
} }
/* Definition locale */ /* Definition locale */
| vd=vdefinition_local SEMICOLON e=located(expression) { | vd=vdefinition_local SEMICOLON e=located(expression) {
Define(vd, e) Define(vd, e)
@ -384,7 +359,6 @@ expression:
| BACKSLASH p=located(pattern) ARROW e=located(expression) { | BACKSLASH p=located(pattern) ARROW e=located(expression) {
Fun(FunctionDefinition(p, e)) Fun(FunctionDefinition(p, e))
} }
/* Operateurs binaires - Application infixe */ /* Operateurs binaires - Application infixe */
| e1=located(expression) b=binop e2=located(expression) { | e1=located(expression) b=binop e2=located(expression) {
Apply( Apply(
@ -400,8 +374,6 @@ expression:
e2 e2
) )
} }
/* Analyse de motifs /* Analyse de motifs
* match (exp) {| ...| ... | ...} */ * match (exp) {| ...| ... | ...} */
| MATCH LPAREN e=located(expression) RPAREN LBRACE b=branches RBRACE { | MATCH LPAREN e=located(expression) RPAREN LBRACE b=branches RBRACE {
@ -421,7 +393,6 @@ expression:
ELSE LBRACE e3=located(expression) RBRACE { ELSE LBRACE e3=located(expression) RBRACE {
IfThenElse(e1, e2, e3) IfThenElse(e1, e2, e3)
} }
/* Affectation /* Affectation
* expr := expr */ * expr := expr */
| e1=located(expression) ASSIGN e2=located(expression) { | e1=located(expression) ASSIGN e2=located(expression) {
@ -447,7 +418,6 @@ expression:
LBRACE e3=located(expression) RBRACE { LBRACE e3=located(expression) RBRACE {
For(var, e1, e2, e3) For(var, e1, e2, e3)
} }
/* Allocation /* Allocation
* ref expr */ * ref expr */
| REF e=located(mid_expression) { | REF e=located(mid_expression) {
@ -472,7 +442,6 @@ constructor:
KId constr_id KId constr_id
} }
type_constructor: type_constructor:
| type_con=ID { | type_con=ID {
TCon type_con TCon type_con
@ -483,7 +452,6 @@ label_identifier:
LId label LId label
} }
literal: literal:
/* Entier positif */ /* Entier positif */
| i=INT { | i=INT {
@ -499,6 +467,7 @@ literal:
} }
/****************************** INLINE FUNCTIONS ******************************/
%inline binop: %inline binop:
| PLUS { "`+`" } | PLUS { "`+`" }
| MINUS { "`-`" } | MINUS { "`-`" }
@ -512,32 +481,6 @@ literal:
| INF_OP { "`<?`" } | INF_OP { "`<?`" }
| SUP_OP { "`>?`" } | SUP_OP { "`>?`" }
// Utile pour le binop avec location :
// %inline binop:
// | loc=location(PLUS) { ("`+`", loc) }
// | loc=location(MINUS) { ("`-`", loc) }
// | loc=location(STAR) { ("`*`", loc) }
// | loc=location(SLASH) { ("`/`", loc) }
// | loc=location(D_AND) { ("`&&`", loc) }
// | loc=location(D_OR) { ("`||`", loc) }
// | loc=location(EQUAL_OP) { ("`=?`", loc) }
// | loc=location(INF_EQUAL_OP) { ("`<=?`", loc) }
// | loc=location(SUP_EQUAL_OP) { ("`>=?`", loc) }
// | loc=location(INF_OP) { ("`<?`", loc) }
// | loc=location(SUP_OP) { ("`>?`", loc) }
// /* On récupère juste la position de X */
// %inline location(X): X {
// Position.position (Position.with_poss $startpos $endpos None)
// }
// /* On transforme notre binop en variable located */
// %inline var_binop(X): x=X {
// Position.with_pos
// (snd x)
// (Variable (Position.with_pos (snd x) (Id (fst x)), None))
// }
%inline located(X): x=X { %inline located(X): x=X {
Position.with_poss $startpos $endpos x Position.with_poss $startpos $endpos x
} }