From 04e06654de0718591d4b29abf68f3c697deb5a25 Mon Sep 17 00:00:00 2001 From: Nicolas PENELOUX Date: Wed, 11 Oct 2023 20:16:46 +0200 Subject: [PATCH 1/2] ajout parser et lexer (prof au niveau de RecFunctions) --- flap/src/hopix/hopixLexer.mll | 8 ++++++ flap/src/hopix/hopixParser.mly | 45 ++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/flap/src/hopix/hopixLexer.mll b/flap/src/hopix/hopixLexer.mll index 6808086..6523454 100644 --- a/flap/src/hopix/hopixLexer.mll +++ b/flap/src/hopix/hopixLexer.mll @@ -30,6 +30,7 @@ let integers = '-'? (digit+ let ident = ['a'-'z']['A'-'Z' 'a'-'z' '0'-'9' '_']* let constr_id = ['A'-'Z']['A'-'Z' 'a'-'z' '0'-'9' '_']* let type_variable = '\''ident +(*TODO type_con*) let binop = '+' | '-' | '*' | '/' | "&&" | "||"| "=?"| "<=?" |">=?" |"?" @@ -65,6 +66,13 @@ rule token = parse (** Operators *) | '=' { EQUAL } + (* ponctuation *) + | '(' { LPAREN } + | ')' { RPAREN } + | '[' { LBRACK } + | ']' { RBRACK } + | '_' { WILDCARD } + (** Values *) | integers as i { INT (Mint.of_string i) } diff --git a/flap/src/hopix/hopixParser.mly b/flap/src/hopix/hopixParser.mly index 50e9587..af3b0d1 100644 --- a/flap/src/hopix/hopixParser.mly +++ b/flap/src/hopix/hopixParser.mly @@ -5,10 +5,10 @@ %} -%token EOF LET TYPE EXTERN FUN MATCH IF THEN ELSE REF WHILE DO UNTIL FOR FROM TO BINOP EQUAL +%token EOF LET TYPE WILDCARD EXTERN FUN MATCH IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK %token INT -%token ID +%token ID TID %start program @@ -28,6 +28,47 @@ vdefinition: // manque le type ici, on met None en attendant | LET i=located(identifier) EQUAL e=located(expression) { SimpleValue(i, None, e) } + (* marche pas*) +| FUN f=fundef AND* f2=fundef* { + RecFunctions([f::f2]) +} + + +(* de même*) +fundef: +|(*ts est temp*) ts= type_scheme* i=located(identifier) p=pattern EQUAL e=located(expression){ + FunctionDefinition(p,e) +} + + +pattern: +| i=located(identifier){ + PVariable i +} +| WILDCARD { + PWildcard +} + + + +ty: +| LPAREN type_var=type_variable RPAREN { + TyVar type_var +} + +type_scheme: +| LBRACK type_variable=located(type_variable)+ RBRACK ty=located(ty){ + ForallTy(type_variable,ty) +} + + +type_variable: +| tid=TID { + TId tid +} + + + expression: | l=located(literal) { From a58be772acb0d2e2a1f1d0c59f6863294c3a3735 Mon Sep 17 00:00:00 2001 From: Nicolas PENELOUX Date: Mon, 16 Oct 2023 22:59:17 +0200 Subject: [PATCH 2/2] ajout parser et un peu lexer --- flap/src/hopix/hopixLexer.mll | 24 ++++++++++++---- flap/src/hopix/hopixParser.mly | 51 +++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/flap/src/hopix/hopixLexer.mll b/flap/src/hopix/hopixLexer.mll index 6523454..2f02e21 100644 --- a/flap/src/hopix/hopixLexer.mll +++ b/flap/src/hopix/hopixLexer.mll @@ -27,12 +27,18 @@ let integers = '-'? (digit+ | bina+ | octa+) -let ident = ['a'-'z']['A'-'Z' 'a'-'z' '0'-'9' '_']* +let printable = [' ' '\t' '\n' '\r' (*33-126*)] (*pas sûr*) +let ident = ['a'-'z']['A'-'Z' 'a'-'z' '0'-'9' '_']* let constr_id = ['A'-'Z']['A'-'Z' 'a'-'z' '0'-'9' '_']* -let type_variable = '\''ident -(*TODO type_con*) +let type_variable = ['\'']['a'-'z']['A'-'Z' 'a'-'z' '0'-'9' '_']* +(* On ne peut pas différencier au niveau du lexer var_id label_id et type_con, il faudra le faire à l'analyseur syntaxique*) + +let atom = '"' +let char = ['\'']atom['\''] +let string = ['\"']((atom | '\'' | "\\\""))['\"'] + +(*let binop = '+' | '-' | '*' | '/' | "&&" | "||"| "=?"| "<=?" |">=?" |"?"*) -let binop = '+' | '-' | '*' | '/' | "&&" | "||"| "=?"| "<=?" |">=?" |"?" rule token = parse (** Layout *) @@ -58,11 +64,14 @@ rule token = parse | "for" { FOR } | "from" { FROM } | "to" { TO } + | "and" { AND } + | "for" { FOR } (* Fini ? *) (* binar operation : pas sûr pour celui là*) + (* | binop as b { BINOP (* TODO *) } - +*) (** Operators *) | '=' { EQUAL } @@ -72,7 +81,10 @@ rule token = parse | '[' { LBRACK } | ']' { RBRACK } | '_' { WILDCARD } - + | ':' { DPOINT } + | "->" { ARROW } + | "<" { INFERIOR } + | ">" { SUPERIOR } (** Values *) | integers as i { INT (Mint.of_string i) } diff --git a/flap/src/hopix/hopixParser.mly b/flap/src/hopix/hopixParser.mly index af3b0d1..4dcdd8f 100644 --- a/flap/src/hopix/hopixParser.mly +++ b/flap/src/hopix/hopixParser.mly @@ -5,7 +5,7 @@ %} -%token EOF LET TYPE WILDCARD EXTERN FUN MATCH 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 DPOINT 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 @@ -27,16 +27,23 @@ definition: vdefinition: // manque le type ici, on met None en attendant | 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){ + SimpleValue(i,ts,e) +}*) (* marche pas*) -| FUN f=fundef AND* f2=fundef* { - RecFunctions([f::f2]) + +| FUN f=fundef { + RecFunctions([f]) } - +(*fun : int f a = 1*) (* de même*) fundef: -|(*ts est temp*) ts= type_scheme* i=located(identifier) p=pattern EQUAL e=located(expression){ +|(*ts est temp*) i=located(identifier) p=pattern EQUAL e=located(expression){ + FunctionDefinition(p,e) +} +|(*ts est temp*) DPOINT ts=located(type_scheme) i=located(identifier) p=pattern EQUAL e=located(expression){ FunctionDefinition(p,e) } @@ -52,14 +59,33 @@ pattern: ty: +| tc = type_constructor{ + TyCon(tc,None) +} + +(* 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) +} | LPAREN type_var=type_variable RPAREN { TyVar type_var } -type_scheme: -| LBRACK type_variable=located(type_variable)+ RBRACK ty=located(ty){ - ForallTy(type_variable,ty) -} + type_scheme: + (* 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) + } + | ty=located(ty){ + ForallTy([],ty) + } type_variable: @@ -67,6 +93,11 @@ type_variable: TId tid } +type_constructor: +| tcon = TID { + TCon tcon +} +