MiniD

From Wikipedia, the free encyclopedia

MiniD
Paradigm Multi-paradigm
Appeared in 2006
Designed by Jarrett Billingsley
Latest release 1.1/ April 27, 2008
Typing discipline Dynamic
Influenced by Lua, Squirrel, Python, ECMAScript
OS Cross-platform
License zlib/libpng
Website MiniD DSource Page

The MiniD programming language is a small, lightweight, extension language in the vein of Lua or Squirrel, but designed to be used mainly with the D programming language. It supports both object-oriented and imperative programming paradigms, as well as some simple functional aspects.

Distributed under the licence of zlib/libpng, MiniD is free software.

Contents

[edit] History

MiniD began in June 2006 as an idea for a statically-typed language, much like a stripped-down version of the D programming language. This is the reason for the name "MiniD." After work began on the compiler, the creator, Jarrett Billingsley, realized just how large a project this language was becoming, and decided to recast the language into something simpler to implement. The result was a Lua-like language with a C-style syntax. Over the next several months, MiniD acquired features from various languages, such as Squirrel-like classes, a D-like module system, and collaborative multithreading like Lua. On August 1 2007, after more than thirteen months of planning and programming, version 1.0 of the reference implementation was released. The version 1.0 language specification is frozen. MiniD development, however, will continue. New features are in development for what will eventually be version 2 of the language. There will likely be a fork in the development tree, as well as a separate language specification defined for this new version.

[edit] Features

MiniD provides a small but flexible set of data types, similar to that of Lua's or Squirrel's. Unlike Lua, MiniD provides explicit support for object-oriented programming with classes. MiniD also provides a module system and coroutines as core language features, unlike Lua. MiniD is garbage-collected, with support for first-class functions, closures, and tail recursion.

[edit] Example Code

Here is the Hello world program in MiniD.

module test;
writefln("Hello, world!");

Every MiniD source file must begin with a module declaration. For simplicity, the module declaration has been omitted in the rest of the examples.

class Test
{
    x = 0;
    y = 0;

    this(x, y)
    {
        this.x = x;
        this.y = y;
    }

    function toString()
    {
        return format("x = ", x, " y = ", y);
    }
}

local t = Test(3, 4);
writefln(t);

This example shows a simple class with two fields, x and y, which are initialized to 0 by default. The class's constructor, declared with the 'this' keyword, takes two parameters and assigns them to the instance's fields. Note that 'this' is used in the constructor to refer to the object at hand. The 'this' parameter is passed implicitly to all functions, not just object methods.

The class has one method, 'toString', which is called automatically when the object needs to be formatted to a string. Note that in 'toString', the fields 'x' and 'y' are accessed without explicitly referring to 'this', unlike in Lua or Python.

Finally, the class is instantiated by calling it like a function. When the instance is printed out using 'writefln', the 'toString' method is called, and so this program outputs "x = 3 y = 4".

local a = array.range(1, 11);
local b = a.map(function(x) x * x);
writefln(b);

This example demonstrates some of MiniD's array manipulation abilities. Unlike Lua, MiniD has a separate array type. In this example, an array is created that holds the values 1 through 10 using the 'array.range' function. Then the 'map' method of arrays is used on 'a', and it takes a function literal which returns the square of its parameter. Thus, when 'b' is printed, it shows the first ten squares, that is "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]".

local a = [1, 2, 3, 4, 5];
local t = { x = 5, y = 10 };
local s = "hello";

foreach(i, v; a)
    writefln("a[", i, "] = ", v);

writefln();

foreach(i, v; t)
    writefln("t.", i, " = ", v);

writefln();

foreach(i, v; s)
    writefln("s[", i, "] = ", v);

This example shows the 'foreach' loop, which can be used to iterate over arrays, tables, and strings as shown here, as well as other types.

local co = coroutine function(val)
{
    // The iterator expects the coroutine to yield once
    // before it starts yielding values.
    yield();

    while(val > 0)
    {
        yield(val);
        val--;
    }
};

foreach(v; co, 5)
    writefln(v);

This example shows the use of the 'foreach' loop to iterate over a coroutine. In this way, coroutines can be used as generators.

[edit] External links