Merge branch 'nico' into anri

This commit is contained in:
Mylloon 2023-10-17 18:31:29 +02:00
commit 4ffd33ccc8
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 85 additions and 25 deletions

View file

@ -79,11 +79,12 @@ rule token = parse
| '{' { LBRACE } | '{' { LBRACE }
| '}' { RBRACE } | '}' { RBRACE }
| '_' { WILDCARD } | '_' { WILDCARD }
| ':' { DPOINT } | ':' { COLON }
| "->" { ARROW } | "->" { ARROW }
| '<' { INFERIOR } | '<' { INFERIOR }
| '>' { SUPERIOR } | '>' { SUPERIOR }
| '|' { PIPE } | '|' { PIPE }
| '&' { AND }
(** Values *) (** Values *)
| int as i { INT (Mint.of_string i) } | int as i { INT (Mint.of_string i) }

View file

@ -5,17 +5,22 @@
%} %}
%token EOF LET TYPE WILDCARD STAR ARROW DPOINT EXTERN FUN COMMA AND EQUAL LPAREN %token EOF LET TYPE WILDCARD STAR ARROW COLON EXTERN FUN COMMA AND EQUAL LPAREN
%token RPAREN LBRACK RBRACK LBRACE RBRACE INFERIOR SUPERIOR BINOP DO ELSE FOR %token RPAREN LBRACK RBRACK LBRACE RBRACE INFERIOR SUPERIOR BINOP DO ELSE FOR
%token FROM IF MATCH PIPE REF THEN TO UNTIL WHILE %token FROM IF MATCH PIPE REF THEN TO UNTIL WHILE
%token<Mint.t> INT %token<Mint.t> INT
%token<string> ID TID CID %token<string> ID TID CID STRING
%token<char> CHAR
%start<HopixAST.t> program %start<HopixAST.t> program
%right PIPE
%% %%
/********************************** PROGRAM ***********************************/
program: program:
/* Programme */ /* Programme */
| definition=located(definition)* EOF { | definition=located(definition)* EOF {
@ -38,6 +43,7 @@ definition:
DefineValue v DefineValue v
} }
tdefinition: tdefinition:
/* Type sommes */ /* Type sommes */
/* | option(PIPE) type_constructor option() separated_nonempty_list(COMMA, ty) { /* | option(PIPE) type_constructor option() separated_nonempty_list(COMMA, ty) {
@ -49,31 +55,36 @@ tdefinition:
} }
label_with_type: label_with_type:
| l=located(label) DPOINT t=located(ty) { l, t } | l=located(label) COLON t=located(ty) { l, t }
vdefinition: vdefinition:
/* Valeur simple */ /* Valeur simple */
// Manque le type ici, on met None en attendant | LET i=located(identifier) COLON ts=option(located(type_scheme)) EQUAL e=located(expression) {
| LET i=located(identifier) DPOINT ts=option(located(type_scheme)) EQUAL e=located(expression) {
SimpleValue(i, ts, e) SimpleValue(i, ts, e)
} }
/* Fonction(s) /* Fonction(s)
* Exemple : * Exemple :
* - fun : int f a = 1 * - fun : int f a = 1
* - fun f a = 1 and : int g a = 2 */ * - fun f a = 1 and : int g a = 2 */
| FUN fs=separated_nonempty_list(AND, fundef) { | FUN fl=separated_nonempty_list(AND, fundef) {
RecFunctions(fs) RecFunctions(fl)
} }
fundef: fundef:
| DPOINT 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, t, FunctionDefinition(p, e)
} }
| i=located(identifier) p=located(pattern) EQUAL e=located(expression) { | i=located(identifier) p=located(pattern) EQUAL e=located(expression) {
i, None, FunctionDefinition(p, e) i, None, FunctionDefinition(p, e)
} }
/********************************** 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 */
pattern: pattern:
| i=located(identifier) { | i=located(identifier) {
PVariable i PVariable i
@ -81,8 +92,44 @@ pattern:
| WILDCARD { | WILDCARD {
PWildcard PWildcard
} }
| p=located(pattern) COLON ty=located(ty) {
PTypeAnnotation(p,ty)
}
| l=located(literal) {
PLiteral l
}
| const=located(constructor) {
PTaggedValue(const, None, [])
}
| 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 {
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 {
PTaggedValue(const, liste_ty, liste_pattern)
}
/* à refaire */
| 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 {
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 {
PRecord(l, liste_ty)
}
| p1=located(pattern) PIPE p_list=separated_nonempty_list(PIPE, located(pattern)) {
POr(p1 :: p_list)
}
| p1=located(pattern) AND p_list=separated_nonempty_list(AND, located(pattern)) {
PAnd(p1 :: p_list)
}
/********************************* DATA TYPE **********************************/
/* Pour résoudre un conflit, on a du split ty en 2 règles /* Pour résoudre un conflit, on a du split ty en 2 règles
* *
* separated_nonempty_list(STAR,located(ty)) -> ty STAR separated_nonempty_list(STAR,located(ty)) * separated_nonempty_list(STAR,located(ty)) -> ty STAR separated_nonempty_list(STAR,located(ty))
@ -93,8 +140,11 @@ pattern:
*/ */
simple_ty: simple_ty:
/* Application d'un constructeur de type */ /* Application d'un constructeur de type */
| tc=type_constructor l=list_ty { | tc=type_constructor {
TyCon(tc, l) TyCon(tc, [])
}
| tc=type_constructor INFERIOR liste_ty=separated_nonempty_list(COMMA, located(ty)) SUPERIOR {
TyCon(tc, liste_ty)
} }
/* Variables de type */ /* Variables de type */
| type_var=type_variable { | type_var=type_variable {
@ -120,15 +170,9 @@ ty:
TyTuple(th :: tt) TyTuple(th :: tt)
} }
/* Auxilliaire pour TyCon dans ty */
list_ty:
| INFERIOR tl=separated_list(COMMA, located(ty)) SUPERIOR {
tl
}
type_scheme: type_scheme:
/* il faut peut être modifié le séparateur */ /* 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) ForallTy(liste_typevar, ty)
} }
@ -137,6 +181,14 @@ type_scheme:
} }
/********************************* EXPRESSION *********************************/
expression:
| l=located(literal) {
Literal l
}
/******************************** BASIC TYPES *********************************/
type_variable: type_variable:
| tid=TID { | tid=TID {
TId tid TId tid
@ -149,9 +201,15 @@ type_constructor:
} }
expression: constructor:
| l=located(literal) { | kid=CID {
Literal l KId kid
}
label:
| label=ID {
LId label
} }
@ -159,10 +217,11 @@ literal:
| i=INT { | i=INT {
LInt i LInt i
} }
| c=CHAR {
label: LChar c
| i=ID { }
LId i | s=STRING {
LString s
} }