ObjectScript — - . 459 (, )
source\objectscript.h
source\objectscript.cpp
. . ObjectScript — , C++.
ObjectScript , JavaScript, Lua PHP. , JavaScript, — Lua, — PHP.
, ObjectScript .
x = 12;
y = "Hello World!";
?
x = 12
y = "Hello World!"
ObjectScript ( , ), (;) .
, :
print(5, " differences")
?
print(5 " differences")
ObjectScript . , concat, , :
var s = concat("name: " name ", count: " count ", time: " time)
! name, count time — . , .. ( ) , concat , .
, :
print({firstname:"Ivan", lastname:"Petrov"})
JavaScript . ObjectScript, . ?
print {firstname:"Ivan", lastname:"Petrov"}
?! Lua. , :
print "Hello World!"
!
. ? ?
print {firstname:"Ivan" lastname:"Petrov"}
, , :
print {firstname="Ivan" lastname="Petrov"}
.. ( ) , . , , (;) . , , , ObjectScript:
a = {x=1, y=3; "zero" "one", "two" last:7,}
, , . . :
print a[1]
one. , , :
a = {[2+3]="five" y=3}
.. . :
print a[5]
five
, ( , ).
, , :
a = {x=1 y=2}
b = {[a]="powerful" 7="greate"}
print b[a]
powerful, . , , .
— . JavaScript :
a = [10, 20, 30, 40]
, , — :
a = [10 20 30 40]
ObjectScript.
ObjectScript :
i, j, k = 0, 1, 3
i 0, j 1, k — 3. :
i, j = j, i
. , , , :
var test = function(){ return 1, 2 }
var a, b = test()
test , a 1, b — 2. , , — null
var a, b, c = test()
print(a, b, c)
: 1 2 null
- . , :
obj = { null awesome=true 12 "excellent" }
for(k, v in obj){
print( k " --> " v )
}
:
0 --> null
awesome --> true
1 --> 12
2 --> excellent
ObjectScript for in, , :
obj = { null awesome=true 12 "excellent" };
{
var iter_func = obj.__iter()
for(var iter_valid;;){
iter_valid, k, v = iter_func()
if(!iter_valid) break
print( k " --> " v )
}
}
- obj __iter, :
var iter_func = obj.__iter()
:
iter_valid, k, v = iter_func()
, true, , , . :
if(!iter_valid) break
, break .
, . , , , — . , , , :
obj = { null awesome=true 12 "excellent" }
for(k in obj){
print k
}
, . ObjectScript :
Array.__iter = function(){
var i, self = 0, this
return function(){
if(i < #self){
return true, i, self[i++]
}
}
}
, -. Array — , . ++ class Array. , Array . :
print [1 2 3].prototype === Array
true ( === ). . Array __iter ( ). (. closure). , ObjectScript — - ( ). , i, self.
var i, self = 0, this
i 0, self this ( , , ).
return function(){
if(i < #self){
return true, i, self[i++]
}
}
, . , (# — , ), , true (, ), . . , .. . , , iter_valid, null :
if(!iter_valid) break
iter_valid , , .
.
var range = function(a, b){
return function(){
if(a <= b){
return true, a++
}
}
}
for(var i in range(10, 13)){
print( "i = ", i )
}
:
i = 10
i = 11
i = 12
i = 13
« , __iter?» . , :
Function.__iter = function(){ return this }
Function — , , call apply, JavaScript.
- () ObjectScript
, .
:
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!"
}
}
:
var p = Person("James", "Bond")
Person — , , , ObjectScript __construct. :
var p = {}
p.prototype = Person
p.__construct("James", "Bond")
:
p.walk() print p
:
James Bond is walking!
{"firstname":"James","lastname":"Bond"}
__get@fullname, walk:
__get@fullname = function(){
return this.firstname .. " " .. this.lastname
}
walk = function(){
print this.fullname .. " is walking!"
}
__get@fullname fullname. , , getter- setter-.
, __get@fullname @, ObjectScript , $ .
Person.
// IvanPerson
var IvanPerson = extends Person {
__construct = function(){
super("Ivan", "Petrov")
}
}
var p = IvanPerson() // IvanPerson
p.walk()
print p
:
Ivan Petrov is walking!
{"firstname":"Ivan","lastname":"Petrov"}
extends, exp1 exp2, , :
(function(exp1, exp2){
exp2.prototype = exp1
return exp2
})()
:
super("Ivan", "Petrov")
super () , , — __construct, .
, :
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
: {«x»:11,«y»:24,«z»:39}
, getter- setter-
— (, , ...), , .
(getter) — , (setter) — . ObjectScript , , .
a = {
_color = "red"
__get@color = function(){ return this._color }
__set@color = function(v){ this._color = v }
}
print a["color"]
a.color = "blue"
print a.color
:
red
blue
? color ObjectScript color. , . , __get@color, — , ObjectScript . , , ObjectScript __get. , ObjectScript . , null .
, , __get __set. :
a = {
_color = "white"
__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
a.color = "green"
print a.color
delete a.color
print a.color
:
white
green
null
delete, __del.
ObjectScript ( ) , :
print a.color
print a["color"]
, ? ObjectScript !
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 // a.__setdim(5, 1, 2)
print a[1, 2] // print(a.__getdim(1, 2))
delete a[1, 2] // a.__deldim(1, 2)
print a[1, 2] // print(a.__getdim(1, 2))
:
5
null
__setdim, , — ( ).
?
b = a[]
a[] = 2
delete a[]
! ObjectScript : __getempty, __setempty, __delempty. , .
.
, :
print function(a b c){ return a + b * c }(1 2 3)
7
arguments — , , ... ( ) — , .
# __len. , — . Object :
Object.__get@length = function(){ return #this }
.. , , Object, length, , , JavaScript.
** — .
:
if(exp) block [elseif(exp) block ][else block ]
for(pre_block; exp; post_block) block
for(assign_list in exp) block
break
continue
function(var_list){ block }
ObjectScript , . , .
, .
var i = 1;
{
var i = i
i++
print i
}
print i
2 1
ObjectScript , — debugger ( JavaScript). debugger , . — debuglocals, debuglocals . :
function(a){
var c = a * 2;
{
var c = a - 1
print debuglocals
}
}(10)
:
{a:10,c:9}
ObjectScript tail call. . . call stack , call stack, .
boolean : null, false NaN false, . — true, 0.
&& || , , :
print 7 && 9
print 7 || 9
:
9 7
, , . , clone, . , , . , . __clone, - . . , __clone. , userdata, .
typeof, valueof, numberof, stringof, arrayof, objectof, functionof, userdataof. .
- . ObjectScript , . , — , , , , environment , , , -, JavaScript ( ). .
, .
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())"); // 123
os->release();
return 0;
}
, ? , github, .
OS\examples
test3.cmd.
test3.txt , ObjectScript , , , :
[14] print( a[v1] a.v2 )
begin call
get env var print
begin params 2
begin get property
get local var a (1 0 param)
get local var v1 (0 0 param)
end get property ret values 1
,
begin get property
get local var a (1 0 param)
push const string "v2"
end get property ret values 1
end params ret values 2
end call ret values 0
ObjectScript JSON, .. , , . ObjectScript , JavaScript, Lua PHP, . ObjectScript — - , . new , . ObjectScript , . ObjectScript C++, ++ ( -). ObjectScript — , 459 . .
ObjectScript , :
www.youtube.com/watch?v=OCWIfQYW9rc
www.youtube.com/watch?v=P5KPJOVSs3E
www.youtube.com/watch?v=htDqDNqHX-I
www.youtube.com/watch?v=wqiDeuf7yu8
www.youtube.com/watch?v=uep2SvXdCNU
, .