Skip to main content
GameDev.net gamedev.net
🔒 Locked

ObjectScript: A new programming language

Started by unitpoint Oct 10, 2012 at 10:00 PM 0 replies 2.2k views
Original Post
unitpoint
unitpoint
Welcome to the ObjectScript!

The ObjectScript is a new embedded programing language that mixes benefits of JavaScript, Lua and PHP. The ObjectScript has syntax from JavaScript, multiple results from Lua, OOP from PHP and much more.

Getting Started

ObjectScript 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
[source lang="jscript"]
var a = 12;
[/source]
And later, you could assign the same variable a string value, for example
[source lang="jscript"]
a = "Hello World!";
[/source]
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.

    Nulls

    Null 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.

    Bolleans

    The 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.

    Numbers

    The 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
    [source lang="jscript"]
    12 // decimal, base 10
    3.14
    2e10
    0xfe // hexadecimal, "hex" or base 16
    0123 // octal, base 8
    [/source]
    Strings

    A string literal is zero or more characters enclosed in double (") quotation marks. The following are examples of string literals
    [source lang="jscript"]
    "Hello World!"
    "one line \n another line"
    [/source]
    You can call any of the methods of the String object, for example, you can use the String.length property with a string literal
    [source lang="jscript"]
    "Hello World!".length
    [/source]
    or just use length operator
    [source lang="jscript"]
    #"Hello World!"
    [/source]
    Objects

    An object is a list of zero or more pairs of property names and associated values of an object
    [source lang="jscript"]
    {"one", "two", 12:"at index 12", ["on" .. "e"]: "at index one", some = "extended syntax"}
    [/source]
    The simplest constructor of object is the empty constructor {}
    [source lang="jscript"]
    a = {}; // create an empty object and store its reference in a
    [/source]
    You 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
    [source lang="jscript"]
    {
    var a = 12;
    }
    [/source]
    Lets view some examples
    [source lang="jscript"]
    days = {"Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"}
    [/source]
    will initialize days[0] with the string "Sunday" (the first element has always index 0), days[1] with "Monday", and so on
    [source lang="jscript"]
    print(days[4]) --> Thursday
    [/source]
    Constructors do not need to use only constant expressions. You can use any kind of expression for the value
    [source lang="jscript"]
    tab = {sin(1), sin(2), sin(3), sin(4)}
    [/source]
    To initialize an object to be used as a record, ObjectScript offers the following syntax
    [source lang="jscript"]
    a = {x=0, y=0}
    [/source]
    is equivalent to
    [source lang="jscript"]
    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}
    [/source]
    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 (,)
    [source lang="jscript"]
    a = {x=1, y=3; "one", "two"}
    [/source]
    Finally, you can avoid usage of comma and semicolon
    [source lang="jscript"]
    a = {x=1 y=3 "one" "two"}

    c = "one";
    a = { [c] = "great" }; // the same as { [c]: "great" }
    print(a[c]) --> great
    print(a["one"]) --> great
    print(a.one) --> great

    a = { one = "great" }; // the same as { "one": "great" }
    print(a[c]) --> great
    print(a["one"]) --> great
    print(a.one) --> great

    a = { one="great", two="awesome" };
    a = { one: "great", two: "awesome" }; // JavaScript object notation is fully supported
    a = { one: "great", two: "awesome", }; // a comma after the last entry is valid
    a = { one: "great" two="awesome" 1="value at index 1" }; // syntax without "," is also valid

    a = {"one", "two"};
    print(#a) --> 2
    [/source]
    Arrays

    An array is a list of indexed values
    [source lang="jscript"]
    a = ["zero", "one", "two"]
    print(a[1]) --> one
    print(#a) --> 3
    [/source]
    Functions

    You can think of functions as procedures that your application can perform
    [source lang="jscript"]
    var max = function(a, b){ return a < b ? a : b }
    [/source]
    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
    [source lang="jscript"]
    var func = function(){ return 1, 2 }
    var x, y = func()
    print x --> 1
    print y --> 2
    [/source]
    The functions are objects themselves. As such, they have properties and methods. Any reference to a function allows it to be invoked using the () operator
    [source lang="jscript"]
    var func = function(){ print arguments }
    var func2 = function(f){ f.apply(null, ...) }
    func2(func,1,2,3) --> {1,2,3}
    [/source]
    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
    [source lang="jscript"]
    var a = function(){
    var x = 2
    return function(){ retutn x++ }
    }
    var b = a()
    print b() --> 2
    print b() --> 3
    print b() --> 4
    [/source]
    ObjectScript 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
    [source lang="jscript"]
    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;
    }
    [/source]
    Userdata

    An 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 operator

    Assignment is the basic means of changing the value of a variable or a object property:
    [source lang="jscript"]
    a = "Hello" .. " world!"
    t.n = t.n + 1
    [/source]
    ObjectScript 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
    [source lang="jscript"]
    a, b = 1, 2
    [/source]
    the 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
    [source lang="jscript"]
    x, y = y, x // swap x for y
    a, a[j] = a[j], a // swap a for a[j]
    [/source]
    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
    [source lang="jscript"]
    a, b, c = 0, 1
    print(a, b, c) --> 0 1 null
    a, b = a+1, b+1, b+2 -- value of b+2 is ignored
    print(a,b) --> 1 2
    a, b, c = 0
    print(a,b,c) --> 0 null null
    [/source]
    The last assignment in the above example shows a common mistake. To initialize a set of variables, you must provide a value for each one:
    [source lang="jscript"]
    a, b, c = 0, 0, 0
    print(a, b, c) --> 0 0 0
    [/source]
    You 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
    [source lang="jscript"]
    a, b = f()
    [/source]
    f() returns two results: a gets the first and b gets the second.

    Global Variables

    Global 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
    [source lang="jscript"]
    print(a) --> null
    a = 10
    print(a) --> 10
    [/source]
    Usually 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
    [source lang="jscript"]
    a = null
    print(a) --> null
    [/source]
    After 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 Environment

    Any 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:
    [source lang="jscript"]
    function eval(str, env){
    return compileText(str).applyEnv(env || _G, null, ...)
    }
    [/source]
    Iterators

    An iterator allows you to iterate over the elements of a collection. For example
    [source lang="jscript"]
    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 )
    }
    [/source]
    outputs
    [source]
    0 --> null
    1 --> true
    2 --> 12
    3 --> 0
    [/source]
    ObjectScript compiles the above program to
    [source lang="jscript"]
    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 )
    }
    }
    [/source]
    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
    [source lang="jscript"]
    Array.__iter = function(){
    var i, self = 0, this
    return function(){
    if(i < #self){
    return true, i, self[i++]
    }
    }
    }
    [/source]
    Note that iterator of function is itself. It's declared as
    [source lang="jscript"]
    Function.__iter = function(){ return this }
    [/source]
    So we can write iterator like that
    [source lang="jscript"]
    var range = function(a, b){
    return function(){
    if(a <= b){
    return true, a++
    }
    }
    }

    for(var i in range(10, 13)){
    print( "i = ", i )
    }
    [/source]
    outputs
    [source]
    i = 10
    i = 11
    i = 12
    i = 13
    [/source]
    Properties, Getters And Setters

    A 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.
    [source lang="jscript"]
    a = {
    _color: "red"
    __get@color = function(){ return this._color }
    __set@color = function(v){ this._color = v }
    }

    print a.color --> red
    print a["color"] --> red
    a.color = "blue"
    print a.color --> blue
    print a["color"] --> blue
    [/source]
    Note that @ is not a special symbol, any functions and variables can contain @.

    Another example
    [source lang="jscript"]
    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 --> red
    a.color = "blue"
    print a.color --> blue
    delete a.color
    print a.color --> null
    [/source]
    Multi dimensional properties

    ObjectScript supports a.color and a["color"] syntax. What about?
    [source lang="jscript"]
    a[x, y] = 12
    [/source]
    Yes, ObjectScript supports the above syntax, it's called multi dimensional properties
    [source lang="jscript"]
    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] --> null
    [/source]
    Note that __setdim method receives the first argument as new property value and other arguments as dimensional attributes of property. For example
    [source lang="jscript"]
    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] = 5
    print a[1, 2, 3] --> 5
    [/source]
    Empty dimensional properties

    What about?
    [source lang="jscript"]
    b = a[]
    a[] = 2
    delete a[]
    [/source]
    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 Objects

    ObjectScript 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.

    Custom Objects

    [source lang="jscript"]
    var a = {
    num: 1
    __get@number: function(){
    return this.num
    }
    __add = function(a, b){
    return a.number + b.number
    }
    }

    var b = extends a {
    num: 2
    }

    print a + b --> 3
    [/source]
    The Class

    ObjectScript is a prototype-based language which contains no class statement. Any object could be used as class.
    [source lang="jscript"]
    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!"
    }
    }
    [/source]
    The Object (Class Instance)

    To create a new instance of an object we use () operator for class variable
    [source lang="jscript"]
    var p = Person("James", "Bond")
    [/source]
    is equivalent to
    [source lang="jscript"]
    var p = {}
    p.prototype = Person
    p.__construct("James", "Bond")
    [/source]
    run
    [source lang="jscript"]
    p.walk() --> James Bond is walking!
    print p --> {firstname:James,lastname:Bond}
    [/source]
    Inheritance

    Inheritance is a way to create a class as a specialized version of class. You need to use extends operator
    [source lang="jscript"]
    var IvanPerson = extends Person {
    __construct: function(){
    super("Ivan", "Petrov")
    }
    }
    var p = IvanPerson()
    p.walk() --> Ivan Petrov is walking!
    print p --> {firstname:Ivan,lastname:Petrov}
    [/source]
    the extends operator has syntax extends exp1 exp2 where exp1 and exp2 are any valid expressions, it's equivalent to
    [source lang="jscript"]
    (function(exp1, exp2){
    exp2.prototype = exp1
    return exp2
    })()
    [/source]
    Encapsulation

    In 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 example

    [source lang="jscript"]
    var vec3 = {
    __construct = function(x, y, z){
    this.x = x
    this.y = y
    this.z = z
    }
    __add = function(a, b){
    return vec3(a.x + b.x, a.y + b.y, a.z + b.z)
    }
    __mul = function(a, b){
    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 * v2
    print v3
    [/source]
    outputs
    [source]
    {x:11,y:24,z:39}
    [/source]

    You could download all above examples here https://github.com/unitpoint/objectscript Just clone repo to your computer and play with OS\examples-os\test.os. You can run OS\OS\examples-os\test.cmd to execute test.os.

    You could view more examples here OS\proj.win32\examples.sln

    P.S. some other articles in russian:


    • binding C++ classes & functions http://habrahabr.ru/post/154221
    • execute ObjectScript from C++ code http://habrahabr.ru/post/152813


      It's awesome if you've read this article! Thanks, looking forward to your reply!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.