ajout type et fonction dans le parser
This commit is contained in:
parent
a58be772ac
commit
be467f3503
2 changed files with 55 additions and 32 deletions
|
@ -81,7 +81,7 @@ rule token = parse
|
|||
| '[' { LBRACK }
|
||||
| ']' { RBRACK }
|
||||
| '_' { WILDCARD }
|
||||
| ':' { DPOINT }
|
||||
| ':' { COLON }
|
||||
| "->" { ARROW }
|
||||
| "<" { INFERIOR }
|
||||
| ">" { SUPERIOR }
|
||||
|
|
|
@ -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<string> ID TID
|
||||
%token<string> ID TID CID STRING
|
||||
%token<char> CHAR
|
||||
|
||||
%start<HopixAST.t> 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
expression:
|
||||
| l=located(literal) {
|
||||
Literal l
|
||||
constructor:
|
||||
| kid = CID {
|
||||
KId kid
|
||||
}
|
||||
|
||||
label:
|
||||
| label = TID {
|
||||
TId = label
|
||||
}
|
||||
|
||||
|
||||
literal:
|
||||
| i=INT {
|
||||
LInt i
|
||||
}
|
||||
| i=INT { LInt i}
|
||||
| c=CHAR { LChar c}
|
||||
| s = STRING { LString s }
|
||||
|
||||
identifier:
|
||||
| i=ID {
|
||||
|
|
Reference in a new issue