OSDN Git Service

add the argument type "Sign" to support checking signature for message (#27)
[bytom/equity.git] / compiler / types.go
1 package compiler
2
3 type typeDesc string
4
5 var (
6         amountType   = typeDesc("Amount")
7         assetType    = typeDesc("Asset")
8         boolType     = typeDesc("Boolean")
9         contractType = typeDesc("Contract")
10         hashType     = typeDesc("Hash")
11         intType      = typeDesc("Integer")
12         listType     = typeDesc("List")
13         nilType      = typeDesc("")
14         predType     = typeDesc("Predicate")
15         progType     = typeDesc("Program")
16         pubkeyType   = typeDesc("PublicKey")
17         sigType      = typeDesc("Signature")
18         signType     = typeDesc("Sign")
19         strType      = typeDesc("String")
20
21         sha3StrType      = typeDesc("Sha3(String)")
22         sha3PubkeyType   = typeDesc("Sha3(PublicKey)")
23         sha256StrType    = typeDesc("Sha256(String)")
24         sha256PubkeyType = typeDesc("Sha256(PublicKey)")
25 )
26
27 var types = map[string]typeDesc{
28         string(amountType): amountType,
29         string(assetType):  assetType,
30         string(boolType):   boolType,
31         string(hashType):   hashType,
32         string(intType):    intType,
33         string(listType):   listType,
34         string(nilType):    nilType,
35         string(predType):   predType,
36         string(progType):   progType,
37         string(pubkeyType): pubkeyType,
38         string(sigType):    sigType,
39         string(signType):   signType,
40         string(strType):    strType,
41
42         string(sha3StrType):      sha3StrType,
43         string(sha3PubkeyType):   sha3PubkeyType,
44         string(sha256StrType):    sha256StrType,
45         string(sha256PubkeyType): sha256PubkeyType,
46 }
47
48 func isHashSubtype(t typeDesc) bool {
49         switch t {
50         case sha3StrType, sha3PubkeyType, sha256StrType, sha256PubkeyType:
51                 return true
52         }
53         return false
54 }
55
56 func propagateType(contract *Contract, clause *Clause, env *environ, t typeDesc, e expression) {
57         v, ok := e.(varRef)
58         if !ok {
59                 return
60         }
61         if entry := env.lookup(string(v)); entry != nil {
62                 entry.t = t
63                 for _, p := range contract.Params {
64                         if p.Name == string(v) {
65                                 p.InferredType = t
66                                 return
67                         }
68                 }
69                 for _, p := range clause.Params {
70                         if p.Name == string(v) {
71                                 p.InferredType = t
72                                 return
73                         }
74                 }
75         }
76 }