Needle Language Specification
website·Indexed 29 days ago
https://scottafk.github.io/needle-docsTechnical 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.
What's inside Needle
- 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.
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.Increment and decrement variables in Needle
The++and--operators increase or decrease the value ofint,float, andmoneyvariables by 1.var i int f float m money i++ f-- m++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 thedatasection 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 } }Define a Needle Smart Contract structure
A smart contract in Needle is declared using thecontractkeyword followed by an identifier and a code block enclosed in curly braces. A contract typically consists of three main sections:datafor input parameters,settingsfor constants, andfuncfor logic.contract Name { data{} settings{} func name(){} }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 tonil) 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- Arrays: Indices must be of type
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") }Declare local variables in Needle
Local variables are declared using the
varkeyword. 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 = 1Control loop execution with break and continue
Use thebreakstatement to immediately terminate the innermostwhileloop. Use thecontinuestatement to skip the remaining code in the current iteration of the innermostwhileloop 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++ }Pass contract return values using $result
A standardreturnstatement 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$resultvariable.contract NameB { action { $result = 11 } } contract NameA { action { var a int a = NameB() } }Declare Local Variables in Needle
Local variables are declared using the
varkeyword. 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
mapandarraytypes 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 = 1Define a Smart Contract structure in Needle
A smart contract is declared using thecontractkeyword followed by the contract name and a code block{}. A contract typically consists of three main sections:datafor input parameters,settingsfor constants, andfuncfor logic.contract Name { data{} settings{} func name(){} }