ligo/AST.mli

502 lines
14 KiB
OCaml
Raw Normal View History

2019-02-26 01:29:29 +04:00
(* Abstract Syntax Tree (AST) for Ligo *)
[@@@warning "-30"]
2019-02-26 01:29:29 +04:00
open Utils
(* Regions
The AST carries all the regions where tokens have been found by the
lexer, plus additional regions corresponding to whole subtrees
(like entire expressions, patterns etc.). These regions are needed
for error reporting and source-to-source transformations. To make
these pervasive regions more legible, we define singleton types for
the symbols, keywords etc. with suggestive names like "kwd_and"
denoting the _region_ of the occurrence of the keyword "and".
*)
type 'a reg = 'a Region.reg
val nseq_to_region : ('a -> Region.t) -> 'a nseq -> Region.t
val nsepseq_to_region : ('a -> Region.t) -> ('a,'sep) nsepseq -> Region.t
val sepseq_to_region : ('a -> Region.t) -> ('a,'sep) sepseq -> Region.t
(* Keywords of Ligo *)
type kwd_begin = Region.t
type kwd_const = Region.t
type kwd_down = Region.t
type kwd_fail = Region.t
2019-02-26 01:29:29 +04:00
type kwd_if = Region.t
type kwd_in = Region.t
type kwd_is = Region.t
type kwd_for = Region.t
type kwd_function = Region.t
type kwd_parameter = Region.t
type kwd_storage = Region.t
type kwd_type = Region.t
type kwd_of = Region.t
type kwd_operations = Region.t
type kwd_var = Region.t
type kwd_end = Region.t
type kwd_then = Region.t
type kwd_else = Region.t
type kwd_match = Region.t
type kwd_procedure = Region.t
type kwd_null = Region.t
type kwd_record = Region.t
type kwd_step = Region.t
type kwd_to = Region.t
type kwd_mod = Region.t
type kwd_not = Region.t
type kwd_while = Region.t
type kwd_with = Region.t
(* Data constructors *)
type c_False = Region.t
type c_None = Region.t
type c_Some = Region.t
type c_True = Region.t
type c_Unit = Region.t
(* Symbols *)
type semi = Region.t
type comma = Region.t
type lpar = Region.t
type rpar = Region.t
type lbrace = Region.t
type rbrace = Region.t
type lbracket = Region.t
type rbracket = Region.t
type cons = Region.t
type vbar = Region.t
type arrow = Region.t
type ass = Region.t
2019-02-26 01:29:29 +04:00
type equal = Region.t
type colon = Region.t
type bool_or = Region.t
type bool_and = Region.t
type lt = Region.t
type leq = Region.t
type gt = Region.t
type geq = Region.t
type neq = Region.t
type plus = Region.t
type minus = Region.t
type slash = Region.t
type times = Region.t
type dot = Region.t
type wild = Region.t
type cat = Region.t
(* Virtual tokens *)
type eof = Region.t
(* Literals *)
type 'a variable = string reg
type 'a fun_name = string reg
type 'a type_name = string reg
type 'a field_name = string reg
type 'a map_name = string reg
type 'a constr = string reg
2019-02-26 01:29:29 +04:00
(* Comma-separated non-empty lists *)
type 'a csv = ('a, comma) nsepseq
(* Bar-separated non-empty lists *)
type 'a bsv = ('a, vbar) nsepseq
(* Parentheses *)
type 'a par = (lpar * 'a * rpar) reg
(* Brackets compounds *)
type 'a brackets = (lbracket * 'a * rbracket) reg
(* Braced compounds *)
type 'a braces = (lbrace * 'a * rbrace) reg
(* The Abstract Syntax Tree *)
type t = < ty:unit > ast
2019-03-05 21:13:09 +04:00
and 'a ast = {
types : 'a type_decl reg list;
constants : 'a const_decl reg list;
parameter : 'a parameter_decl reg;
storage : 'a storage_decl reg;
operations : 'a operations_decl reg;
lambdas : 'a lambda_decl list;
block : 'a block reg;
2019-03-05 21:13:09 +04:00
eof : eof
}
2019-02-26 01:29:29 +04:00
and 'a parameter_decl = {
2019-03-05 21:13:09 +04:00
kwd_parameter : kwd_parameter;
name : 'a variable;
2019-03-05 21:13:09 +04:00
colon : colon;
param_type : 'a type_expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
and 'a storage_decl = {
2019-03-05 21:13:09 +04:00
kwd_storage : kwd_storage;
store_type : 'a type_expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
and 'a operations_decl = {
2019-03-05 21:13:09 +04:00
kwd_operations : kwd_operations;
op_type : 'a type_expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
(* Type declarations *)
and 'a type_decl = {
2019-03-05 21:13:09 +04:00
kwd_type : kwd_type;
name : 'a type_name;
2019-03-05 21:13:09 +04:00
kwd_is : kwd_is;
type_expr : 'a type_expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
and 'a type_expr =
Prod of 'a cartesian
| Sum of ('a variant, vbar) nsepseq reg
| Record of 'a record_type
| TypeApp of ('a type_name * 'a type_tuple) reg
| ParType of 'a type_expr par
| TAlias of 'a variable
2019-02-26 01:29:29 +04:00
and 'a cartesian = ('a type_expr, times) nsepseq reg
2019-02-26 01:29:29 +04:00
and 'a variant = ('a constr * kwd_of * 'a cartesian) reg
2019-02-26 01:29:29 +04:00
and 'a record_type = (kwd_record * 'a field_decls * kwd_end) reg
2019-02-26 01:29:29 +04:00
and 'a field_decls = ('a field_decl, semi) nsepseq
2019-02-26 01:29:29 +04:00
and 'a field_decl = ('a variable * colon * 'a type_expr) reg
2019-02-26 01:29:29 +04:00
and 'a type_tuple = ('a type_name, comma) nsepseq par
2019-02-26 01:29:29 +04:00
(* Function and procedure declarations *)
and 'a lambda_decl =
FunDecl of 'a fun_decl reg
| ProcDecl of 'a proc_decl reg
2019-03-05 21:13:09 +04:00
and 'a fun_decl = {
2019-03-05 21:13:09 +04:00
kwd_function : kwd_function;
name : 'a variable;
param : 'a parameters;
2019-03-05 21:13:09 +04:00
colon : colon;
ret_type : 'a type_expr;
2019-03-05 21:13:09 +04:00
kwd_is : kwd_is;
local_decls : 'a local_decl list;
block : 'a block reg;
2019-03-05 21:13:09 +04:00
kwd_with : kwd_with;
return : 'a expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
and 'a proc_decl = {
2019-03-05 21:13:09 +04:00
kwd_procedure : kwd_procedure;
name : 'a variable;
param : 'a parameters;
2019-03-05 21:13:09 +04:00
kwd_is : kwd_is;
local_decls : 'a local_decl list;
block : 'a block reg;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
and 'a parameters = ('a param_decl, semi) nsepseq par
2019-02-26 01:29:29 +04:00
and 'a param_decl =
ParamConst of 'a param_const
| ParamVar of 'a param_var
and 'a param_const = (kwd_const * 'a variable * colon * 'a type_expr) reg
and 'a param_var = (kwd_var * 'a variable * colon * 'a type_expr) reg
2019-02-26 01:29:29 +04:00
and 'a block = {
2019-03-05 21:13:09 +04:00
opening : kwd_begin;
instr : 'a instructions;
2019-03-05 21:13:09 +04:00
terminator : semi option;
close : kwd_end
}
2019-02-26 01:29:29 +04:00
and 'a local_decl =
LocalLam of 'a lambda_decl
| LocalConst of 'a const_decl reg
| LocalVar of 'a var_decl reg
2019-03-05 21:13:09 +04:00
and 'a const_decl = {
2019-03-05 21:13:09 +04:00
kwd_const : kwd_const;
name : 'a variable;
2019-03-05 21:13:09 +04:00
colon : colon;
vtype : 'a type_expr;
2019-03-05 21:13:09 +04:00
equal : equal;
init : 'a expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
2019-03-05 14:15:02 +04:00
}
2019-02-26 01:29:29 +04:00
and 'a var_decl = {
2019-03-05 21:13:09 +04:00
kwd_var : kwd_var;
name : 'a variable;
2019-03-05 21:13:09 +04:00
colon : colon;
vtype : 'a type_expr;
2019-03-05 21:13:09 +04:00
ass : ass;
init : 'a expr;
2019-03-05 21:13:09 +04:00
terminator : semi option
}
2019-02-26 01:29:29 +04:00
and 'a instructions = ('a instruction, semi) nsepseq reg
2019-02-26 01:29:29 +04:00
and 'a instruction =
Single of 'a single_instr
| Block of 'a block reg
2019-02-26 01:29:29 +04:00
and 'a single_instr =
Cond of 'a conditional reg
| Match of 'a match_instr reg
| Ass of 'a ass_instr
| Loop of 'a loop
| ProcCall of 'a fun_call
2019-02-26 01:29:29 +04:00
| Null of kwd_null
| Fail of (kwd_fail * 'a expr) reg
2019-03-05 21:13:09 +04:00
and 'a conditional = {
2019-03-05 21:13:09 +04:00
kwd_if : kwd_if;
test : 'a expr;
2019-03-05 21:13:09 +04:00
kwd_then : kwd_then;
ifso : 'a instruction;
2019-03-05 21:13:09 +04:00
kwd_else : kwd_else;
ifnot : 'a instruction
}
2019-02-26 01:29:29 +04:00
and 'a match_instr = {
2019-03-05 21:13:09 +04:00
kwd_match : kwd_match;
expr : 'a expr;
2019-03-05 21:13:09 +04:00
kwd_with : kwd_with;
lead_vbar : vbar option;
cases : 'a cases;
2019-03-05 21:13:09 +04:00
kwd_end : kwd_end
}
2019-02-26 01:29:29 +04:00
and 'a cases = ('a case, vbar) nsepseq reg
2019-02-26 01:29:29 +04:00
and 'a case = ('a pattern * arrow * 'a instruction) reg
2019-02-26 01:29:29 +04:00
and 'a ass_instr = ('a variable * ass * 'a expr) reg
2019-02-26 01:29:29 +04:00
and 'a loop =
While of 'a while_loop
| For of 'a for_loop
2019-02-26 01:29:29 +04:00
and 'a while_loop = (kwd_while * 'a expr * 'a block reg) reg
2019-02-26 01:29:29 +04:00
and 'a for_loop =
ForInt of 'a for_int reg
| ForCollect of 'a for_collect reg
2019-02-26 01:29:29 +04:00
and 'a for_int = {
2019-03-05 21:13:09 +04:00
kwd_for : kwd_for;
ass : 'a ass_instr;
2019-03-05 21:13:09 +04:00
down : kwd_down option;
kwd_to : kwd_to;
bound : 'a expr;
step : (kwd_step * 'a expr) option;
block : 'a block reg
}
2019-02-26 01:29:29 +04:00
and 'a for_collect = {
2019-03-05 21:13:09 +04:00
kwd_for : kwd_for;
var : 'a variable;
bind_to : (arrow * 'a variable) option;
2019-03-05 21:13:09 +04:00
kwd_in : kwd_in;
expr : 'a expr;
block : 'a block reg
}
2019-02-26 01:29:29 +04:00
(* Expressions *)
and 'a expr =
Or of ('a expr * bool_or * 'a expr) reg
| And of ('a expr * bool_and * 'a expr) reg
| Lt of ('a expr * lt * 'a expr) reg
| Leq of ('a expr * leq * 'a expr) reg
| Gt of ('a expr * gt * 'a expr) reg
| Geq of ('a expr * geq * 'a expr) reg
| Equal of ('a expr * equal * 'a expr) reg
| Neq of ('a expr * neq * 'a expr) reg
| Cat of ('a expr * cat * 'a expr) reg
| Cons of ('a expr * cons * 'a expr) reg
| Add of ('a expr * plus * 'a expr) reg
| Sub of ('a expr * minus * 'a expr) reg
| Mult of ('a expr * times * 'a expr) reg
| Div of ('a expr * slash * 'a expr) reg
| Mod of ('a expr * kwd_mod * 'a expr) reg
| Neg of (minus * 'a expr) reg
| Not of (kwd_not * 'a expr) reg
2019-02-26 01:29:29 +04:00
| Int of (Lexer.lexeme * Z.t) reg
| Var of Lexer.lexeme reg
| String of Lexer.lexeme reg
| Bytes of (Lexer.lexeme * MBytes.t) reg
| False of c_False
| True of c_True
| Unit of c_Unit
| Tuple of 'a tuple
| List of ('a expr, comma) nsepseq brackets
| EmptyList of 'a empty_list
| Set of ('a expr, comma) nsepseq braces
| EmptySet of 'a empty_set
| NoneExpr of 'a none_expr
| FunCall of 'a fun_call
| ConstrApp of 'a constr_app
| SomeApp of (c_Some * 'a arguments) reg
| MapLookUp of 'a map_lookup reg
| ParExpr of 'a expr par
2019-02-26 01:29:29 +04:00
and 'a tuple = ('a expr, comma) nsepseq par
2019-02-26 01:29:29 +04:00
and 'a empty_list =
(lbracket * rbracket * colon * 'a type_expr) par
2019-02-26 01:29:29 +04:00
and 'a empty_set =
(lbrace * rbrace * colon * 'a type_expr) par
2019-02-26 01:29:29 +04:00
and 'a none_expr =
(c_None * colon * 'a type_expr) par
2019-02-26 01:29:29 +04:00
and 'a fun_call = ('a fun_name * 'a arguments) reg
2019-02-26 01:29:29 +04:00
and 'a arguments = 'a tuple
2019-02-26 01:29:29 +04:00
and 'a constr_app = ('a constr * 'a arguments) reg
2019-02-26 01:29:29 +04:00
and 'a map_lookup = {
map_name : 'a variable;
2019-03-05 21:13:09 +04:00
selector : dot;
index : 'a expr brackets
}
2019-02-26 01:29:29 +04:00
(* Patterns *)
and 'a pattern = ('a core_pattern, cons) nsepseq reg
2019-02-26 01:29:29 +04:00
and 'a core_pattern =
2019-02-26 01:29:29 +04:00
PVar of Lexer.lexeme reg
| PWild of wild
| PInt of (Lexer.lexeme * Z.t) reg
| PBytes of (Lexer.lexeme * MBytes.t) reg
| PString of Lexer.lexeme reg
| PUnit of c_Unit
| PFalse of c_False
| PTrue of c_True
| PNone of c_None
| PSome of (c_Some * 'a core_pattern par) reg
| PList of 'a list_pattern
| PTuple of ('a core_pattern, comma) nsepseq par
2019-02-26 01:29:29 +04:00
and 'a list_pattern =
Sugar of ('a core_pattern, comma) sepseq brackets
| Raw of ('a core_pattern * cons * 'a pattern) par
2019-02-26 01:29:29 +04:00
(* Projecting regions *)
val type_expr_to_region : 'a type_expr -> Region.t
2019-03-05 21:13:09 +04:00
val expr_to_region : 'a expr -> Region.t
2019-03-05 21:13:09 +04:00
val instr_to_region : 'a instruction -> Region.t
2019-03-05 21:13:09 +04:00
val core_pattern_to_region : 'a core_pattern -> Region.t
2019-03-05 21:13:09 +04:00
val local_decl_to_region : 'a local_decl -> Region.t
2019-03-05 21:13:09 +04:00
type 'a visitor = {
ass_instr : 'a ass_instr -> unit;
bind_to : (Region.t * 'a variable) option -> unit;
block : 'a block reg -> unit;
2019-03-05 21:13:09 +04:00
bytes : (string * MBytes.t) reg -> unit;
cartesian : 'a cartesian -> unit;
case : 'a case -> unit;
cases : 'a cases -> unit;
conditional : 'a conditional -> unit;
const_decl : 'a const_decl reg -> unit;
constr : 'a constr -> unit;
constr_app : 'a constr_app -> unit;
core_pattern : 'a core_pattern -> unit;
2019-03-05 21:13:09 +04:00
down : Region.t option -> unit;
empty_list : 'a empty_list -> unit;
empty_set : 'a empty_set -> unit;
expr : 'a expr -> unit;
fail : (kwd_fail * 'a expr) -> unit;
field_decl : 'a field_decl -> unit;
field_decls : 'a field_decls -> unit;
for_collect : 'a for_collect reg -> unit;
for_int : 'a for_int reg -> unit;
for_loop : 'a for_loop -> unit;
fun_call : 'a fun_call -> unit;
fun_decl : 'a fun_decl reg -> unit;
instruction : 'a instruction -> unit;
instructions : 'a instructions -> unit;
2019-03-05 21:13:09 +04:00
int : (string * Z.t) reg -> unit;
lambda_decl : 'a lambda_decl -> unit;
list : ('a expr, Region.t) nsepseq brackets -> unit;
list_pattern : 'a list_pattern -> unit;
loop : 'a loop -> unit;
map_lookup : 'a map_lookup reg -> unit;
match_instr : 'a match_instr -> unit;
none_expr : 'a none_expr -> unit;
nsepseq : 'a.string -> ('a -> unit) -> ('a, Region.t) nsepseq -> unit;
operations_decl : 'a operations_decl reg -> unit;
par_expr : 'a expr par -> unit;
par_type : 'a type_expr par -> unit;
param_decl : 'a param_decl -> unit;
parameter_decl : 'a parameter_decl reg -> unit;
parameters : 'a parameters -> unit;
param_const : 'a param_const -> unit;
param_var : 'a param_var -> unit;
pattern : 'a pattern -> unit;
patterns : 'a core_pattern par -> unit;
proc_decl : 'a proc_decl reg -> unit;
psome : (Region.t * 'a core_pattern par) reg -> unit;
ptuple : ('a core_pattern, Region.t) nsepseq par -> unit;
raw : ('a core_pattern * Region.t * 'a pattern) par -> unit;
record_type : 'a record_type -> unit;
sepseq : 'a.string -> ('a -> unit) -> ('a, Region.t) sepseq -> unit;
set : ('a expr, Region.t) nsepseq braces -> unit;
single_instr : 'a single_instr -> unit;
some_app : (Region.t * 'a arguments) reg -> unit;
step : (Region.t * 'a expr) option -> unit;
storage_decl : 'a storage_decl reg -> unit;
2019-03-05 21:13:09 +04:00
string : string reg -> unit;
sugar : ('a core_pattern, Region.t) sepseq brackets -> unit;
sum_type : ('a variant, Region.t) nsepseq reg -> unit;
2019-03-05 21:13:09 +04:00
terminator : semi option -> unit;
token : Region.t -> string -> unit;
tuple : 'a arguments -> unit;
type_app : ('a type_name * 'a type_tuple) reg -> unit;
type_decl : 'a type_decl reg -> unit;
type_expr : 'a type_expr -> unit;
type_tuple : 'a type_tuple -> unit;
local_decl : 'a local_decl -> unit;
local_decls : 'a local_decl list -> unit;
var : 'a variable -> unit;
var_decl : 'a var_decl reg -> unit;
variant : 'a variant -> unit;
while_loop : 'a while_loop -> unit
2019-03-05 21:13:09 +04:00
}