Last time, I wrote about a high-level explanation of DLCL and some of the VM design. Now, I'll show you a couple examples.
DLCL is boring in some ways, but it's interesting to examine in the view of what such a small language can accomplish. Let's take a Hello World example:
call Print: "Hello, World!\n".
Calls are prefixed with 'call' (the prefix notation is used elsewhere), and arguments come between a colon and a dot. In fact, the colon and dot are used most places where parentheses or brackets would be used in C or Java.
int i 0 bool was_set false loop i < 100: set was_set false % The [...] notation is for calls. % `[Mod i 3]` is the same as `call Mod: i 3.` if [Mod i 3] = 0: [Print "Fizz\n"] set was_set true . if [Mod i 5] = 0: [Print "Buzz\n"] set was_set true . if get was_set = false: [Print i "\n"] . % Increment i set i (get i + 1) .
This uses the alternative call notation. We use the builtin modulo function here (Mod). There is no operator for this, just because I used '%' for comments and it seems best to not reuse some other symbol. The increment operation uses both 'set' and 'get' prefixes. All accesses must be prefixed with 'get', and all assignments are prefixed with 'set'. The parentheses here are optional, and included for clarity.
This example also demonstates declaring variables. All declarations must have a
default value (here, `i` is given 0).
This also shows that DLCL is statically and strongly typed. While this may seem a strange choice for a scripting language, it makes the interpreter simpler, and since there is no object model in DLCL (it's intended for lightweight scripts) it doesn't really impose on the scripter.
I'll show one last example. This is an example taken straight from DarkLight, and is used in the prototype entity system.
% Check if an emeny is withing sight range to set as target. float dx [GetEntityX] - [GetOtherX] float dy [GetEntityY] - [GetOtherY] float distance [Sqrt (dx * dx) + (dy * dy)] if distance < [GetEntitySight]: call SetTarget:[GetOtherID]. .
DLCL isn't really useful for too many serious things, but it's a lot of fun to play around with VM, and it's amazing how much language you can get from such a small interpreter.