bc-vnt Posted October 11, 2012 Report Posted October 11, 2012 Getting StartedObjectScript is a dynamically typed language. That means you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution. So, for example, you could define a variable as follows:var a = 12;And later, you could assign the same variable a string value, for example: a = "Hello World!";Because ObjectScript is dynamically typed, this assignment does not cause an error message. ObjectScript is case-sensitive, null is not the same as Null, NULL, or any other variant.Types and Values ObjectScript recognizes the following types of values: null, a special keyword denoting a `null` value boolean, either `true` or `false` number, such as `10`, `0x123`, `2.3`, `5.7e23` string, such as `"Hello World!"` object, such as `{"one", "two", 12:"at index 12"}` array, such as `["one", 31]` function, such as `function(){ print "Hello World!" }` userdata, allows arbitrary C data to be stored in ObjectScript variables null, boolean, number and string are primitive types.object, array, function and userdata are not primitive types and could be used as named containers for values.NullsNull is a type with a single value null. All variables have a null value by default, before a first assignment, and you can assign null to a variable to delete it. ObjectScript uses null as a kind of non-value.BolleansThe boolean type has two values, false and true, which represent the traditional boolean values. However, they do not hold a monopoly of condition values: In ObjectScript, any value may represent a condition. Conditionals (such as the ones in control structures) consider false, null and NaN (not a number) as false and anything else as true.Beware that ObjectScript considers both zero and the empty string as true in conditional tests.NumbersThe number type represents real (double-precision floating-point) numbers. You can write numeric constants with an optional decimal part, plus an optional decimal exponent. Examples of valid numeric constants are:12 // decimal, base 103.142e100xfe // hexadecimal, "hex" or base 160123 // octal, base 8StringsA string literal is zero or more characters enclosed in double (") quotation marks. The following are examples of string literals."Hello World!""one line \n another line"You can call any of the methods of the String object, for example, you can use the String.length property with a string literal."Hello World!".lengthor just use length operator:#"Hello World!"ObjectsAn object is a list of zero or more pairs of property names and associated values of an object.{"one", "two", 12:"at index 12", ["on" .. "e"]: "at index one", some = "extended syntax"}The simplest constructor of object is the empty constructor `{}`a = {}; // create an empty object and store its reference in aYou should not use an object literal { at the beginning of a statement. This will lead to not behave as you expect, because the { will be interpreted as the beginning of a block:{ var a = 12;}Let's view some examples:days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}will initialize days[0] with the string "Sunday" (the first element has always index 0), days[1] with "Monday", and so on:print(days[4]) --> ThursdayConstructors do not need to use only constant expressions. You can use any kind of expression for the value:tab = {sin(1), sin(2), sin(3), sin(4)}To initialize an object to be used as a record, ObjectScript offers the following syntax:a = {x=0, y=0}is equivalent to:a = {["x"]=0, ["y"]=0}a = {x:0, y:0}a = {x:0; y:0}a = {x:0, y:0,}a = {x=0, y=0}a = {x:0, y=0}So you can always put a comma after the last entry, you can use ":" or "=" to make pair of a property. You can always use a semicolon ( instead of a comma (,)a = {x=1, y=3; "one", "two"}Finally, you can avoid usage of comma and semicolon:a = {x=1 y=3 "one" "two"}b = "one";a = { [b] = "great" }; // the same as { [b]: "great" }print(a[b]) --> greatprint(a["one"]) --> greatprint(a.one) --> greata = { one = "great" }; // the same as { "one": "great" }print(a[b]) --> greatprint(a["one"]) --> greatprint(a.one) --> greata = { one="great", two="awesome" };a = { one: "great", two: "awesome" }; // JavaScript object notation is fully supporteda = { one: "great", two: "awesome", }; // a comma after the last entry is valida = { one: "great" two="awesome" 1="value at index 1" }; // syntax without "," is also valida = {"one", "two"};print(#a) --> 2ArraysAn array is a list of indexed values:a = ["zero", "one", "two"]print(a[1]) --> oneprint(#a) --> 3FunctionsYou can think of functions as procedures that your application can perform:var max = function(a, { return a < b ? a : b }Functions are first-class values in ObjectScript. That means that functions can be stored in variables, passed as arguments to other functions, and returned as results. An unconventional, but quite convenient feature of ObjectScript is that functions may return multiple results:var func = function(){ return 1, 2 }var x, y = func()print x --> 1print y --> 2The functions are objects themselves. As such, they have properties and methods. Any reference to a function allows it to be invoked using the () operator:var func = function(){ print arguments }var func2 = function(f){ f.apply(null, ...) }func2(func,1,2,3) --> {1,2,3}Moreover, ObjectScript supports nested functions and closures. The nested functions are functions defined within another function. They are created each time the outer function is invoked. In addition to that, each created function forms a lexical closure: the lexical scope of the outer function, including any local variables and arguments, become part of the internal state of each nested function object, even after execution of the outer function concludes:var a = function(){ var x = 2 return function(){ retutn x++ }}var b = a()print b() --> 2print b() --> 3print b() --> 4ObjectScript can call functions written in ObjectScript and functions written in C++ or C. All the standard library in ObjectScript is written in C++. Application programs may define other functions in C.int test(OS * os, int, int, int, void*){ os->pushNumber(123); return 1;}int main(int argc, char* argv[]){ OS * os = OS::create(); os->pushCFunction(test); os->setGlobal("test"); os->eval("print(test())"); // outputs 123 os->release(); return 0;}UserdataAn userdata allows arbitrary C++ or C data to be stored in ObjectScript variables. It's used to represent new types created by an application program or a library written in C++ or C.Assignment and Multiple assignment operatorAssignment is the basic means of changing the value of a variable or a object property:a = "Hello" .. " world!"t.n = t.n + 1ObjectScript allows multiple assignment, where a list of values is assigned to a list of variables in one step. Both lists have their elements separated by commas:a, b = 1, 2The variable a gets the value 1 and b gets 2. In a multiple assignment, ObjectScript first evaluates all values and only then executes the assignments. Therefore, you can use a multiple assignment to swap two values, as in:x, y = y, x // swap x for ya[i], a[j] = a[j], a[i] // swap a[i] for a[j]ObjectScript always adjusts the number of values to the number of variables. When the list of values is shorter than the list of variables, the extra variables receive null as their values, when the list of values is longer, the extra values are silently discarded.a, b, c = 0, 1print(a, b, c) --> 0 1 nulla, b = a+1, b+1, b+2 -- value of b+2 is ignoredprint(a, --> 1 2a, b, c = 0print(a,b,c) --> 0 null nullThe last assignment in the above example shows a common mistake. To initialize a set of variables, you must provide a value for each one:a, b, c = 0, 0, 0print(a, b, c) --> 0 0 0You can use multiple assignment simply to write several assignments in one line. But often you really need multiple assignment, for example, to swap two values. A more frequent use is to collect multiple returns from function calls.a, b = f()`f()` returns two results: a gets the first and b gets the second.Global VariablesGlobal variables do not need declarations. You simply assign a value to a global variable to create it. It is not an error to access a non-initialized variable, you just get the special value `null` as the result.print(a) --> nulla = 10print(a) --> 10Usually you do not need to delete global variables, if your variable is going to have a short life, you should use a local variable. But, if you need to delete a global variable, just assign `null` to it.a = nullprint(a) --> nullAfter that, it is as if the variable had never been used. In other words, a global variable is existent if (and only if) it has a non-null value.Environments and the Global EnvironmentAny reference to a global name v is syntactically translated to _E.v. Moreover, every function is compiled in the scope of an external local variable called _E, so _E itself is never a global name in a function.Despite the existence of this external _E variable and the translation of global names, _E is a completely regular name. In particular, you can define new variables and parameters with that name. Each reference to a global name uses the _E that is visible at that point in the program, following the usual visibility rules of ObjectScript.Any object used as the value of _E is called an environment.ObjectScript keeps a distinguished environment called the global environment. This value is kept at a special object in the C registry. In ObjectScript, the variable _G is initialized with this same value.When ObjectScript compiles a function, it initializes the value of its _E upvalue with the global environment. Therefore, by default, global variables in ObjectScript code refer to entries in the global environment. Moreover, all standard libraries are loaded in the global environment and several functions there operate on that environment.You can execute any function with a different environment using Function.applyEnv method. For example lets view code of eval functions inside of core.os file:function eval(str, env){ return compileText(str).applyEnv(env || _G, null, ...)}IteratorsAn iterator allows you to iterate over the elements of a collection. For example:a = { null true 12 "0" } // is equivalent to { null, true, 12, "0" }for(k, v in a){ print( k " --> " v ) // is equivalent to print( k, " => ", v )}Outputs:0 --> null1 --> true2 --> 123 --> 0ObjectScript compiles the above program to:a = { null true 12 "0" }{ var iter_func = a.__iter(); for(var iter_valid;{ // infinite loop iter_valid, k, v = iter_func(); if(!iter_valid) break; print( k " --> " v ) }}The first result value of iter_func indicates either valid or not valid current step, second and other return values are user used values.Any iterator needs to keep some state between successive calls, so that it knows where it is and how to proceed from there. Closures provide an excellent mechanism for that task. Remember that a closure is a function that accesses local variables from its enclosing function.For example, array iterator is declated as:Array.__iter = function(){ var i, self = 0, this return function(){ if(i < #self){ return true, i, self[i++] } }}Note that iterator of function is itself. It's declared as:Function.__iter = function(){ return this }So we can write iterator like that:var range = function(a, { return function(){ if(a <= { return true, a++ } }}for(var i in range(10, 13)){ print( "i = ", i )}Outputs:i = 10i = 11i = 12i = 13Properties, Getters And SettersA getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property.a = { _color: "red" __get@color = function(){ return this._color } __set@color = function(v){ this._color = v }}print a.color --> redprint a["color"] --> reda.color = "blue"print a.color --> blueprint a["color"] --> blueNote that @ is not a special symbol, any functions and variables can contain @.Another example:a = { _color: "red" __get = function(name){ if(name == "color") return this._color } __set = function(name, v){ if(name == "color") this._color = v } __del = function(name){ if(name == "color") delete this._color }}print a.color --> reda.color = "blue"print a.color --> bluedelete a.colorprint a.color --> nullMulti dimensional propertiesObjectScript supports `a.color` and `a["color"]` syntax. What about?a[x, y] = 12Yes, ObjectScript supports the above syntax, it's called multi dimensional properties.a = { _matrix = {} __getdim = function(x, y){ return this._matrix[y*4 + x] } __setdim = function(value, x, y){ this._matrix[y*4 + x] = value } __deldim = function(x, y){ delete this._matrix[y*4 + x] }}a[1, 2] = 5 // is equivalent to a.__setdim(5, 1, 2)print a[1, 2] --> 5 // print(a.__getdim(1, 2)delete a[1, 2] // a.__deldim(1, 2)print a[1, 2] --> nullNote that `__setdim` method receives the first argument as new property value and other arguments as dimensional attributes of property. For example:a = { _matrix = {} __getdim = function(x, y, z){ return this._matrix[z*16 + y*4 + x] } __setdim = function(value, x, y, z){ this._matrix[z*16 + y*4 + x] = value }}a[1, 2, 3] = 5print a[1, 2, 3] --> 5Empty dimensional propertiesWhat about?b = a[]a[] = 2delete a[]ObjectScript provides following special methods `__getempty`, `__setempty` and `__delempty` that you can use if necessary.Object-oriented programming (OOP)Object-oriented programming is a programming paradigm that uses abstraction to create models based on the real world. It uses several techniques and paradigms, including modularity, polymorphism, and encapsulation.ObjectScript is OOP language, also you can override arithmetic, bitwise, concatenation, comparison and unary operators.Core ObjectsObjectScript has several objects included in its core, there are global variables named Object, Array, String, Number, Boolean and Function.Every object in ObjectScript is an instance of the object Object and therefore inherits all its properties and methods.var a = { num: 1 __get@number: function(){ return this.num } __add = function(a, { return a.number + b.number }}var b = extends a { num: 2}print a + b --> 3The ClassObjectScript is a prototype-based language which contains no class statement. Any object could be used as class.Person = { __construct = function(firstname, lastname){ this.firstname = firstname this.lastname = lastname } __get@fullname = function(){ return this.firstname .. " " .. this.lastname } walk = function(){ print this.fullname .. " is walking!" }}The Object (Class Instance)To create a new instance of an object we use () operator for class variable:ar p = Person("James", "Bond")is equivalent to:var p = {}p.prototype = Personp.__construct("James", "Bond")Run:p.walk() --> James Bond is walking!print p --> {firstname:James,lastname:Bond}InheritanceInheritance is a way to create a class as a specialized version of class. You need to use extends operator.ar IvanPerson = extends Person { __construct: function(){ super("Ivan", "Petrov") }}var p = IvanPerson()p.walk() --> Ivan Petrov is walking!print p --> {firstname:Ivan,lastname:Petrov}The extends operator has syntax `extends exp1 exp2` where exp1 and exp2 are any valid expressions, it's equivalent to:(function(exp1, exp2){ exp2.prototype = exp1 return exp2 })()EncapsulationIn the previous example, IvanPerson does not need to know how the Person class's walk() method is implemented, but still can use that method. The IvanPerson class doesn't need to explicitly define that method unless we want to change it. This is called encapsulation, by which every class inherits the methods of its parent and only needs to define things it wishes to change.Simple and powerful examplevar vec3 = { __construct = function(x, y, z){ this.x = x this.y = y this.z = z } __add = function(a, { return vec3(a.x + b.x, a.y + b.y, a.z + b.z) } __mul = function(a, { return vec3(a.x * b.x, a.y * b.y, a.z * b.z) }}var v1 = vec3(10 20 30)var v2 = vec3(1 2 3)var v3 = v1 + v2 * v2print v3Outputs: {x:11,y:24,z:39} http://www.codeproject.com/Articles/466907/ObjectScript-A-new-programming-language Quote
ionut.hulub Posted October 11, 2012 Report Posted October 11, 2012 ce avantaje ofera fata de alte limbaje de scripting precum python sau ruby? Quote
sandabot Posted October 11, 2012 Report Posted October 11, 2012 Din care vad simplitate si fiind mai usor de inteles . Quote
TheTime Posted October 11, 2012 Report Posted October 11, 2012 Simplitatea nu este neaparat un lucru bun. Un astfel de limbaj creaza foarte multe obiceiuri proaste, nu recomand nimanui sa invete sa programeze pe baza limbajelor de scripting.E simplu, rapid pentru unele chestii, dar limitat pe masura. Quote
shaggi Posted October 11, 2012 Report Posted October 11, 2012 Da, e scripting, ceea ce e util, dar care e diferenta intre sintaxa lui si cea a javascriptului? Quote