Needle Language Specification

website·Indexed 29 days ago

https://scottafk.github.io/needle-docs

Technical documentation for the Needle language, featuring a formal specification of its file structure (Smart Contract, Data, Settings, and Function components) and syntax bases, including code blocks, expressions, and control flow statements.

Tokens
5.4K
Snippets
37
Records
47
Agent score
65%

What's inside Needle

  1. Overview of the Needle programming language

    Needle is a concise, expressive, and efficient general-purpose programming language. It provides a set of functions, operators, and structures designed for data algorithm processing and database operations. It is specifically optimized for readability and ease of writing, making it suitable for developing a wide range of Decentralized Applications (DApps) via smart contracts.
  2. Overview of the Needle programming language

    Needle is a concise, expressive, and efficient general-purpose programming language. It provides a set of functions, operators, and structures specifically designed for data algorithm processing, database operations, and the development of decentralized applications (DApps) via smart contracts.
  3. Manage Global Variables in Needle

    Global variables are declared and accessed using the $ prefix. They can be declared within any function in a contract scope but must be declared before use. Additionally, parameters defined in the data section of a contract are treated as global variables within that contract's scope.
    contract Name {
        data {
            param int
        }
        func set() {
            $abc = 1
        }
        func get() int {
            $abc = $abc + $param
            return $abc
        }
    }
  4. Define a Needle Smart Contract structure

    A smart contract in Needle is declared using the contract keyword followed by an identifier and a code block enclosed in curly braces. A contract typically consists of three main sections: data for input parameters, settings for constants, and func for logic.
    contract Name {
        data{}
        settings{}
        func name(){}
    }
  5. Work with arrays and maps in Needle

    Arrays and maps are created using [] and {} operators.

    • Arrays: Indices must be of type int. If a value is assigned to an index greater than the current maximum, empty elements (initialized to nil) are added to fill the gap.
    • Maps: Indices must be of type string.

    Note: Map literals cannot contain function calls or array access expressions; they must be simple values.

    var arr array m map
    arr = [1,2,3]
    arr[0] = 4
    m = {"key": "value"}
    
    // Invalid map literals:
    // m = {"key": myfunc()}
    // m = {"key": arr[0]}
    
    // Valid map assignment:
    m["key1"] = arr[5] // m["key1"] = nil
  6. Implement Tail Functions for flexible parameter passing

    Tail functions allow a main function to have optional parameters that can be called in any order using dot notation. Tail functions do not have their own return values; they contribute to the main function's return value. They must be connected to the main function or another tail function and cannot be called independently.
    func myfunc(name string).Param1(p1 int).Param2(p2 string) int {
        // ...
    }
    
    func Name() {
        // Call main function and only the second tail function
        myfunc("name").Param2("p2")
    }
  7. Declare local variables in Needle

    Local variables are declared using the var keyword. They must be declared with a type and assigned a value after declaration; initialization during declaration is not permitted.

    Multiple variables of the same type can be declared in a single line. If consecutive variables share a type, the type only needs to be specified once at the end.

    // Correct declaration and assignment
    var a int
    a = 1
    
    // Multiple variables of the same type
    var b1, b2 string
    b1, b2 = "string1", "string2"
    
    // Mixed types
    var c bool, c1 float
    c, c1 = true, 1.2
    
    // INVALID: Initialization during declaration
    var a int = 1
  8. Control loop execution with break and continue

    Use the break statement to immediately terminate the innermost while loop. Use the continue statement to skip the remaining code in the current iteration of the innermost while loop and proceed to the next evaluation of the loop condition.
    var a int
    a = 1
    while a < 10 {
        if a == 5 {
            break // Exits the loop entirely
        }
        a++
    }
    
    var b int
    b = 1
    while b < 10 {
        if b == 5 {
            continue // Skips to the next iteration
        }
        b++
    }
  9. Pass contract return values using $result

    A standard return statement in a Needle function does not automatically pass values to other contracts. To pass a return value to another contract, you must assign the value to the special $result variable.
    contract NameB {
        action {
            $result = 11
        }
    }
    
    contract NameA {
        action {
            var a int
            a = NameB()
        }
    }
  10. Declare Local Variables in Needle

    Local variables are declared using the var keyword. They must be declared with a type before they are assigned a value; initialization during declaration is not permitted.

    Multiple variables of the same type can be declared in a single line. Note that map and array types do not support multiple assignments on the same line using {} or [] syntax.

    // Correct declaration and assignment
    var a int
    a = 1
    
    // Multiple declarations
    var b b1, b2 string
    var c bool, c1 float
    
    b1, b2 = "string1", "string2"
    c, c1 = true, 1.2
    
    // Invalid: initialization during declaration
    var a int = 1
  11. Define a Smart Contract structure in Needle

    A smart contract is declared using the contract keyword followed by the contract name and a code block {}. A contract typically consists of three main sections: data for input parameters, settings for constants, and func for logic.
    contract Name {
        data{}
        settings{}
        func name(){}
    }