# Smart Contract Grammar

# Source File Character Encoding

Summoner uses the same character encoding as the processor. Unix is UTF-8, and Windows is GB2312. Except for comments and string literals, other non-ASCII characters cannot be used.

# Keyword

The following words are used as Summoner keywords and cannot be used as variable names, function names, and structure names

if、else、type、struct、var、goto、for、range、break、continue、func、nil、
bool、int64、string、const、amount、asset、hash、pubkey、sig、hex

# Blank Character

Spaces, tabs, and newlines are used to distinguish different identifiers. The newline in the statement of the function indicates the end of the current statement.

# Comment

Same as Golang, /*xxx*/ represent block comment, // represent line comment.

# Identifier

Identifiers used for variable names, function names, structure names, etc. rules are as follows:

  • The first letter must be an English uppercase , lowercase letter or an underscore, A-Za-z_.
  • Other letter can be English uppercase,lowercase letters, underscores or numbers, A-Za-z_0-9.

# Literal

Bool literals: true false Integer literals: [1-9][0-9])|0 Float literals: [0-9]+.[0-9]+ String literals: [A-Za-z_][A-Za-z_0-9]

# Data Type

  • bool type: bool
  • Integer type: int64
  • float type: float64
  • string type: string
  • amount type: amount
  • asset type: asset
  • hash type: hash
  • publicKey type: pubkey
  • signature type: sig
  • hex type: hex

# built-in Function

  • sha3(s) returns the SHA3-256 hash value of string parameter s.
  • sha256(s) returns the SHA-256 hash value of string parameter s.
  • abs(n) returns the absolute value of the number n.
  • min(x, y) returns the smallest of the two values x and y.
  • max(x, y) returns the largest of the two values x and y.
  • checkTxSig(key, sig) verifies whether the transaction signature is correct according to a PublicKey and a Signature.
  • below(height) determine whether the current block height is lower than the parameter height, if it is, it returns true, otherwise it returns false.
  • above(height) determine whether the current block height is higher than the parameter height, if it is, it returns true, otherwise it returns false.
  • lock(amount,asset,program) lock the asset to the specified accepting object (program type)
  • verify(expression) verifies whether the result of the expression is true, the result of the expression must be bool, and the result of the expression must be true when the verification is successful

# Expression

expression connects operators, and the supported types are as follows:

  • Literal expressions
  • Identifier expression
  • Arithmetic expression: addition, subtraction, multiplication, division, modulus and negation
  • Bracket expression
  • Bool expression
  • Function call expression
  • Comparison operation expression
  • Logical operation expression

# Type Conversion

  • Binary operator type conversion
  • Assignment statement type conversion

# Statement

# Declaration statement

The declaration statement defines various entity objects and some or all of the attributes of the program var: corresponds to the variable declaration func: corresponding to the function declaration

The general syntax of variable declaration:

var name type = expression

E.g

var a int = 1

The general syntax of function declaration:

func name (list of parameters) {
     body
}

# Expression statement

# Unary expression

  • - expr: take negative values for mathematical expressions
  • ~ expr: perform bitwise flip on byte string

# Conditional expression

The following expressions must be numeric operands (that is, Integer or Amount type), and return a Boolean result:

  • expr1> expr2: check whether expr1 is bigger than expr2

  • expr1 <expr2: check whether expr1 is smaller than expr2

  • expr1 >= expr2: check whether expr1 is bigger than or equal to expr2

  • expr1 <= expr2: check whether expr1 is smaller than or equal to expr2

  • expr1 == expr2: check whether expr1 is equal to expr2

  • expr1 != expr2: check whether expr1 is not equal to expr2

# Bitwise operation expression

The following expressions must be byte, and the return value is also byte:

  • expr1 ^ expr2: get the result of bitwise exclusive OR (XOR) of two operands

  • expr1 | expr2: get the result of the bitwise OR (OR) of the two operands

  • expr1 & expr2: get the result of the bitwise AND (AND) of the two operands

# Numerical expression

The following expressions must be numeric operands (Integer or Amount), and return a numeric result:

  • expr1 + expr2: add two operands

  • expr1-expr2: subtract two operands, expr1 minus expr2

  • expr1 * expr2: multiply two operands

  • expr1 / expr2: divide two operands, divide expr1 by expr2

  • expr1% expr2: the remainder of the operand, that is, expr1 takes the remainder of expr2

  • expr1 << expr2: shift expr1 to the left by expr2 bits

  • expr1 >> expr2: shift expr1 to the right by expr2 bits

# if statement

The syntax of the if statement is as follows:

if Boolean expression {
   /* Execute when the boolean expression is true */
} else {
   /* Execute when the boolean expression is false */
}

E.g

if b> c {
     a = b
} else {
     a = c
}

# Function

# Function definition

func name (list of parameters) {
    body
}

# Function parameters

Single parameter:

name TypeName

Multiple parameter lists:

name TypeName, name TypeName, ...

If the parameter types are the same, they can be abbreviated as:

name1, name2, ..., TypeName

For example, the following two function declarations are equivalent:

func LockWithMultiSig(pk1 pubkey, pk2 pubkey, pk3 pubkey)

func LockWithMultiSig(pk1, pk2, pk3 pubkey)

The supported types are as follows:

bool, int64, string, const, amount, asset, hash, pubkey, sig, hex
Last Updated: 11/2/2021, 6:32:36 PM