Declarations can now be written in any order. Removed top-level block.
The parser exports now an entry rule for parsing Ligo expressions.
This commit is contained in:
parent
8746802571
commit
29df2ff9aa
31
AST.ml
31
AST.ml
@ -143,17 +143,19 @@ type 'a braces = (lbrace * 'a * rbrace) reg
|
|||||||
(* The Abstract Syntax Tree *)
|
(* The Abstract Syntax Tree *)
|
||||||
|
|
||||||
type t = {
|
type t = {
|
||||||
types : type_decl reg list;
|
decl : declaration nseq;
|
||||||
constants : const_decl reg list;
|
|
||||||
storage : storage_decl reg;
|
|
||||||
operations : operations_decl reg;
|
|
||||||
lambdas : lambda_decl list;
|
|
||||||
block : block reg;
|
|
||||||
eof : eof
|
eof : eof
|
||||||
}
|
}
|
||||||
|
|
||||||
and ast = t
|
and ast = t
|
||||||
|
|
||||||
|
and declaration =
|
||||||
|
TypeDecl of type_decl reg
|
||||||
|
| ConstDecl of const_decl reg
|
||||||
|
| StorageDecl of storage_decl reg
|
||||||
|
| OpDecl of operations_decl reg
|
||||||
|
| LambdaDecl of lambda_decl
|
||||||
|
|
||||||
and const_decl = {
|
and const_decl = {
|
||||||
kwd_const : kwd_const;
|
kwd_const : kwd_const;
|
||||||
name : variable;
|
name : variable;
|
||||||
@ -566,16 +568,17 @@ let print_int {region; value = lexeme, abstract} =
|
|||||||
(* Main printing function *)
|
(* Main printing function *)
|
||||||
|
|
||||||
let rec print_tokens ast =
|
let rec print_tokens ast =
|
||||||
let {types; constants; storage; operations;
|
let {decl; eof} = ast in
|
||||||
lambdas; block; eof} = ast in
|
Utils.nseq_iter print_decl decl;
|
||||||
List.iter print_type_decl types;
|
|
||||||
List.iter print_const_decl constants;
|
|
||||||
print_storage_decl storage;
|
|
||||||
print_operations_decl operations;
|
|
||||||
List.iter print_lambda_decl lambdas;
|
|
||||||
print_block block;
|
|
||||||
print_token eof "EOF"
|
print_token eof "EOF"
|
||||||
|
|
||||||
|
and print_decl = function
|
||||||
|
TypeDecl decl -> print_type_decl decl
|
||||||
|
| ConstDecl decl -> print_const_decl decl
|
||||||
|
| StorageDecl decl -> print_storage_decl decl
|
||||||
|
| OpDecl decl -> print_operations_decl decl
|
||||||
|
| LambdaDecl decl -> print_lambda_decl decl
|
||||||
|
|
||||||
and print_const_decl {value; _} =
|
and print_const_decl {value; _} =
|
||||||
let {kwd_const; name; colon; const_type;
|
let {kwd_const; name; colon; const_type;
|
||||||
equal; init; terminator} = value in
|
equal; init; terminator} = value in
|
||||||
|
14
AST.mli
14
AST.mli
@ -127,17 +127,19 @@ type 'a braces = (lbrace * 'a * rbrace) reg
|
|||||||
(* The Abstract Syntax Tree *)
|
(* The Abstract Syntax Tree *)
|
||||||
|
|
||||||
type t = {
|
type t = {
|
||||||
types : type_decl reg list;
|
decl : declaration nseq;
|
||||||
constants : const_decl reg list;
|
|
||||||
storage : storage_decl reg;
|
|
||||||
operations : operations_decl reg;
|
|
||||||
lambdas : lambda_decl list;
|
|
||||||
block : block reg;
|
|
||||||
eof : eof
|
eof : eof
|
||||||
}
|
}
|
||||||
|
|
||||||
and ast = t
|
and ast = t
|
||||||
|
|
||||||
|
and declaration =
|
||||||
|
TypeDecl of type_decl reg
|
||||||
|
| ConstDecl of const_decl reg
|
||||||
|
| StorageDecl of storage_decl reg
|
||||||
|
| OpDecl of operations_decl reg
|
||||||
|
| LambdaDecl of lambda_decl
|
||||||
|
|
||||||
and const_decl = {
|
and const_decl = {
|
||||||
kwd_const : kwd_const;
|
kwd_const : kwd_const;
|
||||||
name : variable;
|
name : variable;
|
||||||
|
31
Parser.mly
31
Parser.mly
@ -13,8 +13,9 @@ open AST
|
|||||||
|
|
||||||
(* Entry points *)
|
(* Entry points *)
|
||||||
|
|
||||||
%start program
|
%start program interactive_expr
|
||||||
%type <AST.t> program
|
%type <AST.t> program
|
||||||
|
%type <AST.expr> interactive_expr
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
@ -89,24 +90,17 @@ sepseq(X,Sep):
|
|||||||
(* Main *)
|
(* Main *)
|
||||||
|
|
||||||
program:
|
program:
|
||||||
seq(type_decl)
|
nseq(declaration) EOF {
|
||||||
seq(const_decl)
|
{decl = $1; eof = $2}
|
||||||
storage_decl
|
|
||||||
operations_decl
|
|
||||||
seq(lambda_decl)
|
|
||||||
block
|
|
||||||
EOF {
|
|
||||||
{
|
|
||||||
types = $1;
|
|
||||||
constants = $2;
|
|
||||||
storage = $3;
|
|
||||||
operations = $4;
|
|
||||||
lambdas = $5;
|
|
||||||
block = $6;
|
|
||||||
eof = $7;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declaration:
|
||||||
|
type_decl { TypeDecl $1 }
|
||||||
|
| const_decl { ConstDecl $1 }
|
||||||
|
| storage_decl { StorageDecl $1 }
|
||||||
|
| operations_decl { OpDecl $1 }
|
||||||
|
| lambda_decl { LambdaDecl $1 }
|
||||||
|
|
||||||
storage_decl:
|
storage_decl:
|
||||||
Storage var COLON type_expr option(SEMI) {
|
Storage var COLON type_expr option(SEMI) {
|
||||||
let stop =
|
let stop =
|
||||||
@ -485,6 +479,9 @@ arrow_clause:
|
|||||||
|
|
||||||
(* Expressions *)
|
(* Expressions *)
|
||||||
|
|
||||||
|
interactive_expr:
|
||||||
|
expr EOF { $1 }
|
||||||
|
|
||||||
expr:
|
expr:
|
||||||
expr OR conj_expr {
|
expr OR conj_expr {
|
||||||
let start = expr_to_region $1
|
let start = expr_to_region $1
|
||||||
|
@ -69,7 +69,3 @@ let () =
|
|||||||
let () = close_all () in
|
let () = close_all () in
|
||||||
print_error ~offsets EvalOpt.mode error
|
print_error ~offsets EvalOpt.mode error
|
||||||
| Sys_error msg -> Utils.highlight msg
|
| Sys_error msg -> Utils.highlight msg
|
||||||
|
|
||||||
let _ =
|
|
||||||
let open AST2 in
|
|
||||||
map
|
|
||||||
|
@ -6,6 +6,8 @@ type w is K of (U of int) (*v * u*)
|
|||||||
storage s : w // Line comment
|
storage s : w // Line comment
|
||||||
operations o : u;
|
operations o : u;
|
||||||
|
|
||||||
|
type i is int;
|
||||||
|
|
||||||
(* Block comment *)
|
(* Block comment *)
|
||||||
|
|
||||||
entrypoint g (const l : list (int)) is
|
entrypoint g (const l : list (int)) is
|
||||||
@ -19,10 +21,9 @@ entrypoint g (const l : list (int)) is
|
|||||||
match l with
|
match l with
|
||||||
[] -> null
|
[] -> null
|
||||||
| h#t -> q (h+2)
|
| h#t -> q (h+2)
|
||||||
end
|
end;
|
||||||
end
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
g (Unit);
|
g (Unit);
|
||||||
fail "in extremis"
|
fail "in extremis"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user