From 6c5ae52db6441c4ba7eddf1b9abc782417edd71b Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Tue, 19 Mar 2019 10:52:47 +0100 Subject: [PATCH] I relaxed the grammar to support in the parser for initial values of variables to not require type annotation, like var x : list (operation) := [] --- Parser.mly | 73 ++++++++++++++++++++++++++++++++--------- Tests/crowdfunding.ligo | 8 ++--- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/Parser.mly b/Parser.mly index 7e4b3daf2..dd847ab72 100644 --- a/Parser.mly +++ b/Parser.mly @@ -368,7 +368,7 @@ const_decl: } var_decl: - Var var COLON type_expr ASS expr option(SEMI) { + Var var COLON var_decl_type_expr ASS expr option(SEMI) { let stop = match $7 with Some region -> region @@ -384,30 +384,73 @@ var_decl: terminator = $7} in {region; value} } -(* | Var var COLON type_name type_tuple ASS extended_expr option(SEMI) { - let stop = - match $7 with - Some region -> region - | None -> expr_to_region $6 in - let region = cover $1 stop in + let region = cover $4.region $5.region in + let type_app = TypeApp {region; value=$4,$5} in + let cartesian = {region; value = type_app, []} in + let var_type = Prod cartesian in + let stop = match $8 with + Some region -> region + | None -> $7.region in + let region = cover $1 stop in + let init = + match $7.value with + `Expr e -> e + | `EList (lbracket, rbracket) -> + let region = $7.region + and value = { + lbracket; + rbracket; + colon = Region.ghost; + list_type = var_type} in + let value = { + lpar = Region.ghost; + inside = value; + rpar = Region.ghost} in + ListExpr (EmptyList {region; value}) + | `ENone region -> + let value = { + lpar = Region.ghost; + inside = { + c_None = region; + colon = Region.ghost; + opt_type = var_type}; + rpar = Region.ghost} + in ConstrExpr (NoneExpr {region; value}) in let value = { kwd_var = $1; name = $2; colon = $3; - var_type = $4; - ass = $5; - init = $6; - terminator = $7} + var_type; + assign = $6; + init; + terminator = $8} in {region; value} } extended_expr: - expr { } -| LBRACKET RBRACKET { } -| C_None { } - *) + expr { {region = expr_to_region $1; + value = `Expr $1} } +| LBRACKET RBRACKET { {region = cover $1 $2; + value = `EList ($1,$2)} } +| C_None { {region = $1; value = `ENone $1} } + +var_decl_type_expr: + var_decl_cartesian { Prod $1 } +| sum_type { Sum $1 } +| record_type { Record $1 } + +var_decl_cartesian: + nsepseq(var_decl_core_type,TIMES) { + let region = nsepseq_to_region type_expr_to_region $1 + in {region; value=$1} + } + +var_decl_core_type: + type_name { TAlias $1 } +| par(type_expr) { ParType $1 } + instruction: single_instr { Single $1 } | block { Block $1 } diff --git a/Tests/crowdfunding.ligo b/Tests/crowdfunding.ligo index 24fe9161a..cead275ae 100644 --- a/Tests/crowdfunding.ligo +++ b/Tests/crowdfunding.ligo @@ -10,7 +10,7 @@ entrypoint contribute (storage store : state; const sender : address; const amount : mutez) : state * list (operation) is - var operations : list (operation) := ([] : list (operation)) // TODO + var operations : list (operation) := [] begin if now > store.deadline then fail "Deadline passed" @@ -26,9 +26,9 @@ entrypoint contribute (storage store : state; end end with (store, operations) -entrypoint get_funds (storage store : state; const sender : address) +entrypoint withdraw (storage store : state; const sender : address) : state * list (operation) is - var operations : list (operation) := ([] : list (operation)) // TODO + var operations : list (operation) := [] begin if sender = owner then if now >= store.deadline then @@ -44,7 +44,7 @@ entrypoint get_funds (storage store : state; const sender : address) entrypoint claim (storage store : state; const sender : address) : state * list (operation) is - var operations : list (operation) := ([] : list (operation)) // TODO + var operations : list (operation) := [] var amount : mutez := 0 begin if now <= store.deadline then