OSDN Git Service

cp code from bytom repo
[bytom/equity.git] / compiler / builtins.go
1 package compiler
2
3 type builtin struct {
4         name    string
5         opcodes string
6         args    []typeDesc
7         result  typeDesc
8 }
9
10 var builtins = []builtin{
11         {"sha3", "SHA3", []typeDesc{nilType}, hashType},
12         {"sha256", "SHA256", []typeDesc{nilType}, hashType},
13         {"size", "SIZE SWAP DROP", []typeDesc{nilType}, intType},
14         {"abs", "ABS", []typeDesc{intType}, intType},
15         {"min", "MIN", []typeDesc{intType, intType}, intType},
16         {"max", "MAX", []typeDesc{intType, intType}, intType},
17         {"checkTxSig", "TXSIGHASH SWAP CHECKSIG", []typeDesc{pubkeyType, sigType}, boolType},
18         {"concat", "CAT", []typeDesc{nilType, nilType}, strType},
19         {"concatpush", "CATPUSHDATA", []typeDesc{nilType, nilType}, strType},
20         {"below", "BLOCKHEIGHT GREATERTHAN", []typeDesc{intType}, boolType},
21         {"above", "BLOCKHEIGHT LESSTHAN", []typeDesc{intType}, boolType},
22         {"checkTxMultiSig", "", []typeDesc{listType, listType}, boolType}, // WARNING WARNING WOOP WOOP special case
23 }
24
25 type binaryOp struct {
26         op         string
27         precedence int
28         opcodes    string
29
30         left, right, result typeDesc
31 }
32
33 var binaryOps = []binaryOp{
34         // disjunctions disallowed (for now?)
35         // {"||", 1, "BOOLOR", "Boolean", "Boolean", "Boolean"},
36
37         // and disallow this too
38         // {"&&", 2, "BOOLAND", "Boolean", "Boolean", "Boolean"},
39
40         {">", 3, "GREATERTHAN", "Integer", "Integer", "Boolean"},
41         {"<", 3, "LESSTHAN", "Integer", "Integer", "Boolean"},
42         {">=", 3, "GREATERTHANOREQUAL", "Integer", "Integer", "Boolean"},
43         {"<=", 3, "LESSTHANOREQUAL", "Integer", "Integer", "Boolean"},
44
45         {"==", 3, "EQUAL", "", "", "Boolean"},
46         {"!=", 3, "EQUAL NOT", "", "", "Boolean"},
47
48         {"^", 4, "XOR", "", "", ""},
49         {"|", 4, "OR", "", "", ""},
50
51         {"+", 4, "ADD", "Integer", "Integer", "Integer"},
52         {"-", 4, "SUB", "Integer", "Integer", "Integer"},
53
54         // {"&^", 5, "INVERT AND", "", "", ""},
55         {"&", 5, "AND", "", "", ""},
56
57         {"<<", 5, "LSHIFT", "Integer", "Integer", "Integer"},
58         {">>", 5, "RSHIFT", "Integer", "Integer", "Integer"},
59
60         {"%", 5, "MOD", "Integer", "Integer", "Integer"},
61         {"*", 5, "MUL", "Integer", "Integer", "Integer"},
62         {"/", 5, "DIV", "Integer", "Integer", "Integer"},
63 }
64
65 type unaryOp struct {
66         op      string
67         opcodes string
68
69         operand, result typeDesc
70 }
71
72 var unaryOps = []unaryOp{
73         {"-", "NEGATE", "Integer", "Integer"},
74
75         // not not allowed (for now?)
76         // {"!", "NOT", "Boolean", "Boolean"},
77
78         {"~", "INVERT", "", ""},
79 }