Click here to Skip to main content
15,861,366 members
Articles / Programming Languages / Lua

ObjectScript: A new programming language

Rate me:
Please Sign up or sign in to vote.
4.24/5 (6 votes)
9 Oct 2012MIT9 min read 66K   107   21   14
The ObjectScript is a new 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.

Welcome to ObjectScript!

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

The source code of ObjectScript is less than 500 KB and you could download it here https://github.com/unitpoint/objectscript.

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:

JavaScript
var a = 12;
And later, you could assign the same variable a string value, for example:
JavaScript
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:

  1. null, a special keyword denoting a `null` value
  2. boolean, either `true` or `false`
  3. number, such as `10`, `0x123`, `2.3`, `5.7e23`
  4. string, such as `"Hello World!"`
  5. object, such as `{"one", "two", 12:"at index 12"}`
  6. array, such as `["one", 31]`
  7. function, such as `function(){ print "Hello World!" }`
  8. 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:

JavaScript
12 // decimal, base 10
3.14
2e10
0xfe // hexadecimal, "hex" or base 16
0123 // octal, base 8

Strings

A string literal is zero or more characters enclosed in double (") quotation marks. The following are examples of string literals.

JavaScript
"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.

JavaScript
"Hello World!".length

or just use length operator:

JavaScript
#"Hello World!"

Objects

An object is a list of zero or more pairs of property names and associated values of an object.

JavaScript
{"one", "two", 12:"at index 12", ["on" .. "e"]: "at index one", some = "extended syntax"}

The simplest constructor of object is the empty constructor `{}`

JavaScript
a = {}; // create an empty object and store its reference in a

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:

JavaScript
{
    var a = 12;
}

Let's view some examples:

JavaScript
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:

JavaScript
print(days[4])  --> Thursday

Constructors do not need to use only constant expressions. You can use any kind of expression for the value:

JavaScript
tab = {sin(1), sin(2), sin(3), sin(4)}

To initialize an object to be used as a record, ObjectScript offers the following syntax:

JavaScript
a = {x=0, y=0}

is equivalent to:

JavaScript
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 (,)

JavaScript
a = {x=1, y=3; "one", "two"}

Finally, you can avoid usage of comma and semicolon:

JavaScript
a = {x=1 y=3 "one" "two"}

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

a = { one = "great" }; // the same as { "one": "great" }
print(a[b]) --> 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

Arrays

An array is a list of indexed values:

JavaScript
a = ["zero", "one", "two"]
print(a[1]) --> one
print(#a) --> 3

Functions

You can think of functions as procedures that your application can perform:

JavaScript
var max = function(a, b){ 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:

JavaScript
var func = function(){ return 1, 2 }
var x, y = func()
print x --> 1
print y --> 2

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:

JavaScript
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:

JavaScript
var a = function(){
    var x = 2
    return function(){ retutn x++ }
}
var b = a()
print b() --> 2
print b() --> 3
print b() --> 4

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.

JavaScript
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;
}

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:

JavaScript
a = "Hello" .. " world!"
t.n = t.n + 1

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:

JavaScript
a, b = 1, 2

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:

JavaScript
x, y = y, x                // swap x for y
a[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.

JavaScript
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

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:

JavaScript
a, b, c = 0, 0, 0
print(a, b, c)           --> 0   0   0

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.

JavaScript
a, b = f()

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

JavaScript
print(a)  --> null
a = 10
print(a)  --> 10

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.

JavaScript
a = null
print(a)  --> null

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:

JavaScript
function eval(str, env){
    return compileText(str).applyEnv(env || _G, null, ...)
}

Iterators

An iterator allows you to iterate over the elements of a collection. For example:

JavaScript
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 --> null
1 --> true
2 --> 12
3 --> 0

ObjectScript compiles the above program to:

JavaScript
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:

JavaScript
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:

JavaScript
Function.__iter = function(){ return this }

So we can write iterator like that:

JavaScript
var range = function(a, b){
    return function(){
        if(a <= b){
            return true, a++
        }
    }
}

for(var i in range(10, 13)){
    print( "i = ", i )
}

Outputs:

i = 10
i = 11
i = 12
i = 13

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.

JavaScript
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

Note that @ is not a special symbol, any functions and variables can contain @.

Another example:

JavaScript
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

Multi dimensional properties

ObjectScript supports `a.color` and `a["color"]` syntax. What about?

JavaScript
a[x, y] = 12

Yes, ObjectScript supports the above syntax, it's called multi dimensional properties.

JavaScript
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

Note that `__setdim` method receives the first argument as new property value and other arguments as dimensional attributes of property. For example:

JavaScript
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

Empty dimensional properties

What about?

JavaScript
b = a[]
a[] = 2
delete 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 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

JavaScript
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

The Class

ObjectScript is a prototype-based language which contains no class statement. Any object could be used as class.

JavaScript
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:

JavaScript
var p = Person("James", "Bond")

is equivalent to:

JavaScript
var p = {}
p.prototype = Person
p.__construct("James", "Bond")

Run:

JavaScript
p.walk()    --> James Bond is walking!
print p        --> {firstname:James,lastname:Bond}

Inheritance

Inheritance is a way to create a class as a specialized version of class. You need to use extends operator.

JavaScript
var 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:

JavaScript
(function(exp1, exp2){ 
    exp2.prototype = exp1 
    return exp2 
})()

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

JavaScript
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

Outputs: 

{x:11,y:24,z:39} 

You can download full source code of the ObjectScript: 

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionObject scrip is not new. Pin
tlford659-Oct-12 8:34
professionaltlford659-Oct-12 8:34 
GeneralRe: Object scrip is not new. Pin
unitpoint9-Oct-12 8:41
unitpoint9-Oct-12 8:41 
GeneralRe: Object scrip is not new. Pin
tlford659-Oct-12 8:57
professionaltlford659-Oct-12 8:57 
QuestionFalls foul of one of the tenets of Code Project articles Pin
Pete O'Hanlon9-Oct-12 5:21
subeditorPete O'Hanlon9-Oct-12 5:21 
AnswerRe: Falls foul of one of the tenets of Code Project articles Pin
unitpoint9-Oct-12 9:00
unitpoint9-Oct-12 9:00 
QuestionFew questions about the OS Pin
Andriy Syrovenko2-Oct-12 1:57
Andriy Syrovenko2-Oct-12 1:57 
AnswerRe: Few questions about the OS Pin
unitpoint2-Oct-12 5:51
unitpoint2-Oct-12 5:51 
QuestionVery interesting and lots of possibilities! Pin
dalstorp28-Sep-12 21:51
dalstorp28-Sep-12 21:51 
I have been looking for a simple and portable scripting language that is easy integrated in C++ without lots of code files, libraries, … This looks like a great candidate!

I have lots of thoughts but will start with one question: How to call precompiled OS functions from C++ and then have the OS functions return data on the stack (multiple return values) and then pick them up in C++?

Final thoughts on scripting:
- I like the JavaScript syntax, IMO the cleanest of all scripting languages; Unfortunate not so easy to embed in other programs and the OOP is put on afterward.
- I like the multiple returns of LUA and the ease of embedding; I do not like the syntax.
- I have not used PHP
- Since I come from C/C++ I don’t mind case sensitive languages, looks cleaner =)
So keep up the good work!
Dalstorp

AnswerRe: Very interesting and lots of possibilities! Pin
unitpoint29-Sep-12 3:29
unitpoint29-Sep-12 3:29 
GeneralRe: Very interesting and lots of possibilities! Pin
dalstorp30-Sep-12 1:24
dalstorp30-Sep-12 1:24 
GeneralRe: Very interesting and lots of possibilities! Pin
unitpoint30-Sep-12 3:46
unitpoint30-Sep-12 3:46 
Question"ObjectScript is Case Sensitive" Pin
vbfengshui27-Sep-12 12:13
vbfengshui27-Sep-12 12:13 
AnswerRe: "ObjectScript is Case Sensitive" Pin
Florian Rappl9-Oct-12 8:31
professionalFlorian Rappl9-Oct-12 8:31 
GeneralRe: "ObjectScript is Case Sensitive" Pin
vbfengshui9-Oct-12 9:00
vbfengshui9-Oct-12 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.