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 }
| '}' { RBRACE }
| '_' { WILDCARD }
| ':' { DPOINT }
| ':' { COLON }
| "->" { ARROW }
| '<' { INFERIOR }
| '>' { SUPERIOR }
| '|' { PIPE }
| '&' { AND }
(** Values *)
| 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 FROM IF MATCH PIPE REF THEN TO UNTIL WHILE
%token<Mint.t> INT
%token<string> ID TID CID
%token<string> ID TID CID STRING
%token<char> CHAR
%start<HopixAST.t> program
%right PIPE
%%
/********************************** PROGRAM ***********************************/
program:
/* Programme */
| definition=located(definition)* EOF {
@ -38,6 +43,7 @@ definition:
DefineValue v
}
tdefinition:
/* Type sommes */
/* | option(PIPE) type_constructor option() separated_nonempty_list(COMMA, ty) {
@ -49,31 +55,36 @@ tdefinition:
}
label_with_type:
| l=located(label) DPOINT t=located(ty) { l, t }
| l=located(label) COLON t=located(ty) { l, t }
vdefinition:
/* Valeur simple */
// Manque le type ici, on met None en attendant
| LET i=located(identifier) DPOINT ts=option(located(type_scheme)) EQUAL e=located(expression) {
| LET i=located(identifier) COLON ts=option(located(type_scheme)) EQUAL e=located(expression) {
SimpleValue(i, ts, e)
}
/* Fonction(s)
* Exemple :
* - fun : int f a = 1
* - fun f a = 1 and : int g a = 2 */
| FUN fs=separated_nonempty_list(AND, fundef) {
RecFunctions(fs)
| FUN fl=separated_nonempty_list(AND, fundef) {
RecFunctions(fl)
}
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=located(identifier) p=located(pattern) EQUAL e=located(expression) {
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:
| i=located(identifier) {
PVariable i
@ -81,8 +92,44 @@ pattern:
| WILDCARD {
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
*
* separated_nonempty_list(STAR,located(ty)) -> ty STAR separated_nonempty_list(STAR,located(ty))
@ -93,8 +140,11 @@ pattern:
*/
simple_ty:
/* Application d'un constructeur de type */
| tc=type_constructor l=list_ty {
TyCon(tc, l)
| tc=type_constructor {
TyCon(tc, [])
}
| tc=type_constructor INFERIOR liste_ty=separated_nonempty_list(COMMA, located(ty)) SUPERIOR {
TyCon(tc, liste_ty)
}
/* Variables de type */
| type_var=type_variable {
@ -120,15 +170,9 @@ ty:
TyTuple(th :: tt)
}
/* Auxilliaire pour TyCon dans ty */
list_ty:
| INFERIOR tl=separated_list(COMMA, located(ty)) SUPERIOR {
tl
}
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) {
ForallTy(liste_typevar, ty)
}
@ -137,6 +181,14 @@ type_scheme:
}
/********************************* EXPRESSION *********************************/
expression:
| l=located(literal) {
Literal l
}
/******************************** BASIC TYPES *********************************/
type_variable:
| tid=TID {
TId tid
@ -149,9 +201,15 @@ type_constructor:
}
expression:
| l=located(literal) {
Literal l
constructor:
| kid=CID {
KId kid
}
label:
| label=ID {
LId label
}
@ -159,10 +217,11 @@ literal:
| i=INT {
LInt i
}
label:
| i=ID {
LId i
| c=CHAR {
LChar c
}
| s=STRING {
LString s
}