2019-05-12 21:31:22 +04:00
|
|
|
[@@@warning "-30-40-42"]
|
|
|
|
|
|
|
|
(* Abstract Syntax Tree (AST) for Mini-ML *)
|
|
|
|
|
|
|
|
type 'a reg = 'a Region.reg
|
|
|
|
|
|
|
|
(* Keywords of OCaml *)
|
|
|
|
|
|
|
|
type keyword = Region.t
|
|
|
|
type kwd_and = Region.t
|
|
|
|
type kwd_begin = Region.t
|
|
|
|
type kwd_else = Region.t
|
|
|
|
type kwd_end = Region.t
|
|
|
|
type kwd_false = Region.t
|
|
|
|
type kwd_fun = Region.t
|
|
|
|
type kwd_if = Region.t
|
|
|
|
type kwd_in = Region.t
|
|
|
|
type kwd_let = Region.t
|
|
|
|
type kwd_match = Region.t
|
|
|
|
type kwd_mod = Region.t
|
|
|
|
type kwd_not = Region.t
|
|
|
|
type kwd_of = Region.t
|
|
|
|
type kwd_or = Region.t
|
|
|
|
type kwd_then = Region.t
|
|
|
|
type kwd_true = Region.t
|
|
|
|
type kwd_type = Region.t
|
|
|
|
type kwd_with = Region.t
|
|
|
|
type kwd_let_entry = Region.t
|
|
|
|
|
|
|
|
(* Symbols *)
|
|
|
|
|
|
|
|
type arrow = Region.t (* "->" *)
|
|
|
|
type cons = Region.t (* "::" *)
|
|
|
|
type cat = Region.t (* "^" *)
|
|
|
|
type append = Region.t (* "@" *)
|
|
|
|
type dot = Region.t (* "." *)
|
|
|
|
|
|
|
|
(* Arithmetic operators *)
|
|
|
|
|
|
|
|
type minus = Region.t (* "-" *)
|
|
|
|
type plus = Region.t (* "+" *)
|
|
|
|
type slash = Region.t (* "/" *)
|
|
|
|
type times = Region.t (* "*" *)
|
|
|
|
|
|
|
|
(* Boolean operators *)
|
|
|
|
|
|
|
|
type bool_or = Region.t (* "||" *)
|
|
|
|
type bool_and = Region.t (* "&&" *)
|
|
|
|
|
|
|
|
(* Comparisons *)
|
|
|
|
|
|
|
|
type equal = Region.t (* "=" *)
|
|
|
|
type neq = Region.t (* "<>" *)
|
|
|
|
type lt = Region.t (* "<" *)
|
|
|
|
type gt = Region.t (* ">" *)
|
|
|
|
type leq = Region.t (* "=<" *)
|
|
|
|
type geq = Region.t (* ">=" *)
|
|
|
|
|
|
|
|
(* Compounds *)
|
|
|
|
|
|
|
|
type lpar = Region.t (* "(" *)
|
|
|
|
type rpar = Region.t (* ")" *)
|
|
|
|
type lbracket = Region.t (* "[" *)
|
|
|
|
type rbracket = Region.t (* "]" *)
|
|
|
|
type lbrace = Region.t (* "{" *)
|
|
|
|
type rbrace = Region.t (* "}" *)
|
|
|
|
|
|
|
|
(* Separators *)
|
|
|
|
|
|
|
|
type comma = Region.t (* "," *)
|
|
|
|
type semi = Region.t (* ";" *)
|
|
|
|
type vbar = Region.t (* "|" *)
|
|
|
|
type colon = Region.t (* ":" *)
|
|
|
|
|
|
|
|
(* Wildcard *)
|
|
|
|
|
|
|
|
type wild = Region.t (* "_" *)
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
(* Virtual tokens *)
|
|
|
|
|
|
|
|
type eof = Region.t
|
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
(* Literals *)
|
|
|
|
|
|
|
|
type variable = string reg
|
|
|
|
type fun_name = string reg
|
|
|
|
type type_name = string reg
|
|
|
|
type field_name = string reg
|
|
|
|
type type_constr = string reg
|
|
|
|
type constr = string reg
|
|
|
|
|
|
|
|
(* Parentheses *)
|
|
|
|
|
|
|
|
type 'a par = {
|
|
|
|
lpar : lpar;
|
|
|
|
inside : 'a;
|
|
|
|
rpar : rpar
|
|
|
|
}
|
|
|
|
|
|
|
|
type the_unit = lpar * rpar
|
|
|
|
|
|
|
|
(* The Abstract Syntax Tree *)
|
|
|
|
|
|
|
|
type t = {
|
|
|
|
decl : declaration Utils.nseq;
|
|
|
|
eof : eof
|
|
|
|
}
|
|
|
|
|
|
|
|
and ast = t
|
|
|
|
|
|
|
|
and declaration =
|
2019-05-15 17:03:15 +04:00
|
|
|
Let of (kwd_let * let_binding) reg
|
2019-05-12 21:31:22 +04:00
|
|
|
| LetEntry of (kwd_let_entry * let_binding) reg
|
|
|
|
| TypeDecl of type_decl reg
|
|
|
|
|
|
|
|
(* Non-recursive values *)
|
|
|
|
|
|
|
|
and let_binding = {
|
2019-06-01 12:37:43 +04:00
|
|
|
bindings : pattern list;
|
2019-05-12 21:31:22 +04:00
|
|
|
lhs_type : (colon * type_expr) option;
|
|
|
|
eq : equal;
|
|
|
|
let_rhs : expr
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
(* Type declarations *)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
and type_decl = {
|
|
|
|
kwd_type : kwd_type;
|
|
|
|
name : type_name;
|
|
|
|
eq : equal;
|
|
|
|
type_expr : type_expr
|
|
|
|
}
|
|
|
|
|
|
|
|
and type_expr =
|
|
|
|
TProd of cartesian
|
|
|
|
| TSum of (variant reg, vbar) Utils.nsepseq reg
|
|
|
|
| TRecord of record_type
|
|
|
|
| TApp of (type_constr * type_tuple) reg
|
|
|
|
| TFun of (type_expr * arrow * type_expr) reg
|
|
|
|
| TPar of type_expr par reg
|
|
|
|
| TAlias of variable
|
|
|
|
|
|
|
|
and cartesian = (type_expr, times) Utils.nsepseq reg
|
|
|
|
|
|
|
|
and variant = {
|
|
|
|
constr : constr;
|
|
|
|
args : (kwd_of * cartesian) option
|
|
|
|
}
|
|
|
|
|
|
|
|
and record_type = field_decl reg injection reg
|
|
|
|
|
|
|
|
and field_decl = {
|
|
|
|
field_name : field_name;
|
|
|
|
colon : colon;
|
|
|
|
field_type : type_expr
|
|
|
|
}
|
|
|
|
|
2019-05-14 17:56:08 +04:00
|
|
|
and type_tuple = (type_expr, comma) Utils.nsepseq par reg
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
and pattern =
|
|
|
|
PTuple of (pattern, comma) Utils.nsepseq reg
|
2019-05-13 14:28:10 +04:00
|
|
|
| PList of list_pattern
|
2019-05-12 21:31:22 +04:00
|
|
|
| PVar of variable
|
|
|
|
| PUnit of the_unit reg
|
|
|
|
| PInt of (string * Z.t) reg
|
|
|
|
| PTrue of kwd_true
|
|
|
|
| PFalse of kwd_false
|
|
|
|
| PString of string reg
|
|
|
|
| PWild of wild
|
|
|
|
| PPar of pattern par reg
|
|
|
|
| PConstr of (constr * pattern option) reg
|
|
|
|
| PRecord of record_pattern
|
|
|
|
| PTyped of typed_pattern reg
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
and list_pattern =
|
|
|
|
Sugar of pattern injection reg
|
|
|
|
| PCons of (pattern * cons * pattern) reg
|
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
and typed_pattern = {
|
|
|
|
pattern : pattern;
|
|
|
|
colon : colon;
|
|
|
|
type_expr : type_expr
|
|
|
|
}
|
|
|
|
|
|
|
|
and record_pattern = field_pattern reg injection reg
|
|
|
|
|
|
|
|
and field_pattern = {
|
|
|
|
field_name : field_name;
|
|
|
|
eq : equal;
|
|
|
|
pattern : pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
and expr =
|
2019-05-13 19:35:31 +04:00
|
|
|
ECase of expr case reg
|
2019-05-14 17:56:08 +04:00
|
|
|
| EAnnot of annot_expr reg
|
2019-05-13 19:35:31 +04:00
|
|
|
| ELogic of logic_expr
|
|
|
|
| EArith of arith_expr
|
|
|
|
| EString of string_expr
|
|
|
|
| EList of list_expr
|
2019-05-14 17:56:08 +04:00
|
|
|
| EConstr of constr_expr reg
|
2019-05-13 19:35:31 +04:00
|
|
|
| ERecord of record_expr
|
|
|
|
| EProj of projection reg
|
|
|
|
| EVar of variable
|
2019-05-14 18:04:03 +04:00
|
|
|
| ECall of (expr * expr Utils.nseq) reg
|
2019-05-14 17:56:08 +04:00
|
|
|
| EBytes of (string * Hex.t) reg
|
2019-05-13 19:35:31 +04:00
|
|
|
| EUnit of the_unit reg
|
|
|
|
| ETuple of (expr, comma) Utils.nsepseq reg
|
|
|
|
| EPar of expr par reg
|
|
|
|
| ELetIn of let_in reg
|
2019-05-20 23:42:11 +04:00
|
|
|
| EFun of fun_expr reg
|
2019-05-13 19:35:31 +04:00
|
|
|
| ECond of conditional reg
|
|
|
|
| ESeq of sequence
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-14 17:56:08 +04:00
|
|
|
and constr_expr = constr * expr option
|
|
|
|
|
|
|
|
and annot_expr = expr * type_expr
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
and 'a injection = {
|
|
|
|
opening : opening;
|
|
|
|
elements : ('a, semi) Utils.sepseq;
|
|
|
|
terminator : semi option;
|
|
|
|
closing : closing
|
|
|
|
}
|
|
|
|
|
|
|
|
and opening =
|
|
|
|
Begin of kwd_begin
|
2019-05-13 19:35:31 +04:00
|
|
|
| With of kwd_with
|
2019-05-13 14:28:10 +04:00
|
|
|
| LBrace of lbrace
|
|
|
|
| LBracket of lbracket
|
|
|
|
|
|
|
|
and closing =
|
|
|
|
End of kwd_end
|
|
|
|
| RBrace of rbrace
|
|
|
|
| RBracket of rbracket
|
|
|
|
|
|
|
|
and list_expr =
|
|
|
|
Cons of cons bin_op reg
|
|
|
|
| List of expr injection reg
|
2019-05-15 17:03:15 +04:00
|
|
|
(*| Append of (expr * append * expr) reg*)
|
2019-05-13 14:28:10 +04:00
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
and string_expr =
|
|
|
|
Cat of cat bin_op reg
|
|
|
|
| String of string reg
|
|
|
|
|
|
|
|
and arith_expr =
|
|
|
|
Add of plus bin_op reg
|
|
|
|
| Sub of minus bin_op reg
|
|
|
|
| Mult of times bin_op reg
|
|
|
|
| Div of slash bin_op reg
|
|
|
|
| Mod of kwd_mod bin_op reg
|
|
|
|
| Neg of minus un_op reg
|
|
|
|
| Int of (string * Z.t) reg
|
|
|
|
| Nat of (string * Z.t) reg
|
|
|
|
| Mtz of (string * Z.t) reg
|
|
|
|
|
|
|
|
and logic_expr =
|
|
|
|
BoolExpr of bool_expr
|
|
|
|
| CompExpr of comp_expr
|
|
|
|
|
|
|
|
and bool_expr =
|
|
|
|
Or of kwd_or bin_op reg
|
|
|
|
| And of kwd_and bin_op reg
|
|
|
|
| Not of kwd_not un_op reg
|
|
|
|
| True of kwd_true
|
|
|
|
| False of kwd_false
|
|
|
|
|
|
|
|
and 'a bin_op = {
|
|
|
|
op : 'a;
|
|
|
|
arg1 : expr;
|
|
|
|
arg2 : expr
|
|
|
|
}
|
|
|
|
|
|
|
|
and 'a un_op = {
|
|
|
|
op : 'a;
|
|
|
|
arg : expr
|
|
|
|
}
|
|
|
|
|
|
|
|
and comp_expr =
|
|
|
|
Lt of lt bin_op reg
|
|
|
|
| Leq of leq bin_op reg
|
|
|
|
| Gt of gt bin_op reg
|
|
|
|
| Geq of geq bin_op reg
|
|
|
|
| Equal of equal bin_op reg
|
|
|
|
| Neq of neq bin_op reg
|
|
|
|
|
2019-05-13 19:35:31 +04:00
|
|
|
and projection = {
|
|
|
|
struct_name : variable;
|
|
|
|
selector : dot;
|
|
|
|
field_path : (selection, dot) Utils.nsepseq
|
2019-05-12 21:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and selection =
|
2019-05-13 19:35:31 +04:00
|
|
|
FieldName of variable
|
2019-05-12 21:31:22 +04:00
|
|
|
| Component of (string * Z.t) reg par reg
|
|
|
|
|
2019-05-15 17:03:15 +04:00
|
|
|
and record_expr = field_assign reg injection reg
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-15 17:03:15 +04:00
|
|
|
and field_assign = {
|
2019-05-12 21:31:22 +04:00
|
|
|
field_name : field_name;
|
|
|
|
assignment : equal;
|
|
|
|
field_expr : expr
|
|
|
|
}
|
|
|
|
|
|
|
|
and sequence = expr injection reg
|
|
|
|
|
2019-05-13 19:35:31 +04:00
|
|
|
and 'a case = {
|
|
|
|
kwd_match : kwd_match;
|
|
|
|
expr : expr;
|
|
|
|
opening : opening;
|
|
|
|
lead_vbar : vbar option;
|
|
|
|
cases : ('a case_clause reg, vbar) Utils.nsepseq reg;
|
|
|
|
closing : closing
|
|
|
|
}
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-13 19:35:31 +04:00
|
|
|
and 'a case_clause = {
|
|
|
|
pattern : pattern;
|
|
|
|
arrow : arrow;
|
|
|
|
rhs : 'a
|
|
|
|
}
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and let_in = {
|
|
|
|
kwd_let : kwd_let;
|
2019-05-24 21:31:39 +04:00
|
|
|
binding : let_binding;
|
2019-05-20 23:42:11 +04:00
|
|
|
kwd_in : kwd_in;
|
|
|
|
body : expr
|
|
|
|
}
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and fun_expr = {
|
|
|
|
kwd_fun : kwd_fun;
|
2019-06-01 15:29:31 +04:00
|
|
|
params : pattern list;
|
2019-05-28 19:00:43 +04:00
|
|
|
p_annot : (colon * type_expr) option;
|
2019-05-20 23:42:11 +04:00
|
|
|
arrow : arrow;
|
|
|
|
body : expr
|
|
|
|
}
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-13 19:35:31 +04:00
|
|
|
and conditional = {
|
2019-05-20 23:42:11 +04:00
|
|
|
kwd_if : kwd_if;
|
|
|
|
test : expr;
|
|
|
|
kwd_then : kwd_then;
|
|
|
|
ifso : expr;
|
|
|
|
kwd_else : kwd_else;
|
|
|
|
ifnot : expr
|
2019-05-13 19:35:31 +04:00
|
|
|
}
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
(* Projecting regions of the input source code *)
|
|
|
|
|
|
|
|
let sprintf = Printf.sprintf
|
|
|
|
|
2019-05-22 21:38:09 +04:00
|
|
|
let region_of_type_expr = function
|
|
|
|
TProd {region; _}
|
|
|
|
| TSum {region; _}
|
|
|
|
| TRecord {region; _}
|
|
|
|
| TApp {region; _}
|
|
|
|
| TFun {region; _}
|
|
|
|
| TPar {region; _}
|
|
|
|
| TAlias {region; _} -> region
|
|
|
|
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
let region_of_list_pattern = function
|
|
|
|
Sugar {region; _} | PCons {region; _} -> region
|
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
let region_of_pattern = function
|
2019-05-13 14:28:10 +04:00
|
|
|
PList p -> region_of_list_pattern p
|
|
|
|
| PTuple {region;_} | PVar {region;_}
|
2019-05-22 21:38:09 +04:00
|
|
|
| PUnit {region;_} | PInt {region;_}
|
|
|
|
| PTrue region | PFalse region
|
2019-05-13 14:28:10 +04:00
|
|
|
| PString {region;_} | PWild region
|
2019-05-22 21:38:09 +04:00
|
|
|
| PConstr {region; _} | PPar {region;_}
|
|
|
|
| PRecord {region; _} | PTyped {region; _} -> region
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
let region_of_bool_expr = function
|
|
|
|
Or {region;_} | And {region;_}
|
|
|
|
| True region | False region
|
|
|
|
| Not {region;_} -> region
|
|
|
|
|
|
|
|
let region_of_comp_expr = function
|
|
|
|
Lt {region;_} | Leq {region;_}
|
|
|
|
| Gt {region;_} | Geq {region;_}
|
|
|
|
| Neq {region;_} | Equal {region;_} -> region
|
|
|
|
|
|
|
|
let region_of_logic_expr = function
|
|
|
|
BoolExpr e -> region_of_bool_expr e
|
|
|
|
| CompExpr e -> region_of_comp_expr e
|
|
|
|
|
|
|
|
let region_of_arith_expr = function
|
|
|
|
Add {region;_} | Sub {region;_} | Mult {region;_}
|
|
|
|
| Div {region;_} | Mod {region;_} | Neg {region;_}
|
|
|
|
| Int {region;_} | Mtz {region; _}
|
|
|
|
| Nat {region; _} -> region
|
|
|
|
|
|
|
|
let region_of_string_expr = function
|
|
|
|
String {region;_} | Cat {region;_} -> region
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
let region_of_list_expr = function
|
2019-05-15 17:03:15 +04:00
|
|
|
Cons {region; _} | List {region; _}
|
|
|
|
(* | Append {region; _}*) -> region
|
2019-05-13 14:28:10 +04:00
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
let region_of_expr = function
|
|
|
|
ELogic e -> region_of_logic_expr e
|
|
|
|
| EArith e -> region_of_arith_expr e
|
|
|
|
| EString e -> region_of_string_expr e
|
2019-05-13 14:28:10 +04:00
|
|
|
| EList e -> region_of_list_expr e
|
2019-05-14 17:56:08 +04:00
|
|
|
| EAnnot {region;_ } | ELetIn {region;_} | EFun {region;_}
|
2019-05-13 19:35:31 +04:00
|
|
|
| ECond {region;_} | ETuple {region;_} | ECase {region;_}
|
|
|
|
| ECall {region;_} | EVar {region; _} | EProj {region; _}
|
2019-05-14 17:56:08 +04:00
|
|
|
| EUnit {region;_} | EPar {region;_} | EBytes {region; _}
|
2019-05-13 14:28:10 +04:00
|
|
|
| ESeq {region; _} | ERecord {region; _}
|
|
|
|
| EConstr {region; _} -> region
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
(* Printing the tokens with their source locations *)
|
|
|
|
|
|
|
|
let print_nsepseq sep print (head,tail) =
|
|
|
|
let print_aux ((sep_reg:Region.t), item) =
|
|
|
|
Printf.printf "%s: %s\n" (sep_reg#compact `Byte) sep;
|
|
|
|
print item
|
|
|
|
in print head; List.iter print_aux tail
|
|
|
|
|
|
|
|
let print_sepseq sep print = function
|
|
|
|
None -> ()
|
|
|
|
| Some seq -> print_nsepseq sep print seq
|
|
|
|
|
|
|
|
let print_csv print = print_nsepseq "," print
|
|
|
|
|
|
|
|
let print_token (reg: Region.t) conc =
|
|
|
|
Printf.printf "%s: %s\n" (reg#compact `Byte) conc
|
|
|
|
|
|
|
|
let print_var Region.{region; value} =
|
|
|
|
Printf.printf "%s: Ident %s\n" (region#compact `Byte) value
|
|
|
|
|
|
|
|
let print_uident Region.{region; value} =
|
|
|
|
Printf.printf "%s: Uident %s\n" (region#compact `Byte) value
|
|
|
|
|
|
|
|
let print_str Region.{region; value} =
|
|
|
|
Printf.printf "%s: Str \"%s\"\n" (region#compact `Byte) value
|
|
|
|
|
2019-05-14 17:56:08 +04:00
|
|
|
let print_bytes Region.{region; value=lexeme, abstract} =
|
|
|
|
Printf.printf "%s: Bytes (\"%s\", \"0x%s\")\n"
|
|
|
|
(region#compact `Byte) lexeme (Hex.to_string abstract)
|
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
let rec print_tokens {decl;eof} =
|
|
|
|
Utils.nseq_iter print_statement decl; print_token eof "EOF"
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_statement = function
|
2019-05-15 17:03:15 +04:00
|
|
|
Let {value=kwd_let, let_binding; _} ->
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token kwd_let "let";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_let_binding let_binding
|
2019-05-12 21:31:22 +04:00
|
|
|
| LetEntry {value=kwd_let_entry, let_binding; _} ->
|
|
|
|
print_token kwd_let_entry "let%entry";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_let_binding let_binding
|
2019-05-12 21:31:22 +04:00
|
|
|
| TypeDecl {value={kwd_type; name; eq; type_expr}; _} ->
|
|
|
|
print_token kwd_type "type";
|
|
|
|
print_var name;
|
|
|
|
print_token eq "=";
|
|
|
|
print_type_expr type_expr
|
|
|
|
|
|
|
|
and print_type_expr = function
|
|
|
|
TProd prod -> print_cartesian prod
|
|
|
|
| TSum {value; _} -> print_nsepseq "|" print_variant value
|
|
|
|
| TRecord t -> print_record_type t
|
|
|
|
| TApp app -> print_type_app app
|
|
|
|
| TPar par -> print_type_par par
|
|
|
|
| TAlias var -> print_var var
|
|
|
|
| TFun t -> print_fun_type t
|
|
|
|
|
|
|
|
and print_fun_type {value; _} =
|
|
|
|
let domain, arrow, range = value in
|
|
|
|
print_type_expr domain;
|
|
|
|
print_token arrow "->";
|
|
|
|
print_type_expr range
|
|
|
|
|
|
|
|
and print_type_app {value; _} =
|
|
|
|
let type_constr, type_tuple = value in
|
|
|
|
print_type_tuple type_tuple;
|
|
|
|
print_var type_constr
|
|
|
|
|
2019-05-14 17:56:08 +04:00
|
|
|
and print_type_tuple {value; _} =
|
|
|
|
let {lpar; inside; rpar} = value in
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token lpar "(";
|
|
|
|
print_nsepseq "," print_type_expr inside;
|
|
|
|
print_token rpar ")"
|
|
|
|
|
|
|
|
and print_type_par {value={lpar;inside=t;rpar}; _} =
|
|
|
|
print_token lpar "(";
|
|
|
|
print_type_expr t;
|
|
|
|
print_token rpar ")"
|
|
|
|
|
2019-05-22 21:38:09 +04:00
|
|
|
and print_projection node =
|
|
|
|
let {struct_name; selector; field_path} = node in
|
|
|
|
print_var struct_name;
|
2019-05-13 19:35:31 +04:00
|
|
|
print_token selector ".";
|
|
|
|
print_nsepseq "." print_selection field_path
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
and print_selection = function
|
2019-05-13 19:35:31 +04:00
|
|
|
FieldName id -> print_var id
|
2019-05-12 21:31:22 +04:00
|
|
|
| Component {value; _} ->
|
|
|
|
let {lpar; inside; rpar} = value in
|
|
|
|
let Region.{value=lexeme,z; region} = inside in
|
|
|
|
print_token lpar "(";
|
|
|
|
print_token region
|
|
|
|
(sprintf "Int %s (%s)" lexeme (Z.to_string z));
|
|
|
|
print_token rpar ")"
|
|
|
|
|
|
|
|
and print_cartesian Region.{value;_} =
|
|
|
|
print_nsepseq "*" print_type_expr value
|
|
|
|
|
|
|
|
and print_variant {value = {constr; args}; _} =
|
|
|
|
print_uident constr;
|
|
|
|
match args with
|
|
|
|
None -> ()
|
|
|
|
| Some (kwd_of, cartesian) ->
|
|
|
|
print_token kwd_of "of";
|
|
|
|
print_cartesian cartesian
|
|
|
|
|
|
|
|
and print_record_type record_type =
|
|
|
|
print_injection print_field_decl record_type
|
|
|
|
|
|
|
|
and print_field_decl {value; _} =
|
|
|
|
let {field_name; colon; field_type} = value
|
|
|
|
in print_var field_name;
|
|
|
|
print_token colon ":";
|
|
|
|
print_type_expr field_type
|
|
|
|
|
|
|
|
and print_injection :
|
|
|
|
'a.('a -> unit) -> 'a injection reg -> unit =
|
|
|
|
fun print {value; _} ->
|
|
|
|
let {opening; elements; terminator; closing} = value in
|
|
|
|
print_opening opening;
|
|
|
|
print_sepseq ";" print elements;
|
|
|
|
print_terminator terminator;
|
|
|
|
print_closing closing
|
|
|
|
|
|
|
|
and print_opening = function
|
2019-05-13 14:28:10 +04:00
|
|
|
Begin region -> print_token region "begin"
|
2019-05-13 19:35:31 +04:00
|
|
|
| With region -> print_token region "with"
|
2019-05-13 14:28:10 +04:00
|
|
|
| LBrace region -> print_token region "{"
|
|
|
|
| LBracket region -> print_token region "["
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
and print_closing = function
|
2019-05-13 14:28:10 +04:00
|
|
|
End region -> print_token region "end"
|
|
|
|
| RBrace region -> print_token region "}"
|
|
|
|
| RBracket region -> print_token region "]"
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
and print_terminator = function
|
|
|
|
Some semi -> print_token semi ";"
|
|
|
|
| None -> ()
|
|
|
|
|
2019-06-01 12:37:43 +04:00
|
|
|
and print_let_binding {bindings; lhs_type; eq; let_rhs} =
|
|
|
|
List.iter print_pattern bindings;
|
2019-05-22 21:38:09 +04:00
|
|
|
(match lhs_type with
|
|
|
|
None -> ()
|
|
|
|
| Some (colon, type_expr) ->
|
|
|
|
print_token colon ":";
|
|
|
|
print_type_expr type_expr);
|
|
|
|
(print_token eq "="; print_expr let_rhs)
|
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
and print_pattern = function
|
|
|
|
PTuple {value=patterns;_} -> print_csv print_pattern patterns
|
2019-05-13 14:28:10 +04:00
|
|
|
| PList p -> print_list_pattern p
|
2019-05-12 21:31:22 +04:00
|
|
|
| PVar {region; value} ->
|
|
|
|
Printf.printf "%s: PVar %s\n" (region#compact `Byte) value
|
|
|
|
| PUnit {value=lpar,rpar; _} ->
|
|
|
|
print_token lpar "("; print_token rpar ")"
|
|
|
|
| PInt {region; value=lex,z} ->
|
|
|
|
print_token region (sprintf "PInt %s (%s)" lex (Z.to_string z))
|
|
|
|
| PTrue kwd_true -> print_token kwd_true "true"
|
|
|
|
| PFalse kwd_false -> print_token kwd_false "false"
|
|
|
|
| PString s -> print_str s
|
|
|
|
| PWild wild -> print_token wild "_"
|
|
|
|
| PPar {value={lpar;inside=p;rpar}; _} ->
|
|
|
|
print_token lpar "("; print_pattern p; print_token rpar ")"
|
|
|
|
| PConstr p -> print_constr_pattern p
|
|
|
|
| PRecord r -> print_record_pattern r
|
|
|
|
| PTyped t -> print_typed_pattern t
|
|
|
|
|
2019-05-13 14:28:10 +04:00
|
|
|
and print_list_pattern = function
|
|
|
|
Sugar p -> print_injection print_pattern p
|
|
|
|
| PCons p -> print_raw p
|
|
|
|
|
|
|
|
and print_raw {value=p1,c,p2; _} =
|
|
|
|
print_pattern p1; print_token c "::"; print_pattern p2
|
|
|
|
|
2019-05-12 21:31:22 +04:00
|
|
|
and print_typed_pattern {value; _} =
|
|
|
|
let {pattern; colon; type_expr} = value in
|
|
|
|
print_pattern pattern;
|
|
|
|
print_token colon ":";
|
|
|
|
print_type_expr type_expr
|
|
|
|
|
|
|
|
and print_record_pattern record_pattern =
|
|
|
|
print_injection print_field_pattern record_pattern
|
|
|
|
|
|
|
|
and print_field_pattern {value; _} =
|
|
|
|
let {field_name; eq; pattern} = value in
|
|
|
|
print_var field_name;
|
|
|
|
print_token eq "=";
|
|
|
|
print_pattern pattern
|
|
|
|
|
|
|
|
and print_constr_pattern {value=constr, p_opt; _} =
|
|
|
|
print_uident constr;
|
|
|
|
match p_opt with
|
|
|
|
None -> ()
|
|
|
|
| Some pattern -> print_pattern pattern
|
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_expr = function
|
|
|
|
ELetIn {value;_} -> print_let_in value
|
|
|
|
| ECond cond -> print_conditional cond
|
|
|
|
| ETuple {value;_} -> print_csv print_expr value
|
|
|
|
| ECase {value;_} -> print_match_expr value
|
|
|
|
| EFun e -> print_fun_expr e
|
|
|
|
|
|
|
|
| EAnnot e -> print_annot_expr e
|
|
|
|
| ELogic e -> print_logic_expr e
|
|
|
|
| EArith e -> print_arith_expr e
|
|
|
|
| EString e -> print_string_expr e
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-14 17:56:08 +04:00
|
|
|
| ECall {value=f,l; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr f; Utils.nseq_iter print_expr l
|
2019-05-13 19:35:31 +04:00
|
|
|
| EVar v -> print_var v
|
2019-05-22 21:38:09 +04:00
|
|
|
| EProj p -> print_projection p.value
|
2019-05-13 14:28:10 +04:00
|
|
|
| EUnit {value=lpar,rpar; _} ->
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token lpar "("; print_token rpar ")"
|
2019-05-14 17:56:08 +04:00
|
|
|
| EBytes b -> print_bytes b
|
2019-05-13 14:28:10 +04:00
|
|
|
| EPar {value={lpar;inside=e;rpar}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_token lpar "("; print_expr e; print_token rpar ")"
|
|
|
|
| EList e -> print_list_expr e
|
|
|
|
| ESeq seq -> print_sequence seq
|
|
|
|
| ERecord e -> print_record_expr e
|
2019-05-14 17:56:08 +04:00
|
|
|
| EConstr {value=constr,None; _} -> print_uident constr
|
|
|
|
| EConstr {value=(constr, Some arg); _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_uident constr; print_expr arg
|
2019-05-14 17:56:08 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_annot_expr {value=e,t; _} =
|
|
|
|
print_expr e;
|
2019-05-14 17:56:08 +04:00
|
|
|
print_token Region.ghost ":";
|
|
|
|
print_type_expr t
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_list_expr = function
|
2019-05-13 14:28:10 +04:00
|
|
|
Cons {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1;
|
2019-05-13 14:28:10 +04:00
|
|
|
print_token op "::";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg2
|
|
|
|
| List e -> print_injection print_expr e
|
2019-05-15 17:03:15 +04:00
|
|
|
(*| Append {value=e1,append,e2; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr e1;
|
2019-05-13 14:28:10 +04:00
|
|
|
print_token append "@";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr e2 *)
|
2019-05-13 14:28:10 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_arith_expr = function
|
2019-05-12 21:31:22 +04:00
|
|
|
Add {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "+"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Sub {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "-"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Mult {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "*"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Div {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "/"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Mod {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "mod"; print_expr arg2
|
|
|
|
| Neg {value={op;arg}; _} -> print_token op "-"; print_expr arg
|
2019-05-12 21:31:22 +04:00
|
|
|
| Int {region; value=lex,z} ->
|
|
|
|
print_token region (sprintf "Int %s (%s)" lex (Z.to_string z))
|
|
|
|
| Mtz {region; value=lex,z} ->
|
|
|
|
print_token region (sprintf "Mtz %s (%s)" lex (Z.to_string z))
|
|
|
|
| Nat {region; value=lex,z} ->
|
|
|
|
print_token region (sprintf "Nat %s (%s)" lex (Z.to_string z))
|
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_string_expr = function
|
2019-05-12 21:31:22 +04:00
|
|
|
Cat {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "^"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| String s -> print_str s
|
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_logic_expr = function
|
|
|
|
BoolExpr e -> print_bool_expr e
|
|
|
|
| CompExpr e -> print_comp_expr e
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_bool_expr = function
|
2019-05-12 21:31:22 +04:00
|
|
|
Or {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "||"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| And {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "&&"; print_expr arg2
|
|
|
|
| Not {value={op;arg}; _} -> print_token op "not"; print_expr arg
|
2019-05-12 21:31:22 +04:00
|
|
|
| True kwd_true -> print_token kwd_true "true"
|
|
|
|
| False kwd_false -> print_token kwd_false "false"
|
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_comp_expr = function
|
2019-05-12 21:31:22 +04:00
|
|
|
Lt {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "<"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Leq {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "<="; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Gt {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op ">"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Geq {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op ">="; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Neq {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "<>"; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
| Equal {value={arg1;op;arg2}; _} ->
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr arg1; print_token op "="; print_expr arg2
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_record_expr e =
|
|
|
|
print_injection print_field_assign e
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_field_assign {value; _} =
|
2019-05-12 21:31:22 +04:00
|
|
|
let {field_name; assignment; field_expr} = value in
|
|
|
|
print_var field_name;
|
|
|
|
print_token assignment "=";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr field_expr
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_sequence seq = print_injection print_expr seq
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_match_expr expr =
|
2019-05-13 19:35:31 +04:00
|
|
|
let {kwd_match; expr; opening;
|
|
|
|
lead_vbar; cases; closing} = expr in
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token kwd_match "match";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr expr;
|
2019-05-13 19:35:31 +04:00
|
|
|
print_opening opening;
|
|
|
|
print_token_opt lead_vbar "|";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_cases cases;
|
2019-05-13 19:35:31 +04:00
|
|
|
print_closing closing
|
|
|
|
|
|
|
|
and print_token_opt = function
|
|
|
|
None -> fun _ -> ()
|
|
|
|
| Some region -> print_token region
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_cases {value; _} =
|
|
|
|
print_nsepseq "|" print_case_clause value
|
2019-05-13 19:35:31 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_case_clause {value; _} =
|
2019-05-13 19:35:31 +04:00
|
|
|
let {pattern; arrow; rhs} = value in
|
2019-05-12 21:31:22 +04:00
|
|
|
print_pattern pattern;
|
|
|
|
print_token arrow "->";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr rhs
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-22 21:38:09 +04:00
|
|
|
and print_let_in (bind: let_in) =
|
|
|
|
let {kwd_let; binding; kwd_in; body} = bind in
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token kwd_let "let";
|
2019-05-24 21:31:39 +04:00
|
|
|
print_let_binding binding;
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token kwd_in "in";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr body
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_fun_expr {value; _} =
|
2019-06-01 15:29:31 +04:00
|
|
|
let {kwd_fun; params; p_annot; arrow; body} = value in
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token kwd_fun "fun";
|
2019-05-28 19:00:43 +04:00
|
|
|
(match p_annot with
|
2019-06-01 15:29:31 +04:00
|
|
|
None -> List.iter print_pattern params
|
2019-05-28 19:00:43 +04:00
|
|
|
| Some (colon, type_expr) ->
|
|
|
|
print_token colon ":";
|
|
|
|
print_type_expr type_expr);
|
2019-05-12 21:31:22 +04:00
|
|
|
print_token arrow "->";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr body
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-20 23:42:11 +04:00
|
|
|
and print_conditional {value; _} =
|
2019-05-13 19:35:31 +04:00
|
|
|
let open Region in
|
|
|
|
let {kwd_if; test; kwd_then; ifso; kwd_else; ifnot} = value
|
|
|
|
in print_token ghost "(";
|
|
|
|
print_token kwd_if "if";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr test;
|
2019-05-13 19:35:31 +04:00
|
|
|
print_token kwd_then "then";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr ifso;
|
2019-05-13 19:35:31 +04:00
|
|
|
print_token kwd_else "else";
|
2019-05-20 23:42:11 +04:00
|
|
|
print_expr ifnot;
|
2019-05-13 19:35:31 +04:00
|
|
|
print_token ghost ")"
|
2019-05-15 17:03:15 +04:00
|
|
|
|
|
|
|
let rec unpar = function
|
|
|
|
EPar {value={inside=expr;_}; _} -> unpar expr
|
|
|
|
| e -> e
|