forked from AU-COBRA/ConCert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackInterpreterLIGOExtract.v
98 lines (81 loc) · 3.4 KB
/
StackInterpreterLIGOExtract.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(** * Extraction of an interpreter for a stack based DSL *)
From MetaCoq.Template Require Import All.
From ConCert.Embedding.Extraction Require Import PreludeExt.
From ConCert.Execution Require Import Blockchain.
From ConCert.Execution Require Import ResultMonad.
From ConCert.Extraction Require Import Common.
From ConCert.Extraction Require CameLIGOPretty.
From ConCert.Extraction Require CameLIGOExtract.
From ConCert.Examples.StackInterpreter Require Import StackInterpreterExtract.
From Coq Require Import String.
From Coq Require Import ZArith_base.
Local Open Scope string_scope.
Import MCMonadNotation.
Import Interpreter.
Module CameLIGOInterp.
Import CameLIGOExtract CameLIGOPretty.
#[local]
Existing Instance PrintConfShortNames.PrintWithShortNames.
Definition init (setup : unit)
: result storage Error :=
(* prevents optimizations from removing unused [setup]. TODO: override masks instead *)
let setup0 := setup in
Ok [].
Definition receive_ (c : Chain)
(ctx : ContractCallContext)
(s : storage)
(msg : option params)
: result (list action * storage) Error :=
(* prevent optimizations from deleting these arguments from receive_'s type signature *)
let c_ := c in
let ctx_ := ctx in
match msg with
| Some msg => receive msg s
| None => Err default_error
end.
Definition TT_remap_ligo : list (kername * string) :=
[ (* remapping types *)
remap <%% Z %%> "int"
; remap <%% bool %%> "bool"
; remap <%% unit %%> "unit"
; remap <%% option %%> "option"
; remap <%% Amount %%> "tez"
; remap <%% address_coq %%> "address"
; remap <%% time_coq %%> "timestamp"
; remap <%% list %%> "list"
; remap <%% string %%> "string"
; remap <%% ext_map %%> (print_finmap_type "string * int" "value")
; remap <%% action %%> "operation"
(* remapping operations *)
; remap <%% Z.add %%> "addInt"
; remap <%% Z.mul %%> "multInt"
; remap <%% Z.sub %%> "subInt"
; remap <%% Z.eqb %%> "eqInt"
; remap <%% Z.leb %%> "leInt"
; remap <%% Z.ltb %%> "ltInt"
; remap <%% @lookup %%> "Map.find_opt"
; remap <%% @fst %%> "fst"
; remap <%% @snd %%> "snd"
; remap <%% andb %%> "andb"
; remap <%% one %%> "1"
].
Definition LIGO_INTERP_MODULE : CameLIGOMod params ContractCallContext unit storage action Error :=
{| (* a name for the definition with the extracted code *)
lmd_module_name := "cameligo_interp" ;
(* definitions of operations on ints, bools, pairs, ect. *)
lmd_prelude := CameLIGOPrelude;
lmd_init := init ;
lmd_init_prelude := "";
lmd_receive_prelude := "";
lmd_receive := receive_ ;
(* code for the entry point *)
lmd_entry_point :=
CameLIGOPretty.printMain "receive_" "params" "value list"
|}.
Time MetaCoq Run
(CameLIGO_prepare_extraction [] TT_remap_ligo TT_rename_ctors_default [] "cctx_instance" LIGO_INTERP_MODULE).
Time Definition cameligo_interp := Eval vm_compute in cameligo_interp_prepared.
(** We redirect the extraction result for later processing and compiling with the CameLIGO compiler *)
Redirect "../extraction/tests/extracted-code/cameligo-extract/StackInterpreter.mligo"
MetaCoq Run (tmMsg (String.of_string cameligo_interp)).
End CameLIGOInterp.