a .NET hosted scripting language with a focus on meta-programming and embedded DSLs
While Prexonite Script does not formally provide any means of object-oriented programming (not just consuming objects but actually defining new ones) there are mechanisms in Prexonite (the runtime) that try to make up for this.
The Structure data type
function create_complex(_re, _im)
{
_re ??= 0.0;
_im ??= 0.0;
var z = new Structure;
z.\("re") = _re;
z.\("im") = _im;
z.\\("norm") = (this) =>
sqrt(this.re^2 + this.im^2);
z.\\("add") = (this,other) =>
create_complex(this.re + other.re,this.im + other.im);
z.\\("ToString") = (this) =>
this.re + " " + this.im + "i";
return z;
}
The ExtendableObject base class
The struct function (in psr/struct.pxs
)
build does require("psr\\struct.pxs");
function create_complex(_re, _im)
{
_re ??= 0.0;
_im ??= 0.0;
function re(this, new_re)
{
if(new_re is not Null)
_re = new_re;
return _re;
}
function im(this, new_im)
{
if(new_im is not Null)
_im = new_im;
return _im;
}
function norm = sqrt(_re^2 + _im^2);
function add(this, other) =
create_complex(re + other.re,im + other.im);
function ToString =
re + " " + im + "i";
return struct;
}
auto-property, proxy-property, property support via psr/prop.pxs
.
build does require("psr\\struct.pxs","psr\\prop.pxs");
function create_car(a_color)
{
_re ??= 0.0;
_im ??= 0.0;
function re = struct_prop(_re);
function im = struct_prop(_im);
function norm = sqrt(re^2 + im^2);
function add(this, other) =
create_complex(re + other.re,im + other.im);
function ToString =
re + " " + im + "i";
return struct;
}
operator overloading via (+)
, (==)
etc.
//...
function (+) this other =
create_complex(re + other.re,im + other.im);
function (-.) =
create_complex(-re, -im);
function (-) this other = this + (-other);
//...
Let's discuss those two extensions one by one.
First of all, there still is no language support for structures and properties.
As of right now, they are implemented via compile time transformations.
These two transformations can be enabled via importing the Prexonite Standard Repository scripts psr/struct.pxs
and psr/prop.pxs
.
While the former has been around for some time now, the latter has only recently been implemented in managed code.
psr/prop.pxs
defines the special function prop
which is expanded into get and set code depending on the way it is used.
The easiest mode of operation implements a property with an anonymous backing field (defined in the surrounding scope, i.e. as a global variable in a top-level function or a shared variable in the outer function).
The second mode mimics a simple get-set proxy where the expression passed as an argument is used as the backing field. This works for arbitrary expressions as long as they can be the target of an assignment (i.e. they are get-set expressions). The third mode finally is a replication of C# properties taking separate lambda expressions for its get and set implementations.
struct_prop
works the same way but ignores the additional this
parameter for structure methods.
When resolving operator calls, generic objects (as well as structures) now also try to call special instance methods after failing to find a corresponding static operator overload.
This makes the implementation of operator overloads for structures and ExtendableObjects
(An abstract class that makes an object extendable in the same way a structure can be extended) possible.
In order to avoid strange operator names, I have introduced operator ids, dedicated literals (on the lexical level) that represent operator names.
There is nothing special about these literals, they can be used everywhere an id can be used.
You could for instance define a local variable with the name (+)
.