Factry Historian
...
Scripting
Language Syntax
tengo language syntax tengo's syntax is designed to be familiar to go developers while being a bit simpler and more streamlined you can test the tengo code in online playground values and value types in tengo, everything is a value, and, all values are associated with a type 19 + 84 // int values "aomame" + `kawa` // string values 9 22 + 1e10 // float values true || false // bool values '九' > '9' // char values \[1, false, "foo"] // array value {a 12 34, b "bar"} // map value func() { / / } // function value here's a list of all available value types in tengo tengo type description equivalent type in go int signed 64 bit integer value int64 float 64 bit floating point value float64 bool boolean value bool char unicode character rune string unicode string string bytes byte array \[]byte error language syntax /#error values value time time value time time array value array (mutable) \[]interface{} immutable array language syntax /#immutable values array map value map with string keys (mutable) map\[string]interface{} immutable map language syntax /#immutable values map undefined language syntax /#undefined values value function language syntax /#function values value user defined value of user defined types error values in tengo, an error can be represented using "error" typed values an error value is created using error expression, and, it must have an underlying value the underlying value of an error value can be access using value selector err1 = error("oops") // error with string value err2 = error(1+2+3) // error with int value if is error(err1) { // 'is error' builtin function err val = err1 value // get underlying value } immutable values in tengo, basically all values (except for array and map) are immutable s = "12345" s\[1] = 'b' // illegal string is immutable a = \[1, 2, 3] a\[1] = "two" // ok a is now \[1, "two", 3] an array or map value can be made immutable using immutable expression b = immutable(\[1, 2, 3]) b\[1] = "foo" // illegal 'b' references to an immutable array note that re assigning a new value to the variable has nothing to do with the value immutability s = "abc" s = "foo" // ok a = immutable(\[1, 2, 3]) a = false // ok note that, if you copy (using copy builtin function) an immutable value, it will return a "mutable" copy also, immutability is not applied to the individual elements of the array or map value, unless they are explicitly made immutable a = immutable({b 4, c \[1, 2, 3]}) a b = 5 // illegal a c\[1] = 5 // ok because 'a c' is not immutable a = immutable({b 4, c immutable(\[1, 2, 3])}) a c\[1] = 5 // illegal undefined values in tengo, an "undefined" value can be used to represent an unexpected or non existing value a function that does not return a value explicitly considered to return undefined value indexer or selector on composite value types may return undefined if the key or index does not exist type conversion builtin functions without a default value will return undefined if conversion fails tengo a = func() { b = 4 }() // a == undefined b = \[1, 2, 3]\[10] // b == undefined c = {a "foo"}\["b"] // c == undefined d = int("foo") // d == undefined array values in tengo, array is an ordered list of values of any types elements of an array can be accessed using indexer \[] \[1, 2, 3]\[0] // == 1 \[1, 2, 3]\[2] // == 3 \[1, 2, 3]\[3] // == undefined \["foo", "bar", \[1, 2, 3]] // ok array with an array element map values in tengo, map is a set of key value pairs where key is string and the value is of any value types value of a map can be accessed using indexer \[] or selector ' ' operators m = { a 1, b false, c "foo" } m\["b"] // == false m c // == "foo" m x // == undefined {a \[1,2,3], b {c "foo", d "bar"}} // ok map with an array element and a map element function values in tengo, function is a callable value with a number of function arguments and a return value just like any other values, functions can be passed into or returned from another function my func = func(arg1, arg2) { return arg1 + arg2 } adder = func(base) { return func(x) { return base + x } // capturing 'base' } add5 = adder(5) nine = add5(4) // == 9 unlike go, tengo does not have declarations so the following code is illegal func my func(arg1, arg2) { // illegal return arg1 + arg2 } tengo also supports variadic functions/closures variadic = func (a, b, c) { return \[a, b, c] } variadic(1, 2, 3, 4) // \[1, 2, \[3, 4]] variadicclosure = func(a) { return func(b, c) { return \[a, b, c] } } variadicclosure(1)(2, 3, 4) // \[1, 2, \[3, 4]] only the last parameter can be variadic the following code is also illegal // illegal, because a is variadic and is not the last parameter illegal = func(a , b) { / / } when calling a function, the number of passing arguments must match that of function definition f = func(a, b) {} f(1, 2, 3) // runtime error wrong number of arguments want=2, got=3 like go, you can use ellipsis to pass array type value as its last parameter f1 = func(a, b, c) { return a + b + c } f1(\[1, 2, 3] ) // => 6 f1(1, \[2, 3] ) // => 6 f1(1, 2, \[3] ) // => 6 f1(\[1, 2] ) // runtime error wrong number of arguments want=3, got=2 f2 = func(a, b) {} f2(1) // valid; a = 1, b = \[] f2(1, 2) // valid; a = 1, b = \[2] f2(1, 2, 3) // valid; a = 1, b = \[2, 3] f2(\[1, 2, 3] ) // valid; a = 1, b = \[2, 3] variables and scopes a value can be assigned to a variable using assignment operator = and = = operator defines a new variable in the scope and assigns a value = operator assigns a new value to an existing variable in the scope variables are defined either in global scope (defined outside function) or in local scope (defined inside function) a = "foo" // define 'a' in global scope func() { // function scope a b = 52 // define 'b' in function scope a func() { // function scope b c = 19 84 // define 'c' in function scope b a = "bee" // ok assign new value to 'a' from global scope b = 20 // ok assign new value to 'b' from function scope a b = true // ok define new 'b' in function scope b // (shadowing 'b' from function scope a) } a = "bar" // ok assigne new value to 'a' from global scope b = 10 // ok assigne new value to 'b' a = 100 // ok define new 'a' in function scope a // (shadowing 'a' from global scope) c = 9 1 // illegal 'c' is not defined b = \[1, 2] // illegal 'b' is already defined in the same scope } b = 25 // illegal 'b' is not defined a = {d 2} // illegal 'a' is already defined in the same scope unlike go, a variable can be assigned a value of different types a = 123 // assigned 'int' a = "123" // re assigned 'string' a = \[1, 2, 3] // re assigned 'array' type conversions although the type is not directly specified in tengo, one can use type conversion builtin functions docid\ uufozxxguzpcbsx61qiqb to convert between value types tengo s1 = string(1984) // "1984" i2 = int(" 999") // 999 f3 = float( 51) // 51 0 b4 = bool(1) // true c5 = char("x") // 'x' see operators docid qtqflbkduigzvz2v0bqo for more details on type coercions operators unary operators operator usage types + same as 0 + x int, float same as 0 x int, float ! logical not all types ^ bitwise complement int in tengo, all values can be either docid\ smonyfzo12nj6jxyy8eu4 binary operators operator usage types == equal all types != not equal all types && logical and all types || logical or all types + add/concat int, float, string, char, time, array subtract int, float, char, time multiply int, float / divide int, float & bitwise and int | bitwise or int ^ bitwise xor int &^ bitclear (and not) int << shift left int >> shift right int < less than int, float, char, time, string <= less than or equal to int, float, char, time, string > greater than int, float, char, time, string >= greater than or equal to int, float, char, time, string see docid qtqflbkduigzvz2v0bqo for more details ternary operators tengo has a ternary conditional operator (condition expression) ? (true expression) (false expression) a = true ? 1 1 // a == 1 min = func(a, b) { return a < b ? a b } b = min(5, 10) // b == 5 assignment and increment operators operator usage += (lhs) = (lhs) + (rhs) = (lhs) = (lhs) (rhs) = (lhs) = (lhs) (rhs) /= (lhs) = (lhs) / (rhs) %= (lhs) = (lhs) % (rhs) &= (lhs) = (lhs) & (rhs) |= (lhs) = (lhs) | (rhs) &^= (lhs) = (lhs) &^ (rhs) ^= (lhs) = (lhs) ^ (rhs) <<= (lhs) = (lhs) << (rhs) >>= (lhs) = (lhs) >> (rhs) ++ (lhs) = (lhs) + 1 (lhs) = (lhs) 1 operator precedences unary operators have the highest precedence, and, ternary operator has the lowest precedence there are five precedence levels for binary operators multiplication operators bind strongest, followed by addition operators, comparison operators, && (logical and), and finally || (logical or) precedence operator 5 / % << >> & &^ 4 + | ^ 3 == != < <= > >= 2 && 1 || like go, ++ and operators form statements, not expressions, they fall outside the operator hierarchy selector and indexer one can use selector ( ) and indexer ( \[] ) operators to read or write elements of composite types (array, map, string, bytes) \["one", "two", "three"]\[1] // == "two" m = { a 1, b \[2, 3, 4], c func() { return 10 } } m a // == 1 m\["b"]\[1] // == 3 m c() // == 10 m x = 5 // add 'x' to map 'm' m\["b"]\[5] // == undefined m\["b"]\[5] d // == undefined m b\[5] = 0 // == undefined m x y z // == undefined like go, one can use slice operator \[ ] for sequence value types such as array, string, bytes a = \[1, 2, 3, 4, 5]\[1 3] // == \[2, 3] b = \[1, 2, 3, 4, 5]\[3 ] // == \[4, 5] c = \[1, 2, 3, 4, 5]\[ 3] // == \[1, 2, 3] d = "hello world"\[2 10] // == "llo worl" c = \[1, 2, 3, 4, 5]\[ 1 10] // == \[1, 2, 3, 4, 5] note keywords cannot be used as selectors tengo a = {in true} // parse error expected map key, found 'in' a func = "" // parse error expected selector, found 'func' use double quotes and indexer to use keywords with maps a = {"in" true} a\["func"] = "" statements if statement "if" statement is very similar to go tengo if a < 0 { // execute if 'a' is negative } else if a == 0 { // execute if 'a' is zero } else { // execute if 'a' is positive } like go, the condition expression may be preceded by a simple statement, which executes before the expression is evaluated tengo if a = foo(); a < 0 { // execute if 'a' is negative } for statement "for" statement is very similar to go // for (init); (condition); (post) {} for a =0; a<10; a++ { // } // for (condition) {} for a < 10 { // } // for {} for { // } for in statement "for in" statement is new in tengo it's similar to go's for range statement "for in" statement can iterate any iterable value types (array, map, bytes, string, undefined) for v in \[1, 2, 3] { // array element // 'v' is value } for i, v in \[1, 2, 3] { // array index and element // 'i' is index // 'v' is value } for k, v in {k1 1, k2 2} { // map key and value // 'k' is key // 'v' is value } modules module is the basic compilation unit in tengo a module can import another module using import expression main module sum = import(" /sum") // load module from a local file fmt print(sum(10)) // module function another module in sum tengo file base = 5 export func(x) { return x + base } by default, import solves the missing extension name of a module file as " tengo " thus, sum = import(" /sum") is equivalent to sum = import(" /sum tengo") in tengo, modules are very similar to functions import expression loads the module code and execute it like a function module should return a value using export statement module can return export a value of any types int, map, function, etc export in a module is like return in a function it stops execution and return a value to the importing code export ed values are always immutable if the module does not have any export statement, import expression simply returns undefined (just like the function that has no return ) note that export statement is completely ignored and not evaluated if the code is executed as a main module also, you can use import expression to load the standard library docid\ it3q35odjcdlpfjwvsiin as well tengo math = import("math") a = math abs( 19 84) // == 19 84 comments like go, tengo supports line comments ( // ) and block comments ( / / ) tengo / multi line block comments / a = 5 // line comments differences from go unlike go, tengo does not have the following declarations imaginary values structs pointers channels goroutines tuple assignment variable parameters switch statement goto statement defer statement panic type assertion