User:Phoolimin/Dao (programming language)

Dao is an object-oriented scripting language with dynamically typed variables supporting complex data structures. It has text processing abilities, such as regular expression matching. It provides many built-in numerical datatypes such as complex numbers and multi-dimensional numeric arrays, as well as their corresponding arithmetic operations. Support for multi-threaded programming is also an integrated part of Dao. The Dao interpreter is implemented as a lightweight virtual register machine (Dao VM) in standard C. The Dao VM can be extended with C or C++.

Advanced features of the language include:


 * A mixed static and dynamic type system with automatic type inference capabilities
 * A flexible macro system that allows definition of new syntax
 * Concurrent and distributed programming with message Passing Interfaces

Hello World
The classic hello world program can be written as follows:

Here io(=stdio) is the standard library to handle input and output.

The std.listmeth routine can be used to display the methods in a library. The methods in math, for example, can be displayed using:

Datatypes
A Dao variable can be implicitly declared by assigning the result of an expression to a variable name.

By default, a variable will have a fixed type that is inferred from the expression that is used to declare the variable; this is called implicit typing. The type of a variable can also be specified explicitly, using the following Pascal-style declaration syntax:

type can be one of the following built-in types: int, float, double, string, complex, list, map, tuple, array, buffer, routine, or some composition of these types.

For example:

type can also be a Dao class name or the name of a user-defined C type. Special keywords for typing include: any for any type, ? for undefined types and @X for a type holder that can be initialized to a certain type in a parameter list.

All typed variables undergo static checking.

Control flow
If-Elif-Else:

While:

For:

For-in:

For-in also works for hash.

Switch

Functions
The declaration of a basic function (known in Dao as routines) looks like this:

Dao supports first-class functions. The definition of such functions is identical to that of normal function definition, with the following differences:
 * there is no need for a function name, but the created function must be assigned to a variable;
 * the default value expressions do not need to be constant expressions, as they are evaluated at run time when the function is created;
 * the function body may contain variables defined in the "upper" function that creates it.

Macros
Dao has a unique macro system where macro is defined in a way similar to writing Backus–Naur Form (BNF) metasyntax notation. Such macro is composed of two parts, the first part is the source syntax pattern to be matched, and the second part is the syntax to be applied. The transformation from the source syntax pattern to the target syntax pattern is controlled by some predefined markups (for identifiers, expressions, blocks and their repetitions etc.).

Dao macros are defined in the following way:

This example enables "while-do-end" without brackets:

Functional methods
Besides the supporting of first-class function, Dao has additional support of functional style programming by providing a set of higher-order functions as built-in methods, such as map, fold and unfold etc.

The basic syntax for such methods is the following,

Examples,

Asynchronous function call
Probably the simplest way to create multi-threaded programs in Dao is to use asynchronous function calls (AFC). The way of using AFC is almost identical to that of normal function calls, with the exception that the keyword async must follow the call.

Any functions or methods can be invoked in such an asynchronous way.

Normally AFC is executed in a separated native thread, which can be either an idle thread available from the thread pool, or a new thread created on the fly.

Message Passing Interface
With Message Passing Interface APIs provided in library mpi, one can easily do concurrent and distributed programming in Dao. In the MPI library, there are three principal functions: spawn, send and receive. With spawn, one can create lightweight virtual machine processes or real operation system processes, in the local or remote computers; and with send and receive, a process can send messages to or receive messages from other processes.

The process names are of the following form:

virtual_process@real_process@@hostname

"@real_process@@hostname" identifies an operating system process on host "@@hostname", and "virtual_process@real_process@@hostname" identifies a virtual process within "@real_process@@hostname". Message passing can only happen among virtual processes. Each real process has a main virtual process named "self". Any part can be omitted. For example, when "virtual_process" is omitted, it means the "self" virtual process, and "@@hostname" alone identifies the "self" within the operating system process that binds to port 4115 ( D : 4, A : 1, O : 15 ).