HomeForumResearchGuide
<< 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.

First, create a new file MyApp.dn and open it in a text editor. The .dn file extension is used for all Dana source code. In this file we express the component that this source file will represent, declaring the interfaces that the component provides and requires, and coding implementation functions for provided interfaces:

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
        }
    }

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.

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

The App interface is a special one which the Dana language runtime knows about; if a component provides this interface its main method will be called by the Dana runtime. 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.

Our example program asks the user for some input, reads that input from the command-line, and writes that input to a text file. To make this work, we declare three required interfaces io.Output, io.Input, and io.File. You can find details of these interfaces in Dana's standard library documentation. These three required interfaces are wired to provided interfaces of other components when the program is executed.

For convenience, two of our required interfaces here (io.Output and io.Input) were given names. When we do this, Dana will automatically instantiate these required interfaces and allow those instances to be globally accessed within this component using the given name. This notation is shorthand for a static global variable which is automatically instantiated, and can only be used with interfaces that do not declare a constructor.

The other required interface, io.File, is instantiated within the main method using the new operator. The io.File interface declares a constructor function and so must be instantiated in this way. You can see the interface definition in the standard library here, or in the components/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 MyApp.dn and type:

dnc MyApp.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 MyApp

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.