From be467f35031fdad4cd979f112577a5f12e0f0800 Mon Sep 17 00:00:00 2001 From: Nicolas PENELOUX Date: Tue, 17 Oct 2023 16:19:51 +0200 Subject: [PATCH 1/2] ajout type et fonction dans le parser --- flap/src/hopix/hopixLexer.mll | 2 +- flap/src/hopix/hopixParser.mly | 85 +++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/flap/src/hopix/hopixLexer.mll b/flap/src/hopix/hopixLexer.mll index 2f02e21..e4eb1eb 100644 --- a/flap/src/hopix/hopixLexer.mll +++ b/flap/src/hopix/hopixLexer.mll @@ -81,7 +81,7 @@ rule token = parse | '[' { LBRACK } | ']' { RBRACK } | '_' { WILDCARD } - | ':' { DPOINT } + | ':' { COLON } | "->" { ARROW } | "<" { INFERIOR } | ">" { SUPERIOR } diff --git a/flap/src/hopix/hopixParser.mly b/flap/src/hopix/hopixParser.mly index 4dcdd8f..d808b6f 100644 --- a/flap/src/hopix/hopixParser.mly +++ b/flap/src/hopix/hopixParser.mly @@ -5,14 +5,18 @@ %} -%token EOF LET TYPE WILDCARD STAR INFERIOR SUPERIOR ARROW DPOINT EXTERN FUN MATCH COMMA IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK +%token EOF LET TYPE WILDCARD STAR INFERIOR SUPERIOR ARROW COLON EXTERN FUN MATCH COMMA IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK %token INT -%token ID TID +%token ID TID CID STRING +%token CHAR %start program %% + (************** PROGRAM ****************) + + program: | definition=located(definition)* EOF { @@ -24,30 +28,34 @@ definition: DefineValue v } -vdefinition: // manque le type ici, on met None en attendant +vdefinition: | LET i=located(identifier) EQUAL e=located(expression) { SimpleValue(i, None, e) - }(* -| LET i=located(identifier) DPOINT ts=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) -}*) - (* marche pas*) - +} + /* | FUN f=fundef { RecFunctions([f]) +}*/ +| FUN f=separated_nonempty_list(AND,fundef){ + RecFunctions(f) } -(*fun : int f a = 1*) -(* de même*) fundef: -|(*ts est temp*) i=located(identifier) p=pattern EQUAL e=located(expression){ - FunctionDefinition(p,e) +| i=located(identifier) p=located(pattern) EQUAL e=located(expression){ + i,None,FunctionDefinition(p,e) } -|(*ts est temp*) DPOINT ts=located(type_scheme) i=located(identifier) p=pattern EQUAL e=located(expression){ - FunctionDefinition(p,e) +| DPOINT ts=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression){ + i,ts,FunctionDefinition(p,e) } + + (************* PATTERN **************) + + pattern: | i=located(identifier){ PVariable i @@ -57,22 +65,20 @@ pattern: } + (************** DATA TYPE **************) ty: | tc = type_constructor{ - TyCon(tc,None) + TyCon(tc,[]) +} +| tc= type_constructor INFERIOR liste_ty=separated_nonempty_list(COMMA,located(ty)) SUPERIOR{ + TyCon(tc,liste_ty) } - -(* Todo version avec l'optionnel*) - | ty1=located(ty) ARROW ty2=located(ty){ TyArrow(ty1,ty2) } -| ty1=located(ty) STAR ty2=located(ty){ - TyTuple([ty1;ty2]) -} -| ty1=located(ty) STAR ty2= located(ty) STAR liste_ty = separated_list(STAR,located(ty)){ - TyTuple(ty1::ty2::liste_ty) +| ty1=located(ty) STAR liste_ty = separated_nonempty_list(STAR,located(ty)){ + TyTuple(ty1::liste_ty) } | LPAREN type_var=type_variable RPAREN { TyVar type_var @@ -88,6 +94,20 @@ ty: } + (************ EXPRESSION **************) + + +expression: +| l=located(literal) { + Literal l + } + + + + (******* BASIC TYPES *********) + + + type_variable: | tid=TID { TId tid @@ -98,18 +118,21 @@ type_constructor: TCon tcon } +constructor: +| kid = CID { + KId kid +} +label: +| label = TID { + TId = label +} -expression: -| l=located(literal) { - Literal l - } - literal: -| i=INT { - LInt i - } +| i=INT { LInt i} +| c=CHAR { LChar c} +| s = STRING { LString s } identifier: | i=ID { From d957aa29a924a9813f26409b91e9cf3e04724d9c Mon Sep 17 00:00:00 2001 From: Nicolas PENELOUX Date: Tue, 17 Oct 2023 17:02:54 +0200 Subject: [PATCH 2/2] ajout pattern pour le parser, attention aux conflits never reduced de Or et And --- flap/src/hopix/hopixLexer.mll | 4 +++ flap/src/hopix/hopixParser.mly | 46 +++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/flap/src/hopix/hopixLexer.mll b/flap/src/hopix/hopixLexer.mll index e4eb1eb..545ae8a 100644 --- a/flap/src/hopix/hopixLexer.mll +++ b/flap/src/hopix/hopixLexer.mll @@ -82,9 +82,13 @@ rule token = parse | ']' { RBRACK } | '_' { WILDCARD } | ':' { COLON } + | '{' { LBRACE } + | '}' { RBRACE } | "->" { ARROW } | "<" { INFERIOR } | ">" { SUPERIOR } + | '|' { PIPE } + | '&' { AND } (** Values *) | integers as i { INT (Mint.of_string i) } diff --git a/flap/src/hopix/hopixParser.mly b/flap/src/hopix/hopixParser.mly index d808b6f..d3e2e87 100644 --- a/flap/src/hopix/hopixParser.mly +++ b/flap/src/hopix/hopixParser.mly @@ -5,7 +5,7 @@ %} -%token EOF LET TYPE WILDCARD STAR INFERIOR SUPERIOR ARROW COLON EXTERN FUN MATCH COMMA IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK +%token EOF LET TYPE WILDCARD LBRACE RBRACE STAR PIPE INFERIOR SUPERIOR ARROW COLON EXTERN FUN MATCH COMMA IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK %token INT %token ID TID CID STRING @@ -13,6 +13,8 @@ %start program +%right PIPE + %% (************** PROGRAM ****************) @@ -47,7 +49,7 @@ fundef: | i=located(identifier) p=located(pattern) EQUAL e=located(expression){ i,None,FunctionDefinition(p,e) } -| DPOINT ts=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression){ +| COLON ts=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression){ i,ts,FunctionDefinition(p,e) } @@ -56,6 +58,9 @@ fundef: (************* 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 @@ -63,8 +68,43 @@ 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 **************) ty: @@ -125,7 +165,7 @@ constructor: label: | label = TID { - TId = label + LId label }