ajout type et fonction dans le parser

This commit is contained in:
Nicolas PENELOUX 2023-10-17 16:19:51 +02:00
parent a58be772ac
commit be467f3503
2 changed files with 55 additions and 32 deletions

View file

@ -81,7 +81,7 @@ rule token = parse
| '[' { LBRACK } | '[' { LBRACK }
| ']' { RBRACK } | ']' { RBRACK }
| '_' { WILDCARD } | '_' { WILDCARD }
| ':' { DPOINT } | ':' { COLON }
| "->" { ARROW } | "->" { ARROW }
| "<" { INFERIOR } | "<" { INFERIOR }
| ">" { SUPERIOR } | ">" { SUPERIOR }

View file

@ -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<Mint.t> INT %token<Mint.t> INT
%token<string> ID TID %token<string> ID TID CID STRING
%token<char> CHAR
%start<HopixAST.t> program %start<HopixAST.t> program
%% %%
(************** PROGRAM ****************)
program: program:
| definition=located(definition)* EOF { | definition=located(definition)* EOF {
@ -24,30 +28,34 @@ definition:
DefineValue v DefineValue v
} }
vdefinition: // manque le type ici, on met None en attendant vdefinition:
| LET i=located(identifier) EQUAL e=located(expression) { | LET i=located(identifier) EQUAL e=located(expression) {
SimpleValue(i, None, e) 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) SimpleValue(i,ts,e)
}*) }
(* marche pas*) /*
| FUN f=fundef { | FUN f=fundef {
RecFunctions([f]) RecFunctions([f])
}*/
| FUN f=separated_nonempty_list(AND,fundef){
RecFunctions(f)
} }
(*fun : int f a = 1*)
(* de même*)
fundef: fundef:
|(*ts est temp*) i=located(identifier) p=pattern EQUAL e=located(expression){ | i=located(identifier) p=located(pattern) EQUAL e=located(expression){
FunctionDefinition(p,e) i,None,FunctionDefinition(p,e)
} }
|(*ts est temp*) DPOINT ts=located(type_scheme) i=located(identifier) p=pattern EQUAL e=located(expression){ | DPOINT ts=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression){
FunctionDefinition(p,e) i,ts,FunctionDefinition(p,e)
} }
(************* PATTERN **************)
pattern: pattern:
| i=located(identifier){ | i=located(identifier){
PVariable i PVariable i
@ -57,22 +65,20 @@ pattern:
} }
(************** DATA TYPE **************)
ty: ty:
| tc = type_constructor{ | 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){ | ty1=located(ty) ARROW ty2=located(ty){
TyArrow(ty1,ty2) TyArrow(ty1,ty2)
} }
| ty1=located(ty) STAR ty2=located(ty){ | ty1=located(ty) STAR liste_ty = separated_nonempty_list(STAR,located(ty)){
TyTuple([ty1;ty2]) TyTuple(ty1::liste_ty)
}
| 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 { | LPAREN type_var=type_variable RPAREN {
TyVar type_var TyVar type_var
@ -88,6 +94,20 @@ ty:
} }
(************ EXPRESSION **************)
expression:
| l=located(literal) {
Literal l
}
(******* BASIC TYPES *********)
type_variable: type_variable:
| tid=TID { | tid=TID {
TId tid TId tid
@ -98,18 +118,21 @@ type_constructor:
TCon tcon TCon tcon
} }
constructor:
| kid = CID {
KId kid
}
label:
| label = TID {
TId = label
}
expression:
| l=located(literal) {
Literal l
}
literal: literal:
| i=INT { | i=INT { LInt i}
LInt i | c=CHAR { LChar c}
} | s = STRING { LString s }
identifier: identifier:
| i=ID { | i=ID {