a .NET hosted scripting language with a focus on meta-programming and embedded DSLs
This is the first article in the "Philosophy behind"-series, picking up a specialty of one of my projects and explaining how it came to be made.
This time I am talking about a generalization in the Prexonite Script compiler that handles the usage of symbols in expressions and statements.
"There must be a way to unify variable access with function and object member calls"
...is what I thought. As a result the Prexonite Script syntax can treat functions like variables and vice-versa. I decided that there are two fundamental ways of interacting with a symbol: getting a value and setting a value.
So when you are assigning to a variable, you are writing a set-call to that variable. When you want to retrieve a value from a variable, you write a get-call to that variable. Same for object member fields and properties. Functions and object member methods however do not make a difference between get- and set-calls, both are legal. The compiler treats get and set-calls to any symbol the exactly same way, except for a small GetOrSet flag. It is therefore also perfectly legal to pass an argument to a function by "assigning it to the function". This may sound strange but allows for very natural function names:
function property(value = null) =>
{
static \property;
if(value == null)
return \property;
else
\property = value;
}
function main
{
property = 5; //instead of "property(5);"
print(property);
}
In other languages, like PHP or Java, you would have to use getProperty and setProperty. Let's have a look at another example:
function createText(author, msg) does
return author + ": " + msg;
function SealedSun does
return "SealedSun";
var message = "My flat is too small.";
var texts = new List(createText(SealedSun, message));
texts.Add = createText(SealedSun, "I am lying");
The first four lines are just (nested) function definitions. I am going to talk about the following two statements, both variable definitions (declaration and assignment), and the last statement, an object member access. In this little snippet we have four symbols:
createText
SealedSun
message
texts
Variable assignments are set-calls (because of the assignment operator "=
")
In the right hand side of the second assignment you find the symbol references SealedSun
and message
.
Both are turned into get
-calls because there is no assignment operator around.
And no, Prexonite Script does not require you to add ()
, the empty argument list, to a function call.
The last statement is a object member access. Here you can see how the GetSet philosophy can be exploited. While the .NET interop layer uses GetSet information to determine the correct method when accessing properties, it ignores that information for conventional methods. You are free to choose your preferred syntax construct.