some definitions added

This commit is contained in:
Mylloon 2023-10-17 16:49:43 +02:00
parent fadc63e30a
commit 3dc4a1068c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 56 additions and 25 deletions

View file

@ -76,16 +76,20 @@ rule token = parse
| ')' { RPAREN } | ')' { RPAREN }
| '[' { LBRACK } | '[' { LBRACK }
| ']' { RBRACK } | ']' { RBRACK }
| '{' { LBRACE }
| '}' { RBRACE }
| '_' { WILDCARD } | '_' { WILDCARD }
| ':' { DPOINT } | ':' { DPOINT }
| "->" { ARROW } | "->" { ARROW }
| "<" { INFERIOR } | '<' { INFERIOR }
| ">" { SUPERIOR } | '>' { SUPERIOR }
| '|' { PIPE }
(** Values *) (** Values *)
| int as i { INT (Mint.of_string i) } | int as i { INT (Mint.of_string i) }
| ident as s { ID s } | ident as s { ID s }
| type_variable as s { TID s } | type_variable as s { TID s }
| constr_id as s { CID s }
(** Lexing error *) (** Lexing error *)
| _ { error lexbuf "unexpected character." } | _ { error lexbuf "unexpected character." }

View file

@ -1,58 +1,80 @@
%{ (* -*- tuareg -*- *) %{ (* -*- tuareg -*- *)
open HopixAST open HopixAST
open Position (* open Position *)
%} %}
%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 /* Opérateurs à ajouter au fur et a mesure (pour réduire les warnings temporairement) :
* BINOP DO ELSE FOR FROM IF INFERIOR MATCH PIPE REF SUPERIOR THEN TO UNTIL WHILE */
%token EOF LET TYPE WILDCARD STAR ARROW DPOINT EXTERN FUN COMMA AND EQUAL LPAREN
%token RPAREN LBRACK RBRACK LBRACE RBRACE
%token<Mint.t> INT %token<Mint.t> INT
%token<string> ID TID %token<string> ID TID CID
%start<HopixAST.t> program %start<HopixAST.t> program
%% %%
program: program:
/* Programme */
| definition=located(definition)* EOF { | definition=located(definition)* EOF {
definition definition
} }
definition: definition:
/* Définition de types */
// Manque le 'type_variable located list' ici, on met une liste vide en attendant
| TYPE tc=located(type_constructor) EQUAL td=tdefinition {
DefineType (tc, [], td)
}
/* Valeurs externes */
| EXTERN id=located(identifier) ts=located(type_scheme) {
DeclareExtern(id, ts)
}
/* Définition de valeurs */
| v=vdefinition { | v=vdefinition {
DefineValue v DefineValue v
} }
tdefinition:
/* Type sommes */
/* | option(PIPE) type_constructor option() separated_nonempty_list(COMMA, ty) {
DefineSumType()
} */
/* Type produit étiqueté */
| LBRACE lt=separated_nonempty_list(COMMA, label_with_type) RBRACE {
DefineRecordType(lt)
}
label_with_type:
| l=located(label) DPOINT t=located(ty) { l, t }
vdefinition: vdefinition:
// manque le type ici, on met None en attendant /* Valeur simple */
| LET i=located(identifier) EQUAL e=located(expression) { // Manque le type ici, on met None en attendant
SimpleValue(i, None, e) | LET i=located(identifier) DPOINT ts=option(located(type_scheme)) EQUAL e=located(expression) {
}
/* // marche pas
| LET i=located(identifier) DPOINT ts=located(type_scheme) EQUAL e=located(expression) {
SimpleValue(i, ts, e) SimpleValue(i, ts, e)
} */ }
/* fun : int f a = 1 */ /* Fonction(s)
| FUN f=fundef { * Exemple :
RecFunctions([f]) * - fun : int f a = 1
* - fun f a = 1 and : int g a = 2 */
| FUN fs=separated_nonempty_list(AND, fundef) {
RecFunctions(fs)
} }
/* de même */
fundef: fundef:
/* ts est temp */ | DPOINT t=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression) {
| i=located(identifier) p=pattern EQUAL e=located(expression) { i, t, FunctionDefinition(p, e)
FunctionDefinition(p, e)
} }
/* ts est temp */ | i=located(identifier) p=located(pattern) EQUAL e=located(expression) {
| DPOINT ts=located(type_scheme) i=located(identifier) p=located(pattern) EQUAL e=located(expression) { i, None, FunctionDefinition(p, e)
FunctionDefinition(p, e)
} }
pattern: pattern:
| i=located(identifier) { | i=located(identifier) {
PVariable i PVariable i
@ -100,7 +122,7 @@ type_variable:
type_constructor: type_constructor:
| tcon=TID { | tcon=CID {
TCon tcon TCon tcon
} }
@ -116,6 +138,11 @@ literal:
LInt i LInt i
} }
label:
| i=ID {
LId i
}
identifier: identifier:
| i=ID { | i=ID {