MiniD
Encyclopedia
The MiniD programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 is a small, lightweight, extension language in the vein of Lua or Squirrel, but designed to be used mainly with the D programming language
D (programming language)
The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...

. It supports both object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 and imperative programming
Imperative programming
In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state...

 paradigms, as well as some simple functional
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

 aspects.

Distributed under the licence of zlib/libpng, MiniD is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...

.

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
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

-style syntax. Over the next several months, MiniD acquired features from various languages, such as Squirrel-like classes, a D-like module system, and Lua-like collaborative multithreading. 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.

As of June 15, 2009, version 2 of MiniD has been released. Version 2 brings a major reimplementation of most of the library in order to support its own garbage collector rather than relying on the underlying D garbage collector, for better behavior in realtime applications such as games. Version 2 also brings several changes to the language and standard libraries.

The development of MiniD was stopped in June 2011 and used as the base for new language called Croc by the same author.

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 coroutine
Coroutine
Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations...

s as core language features, like Lua. MiniD is garbage-collected
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

, with support for first-class function
First-class function
In computer science, a programming language is said to have first-class functions if it treats functions as first-class objects. Specifically, this means that the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning...

s, closures
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...

, and tail recursion.

MiniD also tries to be more robust than typical dynamic languages, making it easier to catch bugs sooner. For example, it does not have implicit variable declarations and accessing globals that do not exist throws an error instead of giving a default value (as in Lua). Another very helpful feature is "parameter type constraints," which are a way of specifying valid types that function parameters may accept. These checks are still performed at runtime unlike in static languages, but their concise syntax and negligible performance impact make them much more attractive and easy-to-use than similar solutions in other dynamic languages. They can help greatly in catching bugs that would cause functions to malfunction or corrupt data structures if called with unexpected parameter types. A small example is given below.

Example code

The following example code is for MiniD 2. (Note that due to technical limitations, some keywords are not highlighted here, as Wikipedia does not have a source highlighter for MiniD.)

Here is the Hello world program
Hello world program
A "Hello world" program is a computer program that outputs "Hello world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to...

 in MiniD.


module test
writeln("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)
{
:x = x
:y = y
}

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

local t = Test(3, 4)
writeln(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. The syntax ":x" is shorthand for "this.x", where "this" is the object upon which a method was called. Just like in Lua or Python, members of "this" must be accessed explicitly.

The class has one method, 'toString', which is called automatically when the object needs to be converted to a string. The somewhat unusual syntax used here is inspired by many functional languages and is shorthand for the following:


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


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


local a = array.range(1, 11)
local b = a.map(\x -> x * x)
writeln(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. This literal syntax is again inspired by functional languages (such as Haskell) and is shorthand for the following:


local b = a.map(function(x) { return x * x })


When 'b' is printed, it shows the first ten squares, that is "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]".

writeln([x * x for x in 1 .. 11])

This shows a shorter way of achieving the same result, using MiniD's list comprehensions. The syntax is very close to Python's.


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

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

writeln

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

writeln

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.


function countDown(val: int) = coroutine function
{
while(val > 0)
{
yield(null, val) // this is like yielding an index and a value
val--
}
}

foreach(v; countDown(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
Generator (computer science)
In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values...

.


function first(x: array|string) = x[0]

writeln(first([1, 2, 3])) // prints 1
writeln(first("hello")) // prints h
writeln(first(45)) // error, invalid parameter type 'int'


This shows a simple use of parameter type constraints, a way of putting runtime checks on parameters to restrict their allowable types. The 'first' function allows only arrays and strings for its parameter 'x'. The first two print statements work fine, but the third throws an error since integers are not an allowable type for 'x'.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK