EdgeScript syntax

更新时间:
复制 MD 格式

EdgeScript supports comments, identifiers, data types, variables, operators, conditional and loop statements, and user-defined functions.

Note
  • Double quotation marks (") are not allowed in EdgeScript.

  • To escape a character such as a single quotation mark (') in an edge script, use the tochar function. For example, tochar(39) represents a single quotation mark. For more information, see The tochar function.

The following table describes the EdgeScript syntax rules.

Syntax

Rules

Comment

Any text on a line that follows a number sign (#) is a comment. For example: # this is a comment.

Identifier rules

  • Identifiers are case-sensitive and can contain letters, digits, and underscores (_). An identifier cannot start with a digit.

  • These rules apply to all variable and function names, whether built-in or custom.

Data types

  • String

    Literal constant: enclosed in single quotation marks. For example: 'hello, EdgeScript'.

  • Number

    Literal constant: a decimal number. For example: 10, -99, or 1.1.

  • Boolean

    Literal constant: true or false.

  • Dictionary

    The following are literal constants:

    • []: an empty dictionary.

    • ['key1', 'key2', 100]: an indexed dictionary (similar to an array).

      • 1 -> 'key1'

      • 2 -> 'key2'

      • 3 -> 100

    • ['key1' = 'value1', 'key2' = '1000']: a key-value dictionary.

      • 'key1' -> 'value1'

      • 'key2' -> '1000'

Variables

  • Definition

    Variables are defined by assignment.

  • Usage

    • Reference both built-in and custom variables by name.

      • Reference a built-in variable: host.

      • Reference a custom variable: seckey.

    • To explicitly identify a built-in variable, prefix its name with a dollar sign ($).

      Reference a built-in variable: $host.

    • A custom variable cannot have the same name as a built-in variable.

      For a list of built-in variables, see EdgeScript built-in variables.

Operators

  • = : The assignment operator.

    • Example: seckey = 'ASDLFJ234dxvf34sDF'

    • Example: seckeys = ['key1', 'key2']

  • - : The unary minus operator.

    Example: inum = -10

  • All other data type operations use built-in functions instead of operators. For more information, see Conditional logic functions.

    • Supported built-in functions for data types

      • Built-in functions for strings.

      • Built-in functions for numbers.

      • Built-in functions for dictionaries.

    • Examples

      • sval = concat(sval, 'trail')

      • len(arrvar)

Statements

  • Conditional statement

    if condition {   
       ...
    }
    
    if condition1 {   
       if condition2 {
            ...
       }
    }
    
    if condition {
       ...
    } else {
       ...
    }
  • Explanation

    • The condition can consist of the following elements:

      • Literal constants

      • Variables

      • Function calls

    • Body

      • The body can be empty.

      • The body can contain multiple statements, each on a new line.

    • Multiple levels of nesting are supported.

    • Coding style

      The opening brace ({) must be on the same line as the if condition.

  • for loop

    # Example 1: Iterate over an indexed dictionary.
    # This loop iterates through an indexed dictionary and returns true if it finds the value 'c'.
    a = ['a', 'b', 'c', 'd']
    def for_func () {
        for k, v in a {
            if eq(v, 'c') {
                return true
            }
        }   
    }
    for_func()
    
    # Example 2: Iterate over a key-value dictionary.
    # This loop iterates through a key-value dictionary and returns true if it finds the key 'c'.
    a = ['a' = 1, 'b' = 2, 'c' = 3, 'd' = 4, 'e' = 5, 'f' = 6]
    def for_func () {
        for k, v in a {
            if eq(k, 'c') {
                return true
            }
        }   
    }
    for_func()
    
    # Example 3: Use nested loops.
    # This example demonstrates nested loops that iterate through three dictionaries.
    num = 0
    def for_func () {
        a = [0,1,2,3,4,5,6,7,8,9]
        for k ,v in a {
            b = [0,1,2,3,4,5,6,7,8,9]
            for k1 ,v1 in b {
                c = [0,1,2,3,4,5,6,7,8,9]
                for k2 ,v2 in c {
                    num = add(num, 1)
                    if and(eq(v, 3), eq(v1, 5), eq(v2, 7)) {
                        return true
                    }
                }
            }
        } 
    }
    for_func()
  • Notes:

    • A for loop can only iterate over a dictionary or an indexed dictionary.

    • The break keyword is not supported. To exit a loop, use a return statement within a custom function.

    • Nested loops are supported.

    • Coding style

      The opening brace ({) must be on the same line as the for statement.

Functions

  • Definition syntax

    def function_name(parameter_list) {
       ...
    }
  • Definition details

    • Parameter list

      • Functions can have no parameters.

      • Functions can have multiple parameters, separated by commas (,).

    • Function body

      • The body can be empty.

      • The body can contain multiple statements, each on a new line.

      • Return value: The return statement is supported.

    • Coding style

      The opening brace ({) must be on the same line as the def function_name(parameter_list) definition.

  • Function call

    Call both built-in and custom functions by using the function_name() syntax.