HomeForumSourceResearchGuide
<< back to guide home

In this section we'll walk through creating a simple command-line program in Dana. The program will use standard output, command-line input, and the file system. For recommended text editors in which to write Dana programs, see this guide.

We begin by creating a new file MyProgram.dn and opening it in a text editor. In this file we declare the component that this source file will describe, declaring the interfaces that the component provides and requires. All behaviour in Dana is described using interfaces.

component provides App requires io.Output out, io.Input in, io.File {

}

Provided interfaces define the functionality that this component implements (or offers to other components). Required interfaces define the functionality that this component depends upon from other components. Every component must have at least one provided interface to make its functionality accessible. As we'll see shortly, unlike in some other programming languages, Dana's interfaces are instantiable.

The App interface has one function called main. You can see the definition of this function in the file App.dn within the "resources" folder of Dana's home directory.

A provided interface function is implemented by writing a function of the same name but with the interface's type name, followed by a colon, preceding that function name. We'll write a simple implementation of the App:main function, which looks like this:

component provides App requires io.Output out, io.Input in, io.File {

int App:main(AppParam params[])
    {
    out.print("Enter some text: ")
    char input[] = in.readln()
    
    File f = new File("out.txt", File.FILE_ACCESS_WRITE)
    f.write(input)
    f.close()
    
    return 0
    }
}

Here we see a simple program which asks the user for some input, reads that input, and writes that input to a text file. This simple example contains many of the concepts in Dana.

First, notice that two of our required interfaces (io.Output and io.Input) were given names. When we do this, Dana will automatically instantiate this required interface and allow that instance to be globally accessed within this component using the given name. For Java programmers, this is a bit like a private static global variable of object type which is automatically instantiated. In Dana this automatic instantiation can only be used with interfaces that do not declare a constructor.

Next we see another required interface, io.File, being instantiated using the new operator. This interface declares a constructor function and so must be instantiated in this way. You can see the interface definition in the "resources/io/" folder of Dana's home directory.

To run this program we first need to compile it. Dana's compiler is a program called dnc. Open a command-prompt in the directory containing your file MyProgram.dn and type:

dnc MyProgram.dn

And press enter. This will compile your component.

We run the program using Dana's interpreter, a program called dana. In the same command-prompt window, type:

dana MyProgram

And press enter. You should be prompted for some input and the program should then create a text file in this directory containing whatever you choose as input.

The full list of interfaces that you can use in requires directives can be viewed at the Dana API pages along with documentation of each one.