source\objectscript.h
source\objectscript.cpp
. . ObjectScript — , C++.
x = 12;
y = "Hello World!";
x = 12
y = "Hello World!"
print(5, " differences")
print(5 " differences")
var s = concat("name: " name ", count: " count ", time: " time)
print({firstname:"Ivan", lastname:"Petrov"})
print {firstname:"Ivan", lastname:"Petrov"}
print "Hello World!"
print {firstname:"Ivan" lastname:"Petrov"}
print {firstname="Ivan" lastname="Petrov"}
a = {x=1, y=3; "zero" "one", "two" last:7,}
print a[1]
a = {[2+3]="five" y=3}
print a[5]
a = {x=1 y=2}
b = {[a]="powerful" 7="greate"}
print b[a]
a = [10, 20, 30, 40]
a = [10 20 30 40]
i, j, k = 0, 1, 3
i, j = j, i
var test = function(){ return 1, 2 }
var a, b = test()
var a, b, c = test()
print(a, b, c)
obj = { null awesome=true 12 "excellent" }
for(k, v in obj){
print( k " --> " v )
}
0 --> null
awesome --> true
1 --> 12
2 --> excellent
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 )
}
}
var iter_func = obj.__iter()
iter_valid, k, v = iter_func()
if(!iter_valid) break
obj = { null awesome=true 12 "excellent" }
for(k in obj){
print k
}
Array.__iter = function(){
var i, self = 0, this
return function(){
if(i < #self){
return true, i, self[i++]
}
}
}
print [1 2 3].prototype === Array
var i, self = 0, this
return function(){
if(i < #self){
return true, i, self[i++]
}
}
if(!iter_valid) break
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
Function.__iter = function(){ return this }
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")
var p = {}
p.prototype = Person
p.__construct("James", "Bond")
p.walk()
print p
James Bond is walking!
{"firstname":"James","lastname":"Bond"}
__get@fullname = function(){
return this.firstname .. " " .. this.lastname
}
walk = function(){
print this.fullname .. " is walking!"
}
// 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"}
(function(exp1, exp2){
exp2.prototype = exp1
return exp2
})()
super("Ivan", "Petrov")
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
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
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
print a.color
print a["color"]
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
b = a[]
a[] = 2
delete a[]
print function(a b c){ return a + b * c }(1 2 3)
Object.__get@length = function(){ return #this }
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 }
var i = 1;
{
var i = i
i++
print i
}
print i
2
1
function(a){
var c = a * 2;
{
var c = a - 1
print debuglocals
}
}(10)
{a:10,c:9}
print 7 && 9
print 7 || 9
9
7
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;
}
OS\examples
test3.cmd.
[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
Source: https://habr.com/ru/post/152289/